I have 2D array as a grid that can be thought a game board. When the board is initialized so the game is started there are four men. It works for nxn grid. As an example
x o
o x
I do it using a 2D array. Now, I try to convert the array to 1D. I'm stuck on how I can put the symbols on the grid for 1D array.
for(int i = 0; i < row; ++i)
for(int j = 0; j < col; ++j)
{
//grid[i][j] = '.';
grid[i * col + j] = '.'; // I've converted the part
}
int centerh = row / 2;
int centerw = col / 2;
// I'm stuck that part.
grid[centerh][centerw] = 'o';
grid[centerh - 1][centerw - 1] = 'o';
grid[centerh][centerw - 1] = 'x';
grid[centerh - 1][centerw] = 'x';
grid[centerh * col + centerw] = 'o';[centerh - 1][centerw - 1]to[(centerh - 1)*col+centerw - 1]grid[i * col + j]andgrid[i * row + j]when the 2d array is being converted to 1d ?i*col+jorj*row+i(whereirepresents a vertical position andja horizontal position). But you can't mix the two in different accesses to the samegridand you can't switch vertical and horizontal coordinates in a way inconsistent with switching the choice of multiplier (I think youri*row+jis that kind of inconsistent switch of coordinates, so always wrong, but I don't know without seeing the surrounding code).