0

I have a simple issue but it's a little bit confusing for me so I'd appreciate any and all help provided :).

Basically, I want a 2D array, that will be 9 in length + 1 value. So if I were to initialize it it'd be:

int[,] array = new int[8, 1];

My problem is I'm getting confused. I'm making a tic tac toe game, the first value will be the position. I will use an int to increment a value each time a turn is played. However, the second value I want to hold '0' or '1' or '2'. '0' will mean nobody has played here yet '1' will mean x has played here and '2' will mean o has played here.

When accessing the array ideally (although I cannot) I'd like to just specify array[m] but to access the array I'd need to input array[m,0] for example. I want the second value to hold a value, not a position to access the array if that makes sense.

If I can't do this with a 2D array does anyone have a suggestion of what to use? If any more info is needed please let me know.

3
  • Use a dictionary instead? Or some other collection of key/value pairs? Commented Feb 10, 2014 at 16:32
  • 4
    there is no need to specify a dimension of length 1, don't you just want int[] array = new int[9]? Commented Feb 10, 2014 at 16:33
  • 2
    I'd say you either want an array of [3, 3] or, like Gallen said, a Dictionary, possibly with some kind of enum as the key. Either way, I'd use bool? or another enum as the value, since then you have only three states. Commented Feb 10, 2014 at 16:40

4 Answers 4

1

As James Gaunt said:

     int[] array = new int[9];

In this array the index is the position and the value stored at that index is what is there 0=Nothing,1= O's,2 = X's.

Your initial board state will look like

{0,0,0,0,0,0,0,0,0}

Now X to move in the middle say:

// {0,0,0,0,2,0,0,0,0}
array[4] = 2;

Now O to move in the right middle:

// {0,0,0,0,2,1,0,0,0}
array[5] = 1;

Etc.. until a win state is reached or no more 0's exist on the board.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use a 2D array of objects where each object holds the value of the space and another for the position (Im guessing you mean the order that it gets played).

Comments

0

This would work:

IDictionary<int, int> ticTacToeBoard = new Dictionary<int, int>();

//initialize and reset all values on the board to 0
ticTacToeBoard = resetBoard(ticTacToeBoard);

int position = 1;
int value = ticTacToeBoard[position];

Comments

0

Sounds like you're after a 3x3 grid, you can simply use a 2D array for this:

const int width = 3;
const int height = 3;

var board = new int[height, width];

You could then expand it to check to see if all values are 0 (e.g. the board is empty) using something like:

bool AllEmpty(int[,] board, int width, int height)
{
  for (int y = 0; y < height; ++y)
  {
    for (int x = 0; x < width; ++x)
    {
      if (board[y, x] != 0)
      {
        return false;
      }
    }
  }
  return true;
}

(You could probably do something nicer using LINQ, but LINQ and multiple dimensions doesn't play too nice, so I've kept it simple for now).

This is essentially representing this:

000 
000 
000

You'll need to do some initialisation and modifying values for this to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.