You have two solutions: create an array of arrays to store your characters (that's what a matrix is fundamentally), or create a single array of char which would be the linearized version of the matrix, ie. an array containing all the rows of the matrix put together in one.
The first case is trivial:
const int grid_size = size * size; // determines the size of the grid height x width
char **number = new char*[size]; // create the array to the size of the grid
for ( int i = 0; i < size; i++ ) {
number[i] = new char[grid_size];
for( int j = 0; j < size; j++)
number[i][j] = i * size + j;
}
// access any char with a double bracket notation
// beware that coordinates start at `0`
number[2][1] = 'X';
The second solution is trickier, and I recommend using a helper function to translate matrix coordinates into the array index:
const int grid_size = size * size;
int idx(int x, int y){
return y * size + x;
}
char *number = new char[grid_size];
for (int i = 0; i < grid_size; i++)
number[i] = i;
number[idx(2,1)] = 'O';
Characters are actually numbers in disguise, but with values way above 9
, so for a regular tic-tac-toe game it should not be a problem, and you should be able to have it work with size up to 8*8. Starting from 65, the numbers start representing regular alphabet characters (ie. 65 is A
, 66 is B
and so on, look up the ASCII code table for more details). One way to overcome the issue, if you want to store the cell number in the cell and also keep track of the state of the game, is to use 254 and 255 as the values denoting the player moves, instead of O
and X
, but you will still be limited to a 7*7 grid size.
For larger size, you will probably need consider another data structure to track the game state.