Question: Create a game of tic tac toe using arrays #include #include using namespace std; // Define NAMED CONSTANTS for a 3x3 matrix, a 5x5 matrix

Create a game of tic tac toe using arrays

#include #include

using namespace std;

// Define NAMED CONSTANTS for a 3x3 matrix, a 5x5 matrix // called the named constant variables: PLAYER_MOVES, BOARD_SIZE, // respectively. const int MAX_PLAYERS = 2; const int BOARD_SIZE = 5; const int PLAYER_MOVES = 3;

// 10 TODO: // Define NAMED CONSTANTS for QUIT = 2

// Ifunction prototypes void enterPlayerNames(string players[]); void initializeGameBoard(char gameBoard[][BOARD_SIZE]); void displayGameBoard(char gameBoard[][BOARD_SIZE]); void initializePlayerMoves(char moves[][PLAYER_MOVES]); // 10 TODO: nsert the function prototypes for // initializePlayerScores, displayMenu, getChoice, makeMove, getNumberOfPlays int main() { // Array holding player name string players[MAX_PLAYERS]; enterPlayerNames(players); // 20 TODO: // Define an array, playerScores, to hold each player score (up to // MAX_PLAYERS of 2). Must use MAX_PLAYERS

// 30 TODO: // Invoke initializePlayerScores // Define a matrix to hold a gamboard char gameBoard[BOARD_SIZE][BOARD_SIZE]; // Define a matrix to hold the players' moves char moves[PLAYER_MOVES][PLAYER_MOVES]; int choice = QUIT; do { // Display the menu and get player's choice displayMenu(); choice = getChoice(); if (choice != QUIT) { // If user does not want to quit, proceed // Set up the gameboard and players' move initializeGameBoard(gameBoard); displayGameBoard(gameBoard); initializePlayerScores(playerScores);

bool xTurn = true; // Allow player to mark the board when it's his turn to move // Then change xTurn to the other player's turn (oTurn) for (int iPlayer = 0; iPlayer < MAX_PLAYERS; iPlayer++) {

cout << players[iPlayer] << "'s current score: " << playerScores[iPlayer] << endl; // Partially implement player's move makeMove(gameBoard, moves, xTurn); cout << endl; } // endfor } // endif choice != QUIT } while (choice != QUIT); int numberOfPlays = getNumberOfPlays(); cout << "Players have played the tic-tac-toe game " << numberOfPlays << " times" << endl; cout << "Enter to exit ..." << endl; cin.ignore(); cin.get(); return 0; }

// 40 TODO: // Implement initializePlayerScores including the function signature /**************************************************************** * initializePlayerScores * * This function initializes the array that hold players' * * scores to zero * ****************************************************************/

// 50 TODO: // Implement displayMenu /********************************************** * displayMenu * * This function CLEARS the screen and then * * displays the menu choices as follows: * * TIC-TAC_TOE Game * * 1. Play tic-tac-toe game * * 2. Quit * **********************************************/ void displayMenu() { // insert your code here ... }

// 60 TODO: // Implement getChoice /************************************************** * getChoice * * This function inputs, validates, and returns * * the user's menu choice. * **************************************************/ int getChoice() { int choice = QUIT; // non-static local variable // insert your code here ... return choice; }

// 70 TODO: // insert your STUB code to change the xTurn from true to false, // You must display the xTurn value before and after // Set the sign to X or O if xTurn is set true, or false, respectively /************************************************************ * makeMove * * This function allows a player to make a legal move and * * updates the moves array and the gameBoard with the new * * game configuration. * * Set the turn to the next player: O's turn, * * or back to X'sturn * * if xTurn is true, set xTurn to false. * * if xTurn is false, set xTurn to true. * ************************************************************/ void makeMove(char gameBoard[][BOARD_SIZE], char moves[][PLAYER_MOVES], bool &xTurn) { string xTurnDisplay = " X's turn - Enter your move "; string oTurnDisplay = " O's turn - Enter your move "; char sign = ""; // X or O // insert your code here ... }

// 80 TODO: // Determine how many times the game has played /*************************************************** * getNumberOfPlays * * This function keeps track of number of times * * the players chose to play the tic-tac-toe game. * ***************************************************/ int getNumberOfPlays() { // insert your code here ... }

/**************************************************************** * enterPlayerNames * * This function asks for and stores player names in an array * ****************************************************************/ void enterPlayerNames(string playerNames[]) { for (int player = 0; player < MAX_PLAYERS; player++) { cout << "Enter player # " << (player + 1) << " name: "; getline(cin, playerNames[player]); cout << endl; } }

/**************************************************************** * initializeGameBoard * * This function sets up the game board with asterisks and bars * ****************************************************************/ void initializeGameBoard(char gameBoard[][BOARD_SIZE]) { // insert your code here ... for (int row = 0; row < BOARD_SIZE; row++) { for (int ic = 0; ic < BOARD_SIZE; ic++) { if ((row == 0) || ((row % 2) == 0)) { if ((ic == 0) || ((ic % 2) == 0)) { gameBoard[row][ic] = '*'; } else { gameBoard[row][ic] = '|'; } } else { if ((ic == 0) || ((ic % 2) == 0)) { gameBoard[row][ic] = '_'; } else { gameBoard[row][ic] = '|'; } } } } }

/**************************************************************** * initializePlayerMoves * * This function initializes players' moves with asterisks: * * ****************************************************************/ void initializePlayerMoves(char moves[][PLAYER_MOVES]) { // insert your code here ... for (int row = 0; row < PLAYER_MOVES; row++) { for (int ic = 0; ic < PLAYER_MOVES; ic++) { moves[row][ic] = '*'; } } }

/************************************************************ * displayGameBoard * * This function displays the current game board. * ************************************************************/ void displayGameBoard(char gameBoard[][BOARD_SIZE]) { string spaces = " "; for (int row = 0; row < BOARD_SIZE; row++) { cout << endl << spaces; for (int col = 0; col < BOARD_SIZE; col++) cout << gameBoard[row][col]; } cout << endl << endl; }

/************************************************************ * displayPlayerMoves * * This function displays the current player moves. * ************************************************************/ void displayPlayerMoves(char moves[][BOARD_SIZE]) { // Display the initial moves matrix for (int row = 0; row < PLAYER_MOVES; row++) { for (int col = 0; col < PLAYER_MOVES; col++) { cout << '|' << moves[row][col]; } // endfor column cout << '|' << endl; } // endfor row }

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 Databases Questions!