I'm having an error when I run this code any ideas? When I run it and enter the code it works if I enter the same numbers I had entered before his turn, but if i enter different numbers I get Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.

using System;

public class TicTacToe
{
   public static void Main (string[] args)
   {

      int row = 0;
      int column;
      int[,] game;
      game = new int[3, 3] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };


      // Display array
      for (int i = 0; i < game.GetLength(0); i++ )
      {
         for (int j = 0; j < game.GetLength(0); j++)
         {
            Console.Write(game[i, j]);

         }
         Console.WriteLine();
      }
      Console.WriteLine();

      `      // Player 1's first turn
      Console.WriteLine("Player 1's turn.");
      Console.Write("Enter row [1, 2 or 3]: ");
      row = Convert.ToInt32(Console.ReadLine());
      Console.Write("Enter column [1, 2 or 3]: ");
      column = Convert.ToInt32(Console.ReadLine());
      game[--row, --column] = 1;

      // Display array
      for (int i = 0; i < game.GetLength(0); i++ )
      {
         for (int j = 0; j < game.GetLength(0); j++)
         {
            Console.Write(game[i, j]);

         }
         Console.WriteLine();
      }
      Console.WriteLine();

          Console.WriteLine("Player 1's turn.");
          Console.Write("Enter row [1, 2 or 3]: ");
          row = Convert.ToInt32(Console.ReadLine());
          Console.Write("Enter column [1, 2 or 3]: ");
          column = Convert.ToInt32(Console.ReadLine());

          if (game[--row,--column] != 0 )
          {
             Console.WriteLine("Sorry that square is unavailable.  Please choose another one.");
             Console.Write("Enter row [1, 2 or 3]: ");
             row = Convert.ToInt32(Console.ReadLine());
             Console.Write("Enter column [1, 2 or 3]: ");
             column = Convert.ToInt32(Console.ReadLine());
             game[--row, --column] = 1;
          }
          else
          {
             game[--row, --column] = 1;
          }

Arrays are zero (0) based index. You are asking for 1, 2 or 3. 3 is outside the index of the array (which is 0, 1 and 2).

Just like the last time you asked this.

I know, but when I input the row and column into the array I add a decrementor to cancel out the problem

What happens in line 59 when you enter 1 as your number?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.