Public Group

# Array problems

This topic is 4957 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everybody! I hope that someone can help me with this little problem. I'm making a DOS-based "Naughts and Crosses" with C++. On the screen it looks something like this: ------------------- ------------------- ------##----------- ------##----------- ------------------- -------------##---- -------------##---- ------------------- ------------------- The point with having the blocks of "hash chars" (#) is that players are not supposed to be able to move over these nor be able to put a marker there (Nought or Cross). I move around the level using the arrow keys on the keyboard and the program redraw the level for every getch() equal to the arrow keys. I define the position of the players using the following: x + (y * Width) Now the problem: I don't know how to make it impossible for the player to move over these blocks. How do I get the program to check if the position the player is trying to move to is a "#"? If you have any idea...please let me know. A code sample would be great aswell. Thanks!

##### Share on other sites
Well, I'm going to assume you have set up a 2-dimensional array to draw your board in the first place. That array knows where it's a naught or a cross at that point.

So, whenever the player inputs something, check the space that they are moving on against your board drawing array. If the array says that it's a #, don't let them move there, if the array says it's a -, let them move there.

I'm not terribly familiar with the game so I'm not sure what you mean by "move over", but you can check whether it's been moved over by storing the last player move, and then seeing if the spaces between the moves have # in them.

It would look something like this:

// tileArray[x][y] is your 2 dimensional board array// player.x and player.y is the player's input x and y for placementif (tileArray[player.x][player.y] == '#')  cout << "Can't move there";else  tileArray[player.x][player.y] = 'whatever character you're putting';

That help?

-fel

##### Share on other sites
And if it's not a matrix(2D array), you use your formula like this
if (tileArray[x + (y * width)] == '#')  cout << "Can't move there";else  tileArray[x + (y * width)] = 'whatever character you're putting';

[grin]

##### Share on other sites
Thanks man!

Appreciate the help.

• 10
• 16
• 14
• 18
• 15