Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

\f

Answered: 1 week ago

Question

The organ of corti is present in.....?

Answered: 1 week ago

Question

The border between the middle and inner ear is formed by......?

Answered: 1 week ago

Question

It receives sound vibration and passes to the eardrum?

Answered: 1 week ago