Jump to content
  • Advertisement
Sign in to follow this  
Methiazon

Array problems

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 placement
if (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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!