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;
}