Question
(In C++) I need help creating win cases in this tic tac toe game and how to split each win case in a function and
(In C++) I need help creating win cases in this tic tac toe game and how to split each win case in a function and call it properly in the main function. Currently all of my win cases are in bool detectGameOver(char board[][MAX_WIDTH], int n). Please email me if you would like the actual file.
Here is my code:
#include
using namespace std;
const int MAX_WIDTH = 10;
const int MAX_HEIGHT = 10;
void printBoard(char board[][MAX_WIDTH], int n);
void initBoard(char board[][MAX_WIDTH], int n);
int inputN;
void playerInput(char board[][MAX_WIDTH], int n, bool isPlayerOne);
bool detectGameOver(char board[][MAX_WIDTH], int n);
int main()
{
char board[MAX_HEIGHT][MAX_WIDTH];
int n;
n = inputN();
initBoard(board, n);
bool isPlayerOne = true;
bool gameOver = false;
while(!gameOver)
{
printBoard(board, n);
playerInput(board, n, isPlayerOne);
//temporarily
gameOver = true;
isPlayerOne != isPlayerOne;
gameOver = detectGameOver(board, n);
}
return 0;
}
bool detectGameOver(char board[][MAX_WIDTH], int n)
{
///detect row wins
int xCounter = 0, oCounter = 0;
for(int j = 0; j < n; j++)
{
xCounter = 0;
oCounter = 0;
for(int i = 0; i < n; i++)
{
if(board[j][i] == 'X')
{
xCounter++;
}
if(board[j][i] == 'O')
{
oCounter++;
}
}
}
if(xCounter == n)
{
cout << "X Wins!";
return true;
}
if(oCounter == n)
{
cout << "O Wins!";
return true;
}
///detect column wins
int xCounter = 0, oCounter = 0;
for(int j = 0; j < n; j++)
{
xCounter = 0;
oCounter = 0;
for(int i = 0; i < n; i++)
{
if(board[i][j] == 'X')
{
xCounter++;
}
if(board[i][j] == 'O')
{
oCounter++;
}
}
}
if(xCounter == n)
{
cout << "X Wins!";
return true;
}
if(oCounter == n)
{
cout << "O Wins!";
return true;
}
///main diagnal
int xCounter = 0;
int oCounter = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(board[i][j] == 'X')
{
xCounter++;
}
else if(board[i][j] == 'O')
{
oCounter++;
}
}
}
if(xCounter == n)
{
cout << "X Wins!";
return true;
}
if(oCounter == n)
{
cout << "O Wins!";
return true;
}
///off diagnal
int xCounter = 0;
int oCounter = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(board[j][i] == 'X')
{
xCounter++;
}
else if(board[j][i] == 'O')
{
oCounter++;
}
}
}
if(xCounter == n)
{
cout << "X Wins!";
return true;
}
if(oCounter == n)
{
cout << "O Wins!";
return true;
}
///tie
}
void playerInput(char board[][MAX_WIDTH], int n, bool isPlayerOne)
{
int row, col;
cout << "Player ";
if (isPlayerOne)
{
cout << "1: " << endl;
}
else
{
cout << "2: " << endl;
}
do
{
do
{
cout << "Row: ";
cin >> row;
}while(row < 0 || row > n-1);
do
{
cout << "Column: ";
cin >> col;
} while (col < 0 || col > n -1);
}while([board][row][col] != '_');
if(isPlayerOne)
{
board[row][col] = 'X';
}
else
{
board[row][col] = 'O';
}
}
int inputN()
{
int n;
cout << "How big do you want your board: ";
cin >> n;
while (n < 3 || n > 10 || cin.fail())
{
cin.clear();
string garbage;
getline(cin, garbage);
cout << "ERROR: Range must be [3:10]" << endl;
cout << "How big do you want your board: ";
cin >> n;
}
return n;
}
void initBoard(char board[][MAX_WIDTH], int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
board[i][j] = "_";
}
}
}
void printBoard(char board[][MAX_WIDTH], int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started