In C++ Please write a program that will allow two people to play the game of tic
ID: 3760831 • Letter: I
Question
In C++ Please write a program that will allow two people to play the game of tic-tac-toe. The program should ask for moves alternately from each player, one of whom always plays the “crosses” (‘X’) while the other always plays the “circles” (‘O’). The program displays the gameboard positions as follows:
7 | 8 | 9
---+---+---
4 | 5 | 6
---+---+---
1 | 2 | 3
The players enter their moves by entering the position number they wish to mark. After each move, the program displays the changed board. A sample board configuration is as follows:
X | 8 | 9
---+---+---
O | O | 6
---+---+---
X | 2 | 3
At the end of each game, your program must indicate who won or that it was a tied game, after which the players are to be allowed the choice to play again or quit. The players are to alternate who starts first in each game; additionally, the program is to keep track of the number of wins for each player, plus the number of ties, printing this information out at the end of each game.
Your program is required to use functions and either arrays or vectors. You are allowed to use global variables only in the case of symbolic constants. At the start, your program must ask for the names of the two players; these names are to be used when providing information to the players (e.g. whose turn it is, who won, etc.).
The following are the functions that you are required to properly implement for this assignment (you may implement others as well); please note that the tic-tac-toe board is to be internally represented using either a one-dimensional array or a one-dimensional vector. For each function as appropriate, you will need to replace the data type placeholder TYPE with a data type of your choosing for defining the playerMark and board variables.
• int getPlayerInput(string playerName) Returns a value between 1 and 9, inclusive, indicating the square where the current player wants to place his/her mark on the board. The input routine is to use the name of the player whose turn it is and is to check for correct input.
• bool isLegalMove(int location, TYPE board[], int boardSize) - or –
• bool isLegalMove(int location, vector board ) Returns true if the indicated location on the board yet not yet been played, false otherwise.
• void placeMarkOnBoard(TYPE playerMark, int location, TYPE board[], int boardSize) - or –
• void placeMarkOnBoard(TYPE playerMark, int location, vector& board) Places the indicated mark at the specified board location; routine assumes that this is a legal placement.
• void clearBoard(TYPE board[], int boardSize) - or –
• void clearBoard(vector& board) Restores the tic-tac-toe board to its original (i.e. clear, with no crosses or circles) configuration. ( function list continues on next page )
• bool hasThreeInRow(TYPE playerMark, TYPE board[], int boardSize) - or –
• bool hasThreeInRow(TYPE playerMark, vector board) Returns true if, for the specified mark, the board has the equivalent of three of those marks in a row, either vertically, horizontally, or diagonally.
• void displayBoard(TYPE board[], int boardSize) - or –
• void displayBoard(vector board) Prints out the current board; the board display must be as shown above.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
int checkwin();
void board();
int main()
{
int player = 1,i,choice;
char mark;
do
{
board();
player=(player%2)?1:2;
cout << "Player " << player << ", enter a number: ";
cin >> choice;
mark=(player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
cout<<"Invalid move ";
player--;
cin.ignore();
cin.get();
}
i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<"==>Player "<<--player<<" win ";
else
cout<<"==>Game draw";
cin.ignore();
cin.get();
return 0;
}
int checkwin()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[1] == square[5] && square[5] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')
return 0;
else
return -1;
}
void board()
{
system("cls");
cout << " Tic Tac Toe ";
cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
cout << " | | " << endl << endl;
}