This is an assignment in a programming class I am currently in. In a previous assignment we were required to program a 3x3x3 TTT board where you it was player vs computer for a single game. The objective, is to get as many 3-in-a-rows as possible before the entire board is full.
Now I am having two computers play against each other, but they have to play 10 games in a row and whichever computer wins the most of the 10 games is the overall winner. The code runs and the two computers play all 10 games, however, I am having a hard time figuring out how to add up all the scores from the 10 games so that I can create an overall score for each player and ultimately a winner. Each time a new game is played the score is restarted to 0 and the process begins again until all games are played. The code for the program is below, any help is appreaciated. I am fairly new at C/C++ as this is my first semester learning it.
Code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <time.h>
using namespace std;
class boardGame //board class
{
public:
int board [3][3]; //initializes array
boardGame() //constructor that initializes array
{
for(int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
board [x][y] = 0;
}
}
}
~boardGame() {} //frees memory
void setX(int x, int y); //set square to X
void setO(int x, int y); //set square to Y
bool vacancy(int x, int y); //check if space is full
void draw(); //draws board
string checkMarks(int x); //inserts X's & O's
bool checkWinner(); //check for winner
void comp2Move(); //lets user make moves
void comp1Move();
};
class tdBoard: public boardGame
{
public:
int boardTwo [3][3][3];
int comp2Score;
int comp1Score;
tdBoard()
{
for(int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
for(int z = 0; z < 3; z++)
{
boardTwo [x][y][z] = 0;
}
}
}
comp2Score = 0;
comp1Score = 0;
}
void draw();
~tdBoard() {}
void setX(int z, int x, int y);
void setO(int z, int x, int y);
bool vacancy(int z, int x, int y);
string checkMarks(int x);
int checkWinner();
void comp2Move();
void comp1Move();
bool checkFull();
};
//set space to X
void boardGame::setX(int x, int y)
{
if(vacancy(x,y))
{
board[x][y] = 8;
}
else
{
cout<<"This space is taken!";
}
}
//set space to O
void boardGame::setO(int x, int y)
{
if(vacancy(x,y))
{
board [x] [y] = 9;
}
else
{
cout<<"This space is taken!";
}
}
bool boardGame::vacancy(int x, int y)
{
if(board[x][y] != 0)
{
return 0;
}
else
{
return 1;
}
}
bool boardGame::checkWinner()
{
int result = 50;
//check rows
for(int x = 0; x < 3; x++)
{
if(((board [x][0] == 8)&&(board [x][1] == 8)&&(board[x][2] == 8))||((board [x][0] == 9)&&(board[x][1] == 9)&&(board [x][2] == 9)))
{
result = board[x][0];
}
}
//check columns
for(int y = 0; y < 3; y++)
{
if(((board[0][y] == 8)&&(board[1][y] == 8)&&(board[2][y] == 8))||((board[0][y] == 9)&&(board[1][y] == 9)&&(board[2][y])))
{
result = board[0][y];
}
}
//check diagnols
if((((board [0][0] == 8)&&(board [1][1] == 8)&&(board [2][2] == 8))||((board [0][2] == 8)&&(board [1][1] == 8)&&(board [2][0] == 8))||
((board [0][0] == 9)&&(board [1][1] == 9)&&(board [2][2] == 9))||((board [0][2] == 9)&&(board [1][1] == 9)&&(board [2][0] == 9))))
{
result = board[1][1];
}
//check filled spaces
int fullCount = 0;
for(int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
if(board [x][y] != 0)
{
fullCount++;
}
}
}
}
//print board
void boardGame::draw()
{
cout<<"\n";
cout<<" 0 1 2 \n";
for(int x = 0; x < 3; x++)
{
cout<<x;
for(int y = 0; y < 3; y++) //draw coordinates
{
cout<<"|"<<checkMarks(board [x][y])<<"|"; //draw board
}
cout<<endl;
}
}
//print marks
string boardGame::checkMarks(int x)
{
if(x == 0)
{
return " "; //space empty
}
else if(x == 8)
{
return "X"; //space marked by user
}
return "O"; //space marked by computer
}
//user move
void boardGame::comp2Move()
{
int exit = 0;
int xCoord;
int yCoord;
do
{
//rand(time(NULL));
//srand ((unsigned)time(NULL));
xCoord = rand() % 3;
yCoord = rand() % 3;
if(vacancy(xCoord, yCoord))
{
setX(xCoord, yCoord);
exit = 1;
}
}while(exit == 0);
}
//computer move
void boardGame::comp1Move()
{
int exit = 0;
int compXCoord;
int compYCoord;
do
{
//rand(time(NULL));
//srand ((unsigned)time(NULL));
compXCoord = rand() % 3;
compYCoord = rand() % 3;
if(vacancy(compXCoord, compYCoord))
{
setO(compXCoord, compYCoord);
exit = 1;
}
}while(exit == 0);
}
void tdBoard::setX(int z, int x, int y)
{
boardTwo [x][y][z] = 8;
}
void tdBoard::setO(int z, int x, int y)
{
boardTwo [x][y][z] = 9;
}
bool tdBoard::vacancy(int x, int y, int z)
{
if(boardTwo [x][y][z] != 0)
{
return 0;
}
else
{
return 1;
}
}
int tdBoard::checkWinner()
{
for(int z = 0; z < 6; z++)
{
for(int x = 0; x< 6; x++)
{
if(((boardTwo [x][0][z] == 8)&&(boardTwo [x][1][z] == 8)&&(boardTwo [x][2][z] == 8))||
((boardTwo [x][0][z] == 9)&&(boardTwo [x][1][z] == 9)&&(boardTwo [x][2][z] == 9)))
{
if((boardTwo [x][0][z] == 8)&&(boardTwo [x][1][z] == 8)&&(boardTwo [x][2][z] ==8))
{
comp2Score++;
}
else
{
comp1Score++;
}
}
}
for(int y = 0; y < 3; y++)
{
if(((boardTwo [0][y][z] == 8)&&(boardTwo [1][y][z] == 8)&&(boardTwo [2][y][z] == 8))||
((boardTwo [0][y][z] == 9)&&(boardTwo [1][y][z] == 9)&&(boardTwo [2][y][z] == 9)))
{
if((boardTwo [0][y][z] == 8&&(boardTwo [1][y][z] == 8)&&(boardTwo [2][y][z] == 8)))
{
comp2Score++;
}
else
{
comp1Score++;
}
}
}
if((((boardTwo [0][0][z] == 8)&&(boardTwo [1][1][z] == 8)&&(boardTwo [2][2][z] == 8))||
((boardTwo [0][2][z] == 8)&&(boardTwo [1][1][z] == 8)&&(boardTwo [2][0][z] == 8)))||
((boardTwo [0][0][z] == 9)&&(boardTwo [1][1][z] == 9)&&(boardTwo [2][2][z] == 9))||
((boardTwo [0][2][z] == 9)&&(boardTwo [1][1][z] == 9)&&(boardTwo [2][0][z] == 9)))
{
if(((boardTwo [0][0][z] == 8)&&(boardTwo [1][1][z] == 8)&&(boardTwo [2][2][z] == 8))||
((boardTwo [0][2][z] == 8)&&(boardTwo [1][1][z] == 8)&&(boardTwo [2][0][z] == 8)))
{
comp2Score++;
}
else
{
comp1Score++;
}
}
}
int z = 0;
//top left
if(boardTwo [0][0][z]+boardTwo [0][0][z+1]+boardTwo [0][0][z+2] == 24)
{
comp2Score++;
}
//top middle
if(boardTwo [1][0][z]+boardTwo [1][0][z+1]+boardTwo [1][0][z+2] == 24)
{
comp2Score++;
}
//top right
if(boardTwo [2][0][z]+boardTwo [2][0][z+1]+boardTwo [2][0][z+2] == 24)
{
comp2Score++;
}
//middle left
if(boardTwo [0][1][z]+boardTwo [0][1][z+1]+boardTwo [0][1][z+2] == 24)
{
comp2Score++;
}
//middle middle
if(boardTwo[1][1][z]+boardTwo[1][1][z+1]+boardTwo [1][1][z+2] == 24)
{
comp2Score++;
}
//middle right
if(boardTwo[2][1][z]+boardTwo[2][1][z+1]+boardTwo [2][1][z+2] == 24)
{
comp2Score++;
}
//bottom left
if(boardTwo[0][2][z]+boardTwo[0][2][z+1]+boardTwo [0][2][z+2] == 24)
{
comp2Score++;
}
//bottom middle
if(boardTwo[1][2][z]+boardTwo[1][2][z+1]+boardTwo [1][2][z+2] == 24)
{
comp2Score++;
}
//bottom right
if(boardTwo[2][2][z]+boardTwo[2][2][z+1]+boardTwo [2][2][z+2] == 24)
{
comp2Score++;
}
//Diagonals
//top baord top left to bottom board bottom right
if(boardTwo[0][0][z]+boardTwo[1][1][z+1]+boardTwo [2][2][z+2] == 24)
{
comp2Score++;
}
//top board top right to bottom board bottom left
if(boardTwo[2][0][z]+boardTwo[1][1][z+1]+boardTwo [0][2][z+2] == 24)
{
comp2Score++;
}
//top board bottom left to bottom board top right
if(boardTwo[0][2][z]+boardTwo[1][1][z+1]+boardTwo [2][0][z+2] == 24)
{
comp2Score++;
}
//top board top right to bottom board top left
if(boardTwo[2][2][z]+boardTwo[1][1][z+1]+boardTwo [0][0][z+2] == 24)
{
comp2Score++;
}
//top board top middle to bottom board bottom middle
if(boardTwo[1][0][z]+boardTwo[1][1][z+1]+boardTwo [1][2][z+2] == 24)
{
comp2Score++;
}
//bottom board top middle to top board bottom middle
if(boardTwo[1][2][z]+boardTwo[1][1][z+1]+boardTwo [1][0][z+2] == 24)
{
comp2Score++;
}
//top board left middle to bottom board right middle
if(boardTwo[0][1][z]+boardTwo[1][1][z+1]+boardTwo [2][1][z+2] == 24)
{
comp2Score++;
}
//bottom board left middle to top board right middle
if(boardTwo[2][1][z]+boardTwo[1][1][z+1]+boardTwo [0][1][z+2] == 24)
{
comp2Score++;
}
//side diaganols
//top board bottom right to bottom board bottom left
if(boardTwo[2][2][z]+boardTwo[1][2][z+1]+boardTwo [0][2][z+2] == 24)
{
comp2Score++;
}
//top board bottom left to bottom board bottom right
if(boardTwo[0][2][z]+boardTwo[1][2][z+1]+boardTwo [2][2][z+2] == 24)
{
comp2Score++;
}
//top board bottom left to bottom board top left
if(boardTwo[0][2][z]+boardTwo[0][1][z+1]+boardTwo [0][0][z+2] == 24)
{
comp2Score++;
}
//top board top left to bottom board bottom left
if(boardTwo[0][0][z]+boardTwo[0][1][z+1]+boardTwo [0][2][z+2] == 24)
{
comp2Score++;
}
//top board top left to bottom board top right
if(boardTwo[0][0][z]+boardTwo[1][0][z+1]+boardTwo [2][0][z+2] == 24)
{
comp2Score++;
}
//top board top right to bottom board top left
if(boardTwo[2][0][z]+boardTwo[1][0][z+1]+boardTwo [0][0][z+2] == 24)
{
comp2Score++;
}
//top board top right to bottom board bottom right
if(boardTwo[2][0][z]+boardTwo[2][1][z+1]+boardTwo[2][2][z+2] == 24)
{
comp2Score++;
}
//top board bottom right to bottom board top right
if(boardTwo[2][2][z]+boardTwo[2][1][z+1]+boardTwo [2][0][z+2] == 24)
{
comp2Score++;
}
//computer conditions
//top left
if(boardTwo[0][0][z]+boardTwo[0][0][z+1]+boardTwo [0][0][z+2] == 27)
{
comp1Score++;
}
//top middle
if(boardTwo[1][0][z]+boardTwo[1][0][z+1]+boardTwo [1][0][z+2] == 27)
{
comp1Score++;
}
//top right
if(boardTwo[2][0][z]+boardTwo[2][0][z+1]+boardTwo [2][0][z+2] == 27)
{
comp1Score++;
}
//middle left
if(boardTwo[0][1][z]+boardTwo[0][1][z+1]+boardTwo [0][1][z+2] == 27)
{
comp1Score++;
}
//middle middle
if(boardTwo[1][1][z]+boardTwo[1][1][z+1]+boardTwo [1][1][z+2] == 27)
{
comp1Score++;
}
//middle right
if(boardTwo[2][1][z]+boardTwo[2][1][z+1]+boardTwo [2][1][z+2] == 27)
{
comp1Score++;
}
//bottom left
if(boardTwo[0][2][z]+boardTwo[0][2][z+1]+boardTwo [0][2][z+2] == 27)
{
comp1Score++;
}
//bottom middle
if(boardTwo[1][2][z]+boardTwo[1][2][z+1]+boardTwo [1][2][z+2] == 27)
{
comp1Score++;
}
//bottom right
if(boardTwo[2][2][z]+boardTwo[2][2][z+1]+boardTwo [2][2][z+2] == 27)
{
comp1Score++;
}
//Diagonals
//top baord top left to bottom board bottom right
if(boardTwo[0][0][z]+boardTwo[1][1][z+1]+boardTwo [2][2][z+2] == 27)
{
comp1Score++;
}
//top board top right to bottom board bottom left
if(boardTwo[2][0][z]+boardTwo[1][1][z+1]+boardTwo [0][2][z+2] == 27)
{
comp1Score++;
}
//top board bottom left to bottom board top right
if(boardTwo[0][2][z]+boardTwo[1][1][z+1]+boardTwo [2][0][z+2] == 27)
{
comp1Score++;
}
//top board top right to bottom board top left
if(boardTwo[2][2][z]+boardTwo[1][1][z+1]+boardTwo [0][0][z+2] == 27)
{
comp1Score++;
}
//top board top middle to bottom board bottom middle
if(boardTwo[1][0][z]+boardTwo[1][1][z+1]+boardTwo [1][2][z+2] == 27)
{
comp1Score++;
}
//bottom board top middle to top board bottom middle
if(boardTwo[1][2][z]+boardTwo[1][1][z+1]+boardTwo [1][0][z+2] == 27)
{
comp1Score++;
}
//top board left middle to bottom board right middle
if(boardTwo[0][1][z]+boardTwo[1][1][z+1]+boardTwo [2][1][z+2] == 27)
{
comp1Score++;
}
//bottom board left middle to top board right middle
if(boardTwo[2][1][z]+boardTwo[1][1][z+1]+boardTwo [0][1][z+2] == 27)
{
comp1Score++;
}
//side diaganols
//top board bottom right to bottom board bottom left
if(boardTwo[2][2][z]+boardTwo[1][2][z+1]+boardTwo[0][2][z+2] == 27)
{
comp1Score++;
}
//top board bottom left to bottom board bottom right
if(boardTwo[0][2][z]+boardTwo[1][2][z+1]+boardTwo[2][2][z+2] == 27)
{
comp1Score++;
}
//top board bottom left to bottom board top left
if(boardTwo[0][2][z]+boardTwo[0][1][z+1]+boardTwo[0][0][z+2] == 27)
{
comp1Score++;
}
//top board top left to bottom board bottom left
if(boardTwo[0][0][z]+boardTwo[0][1][z+1]+boardTwo[0][2][z+2] == 27)
{
comp1Score++;
}
//top board top left to bottom board top right
if(boardTwo[0][0][z]+boardTwo[1][0][z+1]+boardTwo[2][0][z+2] == 27)
{
comp1Score++;
}
//top board top right to bottom board top left
if(boardTwo[2][0][z]+boardTwo[1][0][z+1]+boardTwo[0][0][z+2] == 27)
{
comp1Score++;
}
//top board top right to bottom board bottom right
if(boardTwo[2][0][z]+boardTwo[2][1][z+1]+boardTwo[2][2][z+2] == 27)
{
comp1Score++;
}
//top board bottom right to bottom board top right
if(boardTwo[2][2][z]+boardTwo[2][1][z+1]+boardTwo[2][0][z+2] == 27)
{
comp1Score++;
}
//results
if(comp2Score<comp1Score)
{
return 0; //user lost
}
else if(comp2Score > comp1Score)
{
return 1; //user won
}
else
{
return 2; //tie game
}
}
//draw baord
void tdBoard::draw()
{
cout<<"\n";
for(int z = 0; z < 3; z++)
{
cout<<"Board: "<<z<<endl;
cout<<" 0 1 2 \n";
for(int y = 0; y < 3; y++)
{
cout<<y;
for(int x = 0; x < 3; x++)
{
cout<<"|"<<checkMarks(boardTwo[x][y][z])<<"|";
}
cout<<endl;
}
cout<<endl;
}
cout<<"\n";
}
//display marks
string tdBoard::checkMarks(int x)
{
if(x == 0)
{
return " ";
}
else if(x == 8)
{
return "X";
}
return "O";
}
void tdBoard::comp2Move()
{
srand(time(NULL));
srand ((unsigned)time(NULL));
int exit = 0;
int xCoord;
int yCoord;
int zCoord;
do
{
xCoord = rand()%3;
yCoord = rand()%3;
zCoord = rand()%3;
if(vacancy(xCoord,yCoord,zCoord))
{
setX(zCoord,xCoord,yCoord);
return;
}
if(vacancy(xCoord,yCoord,zCoord))
{
setO(zCoord,xCoord,yCoord);
exit = 1;
}
}while(exit == 0);
}
//computer move method
void tdBoard::comp1Move()
{
srand(time(NULL));
srand ((unsigned)time(NULL));
int exit = 0;
int compXCoord;
int compYCoord;
int compZCoord;
do
{
compXCoord = rand()%3;
compYCoord = rand()%3;
compZCoord = rand()%3;
if(vacancy(compXCoord,compYCoord,compZCoord))
{
setO(compZCoord,compXCoord,compYCoord);
return;
}
if(vacancy(compXCoord,compYCoord,compZCoord))
{
setO(compZCoord,compXCoord,compYCoord);
exit = 1;
}
}while(exit == 0);
}
//check for board being full
bool tdBoard::checkFull()
{
int totalCount = 0;
for(int z = 0; z < 3; z++)
{
for(int y = 0; y < 3; y++)
{
for(int x = 0; x < 3; x++)
{
if(boardTwo[x][y][z] != 0)
{
totalCount++;
}
}
}
}
if(totalCount == 27)
{
return 1;
}
else
{
return 0;
}
}
//main function
int playgame()
{
tdBoard game;
int userTurn;
srand((unsigned)time(NULL));
userTurn = rand()%2;
bool contGame = 1;
do
{
game.draw();
if(userTurn == 1)
{
game.comp2Move();
userTurn = 0;
cout<<"Player 2's (X) move: "<<endl; //replace with player name
}
else
{
game.comp1Move();
userTurn = 1;
cout<<"Player 1's (O) move: "<<endl; //replace with player name
}
if(game.checkFull())
{
contGame = 0;
}
}
while(contGame == 1);
game.draw();
if(game.checkWinner() == 1)
{
cout<<"\nPlayer 1 (O) Score: "<<game.comp1Score;
cout<<"\nPlayer 2 (X) Score: "<<game.comp2Score<<endl;
cout<<"\nPlayer 2 (X) Won!";
}
else if(game.checkWinner() == 0)
{
cout<<"\nPlayer 1 (O) Score: "<<game.comp1Score;
cout<<"\nPlayer 2 (X) Score: "<<game.comp2Score<<endl;
cout<<"\nPlayer 1 (O) Won!";
}
else if(game.checkWinner() == 2)
{
cout<<"\nPlayer 1 (O) Score: "<<game.comp1Score;
cout<<"\nPlayer 2 (X) Score: "<<game.comp2Score<<endl;
cout<<"\nIts a tie!";
}
cout<<"\nGame Over!";
return 0;
}
int main()
{
int i = 0;
while(i < 10)
{
playgame();
i++;
}
}