I need to modify the following code for Tic Tac Toe game, so that \"break\" is n
ID: 3580208 • Letter: I
Question
I need to modify the following code for Tic Tac Toe game, so that "break" is not used to exit loops:
#include
#include
#include
using namespace std;
void splashScreen(char userName[]);
void askForUserNames(char userName[]);
int switchPlayer(int player);
void resetGame(char grid[]);
void displayGrid(char grid[]);
int playerMakeMove();
bool validatePlayersMove(int input);
bool checkPositionAvailability(int position,char grid[]);
int checkwin(char grid[]);
int checkTie(char grid[]);
int computerMakeMove(char grid[]);
int main()
{
//Declare all required variables
char grid[9];
char userName[50];
//Call method to ask user name
askForUserNames(userName);
//Splash screen
splashScreen(userName);
char ch;
//Start the game
do {
resetGame(grid);
int currentPlayer = 1,i,choice;
char markPosition;
do
{
currentPlayer = switchPlayer(currentPlayer);
if(currentPlayer==1) //if currentPlayer is 1, indicates human turn
displayGrid(grid);
while(true)
{
if(currentPlayer==2) //If currentPlayer is 2 ,its computer turn
{
choice = computerMakeMove(grid);
}
else
{
choice = playerMakeMove();
}
markPosition=(currentPlayer == 1) ? 'H' : 'C';
if(checkPositionAvailability(choice,grid))
{
grid[choice] = markPosition;
break;
}
else
{
if(currentPlayer==1)
{
displayGrid(grid);
cout<<"Position not available ";
cout<<"Make different choice ";
cout<<"It is the human's turn ";
}
}
}
//Check for win
i=checkwin(grid);
currentPlayer++;
}while(i==-1);
displayGrid(grid);
if(i==1)
{
if(--currentPlayer==1) {
cout<<"You won the game! ";
}
else
{
cout<<"c won! ";
}
}
else
{
cout<<"Game draw! ";
}
cout<<"game over!, play again ? y/n ";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}
//Method to splashScreen
void splashScreen(char userName[])
{
cout<<"***********************"< cout<<"***********************"< cout<<"***********************"< cout<<"****** ******"< cout<<"******TIC TAC TOE******"< cout<<"****** By Prof. ******"< cout<<"****** "<
//Method to ask user name
void askForUserNames(char userName[])
{
//Read user name from key board
cout<<"Enter your name : ";
cin>>userName;
}
//Method to swith player
int switchPlayer(int player)
{
//Using ternary operator switching between human and computer
return (player%2)?1:2;
}
//Method to reset grid
void resetGame(char grid[])
{
//Reset grid with initial value
grid[0]='0';
grid[1]='1';
grid[2]='2';
grid[3]='3';
grid[4]='4';
grid[5]='5';
grid[6]='6';
grid[7]='7';
grid[8]='8';
grid[9]='9';
}
//Method to display grid
void displayGrid(char grid[])
{
cout << " TIC TAC TOE ";
cout << " |-----|-----|-----|" << endl;
cout << " | " << grid[0] << " | " << grid[1] << " | " << grid[2] <<" |"<< endl;
cout << " |_____|_____|_____|" << endl;
cout << " | | | |" << endl;
cout << " | " << grid[3] << " | " << grid[4] << " | " << grid[5] <<" |"<< endl;
cout << " |_____|_____|_____|" << endl;
cout << " | | | |" << endl;
cout << " | " << grid[6] << " | " << grid[7] << " | " << grid[8] <<" |"<< endl;
cout << " |-----|-----|-----|" << endl<
}
//Method to player make move
int playerMakeMove()
{
int playerChoice;
//Repeat till valid move found
while(true)
{
cout << "It is the humans's turn ";
cout<<"Give me your best move? ";
cin >> playerChoice;
//Validate playerChoice using validatePlayersMove method
bool valid = validatePlayersMove(playerChoice);
if(valid==true)
break;
}
return playerChoice;
}
//Method to validate player move
bool validatePlayersMove(int input)
{
//Check for invalid input
if(input<0 || input>8)
{
cout<<"----Invalid input Please enter valid input---- ";
return false;
}else{
return true;
}
}
//Method to check position availability
bool checkPositionAvailability(int position,char grid[])
{
if (position == 0 && grid[0] == '0')
{
return true;
}else if (position == 1 && grid[1] == '1')
{
return true;
}
else if (position == 2 && grid[2] == '2')
{
return true;
}
else if (position == 3 && grid[3] == '3')
{
return true;
}
else if (position == 4 && grid[4] == '4')
{
return true;
}
else if (position == 5 && grid[5] == '5')
{
return true;
}
else if (position == 6 && grid[6] == '6')
{
return true;
}
else if (position == 7 && grid[7] == '7')
{
return true;
}
else if (position == 8 && grid[8] == '8')
{
return true;
}
else {
return false;
}
}
//Method to check win
int checkwin(char grid[])
{
//Check all winning combinations
if (grid[0] == grid[1] && grid[1] == grid[2])
return 1;
else if (grid[3] == grid[4] && grid[4] == grid[5])
return 1;
else if (grid[6] == grid[7] && grid[7] == grid[8])
return 1;
else if (grid[0] == grid[3] && grid[3] == grid[6])
return 1;
else if (grid[1] == grid[4] && grid[4] == grid[7])
return 1;
else if (grid[2] == grid[5] && grid[5] == grid[8])
return 1;
else if (grid[0] == grid[4] && grid[4] == grid[8])
return 1;
else if (grid[2] == grid[4] && grid[4] == grid[6])
return 1;
else
return checkTie(grid);
}
//Method to check tie
int checkTie(char grid[])
{
if (grid[0] != '0' && grid[1] != '1' && grid[2] != '2' && grid[3] != '3'
&& grid[4] != '4' && grid[5] != '5' && grid[6] != '6'
&& grid[7] != '7' && grid[8] != '8')
{
return 0;
}
else
{
return -1;
}
}
//Method to make computer move
int computerMakeMove(char grid[])
{
//Check winning all chances of computer
if (grid[0] =='1' && grid[1] == 'C' && grid[2]=='C' )
return 0;
else if (grid[0] =='C' && grid[1] == '1' && grid[2]=='C' )
return 1;
else if (grid[0] =='C' && grid[1] == 'C' && grid[2]=='2' )
return 2;
else if (grid[3] =='3' && grid[4] == 'C' && grid[5]=='C' )
return 3;
else if (grid[3] =='C' && grid[4] == '4' && grid[5]=='C' )
return 4;
else if (grid[3] =='C' && grid[4] == 'C' && grid[5]=='5' )
return 5;
else if (grid[6] =='6' && grid[7] == 'C' && grid[8]=='C' )
return 6;
else if (grid[6] =='C' && grid[7] == '7' && grid[8]=='C' )
return 7;
else if (grid[6] =='C' && grid[7] == 'C' && grid[8]=='8' )
return 8;
else if (grid[0] =='0' && grid[3] == 'C' && grid[6]=='C' )
return 0;
else if (grid[0] =='C' && grid[3] == '3' && grid[6]=='C' )
return 3;
else if (grid[0] =='C' && grid[3] == 'C' && grid[6]=='6' )
return 6;
else if (grid[1] =='1' && grid[4] == 'C' && grid[7]=='C' )
return 1;
else if (grid[1] =='C' && grid[4] == '4' && grid[7]=='C' )
return 4;
else if (grid[1] =='C' && grid[4] == 'C' && grid[7]=='7' )
return 7;
else if (grid[2] =='2' && grid[5] == 'C' && grid[8]=='C' )
return 2;
else if (grid[2] =='C' && grid[5] == '5' && grid[8]=='C' )
return 5;
else if (grid[2] =='C' && grid[5] == 'C' && grid[8]=='8' )
return 8;
else if (grid[0] =='0' && grid[4] == 'C' && grid[8]=='C' )
return 0;
else if (grid[0] =='C' && grid[4] == '4' && grid[8]=='C' )
return 4;
else if (grid[0] =='C' && grid[4] == 'C' && grid[8]=='8' )
return 8;
else if (grid[2] =='2' && grid[4] == 'C' && grid[6]=='C' )
return 2;
else if (grid[2] =='C' && grid[4] == '4' && grid[6]=='C' )
return 4;
else if (grid[2] =='C' && grid[4] == 'C' && grid[6]=='6' )
return 6;
//Losing chances of Human
else if (grid[0] =='1' && grid[1] == 'H' && grid[2]=='H' )
return 0;
else if (grid[0] =='H' && grid[1] == '1' && grid[2]=='H' )
return 1;
else if (grid[0] =='H' && grid[1] == 'H' && grid[2]=='2' )
return 2;
else if (grid[3] =='3' && grid[4] == 'H' && grid[5]=='H' )
return 3;
else if (grid[3] =='H' && grid[4] == '4' && grid[5]=='H' )
return 4;
else if (grid[3] =='H' && grid[4] == 'H' && grid[5]=='5' )
return 5;
else if (grid[6] =='6' && grid[7] == 'H' && grid[8]=='H' )
return 6;
else if (grid[6] =='H' && grid[7] == '7' && grid[8]=='H' )
return 7;
else if (grid[6] =='H' && grid[7] == 'H' && grid[8]=='8' )
return 8;
else if (grid[0] =='0' && grid[3] == 'H' && grid[6]=='H' )
return 0;
else if (grid[0] =='H' && grid[3] == '3' && grid[6]=='H' )
return 3;
else if (grid[0] =='H' && grid[3] == 'H' && grid[6]=='6' )
return 6;
else if (grid[1] =='1' && grid[4] == 'H' && grid[7]=='H' )
return 1;
else if (grid[1] =='H' && grid[4] == '4' && grid[7]=='H' )
return 4;
else if (grid[1] =='H' && grid[4] == 'H' && grid[7]=='7' )
return 7;
else if (grid[2] =='2' && grid[5] == 'H' && grid[8]=='H' )
return 2;
else if (grid[2] =='H' && grid[5] == '5' && grid[8]=='H' )
return 5;
else if (grid[2] =='H' && grid[5] == 'H' && grid[8]=='8' )
return 8;
else if (grid[0] =='0' && grid[4] == 'H' && grid[8]=='H' )
return 0;
else if (grid[0] =='H' && grid[4] == '4' && grid[8]=='H' )
return 4;
else if (grid[0] =='H' && grid[4] == 'H' && grid[8]=='8' )
return 8;
else if (grid[2] =='2' && grid[4] == 'H' && grid[6]=='H' )
return 2;
else if (grid[2] =='H' && grid[4] == '4' && grid[6]=='H' )
return 4;
else if (grid[2] =='H' && grid[4] == 'H' && grid[6]=='6' )
return 6;
else {
rand() % 10;
}
}
Explanation / Answer
There are two places where you have break statement. :
1> int main()
2> int playerMakeMove()
If you dont want to use break statement, but you still want to exit from the loop, then you need to make the condition to false on which the loop is running.
Since, in your code there is true given in the condition in the loop, there is no other option to exit from the loop without using break.
But, we add a condition to the loop which we can change at any point of time, we can achieve this.
Below are the two methods with the rectified code :
int main()
{
//Declare all required variables
char grid[9];
char userName[50];
//Call method to ask user name
askForUserNames(userName);
//Splash screen
splashScreen(userName);
char ch;
//Start the game
do {
resetGame(grid);
int currentPlayer = 1,i,choice;
char markPosition;
do
{
currentPlayer = switchPlayer(currentPlayer);
if(currentPlayer==1) //if currentPlayer is 1, indicates human turn
displayGrid(grid);
bool flag=true;
while(flag)
{
if(currentPlayer==2) //If currentPlayer is 2 ,its computer turn
{
choice = computerMakeMove(grid);
}
else
{
choice = playerMakeMove();
}
markPosition=(currentPlayer == 1) ? 'H' : 'C';
if(checkPositionAvailability(choice,grid))
{
grid[choice] = markPosition;
flag=false; //set flag to false, so that the loop will not run again
}
else
{
if(currentPlayer==1)
{
displayGrid(grid);
cout<<"Position not available ";
cout<<"Make different choice ";
cout<<"It is the human's turn ";
}
}
}
//Check for win
i=checkwin(grid);
currentPlayer++;
}while(i==-1);
displayGrid(grid);
if(i==1)
{
if(--currentPlayer==1) {
cout<<"You won the game! ";
}
else
{
cout<<"c won! ";
}
}
else
{
cout<<"Game draw! ";
}
cout<<"game over!, play again ? y/n ";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}
###############################################################################################
int playerMakeMove()
{
int playerChoice;
bool flag=true;
//Repeat till valid move found
while(flag)
{
cout << "It is the humans's turn ";
cout<<"Give me your best move? ";
cin >> playerChoice;
//Validate playerChoice using validatePlayersMove method
bool valid = validatePlayersMove(playerChoice);
if(valid==true)
{
flag=false; //set flag to false, so that the loop will not run again
}
}
return playerChoice;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++
feel free to ask if you have any doubt :)