# Thread: 3D Tic Tac Toe Help

1. ## 3D Tic Tac Toe Help

So I'm trying to create a 3D Tic Tac Toe game where two AI play against each other.

Right now I have 3 separate [3][3] board with different names, a variable that generates a random number between 1-3 (which represent the 3 boards), and a variable that generates a random number between 1-9 that represent the 9 spaces on the board.

My first idea was to create a switch case that takes the board choice variable, so if the number generated is 2, it will go to case 2, which represents board 2. Inside the case is a bunch of if/else statements which takes the board space number variable and checks which one was generated.

So if Case/Board 1, space 1 was generated, it will first check if that space has an X or O already there. If it does, it will say "This space is already take", else it will place the X or O in that spot.

Except that when I run the code, it automatically runs the "This space is already take" statement even if there is nothing there.

Right now I only have Case 1, Space 1 set in the way I described above. I also have the board and space variables set to 1 so that they automatically go to Case/board 1, Space 1, to test if it would even work.

Code:
```#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

//---------------------------------------------------------------BOARD DECLARATIONS------------------------------------------------------------------------------
char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
char board2[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
char board3[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
//----------------------------------------------------------------GLOBAL VARIABLES/FUNCTIONS--------------------------------------------------------------------------------------
int SpaceChoice = 1;
int BoardChoice = 1;
char AIlaura = 'O';
//----------------------------------------------------------------RANDOM AI SELECTOR----------------------------------------------------------------------------------------
int LauraAI()
{
printf ("\nLaura-AI chose board %d to play in\n", BoardChoice);
switch (BoardChoice){
case 1://----------------------------------------------------BOARD ONE-------------------------
printf("Laura Chose Space %d\n", SpaceChoice);
if (SpaceChoice == 1)
{
if (board [0][0] = 'O' || 'X')
{
cout << "This Spot Is Already Taken" << endl;
}
else board [0][0] == 'O';
}
else if (SpaceChoice == 2)
{
if(board[0][1] == 'O')
board[0][1] = 'X';
else
{
cout << "This Spot Is Already Taken" << endl;
LauraAI();
}
}
else if (SpaceChoice == 3)
{
if (board[0][2] == 'O')
board[0][2] = 'X';
else
{
cout << "This Spot Is Already Taken" << endl;
LauraAI();
}
}
else if (SpaceChoice == 4)
{
if (board[1][0] == 'O')
board[1][0] = 'X';
else
{
cout << "This Spot Is Already Taken" << endl;
LauraAI();
}
}
else if (SpaceChoice == 5)
{
if (board[1][1] == 'O')
board[1][1] == 'X';
else
{
cout << "This Spot Is Already Taken" << endl;
LauraAI();
}
}
else if (SpaceChoice == 6)
{
if (board[1][2] == 'O')
board[1][2] == 'X';
else
{
cout << "This Spot Is Already Taken" << endl;
LauraAI();
}
}
else if (SpaceChoice == 7)
{
if (board[2][0] == 'O')
board[2][0] == 'X';
else
{
cout << "This Spot Is Already Taken" << endl;
LauraAI();
}
}
else if (SpaceChoice == 8)
{
if (board[2][1] == 'O')
board[2][1] == 'X';
else
{
cout << "This Spot Is Already Taken" << endl;
LauraAI();
}
}
else if (SpaceChoice == 9)
{
if (board[2][2] == 'O')
board[2][2] == 'X';
else
{
cout << "This Spot Is Already Taken" << endl;
LauraAI();
}
}
break;
case 2://------------------------------------------------------BOARD TWO
printf (" Hello!");
break;
case 3: //--------------------------------------------------- BOARD THREE
printf ("goodbye!");
break;
}
}

int main ()
{
cout << "\n\tWelcome To Tic Tac Toe!\n" << endl;
printf ("Which Board would you like to play in (1-3): \n");
LauraAI();
return 0;
}```

2. First, fix these.
Code:
```foo.cpp: In function ‘int LauraAI()’:
foo.cpp:20:66: error: ‘printf’ was not declared in this scope
printf ("\nLaura-AI chose board %d to play in\n", BoardChoice);
^
foo.cpp:26:38: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if (board [0][0] = 'O' || 'X')
^
foo.cpp:30:27: warning: statement has no effect [-Wunused-value]
else board [0][0] == 'O';
^
foo.cpp:65:25: warning: statement has no effect [-Wunused-value]
board[1][1] == 'X';
^
foo.cpp:75:25: warning: statement has no effect [-Wunused-value]
board[1][2] == 'X';
^
foo.cpp:85:25: warning: statement has no effect [-Wunused-value]
board[2][0] == 'X';
^
foo.cpp:95:25: warning: statement has no effect [-Wunused-value]
board[2][1] == 'X';
^
foo.cpp:105:25: warning: statement has no effect [-Wunused-value]
board[2][2] == 'X';
^
foo.cpp:120:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
foo.cpp: In function ‘int main()’:
foo.cpp:126:62: error: ‘printf’ was not declared in this scope
printf ("Which Board would you like to play in (1-3): \n");
^```
2. LauraAI();
Calling yourself recursively just to implement a loop is very bad code.

3. Choose C or choose C++
#include <iostream>
#include <string>
#include <stdlib.h>

The random mix of printf and cout isn't doing you any favours.

4. Your global variables aren't doing you any favours either.

5. Why?
Code:
```char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
char board2[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
char board3[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};```
I mean, come on, it's 3D TTT
Code:
```char board[3][3][3] = {
{{'1','2','3'},{'4','5','6'},{'7','8','9'}},
{{'1','2','3'},{'4','5','6'},{'7','8','9'}},
{{'1','2','3'},{'4','5','6'},{'7','8','9'}},
};```