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.
Can someone please help me with this?
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;
}