Public Group

# 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.

## 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 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 falsePieces = bool[9][9]  // filled for 1 sidefor 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 .....

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633700
• Total Posts
3013426
×