Jump to content
  • Advertisement
Sign in to follow this  

Checking an Array

This topic is 4411 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
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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!