You must use the GIVEN code and ADD onto it the following four instructions in B
ID: 3887056 • Letter: Y
Question
You must use the GIVEN code and ADD onto it the following four instructions in BOLD.
Using the code below, fix or add the following four steps:
1. (bug) A tie game does NOT finish the game. Correct this.
2. (feature) Transform the game from player vs. player (PvP) only to player vs. player and player vs. a machine (PvM). The user can have an option to select which type before a game starts. The machine runs at least as smart as a random function on picking the next move (i.e. random spot selection)
3. (feature) A game ends when the user enters “quit” or whoever wins three consecutive games. A user can select to play again, a new fresh start. Each time someone wins they recieve $100,000 The winner can “cash” a $300,000, for three consecutive wins, then stop, or continue and cash $600,000 at six consecutive wins, stop or continue to nine consecutive wins and cash $900,000, and win $1,000,000 at ten wins. Game will end at ten consecutive wins, no more.
4. (feature) Add wins and ties counters for each player (PvP or PvM).
-------tic-tac-toe.cpp----------------------------------------------------------------------------------------------
Explanation / Answer
/**
Simple Tic Tac Toe
tictactoe-v0.9.cpp
Source: http://paste4btc.com/enVArEWu
*/
#include <iostream>
#include <cstdlib>
using namespace std;
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char player = 'X';
void reset(){
int value =48;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
value++;
matrix[i][j] =value;
}
}
}
int getRandom(){
int num = 0;
while(1){
num = rand()%9+1;
int found = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (num == 1)
if(matrix[0][0] == 'X' || matrix[0][0] == 'O')
continue;
if (num == 2)
if(matrix[0][1] == 'X' || matrix[0][1] == 'O')
continue;
if (num == 3)
if(matrix[0][2] == 'X' || matrix[0][2] == 'O')
continue;
if (num == 4)
if(matrix[1][0] == 'X' || matrix[1][0] == 'O')
continue;
if (num == 5)
if(matrix[1][1] == 'X' || matrix[1][1] == 'O')
continue;
if (num == 6)
if(matrix[1][2] == 'X' || matrix[1][2] == 'O')
continue;
if (num == 7)
if(matrix[2][0] == 'X' || matrix[2][0] == 'O')
continue;
if (num == 8)
if(matrix[2][1] == 'X' || matrix[2][1] == 'O')
continue;
if (num == 1)
if(matrix[2][2] == 'X' || matrix[2][2] == 'O')
continue;
found = 1;
break;
}
if(found)
break;
}
if(found)
break;
}
return num;
}
char Win()
{
int x = 0;
if(x == 0){
//first player
if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
return 'X';
if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
return 'X';
if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
return 'X';
if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
return 'X';
if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
return 'X';
//second player
if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
return 'O';
if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
return 'O';
if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
return 'O';
if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
return 'O';
if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
return 'O';
else
x = 1;
}
if(x == 1){
return 'N';
}
return '/';
}
int main()
{
//Draw
int isPvM = 0;
char choice;
cout << "By default Game is Player vs. Player (PvP)"<<endl;
cout << "[Enter 'Y']If you want to play with Machine(PvM) :";
cin >>choice;
if(choice=='Y'||choice=='y')
isPvM=1;
//cout << string( 100, ' ' );
int X_win_count=0;
int O_win_count=0;
int tie_count=0;
char last_winner='T';
start_game:
reset();
cout << "TicTacToe v0.9" << endl;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
cout << matrix[i][j] << " ";
}
cout << endl;
}
int fill_cell_count = 0;
player='X';
int finish_one_round=0;
while (1){
//Input
int a;
if(isPvM){
if(player=='X'){
cout << "Press the number of the field: ";
cin >> a;
}
else{
a=getRandom();
}
}
else{
cout << "Press the number of the field: ";
cin >> a;
}
if(player=='X')
cout << " Player X, selected position :"<<a<<endl;
else
cout << " Player O, selected position :"<<a<<endl;
// if(a != 1 || a != 2|| a != 3|| a != 4|| a != 5|| a != 6|| a != 7|| a != 8|| a != 9){
//return 0;
//}
if (a == 1){
if(matrix[0][0] == 'X' || matrix[0][0] == 'O'){
cout << "Cannot move there. Player already exists, choose again." << endl;
if (player == 'X')
player = 'X';
else
player = 'O';
}
else{
matrix[0][0] = player;
if (player == 'X')
player = 'O';
else
player = 'X';
fill_cell_count++;
}
}
else if (a == 2)
if(matrix[0][1] == 'X' || matrix[0][1] == 'O'){
cout << "Cannot move there. Player already exists, choose again." << endl;
if (player == 'X')
player = 'X';
else
player = 'O';
}
else{
matrix[0][1] = player;
if (player == 'X')
player = 'O';
else
player = 'X';
fill_cell_count++;
}
else if (a == 3)
if(matrix[0][2] == 'X' || matrix[0][2] == 'O'){
cout << "Cannot move there. Player already exists, choose again." << endl;
if (player == 'X')
player = 'X';
else
player = 'O';
}
else{
matrix[0][2] = player;
if (player == 'X')
player = 'O';
else
player = 'X';
fill_cell_count++;
}
else if (a == 4)
if(matrix[1][0] == 'X' || matrix[1][0] == 'O'){
cout << "Cannot move there. Player already exists, choose again." << endl;
if (player == 'X')
player = 'X';
else
player = 'O';
}
else{
matrix[1][0] = player;
if (player == 'X')
player = 'O';
else
player = 'X';
fill_cell_count++;
}
else if (a == 5)
if(matrix[1][1] == 'X' || matrix[1][1] == 'O'){
cout << "Cannot move there. Player already exists, choose again." << endl;
if (player == 'X')
player = 'X';
else
player = 'O';
}
else{
matrix[1][1] = player;
if (player == 'X')
player = 'O';
else
player = 'X';
fill_cell_count++;
}
else if (a == 6)
if(matrix[1][2] == 'X' || matrix[1][2] == 'O'){
cout << "Cannot move there. Player already exists, choose again." << endl;
if (player == 'X')
player = 'X';
else
player = 'O';
}
else{
matrix[1][2] = player;
if (player == 'X')
player = 'O';
else
player = 'X';
fill_cell_count++;
}
else if (a == 7)
if(matrix[2][0] == 'X' || matrix[2][0] == 'O'){
cout << "Cannot move there. Player already exists, choose again." << endl;
if (player == 'X')
player = 'X';
else
player = 'O';
}
else{
matrix[2][0] = player;
if (player == 'X')
player = 'O';
else
player = 'X';
fill_cell_count++;
}
else if (a == 8)
if(matrix[2][1] == 'X' || matrix[2][1] == 'O'){
cout << "Cannot move there. Player already exists, choose again." << endl;
if (player == 'X')
player = 'X';
else
player = 'O';
}
else{
matrix[2][1] = player;
if (player == 'X')
player = 'O';
else
player = 'X';
fill_cell_count++;
}
else if (a == 9)
if(matrix[2][2] == 'X' || matrix[2][2] == 'O'){
cout << "Cannot move there. Player already exists, choose again." << endl;
if (player == 'X')
player = 'X';
else
player = 'O';
}
else{
matrix[2][2] = player;
if (player == 'X')
player = 'O';
else
player = 'X';
fill_cell_count++;
}
//Draw
//cout << string( 100, ' ' );
cout << "tictactoe v0.9" << endl;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
if (Win() == 'X'){
cout << "X wins!" << endl;
X_win_count++;
finish_one_round=1;
break;
}
else if (Win() == 'O'){
cout << "O wins!" << endl;
O_win_count++;
finish_one_round=1;
break;
}
else if(Win() == 'N'&&fill_cell_count==9){
cout << "It was a tie!" << endl;
tie_count++;
finish_one_round=1;
break;
}
}
if(finish_one_round){
finish_one_round=0;
cout << " [Enter Y ] to play again:" ;
cin >>choice;
if(choice=='Y'||choice=='y')
goto start_game;
}
cout << " Player X won : "<<X_win_count<<" times"<<endl ;
cout << "Player O won : "<<O_win_count<<" times"<<endl ;
cout << "Tie the game : "<<tie_count<<" times"<<endl ;
return 0;
}
-------------------------------------------------------OUTPUT-----------------------------------------------------------------------------
By default Game is Player vs. Player (PvP)
[Enter 'Y']If you want to play with Machine(PvM) :y
TicTacToe v0.9
1 2 3
4 5 6
7 8 9
Press the number of the field: 1
Player X, selected position :1
tictactoe v0.9
X 2 3
4 5 6
7 8 9
Player O, selected position :6
tictactoe v0.9
X 2 3
4 5 O
7 8 9
Press the number of the field:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.