Question: Use the following pseudocode for a fully functional c++ program that implements the game of Tic Tac Toe. #include #include void display_board(const std::vector & board)

Use the following pseudocode for a fully functional c++ program that implements the game of Tic Tac Toe.

#include #include

void display_board(const std::vector>& board) { for (const auto& row : board) { for (char cell : row) { std::cout << cell << " "; } std::cout << std::endl; } }

bool check_winner(const std::vector>& board, char player) { for (int i = 0; i < 3; ++i) { if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { return true; } } return (board[0][0] == player && board[1][1] == player && board[2][2] == player) || (board[0][2] == player && board[1][1] == player && board[2][0] == player); }

bool is_full(const std::vector>& board) { for (const auto& row : board) { for (char cell : row) { if (cell == '-') { return false; } } } return true; }

int main() { std::vector> board(3, std::vector(3, '-')); char player = 'X'; int row, col;

while (!is_full(board) && !check_winner(board, 'X') && !check_winner(board, 'O')) { display_board(board); std::cout << "Player " << player << ", enter row and column (0-2): "; std::cin >> row >> col;

while (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != '-') { std::cout << "Invalid move. Try again: "; std::cin >> row >> col; }

board[row][col] = player; player = (player == 'X') ? 'O' : 'X'; }

display_board(board);

if (check_winner(board, 'X')) { std::cout << "Player X wins!" << std::endl; } else if (check_winner(board, 'O')) { std::cout << "Player O wins!" << std::endl; } else { std::cout << "It's a draw!" << std::endl; }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!