Jump to content
  • Advertisement
Sign in to follow this  
password

Checking an Array

This topic is 4326 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

I'm making a 9x9 tic tac toe game where I use a std::vector for the board. I wonder if you know any efficient ways to check if 4-5 pieces in a row occurs somewhere on the board. I realised that this is quite different from a 3x3 tic tac toe game, since all you have to do when the board is 3x3 is to put all winning rows into an array which easily makes the code clumsy and it's hard to do if you have a much bigger board. Should I stick to the array solution where I put all the winning moves into an array or is there a better way?

Share this post


Link to post
Share on other sites
Advertisement
Think about it in terms of positions NOT part of a winning solution.
Each position can be part of a solution in one of 4 directions. You can search for a winning position, storing whether or not each position is part of a solution. This avoids having to check every direction for every position.



Dirs = { V : bool, H : bool, DL : bool, DR : bool }

Directions = Dirs[9][9] // assume all initialized to false
Pieces = bool[9][9] // filled for 1 side

for y = 0; y < 9:
for x = 0; x < 9:
if Pieces[x][y]:
if !Directions[x][y].V:
Directions[x][y].V := true
search vertical, filling the Directions array appropriately
if !Directions[x][y].H:
Directions[x][y].H := true
search horizontal, filling the ...
if ..
...

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!