Question: Create a C++ 2d array that plays tic tac toe with the following functions #include #include #include #include #include using namespace std; const int ROWS
Create a C++ 2d array that plays tic tac toe with the following functions

#include
using namespace std;
const int ROWS = 3; const int COLS = 3;
void randomize(); int random(int a, int b); void draw_board(string b[][COLS], int rows); void reset_board(string b[][COLS], int rows); bool upper_left_to_lower_right(string b[ROWS][COLS]); bool lower_left_to_upper_right(string b[ROWS][COLS]); bool check_col(string b[ROWS][COLS], int col); bool check_row(string b[ROWS][COLS], int row); bool check_for_winner(string b[ROWS][COLS]); bool is_game_tied(string b[ROWS][COLS]); bool move_legit(string b[ROWS][COLS], int loc); void get_move(string b[ROWS][COLS], int& player);
int main() { string board[ROWS][COLS] = { {"X", "O", "X"}, // B[2][0] B[2][1] B[2][2] {"O", "O", " "}, // B[1][0] B[1][1] B[1][2] {" ", "X", " "} }; // B[0][0] B[0][1] B[0][2] string choice; int coin; int current_player = 0; bool someone_won = false; reset_board(board, 3); cout > choice; coin = random(0, 1); if ((coin == 0 && choice == "H") || (coin == 1 && choice == "T")) { cout
while (!is_game_tied(board) && !someone_won) { draw_board(board, 3); cout
void randomize() { srand(static_cast
int random(int a, int b) { return a + rand() % (b - a + 1); }
void reset_board(string b[][COLS], int rows) { for(int i = 0; i
void draw_board(string b[][COLS], int rows) { system("cls"); string line = " +---+---+---+ "; cout = 0; i--) { cout
/** * Return true if all elements on the diagonal joining upper-left * and the lower right corners all the same. * @param b the board a 3 x 3 array of String * @return true if all elements are the same. */ bool upper_left_to_lower_right(string b[ROWS][COLS]) { }
/** * Return true if all elements on the diagonal joining lower-left * and the upper-right corners all the same. * @param b the board a 3 x 3 array of String * @return true if all elements are the same. */ bool lower_left_to_upper_right(string b[ROWS][COLS]) { }
/** * Return true if all elements in column col are the same. * @param b the board a 3 x 3 array of String * @param col the column to check * @return true if all elements are the same. */ bool check_col(string b[ROWS][COLS], int col) { }
/** * Return true if all elements in row row are the same. * @param b the board a 3 x 3 array of String * @param row the row to check * @return true if all elements are the same. */ bool check_row(string b[ROWS][COLS], int row) { }
/** * Return true if there is a winner. * @param b the board a 3 x 3 array of String * @return true if there is a winner, false otherwise */ bool check_for_winner(string b[ROWS][COLS]) { }
/** * Return false if there is a blank space in the board. * @param b the board a 3 x 3 array of String * @return false if there is a blank space in the board, * true if there are no moves to be made. */ bool is_game_tied(string b[ROWS][COLS]) { }
/** * Return true if there is a blank space in the location loc. * @param b the board a 3 x 3 array of String * @param loc the location number of the move as given by * 6 | 7 | 8 * ----------- * 3 | 4 | 5 * ----------- * 0 | 1 | 2 * @return true if there is a blank space in the location loc. */ bool move_legit(string b[ROWS][COLS], int loc) { }
/** * Gets the inputted move from the player, determines if it is a legitimate move * then puts the correct symbol in the spot and updates the player. * The move is based on the number keypad: * 7 | 8 | 9 * ----------- * 4 | 5 | 6 * ----------- * 1 | 2 | 3 * @param b the board a 3 x 3 array of String * @param player the current player number */ void get_move(string b[ROWS][COLS], int& player) { int move; do { cout > move; move--; } while (!move_legit(b, move)); if (player == 1) { b[move / COLS][move % COLS] = "O"; player = 2; } else { b[move / COLS][move % COLS] = "X"; player = 1; }
}
Write a C++ program that plays tic-tac-toe. The tic-tac-toe game is played on a 3x3 grid as in o The game is played by two players, who take turns. The first player marks moves with an "O", the second with an "X." The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, accept numbers to indicate the players move, change player after every successful move, and pronounce the winner. Home PgUp End PgDn The program should flip a coin to see who goes first (Player 1 or Player 2)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
