# help with 3D tic-tac-toe diagonal checking

• 04-01-2006
tunerfreak
help with 3D tic-tac-toe diagonal checking
i need some help with figuring out how to check diagonals in my 3D Tic-Tac-Toe board. i have the horizontal, vertical and depth done fine but i cant figure out how to get diagonals. i only put in the check part since the full code is really long. the problem is that im making it to that you choose what cubic dimensions you want and i dont know how to go through the board and only check diagonal. i can put more code up if you need it too.

Code:

```template <class TYPE> class Grid : public Darray<TYPE> { private:         int currentRow,                 currentDepth,                 currentCol; public:         Grid();         Grid(TYPE depth, TYPE row, TYPE col);         void arrow_keys(Player *player);         bool win();         void clear_screen(void);         void draw ();         void draw (TYPE d);         void topBorder();         void bottomBorder();         bool winHorizontal(Player* player);         bool winVertical(Player* player);         bool winDiagonal(Player* player);         bool winCubic(Player* player); }; template <class TYPE> bool Grid<TYPE>::winHorizontal(Player* player) {         int count;         for(int d = 0; d <DEPTH; d++){                 for(int r = 0; r<ROW; r++){                         for(int c = 0; c< COL; c++)                         {                                 if(*player == *array[d][r][c].getPlayer())                                         count++;                         }                         if(count == COL)                                 return true;                         count = 0;                        }         }                        cout << "Player " << symbol << " Wins!!" << endl;                 return false; } template <class TYPE> bool Grid<TYPE>::winVertical(Player* player) {         int count;         for(int d = 0; d <DEPTH; d++){                 for(int r = 0; r<ROW; r++)                 {                         if(*player == *array[d][r][c].getPlayer())                                 count++;                 }                 if(count == ROW)                         return true;                 count = 0;         }         cout << "Player " << symbol << " Wins!!" << endl;         return false; } template <class TYPE> bool Grid<TYPE>::winDiagonal(Player* player) {         int count;         for(int d = 0; d <DEPTH; d++){                 for(int r = 0; r<ROW; r++){                         for(int c = 0; c< COL; c++)                         {                                 if(*player == *array[d][r][c].getPlayer() || *player == *array[d][r][c].getPlayer() )                                         count++;                         }                         if()                                 return true;                 }         }         cout << "Player " << symbol << " Wins!!" << endl;         return false; } template <class TYPE> bool Grid<TYPE>::winCubic(Player* player) {         int count;         for(int d = 0; d < DEPTH; d++)         {                 if(*player == *array[d][r][c].getPlayer())                         count++;         }         if(count == DEPTH)                 return true;         count  = 0;         cout << "Player " << symbol << " Wins!!" << endl;         return false; } template <class TYPE> bool Grid<TYPE>::win() {         if(winHorizontal())                 return true;         if(winVertical())                 return true;         if(winDiagonal())                 return true;         if(winCubic())                 return true;         else                 return false; }```
• 04-01-2006
jafet
Well, without looking at your code yet (hint: too long methinks!), I'd suggest something along the lines the following:

Code:

```Dimensions x,y,z Foreach(x)     test diagonal in plane x Foreach(y)     test diagonal in plane y Foreach(z)     test diagonal in plane z```
ie. you could extract each plane from each dimension (3 * n times where n is cube length) and test for winDiagonal() on each of them.
• 04-01-2006
VirtualAce
Why is your board a template? What could you possible store in a tic tac toe board other than integers and why?

You've got a mess there.

Suggestion:

1. Make a board class that encapsulate everything possible a board should/can do.
2. Make another class to encapsulate the layers of boards. Use a std vector or list to maintain the boards or if you know how many boards, create an array of class Board.
3. Dump the nested loop. It's hideous.
• 04-01-2006
tunerfreak
its a template cause thats what the assignment is suppose to have. right now im just trying to get it working then i worry about making the code look perty.

jafet could u elaborate a little more i dont quite understand what u mean by multiply by 3*n how would that work?