I created a simple tic-tac-toe. When the player gets the winning shot (for example you got 2 X in a row and all you need is one, and you enter it), the program immediately says:
"someone won"
(I am still trying to figure out, how to say the name of the player who won.)
Without turning "3" to "X" (let's say the last number is 3).
If I enter 3, "3" should turn to "X" and leave "3 X (X X X)". Instead it only leaves "2 (X X 3)" and immediately says "someone won". How can I solve this?
This is my code:
#include <iostream>
#include <string>
using namespace std;
//board numbers
char numbers[10] = {'0','1','2','3','4','5','6','7','8','9'};
//structure of players
struct playersinfo
{
int playersnumber;
string player1name, player2name, playersturn;
char player1mark, player2mark;
};
playersinfo players;
//prototype of progress, drawboard, oneWon and twoWon
int progress();
void drawboard();
bool oneWon();
bool twoWon();
int main()
{
//assigns i to progress;
int i, j = 1;
char choice;
//assign players mark
players.player1mark = 'X';
players.player2mark = 'O';
//ask players their name
cout << "Enter player 1's name : ";
cin >> players.player1name;
system("cls");
cout << "Enter player 2's name : ";
cin >> players.player2name;
do
{
if(j == 1)
{
players.playersturn = players.player1name;
players.playersnumber = 1;
}
else if(j == 2)
{
players.playersturn = players.player2name;
players.playersnumber = 2;
}
drawboard();
cout << '\t' <<players.playersturn << " enter a number : ";
cin >> choice;
if(choice == numbers[1])
{
if(players.playersnumber == 1)
numbers[1] = players.player1mark;
else
numbers[1] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[2])
{
if(players.playersnumber == 1)
numbers[2] = players.player1mark;
else
numbers[2] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[3])
{
if(players.playersnumber == 1)
numbers[3] = players.player1mark;
else
numbers[3] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[4])
{
if(players.playersnumber == 1)
numbers[4] = players.player1mark;
else
numbers[4] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[5])
{
if(players.playersnumber == 1)
numbers[5] = players.player1mark;
else
numbers[5] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[6])
{
if(players.playersnumber == 1)
numbers[6] = players.player1mark;
else
numbers[6] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[7])
{
if(players.playersnumber == 1)
numbers[7] = players.player1mark;
else
numbers[7] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[8])
{
if(players.playersnumber == 1)
numbers[8] = players.player1mark;
else
numbers[8] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
if(choice == numbers[9])
{
if(players.playersnumber == 1)
numbers[9] = players.player1mark;
else
numbers[9] = players.player2mark;
//increment j
j++;
if(j == 3)
j -= 2;
}
//check progress
i = progress();
if(i == -1)
{
cout << "\n\n\t\tGame is tied up";
break;
}
}while(i != 1);
//congratulate player
if(i == 1)
{
}
cin.ignore();
cin.get();
return 0;
}
//definitions of IsTied, IsOver and drawboard
/*********************************
1 = Game is over
0 = Game is sill in progress
-1 = Game is tied up
*********************************/
int progress()
{
if(numbers[1] == numbers[2] && numbers[2] == numbers[3])
return 1;
else if(numbers[4] == numbers[5] && numbers[5] == numbers[6])
return 1;
else if(numbers[7] == numbers[8] && numbers[8] == numbers[9])
return 1;
else if(numbers[1] == numbers[4] && numbers[4] == numbers[7])
return 1;
else if(numbers[2] == numbers[5] && numbers[5] == numbers[8])
return 1;
else if(numbers[3] == numbers[6] && numbers[6] == numbers[9])
return 1;
else if(numbers[7] == numbers[5] && numbers[5] == numbers[3])
return 1;
else if(numbers[9] == numbers[5] && numbers[5] == numbers[9])
return 1;
else if(numbers[1] != '1' && numbers[2] != '2' && numbers[3] != '3'
&& numbers[4] != '4' && numbers[5] != '5' && numbers[6] != '6'
&& numbers[7] != '7' && numbers[8] != '8' && numbers[9] != '9')
return -1;
else
return 0;
}
/****************************
FUNCTION THAT DRAWS THE BOARD
****************************/
void drawboard()
{
//clear system screen
system("cls");
cout << "\n\n\t\tTic Tac Toe\n\n";
cout << '\t' <<players.player1name << " = (X) --- " << players.player2name << " = (O)" << endl << endl;
cout << '\t' << " | | " << endl;
cout << '\t' << " " << numbers[1] << " | " << numbers[2] << " | " << numbers[3] << " " << endl;
cout << '\t' << "_____|_____|_____" << endl;
cout << '\t' << " | | " << endl;
cout << '\t' << " " << numbers[4] << " | " << numbers[5] << " | " << numbers[6] << " " << endl;
cout << '\t' << "_____|_____|_____" << endl;
cout << '\t' << " | | " << endl;
cout << '\t' << " " << numbers[7] << " | " << numbers[8] << " | " << numbers[9] << " " << endl;
cout << '\t' << " | | " << endl;
}
I made a java program to do this in college, I don't know how to program it in C++ but basically, to find out who won, you get a boolean which is true when it's player 1s go and false when it's player 2s go. That way when someone wins you can make the program look at the boolean and that will tell you who made the last move.
To solve the fact it says someone won before it puts in the final X just put the code that checks if they won to run after the code that puts down the X
do
{
if(j == 1)
{
players.playersturn = players.player1name;
players.playersnumber = 1;
}
else if(j == 2)
{
players.playersturn = players.player2name;
players.playersnumber = 2;
}
drawboard(); // 4. this point is not reached with final board configuration
cout << '\t' <<players.playersturn << " enter a number : ";
cin >> choice; // 1. user makes winning move
//check progress
i = progress(); // 2. game ended (i=1)
if(i == -1)
{
cout << "\n\n\t\tGame is tied up";
break;
}
}while(i != 1); // 3. program exits this while
// dirty-fix: just a drawboard here:
drawboard();
Related
I'm building a tictactoe game in c++, and whenever users input a number that isn't 1-9, the output is supposed to be 'invalid move' and continue the game
else
{
cout << "Invalid move ";
player--;
cin.ignore();
cin.get();
}
but it instead ends up generating the board repeatedly, and essentially breaking the game. Here is the full code:
// This is a trial for tic tac toe in C++
#include <iostream>
using namespace std;
char *squares = new char[9]{'1', '2', '3', '4', '5', '6', '7', '8', '9'};
void play();
void getBoard();
int checkWin();
int main()
{
char *playAgain = new char;
do
{
play();
cout << "Do you want to play again(y/n): ";
cin >> *playAgain;
} while (tolower(*playAgain) == 'y');
delete squares, playAgain;
cin.get();
return 0;
}
//Play the game
void play()
{
int *i = new int;
int player = 1;
int *choice = new int;
char *mark = new char;
do
{
getBoard();
player = (player%2) ? 1 : 2;
cout << "Enter your choice: ";
cin >> *choice;
*mark = (player == 1) ? 'X' : 'O';
if (squares[0] == '1' && *choice == 1)
{
squares[0] = *mark;
}
else if (squares[1] == '2' && *choice == 2)
{
squares[1] = *mark;
}
else if (squares[2] == '3' && *choice == 3)
{
squares[2] = *mark;
}
else if (squares[3] == '4' && *choice == 4)
{
squares[3] = *mark;
}
else if (squares[4] == '5' && *choice == 5)
{
squares[4] = *mark;
}
else if (squares[5] == '6' && *choice == 6)
{
squares[5] = *mark;
}
else if (squares[6] == '7' && *choice == 7)
{
squares[6] = *mark;
}
else if (squares[7] == '8' && *choice == 8)
{
squares[7] = *mark;
}
else if (squares[8] == '9' && *choice == 9)
{
squares[8] = *mark;
}
else
{
cout << "Invalid move ";
player--;
cin.ignore();
cin.get();
}
*i = checkWin();
player++;
} while (*i == -1);
getBoard();
if (*i == 1)
{
cout << "\aPlayer " << --player << " Wins" << endl;
delete mark, choice, i;
}
else
{
cout << "\aGame Draw" << endl;
delete mark, choice, i;
}
}
// Print the board
void getBoard()
{
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X) - Player 2 (O)" << endl
<< endl;
cout << endl;
cout << " | | " << endl;
cout << " " << squares[0] << " | " << squares[1] << " | " << squares[2] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << squares[3] << " | " << squares[4] << " | " << squares[5] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << squares[6] << " | " << squares[7] << " | " << squares[8] << endl;
cout << " | | " << endl
<< endl;
}
/**********************************************************************************************************
Return 1 if some one wins
Return 0 if draw
Return -1 if the game is not over
***********************************************************************************************************/
int checkWin()
{
if (squares[0] == squares[1] && squares[1] == squares[2])
{
return 1;
}
else if (squares[0] == squares[3] && squares[3] == squares[6])
{
return 1;
}
else if (squares[0] == squares[4] && squares[4] == squares[8])
{
return 1;
}
else if (squares[3] == squares[4] && squares[4] == squares[5])
{
return 1;
}
else if (squares[1] == squares[4] && squares[4] == squares[7])
{
return 1;
}
else if (squares[6] == squares[4] && squares[4] == squares[2])
{
return 1;
}
else if (squares[6] == squares[7] && squares[7] == squares[8])
{
return 1;
}
else if (squares[2] == squares[5] && squares[5] == squares[8])
{
return 1;
}
else if (squares[0] != '1' && squares[1] != '2' && squares[2] != '3' && squares[3] != '4' && squares[4] != '5' && squares[5] != '6' && squares[6] != '7' && squares[7] != '8' && squares[8] != '9')
{
return 0;
}
else
{
return -1;
}
}
How do I resolve this?
ps: any tips on improving the game logic would also be appreciated
I want to reverse the input string; this is the code I wrote, but I am unable to print the reverse of the string.
#include<iostream>
using namespace std;
void main() {
char Title[] = "\t THE ANALYZER";
char phrase[501];
int charTotal = 0;
int numTotal = 0;
int letterTotal = 0;
int vowelTotal = 0;
int consonTotal = 0;
int spacesTotal = 0;
int specialCharTotal = 0;
cout << Title<< endl;
cout << "\t ____________" << endl;
cout << " Enter your phrase (Max 500): ";
cin.getline(phrase, 501);
cout << " Thanks, Analyzing..." << endl;
cout << " These are the informations about your phrase" << endl;
for (int i = 0; phrase[i] != '\0' ; i += 1) {
if (phrase[i] != '\0') {
charTotal++;
}
if (phrase[i] >= '0' && phrase[i] <= '9') {
numTotal++;
}
if ((phrase[i] >= 'A' && phrase[i] <= 'Z') || (phrase[i] >= 'a' && phrase[i] <= 'z')) {
letterTotal++;
}
if (phrase[i] == 'a' || phrase[i] == 'A' || phrase[i] == 'e' || phrase[i] == 'E' || phrase[i] == 'i' || phrase[i] == 'I' || phrase[i] == 'o' || phrase[i] == 'O' || phrase[i] == 'u' || phrase[i] == 'U') {
vowelTotal++;
}
consonTotal = letterTotal - vowelTotal;
if (phrase[i] == ' ') {
spacesTotal++;
}
specialCharTotal = (charTotal - (numTotal + letterTotal + spacesTotal));
}
cout << " Number of characters: " << charTotal << endl;
cout << " Number of numerics: " << numTotal << endl;
cout << " Number of alphabets: " << letterTotal << endl;
cout << "\tNumber of vowels: " << vowelTotal << endl;
cout << "\tNumber of consonants: " << consonTotal << endl;
cout << " Number of spaces: " << spacesTotal << endl;
cout << " Number of special characters: " << specialCharTotal << endl;
cout << " Your phrase in reverse: " << endl'
system("pause");
}
Make use of std::reverse:
std::string s = "reverse me";
std::reverse(s.begin(), s.end());
I am a beginner programmer.I have built a tic tac toe game using c++. The game is working fine until the user is prompted to repeat. This where the problem is. The program doesn't loop correctly. Any help will be appreciated. Thanks.
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
char matrix[10] = {'0','1','2','3','4','5','6','7','8','9' };
void display();
int checkwin();
int restarter();
int main(){
char repeat;
do {
string playername, player1, player2;
int winner = 0;
char mark = 0;
int number = 0;
int player = 1;
char choice = 0;
cout << "Player 1 please enter your name: ";
getline(cin, player1);
cout << "Player 2 please enter your name: ";
getline(cin, player2);
while (winner == 0)
{
display();
if (player % 2)
{
playername = player1;
}
else
playername = player2;
cout << playername << " " << "Please choose a number you want" << endl;
cin >> number;
if (player % 2)
{
mark = 'X';
}
else
mark = 'O';
if (number == 1 && matrix[1] == '1')
{
matrix[1] = mark;
}
else if (number == 2 && matrix[2] == '2')
{
matrix[2] = mark;
}
else if (number == 3 && matrix[3] == '3')
{
matrix[3] = mark;
}
else if (number == 4 && matrix[4] == '4')
{
matrix[4] = mark;
}
else if (number == 5 && matrix[5] == '5')
{
matrix[5] = mark;
}
else if (number == 6 && matrix[6] == '6')
{
matrix[6] = mark;
}
else if (number == 7 && matrix[7] == '7')
{
matrix[7] = mark;
}
else if (number == 8 && matrix[8] == '8')
{
matrix[8] = mark;
}
else if (number == 9 && matrix[9] == '9')
{
matrix[9] = mark;
}
else
{
cout << "WRONG MOVE!";
player--;
cin.ignore();
cin.get();
}
winner = checkwin();
player++;
display();
if (winner == 1)
{
cout << playername << " " << "WON!" << endl;
}
else
cout << "Its a draw!" << endl;
}
cout << "do u wana repeat?" << endl;
cin >> repeat;
} while (repeat == 'Y');
system("pause");
return 0;
}
void display()
{
system("CLS");
cout << "======= Welcome to You Tic and I Tac Your Toe =======" << endl;
cout << "======= Ivan =======" << endl;
cout << "======= & =======" << endl;
cout << "======= Mostafa =======" << endl;
cout << "=====================================================" << endl;
cout << "\n" << endl;
cout << "PLAYER 1 [X] PLAYER 2 [O]" << endl;
cout << " | | " << endl;
cout << " " << matrix[1] <<" | "<<matrix[2]<<" | "<< matrix[3] << endl;
cout << "___|___|____" << endl;
cout << " | |" << endl;
cout << " " << matrix[4] <<" | "<<matrix[5]<<" | "<< matrix[6] << endl;
cout << "___|___|____" << endl;
cout << " | |" << endl;
cout << " " << matrix[7] <<" | "<<matrix[8]<<" | " << matrix[9] << endl;
cout << " | |" << endl;
}
int checkwin()
{
if (matrix[1] == matrix[2] && matrix[2] == matrix[3])
{
return 1;
}
else if (matrix[4] == matrix[5] && matrix[5] == matrix[6])
{
return 1;
}
else if (matrix[7] == matrix[8] && matrix[8] == matrix[9])
{
return 1;
}
else if (matrix[1] == matrix[4] && matrix[4] == matrix[7])
{
return 1;
}
else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
{
return 1;
}
else if (matrix[3] == matrix[6] && matrix[6] == matrix[9])
{
return 1;
}
else if (matrix[2] == matrix[5] && matrix[5] == matrix[8])
{
return 1;
}
else if (matrix[3] == matrix[5] && matrix[5] == matrix[7])
{
return 1;
}
else if (matrix[1] == matrix[5] && matrix[5] == matrix[9])
{
return 1;
}
else if (matrix[1] != '1' && matrix[2] != '2' && matrix[3] != '3' && matrix[4] != '4' && matrix[5] != '5' && matrix[6] != '6' && matrix[7] != '7' && matrix[8] != '8' && matrix[9] != '9')
return 2;
return 0;
}
The problem is that you aren't clearing your matrix after the game is won. Your matrix is a global variable, so it is created at the beginning of the program, and is not destroyed until the program stops. When you loop, all of the local variables created in the loop are destroyed, but not global variables. You need to manually clear this array. A function like this:
void clearMatrix()
{
for(int i = 0; i<10; i++)
matrix[i] = '0'+i;
}
would clear the matrix if you execute it in the appropriate place.
int main(){
char repeat;
do {
clearMatrix();
string playername, player1, player2;
int winner = 0;
char mark = 0;
...
}
If you call this clearMatrix() function at the beginning of your do-while loop, your matrix will be reset every time.
cout << "Player 1 please enter your name: ";
cin >> player1;
cout << "Player 2 please enter your name: ";
cin >> player2;
note that you can use "cin" instead of using "getline" to avoid an error when typing the names again.
also do this:
else
cout << "Its a draw!" << endl;
}
clearMatrix();
cout << "do u wanna repeat?" << endl;
cin >> repeat;
}
as you have to clear the matrix so it is a good programming practice to call the function clearMatrix(); at the bottom as defined above, while remaining inside the do-while loop.
I am writing a tic-tac-toe game and I am finding it hard to make function that checks when a space is filled by either the noughts or crosses
Here is the full code (sorry a bit long):
#include <iostream>
using namespace std;
const int kingsize = 3;
char board[kingsize][kingsize];
bool p1 = false;
int p2 = -1;
char empty = 0;
/*this functions dispays the board*/
void bord()
{
cout <<
" ";
for (int iX = 0; iX < kingsize; iX++)
cout << " " << iX << " ";
cout << endl;
cout << " ";
for (int iX = 0; iX < kingsize; iX++)
cout << "+---";
cout << "+" << endl;
for (int iY = 0; iY < kingsize; iY++)
{
cout << " " << iY << " ";
for (int iX = 0; iX < kingsize; iX++)
cout << "|" << board[iY][iX] << " ";//this is the space to be filled by Os or Xs
cout << "|" << endl;
cout << " ";
for (int iX = 0; iX < kingsize; iX++)
cout << "+---";
cout << "+" << endl;
}
}
/*when this function is called it will show a blank board*/
void resetBoard()
{
for (int iY = 0; iY<kingsize; iY++)
{
for (int iX = 0; iX<kingsize; iX++)
{
board[iY][iX] = ' ';
}
}
}
/*this funtion is to plot the symbols in the grid based on the column and row*/
bool setboard(int iX, int iY, char cval)
{
if (iX >= kingsize || iY >= kingsize)
return false;
board[iY][iX] = cval;
return true;
}
/*this checks if there space is filled by one of the symbols*/
bool checkbord(int x, int z, char val)
{
bool fill = false;
if (board[x][z] != ' ')
{
fill = true;
}
return fill;
}
bool win(int tir)
{
int win = 3;
return (board[0][0] + board[0][1] + board[0][2] == win) // row 0
|| (board[1][0] + board[1][1] + board[1][2] == win) // chicking row 1
|| (board[2][0] + board[2][0] + board[2][2] == win) // checking row 2
|| (board[0][0] + board[1][1] + board[2][0] == win) // checking column 0
|| (board[0][1] + board[1][1] + board[2][1] == win) // column 1
|| (board[0][2] + board[1][2] + board[2][2] == win) // column 2
|| (board[0][0] + board[1][1] + board[2][2] == win) // diagonal
|| (board[2][0] + board[1][1] + board[0][2] == win) ; // checking diagonal
}
int main()
{
const int r = 3, c = 3;
char sym1 = 'X';
char sym2 = 'O';
char sym = 'O' || 'X';
cout << "TIC TAC TOE" << endl;
cout << endl;
cout << "How To Play" << endl;
cout << "to plot noughts and crosses you write the row number first " << endl;
cout << "then the number of the column" << endl;
cout << "Example :" << endl;
cout << endl;
resetBoard();
bord();
cout << endl;
cout << "plotting row 2 column 0" << endl;
cout << endl;
setboard(0, 2, 'X');
bord();
resetBoard();
cout << endl;
cout << "OK LET'S PLAY!!!" << endl;
bord();
int y, z;
int t=0;
do
{
loop:
cout << "first player to plot X:" << endl;
cout << "col No." << endl;
cin >> y;
cout << "row No." << endl;
cin >> z;
t++;
if (y > 2 || z > 2)//if the player enters numbers higher than too it will show this error message
{
cout << "error enter number between 0-2" << endl;
t--;
goto loop;
}
if (checkbord(y, z, sym) == 1)//if the player plots their symbol in a space that is already filled it will show this error message
{
cout << "spot alreay filled, please try again" << endl;
cout << endl;
t--;
goto loop;
}
setboard(y, z, sym1);//will plot the symbol based on what the player enetered
bord();
loop2:
cout << "second player plot O:" << endl;
cout << "col No." << endl;
cin >> y;
cout << "row No." << endl;
cin >> z;
if (y > 2 || z > 2)
{
cout << "error enter number between 0-2" << endl;
goto loop2;
}
if (checkbord(y, z, sym) == 1)
{
cout << "spot alreay filled" << endl;
t--;
goto loop2;
}
setboard(y, z, sym2);
bord();
} while (t <4);
loop3:
cout << "first player to plot X:" << endl;
cout << "col No." << endl;
cin >> y;
cout << "row No." << endl;
cin >> z;
t++;
if (y > 2 || z > 2)
{
cout << "error enter number between 0-2" << endl;
t--;
goto loop3;
}
if (checkbord(y, z, sym) == 1)
{
cout << "spot alreay filled" << endl;
t--;
goto loop3;
}
setboard(y, z, sym1);
bord();
return 0;
}
The problem is that checkbord function works sometimes or not at all. if the player plots the noughts/cross in a place that is already taken then it will replace it and its not supposed to do that
edit: I am not getting an error but feel the problem is with the checkbord function and I am out of ideas
Look at these functions:
bool setboard(int iX, int iY, char cval)
{
...
board[iY][iX] = cval;
...
}
bool checkbord(int x, int z, ...)
{
bool fill = false;
if (board[x][z] != ' ')
{
fill = true;
}
return fill;
}
So setboard(1,2) sets the value of board[2][1], but checkbord(1,2,...) checks the value of board[1][2].
There are other problems in this code, but you can start with this one.
Okay so right now when I try to determine valid input to play again, it will display the results of the game again right after displaying invalid entry and then ask the user to again enter "y" or "n" . I have uploaded a picture to show you. I cannot figure it out for the life of me.
Here is the image:
http://imgur.com/SRsMo4P
// GAME OVER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while (bGameOver)
{
// Display game board - PRINT
for (int i = 0; i<ROW; ++i)
{
cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
if (i==0 || i==1)
{
cout << " - + - + -" << endl;
}
}
// Player wins - OUTPUT
if(bWinGame)
{
cout << endl;
cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
}
// Play again - OUTPUT & USER INPUT
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// VARIABLES
char again; // Play again = "Y" or "N"
cout << endl;
cout << " Want to play again? ( Y / N )" << endl << endl;
cout << " "; cin >> again; cout << endl;
// Play again - if YES
if (again == 'y' || again == 'Y')
{
bGameOver=false; // Reset game state
bWinGame=true; // Reset assumption
board[0][0] = '*'; // Rest game board - Array
board[0][1] = '*';
board[0][2] = '*';
board[1][0] = '*';
board[1][1] = '*';
board[1][2] = '*';
board[2][0] = '*';
board[2][1] = '*';
board[2][2] = '*';
}
// Play again - if NO
else if (again == 'n' || again == 'N')
{
cout << " Awe oh well, thanks for playing. " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
break;
}
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
}
}
// Game play continue
if (!bGameOver)
{
// Switch player turn
if (playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
}
}
cout << " "; return 0;
}
You have no break in this else statement:
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
}
The code will continue to run without a break, causing strange behavior. Try this:
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
break;
}
Put your input validation code in loop. If Y or N is chosen break the loop.
P.S. Add coorrections in code, break; is in the wrigth place now.
// GAME OVER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while (bGameOver)
{
// Display game board - PRINT
for (int i = 0; i<ROW; ++i)
{
cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
if (i==0 || i==1)
{
cout << " - + - + -" << endl;
}
}
// Player wins - OUTPUT
if(bWinGame)
{
cout << endl;
cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
}
// Play again - OUTPUT & USER INPUT
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// VARIABLES
bool bInvalidInput = true;
while(bInvalidInput)
{
char again; // Play again = "Y" or "N"
cout << endl;
cout << " Want to play again? ( Y / N )" << endl << endl;
cout << " "; cin >> again; cout << endl;
// Play again - if YES
if (again == 'y' || again == 'Y')
{
bGameOver=false; // Reset game state
bWinGame=true; // Reset assumption
bInvalidInput = false; //stop dialog
board[0][0] = '*'; // Rest game board - Array
board[0][1] = '*';
board[0][2] = '*';
board[1][0] = '*';
board[1][1] = '*';
board[1][2] = '*';
board[2][0] = '*';
board[2][1] = '*';
board[2][2] = '*';
}
// Play again - if NO
else if (again == 'n' || again == 'N')
{
cout << " Awe oh well, thanks for playing. " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
bInvalidInput = false; //stop dialog
// break; //need no more
}
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
}
}
if (bGameOver == true)
{
break; //get here only if bInvalidInput is false and N pressed
}
}
// Game play continue
if (!bGameOver)
{
// Switch player turn
if (playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
}
}
Try moving the sections labelled Display game board - PRINT and Player wins - OUTPUT outside of (before) the while loop.
Thank you for the suggestions but for some reason they were not working properly so I ended up doing this here is the completed code:
// FILE: Tic Tac Toe.cpp
// PROGRAMMER: Karolina Sabat CPSC 1103 Section: S11
// ~~ TWO PLAYER TIC TAC TOE GAME ~~
// Asks the users (player 1, followed by player 2) to select a row and column by
entering the corresponding row and column number.
// The program will substitute the player's selection with either an "X" or "O".
// A horizontal, vertical or diagonal row of either X's or O's results in a win or
otherwise game ends in a draw.
// For subsequent plays, player 1 and player 2 alternate going first.
#include<iostream> // For cin, cout
using namespace std;
// MAIN FUNCTION
int main()
{
// VARIABLES
int playerTurn = 1; // Player's turn - Player 1
const int ROW=3; // Table - Number of rows - For array
const int COL=3; // Table - Number of columns - For array
bool bGameOver= false; // Game state - True = Game over, False = Continue play
char again; // Play again = "Y" or "N"
char board[ROW][COL] = { {'*', '*', '*'},
{'*', '*', '*'},
{'*', '*', '*'} }; // Game board - Array
// TITLE
cout << endl;
cout << " TIC TAC TOE" << endl;
cout << " ____________________________________________________" << endl;
cout << " A two player game." << endl;
cout << endl;
// Game state = Continue play - Game is NOT over.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Continue play - LOOP
while (!bGameOver)
{
// Game board - Array - PRINT
for (int i = 0; i < ROW; ++i)
{
cout << " " << board[i][0] <<" | " << board[i][1] <<" | " << board[i][2] << endl;
if (i==0 || i==1)
{
cout << " - + - + -" << endl;
}
}
// Set player mark ( "X" or "O" )
// VARIABLES
char playerMark;
if (playerTurn == 1) // Player 1 = "X"
{
playerMark = 'X';
}
else
{
playerMark = 'O'; // Player 2 = "O"
}
// Player move - USER INPUT
// VARIABLES
int move_r; // Row position - USER INPUT
int move_c; // Column position - USER INPUT
bool validMove = false; // Bool ValidMove - Assume false
cout << endl;
cout << " Player " << playerTurn << " , please pick a row and column to place "<< playerMark << "." << endl;
cout << " Separate the row and column number with a space and press ENTER." << endl;
while (!validMove) // DETERMINE VALIDITY - Check play move entries
{
validMove = true;
cout << endl;
cout << " "; cin >> move_r >> move_c; cout << endl;
// Row 1, Column 1
if(move_r == 1 && move_c == 1 && board[0][0] == '*')
{
board[0][0] = playerMark;
}
// Row 1, Column 2
else if(move_r == 1 && move_c == 2 && board[0][1] == '*')
{
board[0][1] = playerMark;
}
// Row 1, Column 3
else if(move_r == 1 && move_c == 3 && board[0][2] == '*')
{
board[0][2] = playerMark;
}
// Row 2, Column 1
else if(move_r == 2 && move_c == 1 && board[1][0] == '*')
{
board[1][0] = playerMark;
}
// Row 2, Column 2
else if(move_r == 2 && move_c == 2 && board[1][1] == '*')
{
board[1][1] = playerMark;
}
// Row 2, Column 3
else if(move_r == 2 && move_c == 3 && board[1][2] == '*')
{
board[1][2] = playerMark;
}
// Row 3, Column 1
else if(move_r == 3 && move_c == 1 && board[2][0] == '*')
{
board[2][0] = playerMark;
}
// Row 3, Column 2
else if(move_r == 3 && move_c == 2 && board[2][1] == '*')
{
board[2][1] = playerMark;
}
// Row 3, Column 3
else if(move_r == 3 && move_c == 3 && board[2][2] == '*')
{
board[2][2] = playerMark;
}
// INVALID ENTRY
else
{
cout << " Invalid Move, please try again!" << endl << endl;
// Will clear characters
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
validMove = false;
}
}
// Check if game over conditions are met
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// If row 0 or column 0 = same playerMark
if (board[0][0] != '*')
{
if (board[0][0] == board[0][1] && board[0][0] == board[0][2]) // Check row 0
{
bGameOver=true;
}
if (board[0][0] == board[1][0] && board[0][0] == board[2][0]) // Check column 0
{
bGameOver=true;
}
}
// If row 1 or column 1 = same playerMark
if (board[1][1] != '*')
{
if(board[1][1] == board[1][0] && board[1][1] == board[1][2]) // Check row 1
{
bGameOver=true;
}
if(board[1][1] == board[0][1] && board[1][1] == board[2][1]) // Check column 1
{
bGameOver=true;
}
if(board[1][1] == board[0][0] && board[1][1] == board[2][2]) // Diagonals - Check if 1,1 and 3,3 are equal to 2,2
{
bGameOver=true;
}
if(board[1][1] == board[2][0] && board[1][1] == board[0][2]) // Diagnonals - Check if 1,3 and 3,1 are equal to 2,2
{
bGameOver=true;
}
}
// If row 2 or column 2 = same playerMark
if (board[2][2] != '*')
{
if (board[2][2] == board[2][1] && board[2][2] == board[2][0]) // Check row 2
{
bGameOver=true;
}
if (board[2][2] == board[1][2] && board[2][2] == board[0][2]) // Check column 2
{
bGameOver=true;
}
}
// Check if DRAW
bool bWinGame=true; // ASSUMPTION - A player won
if (board[0][0] !='*' && board[0][1] !='*' && board[0][2] !='*' &&
board[1][0] !='*' && board[1][1] !='*' && board[1][2] !='*' &&
board[2][0] !='*' && board[2][1] !='*' && board[2][2] !='*' && !bGameOver)
{
bGameOver=true;
bWinGame=false;
// Tie - OUTPUT
cout << " Oh, won't you look at that, it's a TIE!"<< endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
}
// GAME OVER
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while (bGameOver)
{
// Display game board - PRINT
for (int i = 0; i<ROW; ++i)
{
cout << " " << board[i][0] << " | " << board[i][1] << " | " << board[i][2] << endl;
if (i==0 || i==1)
{
cout << " - + - + -" << endl;
}
}
// Player wins - OUTPUT
if(bWinGame)
{
cout << endl;
cout << " Player "<< playerTurn << " wins! HOORAH! " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
}
// Play again - OUTPUT & USER INPUT
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// VARIABLES
bool bValidInput = false;
while (bValidInput != true)
{
cout << endl;
cout << " Want to play again? ( Y / N )" << endl << endl;
cout << " ";
// Will clear characters
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> again; cout << endl;
// Play again - if YES
if (again == 'y' || again == 'Y')
{
bValidInput = true;
bGameOver=false; // Reset game state
bWinGame=true; // Reset assumption
board[0][0] = '*'; // Rest game board - Array
board[0][1] = '*';
board[0][2] = '*';
board[1][0] = '*';
board[1][1] = '*';
board[1][2] = '*';
board[2][0] = '*';
board[2][1] = '*';
board[2][2] = '*';
}
// Play again - if NO
else if (again == 'n' || again == 'N')
{
bValidInput = true; // Assumes
cout << " Awe oh well, thanks for playing. " << endl << endl;
cout << " Written by: Karolina Sabat - CPSC 1103 - Section: S11" << endl << endl;
cout << " "; return 0;
}
// Play again - INVALID ENTRY
else
{
cout << " INVALID ENTRY: Please input \"Y\" or \"N\" " << endl << endl;
bValidInput = false;
}
}
}
// Game play continue
if (!bGameOver)
{
// Switch player turn
if (playerTurn == 1)
{
playerTurn = 2;
}
else
{
playerTurn = 1;
}
}
}
// EXIT PROGRAM
cout << " "; return 0;
}