0

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';
5
  • The conversion is the same as you have already done in the for loop: grid[centerh * col + centerw] = 'o'; Commented Dec 9, 2015 at 10:39
  • A competent C++ programmer would write an accessor function to enable 2d access to a 1d array. But either inside or instead of that function, the pattern you already used in one place should work everywhere. I guess you copied that first change from someone without understanding it. Maybe one more example will help: Change [centerh - 1][centerw - 1] to [(centerh - 1)*col+centerw - 1] Commented Dec 9, 2015 at 10:39
  • thank @JSF another thing is there any difference between grid[i * col + j] and grid[i * row + j] when the 2d array is being converted to 1d ? Commented Dec 9, 2015 at 16:00
  • Yes, there is a very big difference. If you are consistent, you can choose either i*col+j or j*row+i (where i represents a vertical position and j a horizontal position). But you can't mix the two in different accesses to the same grid and you can't switch vertical and horizontal coordinates in a way inconsistent with switching the choice of multiplier (I think your i*row+j is that kind of inconsistent switch of coordinates, so always wrong, but I don't know without seeing the surrounding code). Commented Dec 9, 2015 at 16:04
  • In C, the really ugly macro suggestion by StephenG might be the best choice (depending on other design constraints). In C++ something that terrible is definitely NOT a good choice. This highlights the problem of putting both C++ and C tags on a question. The answers are not the same. Commented Dec 9, 2015 at 16:10

2 Answers 2

1

This converts your 2D grid into 1D :

grid1D[row*col];
grid2D[row][col];

for(int i = 0; i < row; ++i)
    for(int j = 0; j < col; ++j)
          grid1D[i * col + j] = grid2D[i][j];
Sign up to request clarification or add additional context in comments.

Comments

1

In C I would use macros and a 1D array s a basis for this sort of thing.

Something like :

#define WIDTH 10
#define HEIGHT 10

char grid[ WIDTH * HEIGHT ] ;

#define ELEMENT(row,column)    grid[ ( (row)*WIDTH ) + (column) ]

/* Read an element */

char c ;

c = ELEMENT( 5, 7 ) ;

/* write to an element */

ELEMENT( 5, 7 ) = 'x' ;

/* access the array in 1D is trivial as you simply use grid[] directly */

So you can use the same 1D array as a 2D item without duplication.

One important point : avoid post- and pre- decrement operations when using macros. The reason for this is that they can lead to confusing errors, as macros are not functions and each "parameter" of the macro is simply text that replaces the corresponding macro "parameter".

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.