Unexpected unqualified-id before 'while' - c++

Trying to make a BASIC TicTacToe game using OOP C++
The errors I'm getting are:
line 74 unexpected unqualified-id before 'while' (1)
line 139 error: expected '}' at end of input (2)
line 77 error: expected unqualified-id at end of input (3)
I have no idea how those brackets could be wrong...Thank you in advance!
Here is my code:
#include <iostream>
#include <stdlib.h>
using namespace std;
class TicTacToe
{
private:
int player=1, cw , ch1, ch2; //ch= choice for rows and columns
char pick, grid[10]= {' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
public:
int checkWin()
{
if (grid[1] == grid[2] && grid[2] == grid[3])
return 1;
else if (grid[4] == grid[5] && grid[5] == grid[6])
return 1;
else if (grid[7] == grid[8] && grid[8] == grid[9])
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[3] == grid[6] && grid[6] == grid[9])
return 1;
else if (grid[1] == grid[5] && grid[5] == grid[9])
return 1;
else if (grid[3] == grid[5] && grid[5] == grid[7])
return 1;
else if (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')
return 0;
else
return -1;
}//check for winner
char mark()
{
if(player==1)
return 'X';
else
return 'O';
}
void board()
{
system("cls");
cout << "\n\n\tTic Tac Toe\n\n"; //learned that \t is to tab it in instead of using spaces
cout << "Player 1 = X Player 2 = O" << endl << endl;
cout << endl;
cout << " 1 2 3 ";
cout <<"\n";
cout << " | | " << endl;
cout << "1 " << grid[1] << " | " << grid[2] << " | " << grid[3] << endl;
cout << " | | " << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << "2 " << grid[4] << " | " << grid[5] << " | " << grid[6] << endl;
cout << " | | " << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << "3 " << grid[7] << " | " << grid[8] << " | " << grid[9] << endl;
cout << " | | " << endl;
cout << " | | " << endl << endl;
} // and for some reason this one (3)
while (i==-1) // **this one (1)**
{
if(player %2)
player==1
else
player==2
cout<< "Please enter 1-3 for row: ";
cin>> ch1;
cout<< "Please enter 1-3 for coumns: ";
cin>>ch2;
mark();
if(ch1=1 && ch2 ==1)
mark = grid[1];
else if (ch1=2 && ch2== 1)
mark = grid[2];
else if (ch1=3 && ch2== 1)
mark = grid[3];
else if (ch1=1 && ch2== 2)
mark = grid[4];
else if (ch1=2 && ch2== 2)
mark = grid[5];
else if (ch1=3 && ch2== 2)
mark = grid[6];
else if (ch1=1 && ch2== 3)
mark = grid[7];
else if (ch1=2 && ch2== 3)
mark = grid[8];
else if (ch1=3 && ch2== 3)
mark = grid[9];
else
{
cout<<" Move is invalid";
player--; //so player can retake turn
//cin.ignore(); //ignore what was input
//cin.get(); // get answers
}
cw= checkwin();
}
board();
if(i==1)
cout<<"\aPlayer "<<--player<<" win "; // a makes a beep!
else
cout<<"\aGame draw";
//cin.ignore();
//cin.get();
return 0;
};
int main()
{
cout<<" \tWelcome to TicTacToe!";
TicTacToe game;
return 0;
} // **issue with this one (2)**

You ended your board function here:
cout << " | | " << endl << endl;
} // and for some reason this one (3) <----PROBLEM is this Closing Brace
while (i==-1) // **this one (1)**

you forgot to write ; at the end of a decleration:
while(i == -1) // **this one (1)**
{
if (player % 2)
player == 1
else
AND
player == 2 //You forgot to write ;
cout << "Please enter 1-3 for row: ";
cin >> ch1;
cout << "Please enter 1-3 for coumns: ";
cin >> ch2;
this makes probems later in the code when stuff isn't found!

Related

I'm making a Tic-Tac-Toe game and I'm having a logical error for when the game runs the X's and O's can overtake each other

So, this bug has something to do with converting the player choice 1-9 to displaying it on the board with a X or O. It displays correctly it's just they can over loop each other buy just putting in the same input and I'm not quite sure how to fix this. I've tried moving the convert() around the play game() in the for loop to see if that would somehow fix it by trial and error but no it seems its a coding issue in the convert() function I just don't know where or what that issue is. I'm still learning C++ I'm just testing myself I saw a few people make a tik tac toe game online and I wanted to give it a shot, so any advice is appreciated.
#include "functions.h"
#include <iostream>
#include <vector>
using namespace std;
vector<string> player_icons = { "X", "O" };
vector<string> grid = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
int player1 = 0;
int player2 = 0;
string who_Won1 = "Player 1";
string who_Won2 = "Player 2";
void intro() {//The intro sequence to game
cout << "==================\n";
cout << " Tic Tac Toe Game \n";
cout << "==================\n";
cout << "\n";
cout << "Instructions: This game will require 2 players\n";
cout << "To win you need to match 3 in a row of the same\n";
cout << "\n";
cout << "Player 1: X\n";
cout << "Player 2: O\n";
cout << "\n";
cout << " | | \n";
cout << " 1 | 2 | 3 \n";
cout << "_____|_____|_____ \n";
cout << " | | \n";
cout << " 4 | 5 | 6 \n";
cout << "_____|_____|_____ \n";
cout << " | | \n";
cout << " 7 | 8 | 9 \n";
cout << " | | \n";
cout << "\n";
cout << "Above is the example of what the grid is going to look like when you play\n";
cout << "Each player must select a number 1-9 to put there X or O there\n";
}
void board_make() {//outputs to the terminal the actual tic tac toe board
std::cout << " | | \n";
std::cout << " " << grid[0] << " | " << grid[1] << " | " << grid[2] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << grid[3] << " | " << grid[4] << " | " << grid[5] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << grid[6] << " | " << grid[7] << " | " << grid[8] << "\n";
std::cout << " | | \n";
}
bool win_condition() {//its in the name it checks for wins
bool winner = false;
//rows
if ((grid[0] == grid[1]) && (grid[1] == grid[2]) && grid[0] != " ") {
winner = true;
}
else if ((grid[3] == grid[4]) && (grid[4] == grid[5]) && grid[3] != " ") {
winner = true;
}
else if ((grid[6] == grid[7]) && (grid[7] == grid[8]) && grid[6] != " ") {
winner = true;
}
//columns
if ((grid[0] == grid[3]) && (grid[3] == grid[6]) && grid[0] != " ") {
winner = true;
}
else if ((grid[1] == grid[4]) && (grid[4] == grid[7]) && grid[1] != " ") {
winner = true;
}
else if ((grid[2] == grid[5]) && (grid[5] == grid[8]) && grid[2] != " ") {
winner = true;
}
//across
if ((grid[0] == grid[4]) && (grid[4] == grid[8]) && grid[0] != " ") {
winner = true;
}
else if ((grid[2] == grid[4]) && (grid[4] == grid[6]) && grid[2] != " ") {
winner = true;
}
return winner;
}
void game_start() {
for (int i = 0; i < 5; i ++) {//iterates through both players turns till after the
//Player 1's turn
cout << "\n";
cout << "Player 1 its your turn please enter 1-9 to select your choice: "; cin >> player1; convert(); cout << "\n";
board_make();
win();
cout << "\n";
//Player 2's turn
cout << "\n";
cout << "Player 2 its now your turn select your choice 1-9: "; cin >> player2; convert(); cout << "\n";
board_make();
win();
cout << "\n";
}
}
void convert() {
for (int i = 0; i < 8; i++) {
//Player 1 checking
if (player1 == i + 1 && player2 != i + 1) {
grid.at(i) = "X";
}
if (player2 == i + 1 && player1 != i + 1) {
grid.at(i) = "O";
}
}
}
void win() {
if (win_condition()) {
if ((grid[0] == grid[1]) && (grid[1] == grid[2]) && grid[0] != " ") {
if (grid[0] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[0] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[3] == grid[4]) && (grid[4] == grid[5]) && grid[3] != " ") {
if (grid[3] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[3] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[6] == grid[7]) && (grid[7] == grid[8]) && grid[6] != " ") {
if (grid[6] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[6] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
//columns
if ((grid[0] == grid[3]) && (grid[3] == grid[6]) && grid[0] != " ") {
if (grid[0] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[0] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[1] == grid[4]) && (grid[4] == grid[7]) && grid[1] != " ") {
if (grid[1] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[1] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[2] == grid[5]) && (grid[5] == grid[8]) && grid[2] != " ") {
if (grid[2] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[2] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
//across
if ((grid[0] == grid[4]) && (grid[4] == grid[8]) && grid[0] != " ") {
if (grid[0] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[0] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
else if ((grid[2] == grid[4]) && (grid[4] == grid[6]) && grid[2] != " ") {
if (grid[2] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[2] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
}
}
I think your problem is that player1 and player2 variables only store where that players last move was. So once one player makes their second move the other player can overwrite the first move.
You could fix this by checking to see if the grid contains " " before making the move.
Best of luck!
Update: I made more of my code readable or tried to, but I fixed the bug I wanted to fix here is the updated code
#include "functions.h"
#include <iostream>
#include <vector>
using namespace std;
vector<string> player_icons = { "X", "O" };
vector<string> grid = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
int player1 = 0;
int player2 = 0;
string who_Won1 = "Player 1";
string who_Won2 = "Player 2";
bool filled = false;
void intro() {//The intro sequence to game
cout << "==================\n";
cout << " Tic Tac Toe Game \n";
cout << "==================\n";
cout << "\n";
cout << "Instructions: This game will require 2 players\n";
cout << "To win you need to match 3 in a row of the same\n";
cout << "\n";
cout << "Player 1: X\n";
cout << "Player 2: O\n";
cout << "\n";
cout << " | | \n";
cout << " 1 | 2 | 3 \n";
cout << "_____|_____|_____ \n";
cout << " | | \n";
cout << " 4 | 5 | 6 \n";
cout << "_____|_____|_____ \n";
cout << " | | \n";
cout << " 7 | 8 | 9 \n";
cout << " | | \n";
cout << "\n";
cout << "Above is the example of what the grid is going to look like when you play\n";
cout << "Each player must select a numer 1-9 to put there X or O there\n";
}
void board_make() {
std::cout << " | | \n";
std::cout << " " << grid[0] << " | " << grid[1] << " | " << grid[2] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << grid[3] << " | " << grid[4] << " | " << grid[5] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << grid[6] << " | " << grid[7] << " | " << grid[8] << "\n";
std::cout << " | | \n";
}
void win_condition() {//its in the name it checks for wins
//rows
if ((grid[0] == grid[1]) && (grid[1] == grid[2]) && grid[0] != " ") {
win(0);
}
else if ((grid[3] == grid[4]) && (grid[4] == grid[5]) && grid[3] != " ") {
win(3);
}
else if ((grid[6] == grid[7]) && (grid[7] == grid[8]) && grid[6] != " ") {
win(6);
}
// columns
if ((grid[0] == grid[3]) && (grid[3] == grid[6]) && grid[0] != " ") {
win(0);
}
else if ((grid[1] == grid[4]) && (grid[4] == grid[7]) && grid[1] != " ") {
win(1);
}
else if ((grid[2] == grid[5]) && (grid[5] == grid[8]) && grid[2] != " ") {
win(2);
}
//across
if ((grid[0] == grid[4]) && (grid[4] == grid[8]) && grid[0] != " ") {
win(0);
}
else if ((grid[2] == grid[4]) && (grid[4] == grid[6]) && grid[2] != " ") {
win(2);
}
}
void game_start() {
for (int i = 0; i < 5; i ++) {//iterates through both players turns till after the
//Player 1's turn
cout << "\n";
cout << "Player 1 its your turn please enter 1-9 to select your choice: "; cin >> player1; convert(); cout << "\n";
board_make();
win_condition();
cout << "\n";
//Player 2's turn
cout << "\n";
cout << "Player 2 its now your turn select your choice 1-9: "; cin >> player2; convert(); cout << "\n";
board_make();
win_condition();
cout << "\n";
}
}
void convert() {
for (int i = 0; i < 8; i++) {
//checks if the spot is filled if it is filled it sets filled to true
if ((grid[i] == "X" || grid[i] == "O") && grid[i] != " ") {
filled = true;
}
else if (grid[i] == " "){
filled = false;
}
if (filled == true && player1 == i + 1 && grid[i] != " ") {//if filled is true and player choice is a option that was filled then it outputs whats below
cout << "Looks like that spot is already taken, please select another space\n";
}
if (filled == true && player2 == i + 1 && grid[i] != " ") {//if filled is true and player choice is a option that was filled then it outputs whats below
cout << "Looks like that spot is already taken, please select another space\n";
}
//converting the player choice 1-9 to a X or O
if (filled == false && player1 == i + 1 && player2 != i + 1) {
grid.at(i) = "X";
}
if (filled == false && player2 == i + 1 && player1 != i + 1) {
grid.at(i) = "O";
}
}
}
void win(int num) {
if (grid[num] == "X") {
cout << who_Won1 << " Won\n";
exit(0);
}
else if (grid[num] == "O") {
cout << who_Won2 << " Won\n";
exit(0);
}
}
}

tictactoe: check for invalid input

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

basic Tic tac toe game not winning without player 2 making a move

I am a beginner in c++ and am taking a course at my university, I am creating a C++ [rogram for tic tac toe and im having some struggles I cannot figure out how to make the game stop when player 1 wins at the moment if player 1 wins player 2 still has to take their next turn to make the game output Player 1 wins... I will include the code below. also I need a user defined function and can't think of one that would be good to use besides displaying an empty board if anyone has any ideas that would be great. Thanks for the help in advance!!
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void gameBoard() {
cout << " Reference:" << endl;
cout << " | | "
<< " 1 2 3" << endl;
cout << " | | " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 4 5 6" << endl;
cout << " | | " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 7 8 9" << endl;
cout << " | | " << endl;
}
int main(int argc, const char* argv[]) {
string player1Name;
string player2Name;
int j;
int k;
int l;
int userInput;
const int NUM_ELEMENTS = 10;
vector<char> boxVals(NUM_ELEMENTS);
unsigned int i;
for (i = 0; i < boxVals.size(); ++i) {
boxVals.at(i) = ' ';
}
cout << "enter Player 1's Name: ";
cin >> player1Name;
cout << "enter Player 2's Name: ";
cin >> player2Name;
gameBoard();
for (k = 0; k < 10; k++) {
for (j = 0; j < 10; j++) {
cin >> userInput;
boxVals.at(userInput) = 'x';
break;
}
cout << " Reference:" << endl;
cout << " | | "
<< " 1 2 3" << endl;
cout << " " << boxVals.at(1) << " | " << boxVals.at(2) << " | "
<< boxVals.at(3) << " " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 4 5 6" << endl;
cout << " " << boxVals.at(4) << " | " << boxVals.at(5) << " | "
<< boxVals.at(6) << " " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 7 8 9" << endl;
cout << " " << boxVals.at(7) << " | " << boxVals.at(8) << " | "
<< boxVals.at(9) << " " << endl;
for (l = 0; l < 10; l++) {
cin >> userInput;
if (boxVals.at(userInput != 'x')) {
boxVals.at(userInput) = 'O';
} else if (boxVals.at(userInput) == 'x') {
cout << "invalid move" << endl;
continue;
}
break;
}
cout << " Reference:" << endl;
cout << " | | "
<< " 1 2 3" << endl;
cout << " " << boxVals.at(1) << " | " << boxVals.at(2) << " | "
<< boxVals.at(3) << " " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 4 5 6" << endl;
cout << " " << boxVals.at(4) << " | " << boxVals.at(5) << " | "
<< boxVals.at(6) << " " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 7 8 9" << endl;
cout << " " << boxVals.at(7) << " | " << boxVals.at(8) << " | "
<< boxVals.at(9) << " " << endl;
if ((boxVals.at(1) == 'x' && boxVals.at(2) == 'x' &&
boxVals.at(3) == 'x') ||
(boxVals.at(4) == 'x' && boxVals.at(5) == 'x' &&
boxVals.at(6) == 'x') ||
(boxVals.at(7) == 'x' && boxVals.at(8) == 'x' &&
boxVals.at(9) == 'x') ||
(boxVals.at(1) == 'x' && boxVals.at(5) == 'x' &&
boxVals.at(9) == 'x') ||
(boxVals.at(3) == 'x' && boxVals.at(5) == 'x' &&
boxVals.at(7) == 'x') ||
(boxVals.at(1) == 'x' && boxVals.at(4) == 'x' &&
boxVals.at(7) == 'x') ||
(boxVals.at(2) == 'x' && boxVals.at(5) == 'x' &&
boxVals.at(8) == 'x') ||
(boxVals.at(3) == 'x' && boxVals.at(6) == 'x' &&
boxVals.at(9) == 'x')) {
cout << player1Name << " Wins!!" << endl;
break;
}
if ((boxVals.at(1) == 'O' && boxVals.at(2) == 'O' &&
boxVals.at(3) == 'O') ||
(boxVals.at(4) == 'O' && boxVals.at(5) == 'O' &&
boxVals.at(6) == 'O') ||
(boxVals.at(7) == 'O' && boxVals.at(8) == 'O' &&
boxVals.at(9) == 'O') ||
(boxVals.at(1) == 'O' && boxVals.at(5) == 'O' &&
boxVals.at(9) == 'O') ||
(boxVals.at(3) == 'O' && boxVals.at(5) == 'O' &&
boxVals.at(7) == 'O') ||
(boxVals.at(1) == 'O' && boxVals.at(4) == 'O' &&
boxVals.at(7) == 'O') ||
(boxVals.at(2) == 'O' && boxVals.at(5) == 'O' &&
boxVals.at(8) == 'O') ||
(boxVals.at(3) == 'O' && boxVals.at(6) == 'O' &&
boxVals.at(9) == 'O')) {
cout << player2Name << " Wins!" << endl;
break;
}
}
return 0;
}
please ignore the probably terrible code this is some of my best work so far
The simple explanation is that you don't check for a win condition until after player2 has gone. You have your loop to take player1's input, then it looks like you display the board, then you have the loop to take player2's input, and then you check if a player won.
Move the win-checking code to a function, and call that function after each player's turn. It might look something like this (pseudocode):
bool is_game_over(char player) {
if (player == 'x' && <your code to check for x winning>) {
<your code that returns true or false>
}
if (player == 'o' && <your code to check for o winning>) {
<your code that returns true or false>
}
return false;
}
I would recommend breaking up each 'phase' of the game into its own function. Player input is its own function, win checking as its own, board drawing as its own, etc. It will make your code easier to debug and tweak, and you'll be able to follow the overall game logic a lot easier.
You must merge your two loops waiting for player’s input into one by using a char variable for the expected output. It would looks like :
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void gameBoard() {
cout << " Reference:" << endl;
cout << " | | "
<< " 1 2 3" << endl;
cout << " | | " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 4 5 6" << endl;
cout << " | | " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 7 8 9" << endl;
cout << " | | " << endl;
}
int main(int argc, const char* argv[]) {
string player1Name;
string player2Name;
int j;
int k;
int l;
int userInput;
const int NUM_ELEMENTS = 10;
vector<char> boxVals(NUM_ELEMENTS);
unsigned int i;
char currentChar='x'; //tic-tac-toe begins with x
for (i = 0; i < boxVals.size(); ++i) {
boxVals.at(i) = ' ';
}
cout << "enter Player 1's Name: ";
cin >> player1Name;
cout << "enter Player 2's Name: ";
cin >> player2Name;
gameBoard();
for (k = 0; k < 10; k++) {
for (l = 0; l < 10; l++) {
cin >> userInput;
if (boxVals.at(userInput != ' ')) {
cout << "invalid move" << endl;
continue;
}
else {
boxVals.at(userInput)=currentChar;
break;
}
}
cout << " Reference:" << endl;
cout << " | | "
<< " 1 2 3" << endl;
cout << " " << boxVals.at(1) << " | " << boxVals.at(2) << " | "
<< boxVals.at(3) << " " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 4 5 6" << endl;
cout << " " << boxVals.at(4) << " | " << boxVals.at(5) << " | "
<< boxVals.at(6) << " " << endl;
cout << "______|________|______ " << endl;
cout << " | | "
<< " 7 8 9" << endl;
cout << " " << boxVals.at(7) << " | " << boxVals.at(8) << " | "
<< boxVals.at(9) << " " << endl;
if ((boxVals.at(1) == currentChar && boxVals.at(2) == currentChar &&
boxVals.at(3) == currentChar ) ||
(boxVals.at(4) == currentChar && boxVals.at(5) == currentChar &&
boxVals.at(6) == currentChar ) ||
(boxVals.at(7) == currentChar && boxVals.at(8) == currentChar &&
boxVals.at(9) == currentChar ) ||
(boxVals.at(1) == currentChar && boxVals.at(5) == currentChar &&
boxVals.at(9) == currentChar ) ||
(boxVals.at(3) == currentChar && boxVals.at(5) == currentChar &&
boxVals.at(7) == currentChar ) ||
(boxVals.at(1) == currentChar && boxVals.at(4) == currentChar &&
boxVals.at(7) == currentChar ) ||
(boxVals.at(2) == currentChar && boxVals.at(5) == currentChar &&
boxVals.at(8) == currentChar )||
(boxVals.at(3) == currentChar && boxVals.at(6) == currentChar &&
boxVals.at(9) == currentChar )) {
cout << ((currentChar=='x')?player1Name : player2Name)<< " Wins!!" << endl;
break;
}
else currentChar = (currentChar == 'x')?'O':'x';
}
return 0;
}
Note that I did not change the general approach of your code, so you understand the changes. But this is not the best you could find for this problem.

Tic Tac Toe game - How to not have results print after invalid entry

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;
}

Tic Tac Toe cin input issue

question on tic tac toe. I'm trying to allow the user to input "0,0"(row/column) format exactly, and would be invalid otherwise. Error is that if I input any of the correct grid numbers, it would give an invalid error regardless.
"i.e - '1,2'
invalid error
invalid error
invalid error"
So nothing the user input is inserted in any of the grid if they input the numbers correctly.
So here is my code, any advice/help is appreciated on moving forward. Also note i'm just trying to verify and input the X's and O's, not really checking user if they tie/win
#include <iostream>
#include "Tictactoe.h"
using namespace std;
int main(){
char board[3][3]={{'.','.','.'},{'.','.','.'},{'.','.','.'}};
bool gameover(true);
int iPlayerTurn(1);
do {
// Print board
cout << " - 0 1 2" << endl;
cout << " +---+---+---+" << endl;
cout << " 0" << " | " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 1" << " | " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 2" << " | " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl;
cout << " +---+---+---+" << endl;
char cPlayerMark;
if (iPlayerTurn == 1) {
cPlayerMark = 'X';
} else {
cPlayerMark = 'O';
}
// check if move is valid
std::cout << "Player" << iPlayerTurn << "'s move:" << std::endl;
bool bValidMove;
// Loop until we get a valid move
do {
char cNextMove;
cin >> cNextMove;
bValidMove = true;
// Check for a valid move
if (cNextMove == '0,0' && board[0][0] == '.') {
board[0][0] = cPlayerMark;
} else if (cNextMove == '0,1' && board[0][1] == '.') {
board[0][1] = cPlayerMark;
} else if (cNextMove == '0,2' && board[0][2] == '.') {
board[0][2] = cPlayerMark;
} else if (cNextMove == '1,0' && board[1][0] == '.') {
board[1][0] = cPlayerMark;
} else if (cNextMove == '1,1' && board[1][1] == '.') {
board[1][1] = cPlayerMark;
} else if (cNextMove == '1,2' && board[1][2] == '.') {
board[1][2] = cPlayerMark;
} else if (cNextMove == '2,0' && board[2][0] == '.') {
board[2][0] = cPlayerMark;
} else if (cNextMove == '2,1' && board[2][1] == '.') {
board[2][1] = cPlayerMark;
} else if (cNextMove == '2,2' && board[2][2] == '.') {
board[2][2] = cPlayerMark;
} else {
cout << "Invalid Move. Try again." <<endl;
bValidMove = false;
}
} while (!bValidMove);
}while (!gameover);
}
The problem is that you have defined cNextMove as a char instead of string. So it can only contain one character no matter how many characters the user entered. And the comparison will always be false.
If you compile your code with gcc4.8, you will actually get an warning like:
test.cpp:45:26: warning: multi-character character constant [-Wmultichar]
if (cNextMove == '0,0' && board[0][0] == '.') {