Problem on c++ #include <iostream> using namespace std; const int NUM_ROWS = 3;
ID: 3566405 • Letter: P
Question
Problem on c++
#include <iostream>
using namespace std;
const int NUM_ROWS = 3;
const int NUM_COLS = 3;
void displayBoard(const char t[][NUM_COLS]);
bool isWinner(const char t[][NUM_COLS], char);
int main()
{
// declare variables
char t[NUM_ROWS][NUM_COLS];
char player = 'X'; // player is 'X','O', or 'T' ('T' is a tie game)
int numberTurns; // maximum number of turns
bool gameOver = false;
int row; // player's row choice, should be between 1 and 3
int col; // player's col choice, should be between 1 and 3
int i;
int j;
// initialize variables
numberTurns = NUM_ROWS * NUM_COLS;
for (i = 0; i<NUM_ROWS; i++)
for (j = 0; j<NUM_COLS; j++)
t[i][j] = ' ';
// loop while game is not over
while (!gameOver)
{
// display the board and get player's move
displayBoard(t);
cout << "Enter your move (row and col), player " << player << ": ";
cin >> row >> col;
// check for valid row and column and spot had not already been taken
while (row < 1 || row > NUM_ROWS || col < 1 || col > NUM_COLS)
{
cout << "Invalid row and col -- each must be between 1 and 3 ";
cout << "Enter your move (row and col), player " << player << ": ";
cin >> row >> col;
}
// check that spot has not already been taken
while (t[row - 1][col - 1] != ' ')
{
cout << "That row and col already taken ";
cout << "Enter your move (row and col), player " << player << ": ";
cin >> row >> col;
}
// move is valid, mark it and decrement number of turns
t[row - 1][col - 1] = player;
numberTurns--;
// check for a winner
if (isWinner(t, player))
gameOver = true;
else if (numberTurns == 0)
{
// no more moves available
gameOver = true;
player = 'T';
}
else
{
// game is not over, switch players
if (player == 'X')
player = 'O';
else
player = 'X';
}
}
// game is over, display the board and declare the winner
displayBoard(t);
if (player == 'X')
cout << " X's win!" << endl;
else if (player == 'O')
cout << " O's win!" << endl;
else
cout << " It's a tie!" << endl;
return 0;
} // end main
void displayBoard(const char t[][NUM_COLS])
{
int i, j, k;
for (i = 0; i<NUM_ROWS; i++)
for (j = 0; j<NUM_COLS; j++)
if (j != NUM_COLS - 1)
cout << t[i][j] << '|';
else
{
cout << t[i][j] << endl;
if (i != NUM_ROWS - 1)
for (k = 0; k<NUM_COLS; k++)
if (k != NUM_COLS - 1)
cout << "--";
else
cout << "- ";
} // end of else
} // end displayBoard
bool isWinner(const char t[][NUM_COLS], char player)
{
// there are NUM_ROWS + NUM_COLS + 2 (diagonals) ways to win
// check each of them for a winner
// NOTE: you only have to check against the last player to
// make a move -- that player's mark is in the
// variable player
cout << "Checking if " << player << " is winner!" << endl;
return false;
} // end isWinner
Explanation / Answer
#include <iostream>
using namespace std;
const int NUM_ROWS = 3;
const int NUM_COLS = 3;
void displayBoard(const char t[][NUM_COLS]);
bool isWinner(const char t[][NUM_COLS], char);
int main()
{
// declare variables
char t[NUM_ROWS][NUM_COLS];
char player = 'X'; // player is 'X','O', or 'T' ('T' is a tie game)
int numberTurns; // maximum number of turns
bool gameOver = false;
int row; // player's row choice, should be between 1 and 3
int col; // player's col choice, should be between 1 and 3
int i;
int j;
// initialize variables
numberTurns = NUM_ROWS * NUM_COLS;
for (i = 0; i<NUM_ROWS; i++)
for (j = 0; j<NUM_COLS; j++)
t[i][j] = ' ';
// loop while game is not over
while (!gameOver)
{
// display the board and get player's move
displayBoard(t);
cout << "Enter your move (row and col), player " << player << ": ";
cin >> row >> col;
// check for valid row and column and spot had not already been taken
while (row < 1 || row > NUM_ROWS || col < 1 || col > NUM_COLS)
{
cout << "Invalid row and col -- each must be between 1 and 3 ";
cout << "Enter your move (row and col), player " << player << ": ";
cin >> row >> col;
}
// check that spot has not already been taken
while (t[row - 1][col - 1] != ' ')
{
cout << "That row and col already taken ";
cout << "Enter your move (row and col), player " << player << ": ";
cin >> row >> col;
}
// move is valid, mark it and decrement number of turns
t[row - 1][col - 1] = player;
numberTurns--;
// check for a winner
if (isWinner(t, player))
gameOver = true;
else if (numberTurns == 0)
{
// no more moves available
gameOver = true;
player = 'T';
}
else
{
// game is not over, switch players
if (player == 'X')
player = 'O';
else
player = 'X';
}
}
// game is over, display the board and declare the winner
displayBoard(t);
if (player == 'X')
cout << " X's win!" << endl;
else if (player == 'O')
cout << " O's win!" << endl;
else
cout << " It's a tie!" << endl;
return 0;
} // end main
void displayBoard(const char t[][NUM_COLS])
{
int i, j, k;
for (i = 0; i<NUM_ROWS; i++)
for (j = 0; j<NUM_COLS; j++)
if (j != NUM_COLS - 1)
cout << t[i][j] << '|';
else
{
cout << t[i][j] << endl;
if (i != NUM_ROWS - 1)
for (k = 0; k<NUM_COLS; k++)
if (k != NUM_COLS - 1)
cout << "--";
else
cout << "- ";
} // end of else
} // end displayBoard
bool isWinner(const char t[][NUM_COLS], char player)
{
// there are NUM_ROWS + NUM_COLS + 2 (diagonals) ways to win
// check each of them for a winner
// NOTE: you only have to check against the last player to
// make a move -- that player's mark is in the
// variable player
int i, j;
int count = 0;
cout << "Checking if " << player << " is winner!" << endl;
for (i = 0; i < NUM_ROWS; i++)
{
count = 0;
for (j = 0; j < NUM_COLS; j++)
{
if (t[i][j] == player)
count++;
}
if (count == NUM_COLS)
return true;
}
for (i = 0; i < NUM_COLS; i++)
{
count = 0;
for (j = 0; j < NUM_ROWS; j++)
{
if (t[j][i] == player)
count++;
}
if (count == NUM_ROWS)
return true;
}
count = 0;
for (i = 0; i < NUM_ROWS; i++)
{
if (t[i][i] == player)
count++;
}
if (count == NUM_ROWS)
return true;
count = 0;
for (i = 0; i < NUM_ROWS; i++)
{
if (t[NUM_ROWS - 1 - i][i] == player)
count++;
}
if (count == NUM_ROWS)
return true;
return false;
} // end isWinner
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.