Tic Tac Toe cin input issue - c++

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] == '.') {

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

Restarting a program in c++

Take a look at this code, it's a game of Tic Tac Toe
After the game has finished, the program asks the user whether they want to play again and if yes, the function runs again. However, the array values remain updated as it has been declared globally. The array has to be declared globally as it's being used in multiple functions. Is there a way to reset the values of the array to their original values when the user selects the option to play again?
#include <iostream>
#include <string>
using namespace std;
void contents(); //function to draw the tic tac toe board
char letters[9] = {'a','b','c','d','e','f','g','h','i'}; //array containing
the default characters of the board
string check(); //function to check whether the game is ongoing or has ended
in a win or draw
string result; // string telling us whether the game is ongoing or has ended
in a win or draw
void contents()
{
cout <<endl<<endl<<endl<<" Tic Tac Toe"<<endl<<endl<<endl;
cout << endl;
cout << " | | " << endl;
cout << " " << letters[0] << " | " << letters[1] << " | " << letters[2]
<< endl;
cout << " | | " << endl;
cout << "----- ----- -----" << endl;
cout << " | | " << endl;
cout << " " << letters[3] << " | " << letters[4] << " | " << letters[5]
<< endl;
cout << " | | " << endl;
cout << "----- ----- -----" << endl;
cout << " | | " << endl;
cout << " " << letters[6] << " | " << letters[7] << " | " << letters[8]
<< endl;
cout << " | | " << endl << endl;
}
int main()
{
char selection1; //selection of the mark chosen by player 1 (X or O)
int player = 1;
char mark;//X or O
char selection2; //selection of the character to replace in the array chosen
by the players
char invalid; //character to enter if the user selects a character that
isn't present in the array
string choice;
while(selection1!='X' || selection1!='Y')
{
cout<<"Make your selection player 1 (X or O): ";
cin>>selection1;
if (selection1=='X' || selection1=='O')
break;
else
cout<<"Make a valid selection, press enter to select again"<<endl;
cin.ignore();
cin.get();
}
do
{
player=(player%2)?1:2;
if(selection1 =='X')
mark = (player == 1) ? 'X' : 'O';
else if (selection1=='O')
mark = (player == 1) ? 'O' : 'X';
contents();
cout << "Player " << player << ", enter a letter: ";
cin >> selection2;
if (selection2 == 'a' && letters[0] == 'a')
letters[0] = mark;
else if (selection2 == 'b' && letters[1] == 'b')
letters[1] = mark;
else if (selection2 == 'c' && letters[2] == 'c')
letters[2] = mark;
else if (selection2 == 'd' && letters[3] == 'd')
letters[3] = mark;
else if (selection2 == 'e' && letters[4] == 'e')
letters[4] = mark;
else if (selection2 == 'f' && letters[5] == 'f')
letters[5] = mark;
else if (selection2 == 'g' && letters[6] == 'g')
letters[6] = mark;
else if (selection2 == 'h' && letters[7] == 'h')
letters[7] = mark;
else if (selection2 == 'i' && letters[8] == 'i')
letters[8] = mark;
else
{
cout<<"Invalid move, press enter to continue: ";
cin.ignore();
cin.get();
player--;
}
result=check();
player++;
}while(result=="Ongoing");
contents();
if(result=="Over")
{
cout<<"Player "<<--player<<" wins ";
cin.ignore();
cin.get();
cout<<"Do you want to play again?";
cin>>choice;
if (choice =="Yes")
main();
}
else
{
cout<<"Draw";
cin.ignore();
cin.get();
cout<<"Do you want to play again?";
cin>>choice;
if (choice =="Yes")
main();
}
return 0;
}
string check()
{
if (letters[0] == letters[1] && letters[1] == letters[2])
return "Over";
else if (letters[3] == letters[4] && letters[4] == letters[5])
return "Over";
else if (letters[6] == letters[7] && letters[7] == letters[8])
return "Over";
else if (letters[0] == letters[3] && letters[3] == letters[6])
return "Over";
else if (letters[1] == letters[4] && letters[4] == letters[7])
return "Over";
else if (letters[2] == letters[5] && letters[5] == letters[8])
return "Over";
else if (letters[0] == letters[4] && letters[4] == letters[8])
return "Over";
else if (letters[2] == letters[4] && letters[4] == letters[6])
return "Over";
else if (letters[0] != 'a' && letters[1] != 'b' && letters[2] != 'c'
&& letters[3] != 'd' && letters[4] != 'e' && letters[5] != 'f'
&& letters[6] != 'g' && letters[7] != 'h' && letters[8] != 'i')
return "Draw";
else
return "Ongoing";
}
The obvious way would be to write a new function which does exactly what you want, i.e. set letters[0] to 'a' etc.
You will probably learn about classes later. A class holds multiple functions, and also multiple data members. Each instance of such a class is called an object, and you'd implement each round of the game as a new object.

Struggling with Tic tac toe C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm relatively new to coding and have been tasked with creating a tic tac toe game in C++, I thought I had all the code done but there are various issues such as user input not being allowed, and only allowing to type one players name instead of two, any help/advice you could give me on how to make this work would be really appreciated.
#include <iostream>
using namespace std;
int menumain;
int oneplayer = 'X';
char square1('1');
char square2('2');
char square3('3');
char square4('4');
char square5('5');
char square6('6');
char square7('7');
char square8('8');
char square9('9');
void toggleplayers() {
if (oneplayer == 'X')
oneplayer = 'O';
else
oneplayer = 'X';
}
char win()
{
if (square1 == 'X' && square2 == 'X' && square3 == 'X') return 'X';
if (square4 == 'X' && square5 == 'X' && square6 == 'X') return 'X';
if (square7 == 'X' && square8 == 'X' && square9 == 'X') return 'X';
if (square1 == 'X' && square5 == 'X' && square9 == 'X') return 'X';
if (square3 == 'X' && square5 == 'X' && square7 == 'X') return 'X';
if (square3 == 'X' && square6 == 'X' && square9 == 'X') return 'X';
if (square1 == 'X' && square4 == 'X' && square7 == 'X') return 'X';
if (square2 == 'X' && square5 == 'X' && square8 == 'X') return 'X';
if (square1 == 'O' && square2 == 'O' && square3 == 'O') return 'O';
if (square4 == 'O' && square5 == 'O' && square6 == 'O') return 'O';
if (square7 == 'X' && square8 == 'X' && square9 == 'X') return 'X';
if (square1 == 'O' && square5 == 'O' && square9 == 'O') return 'O';
if (square3 == 'O' && square5 == 'O' && square7 == 'O') return 'O';
if (square3 == 'O' && square6 == 'O' && square9 == 'O') return 'O';
if (square1 == 'O' && square4 == 'O' && square7 == 'O') return 'O';
if (square2 == 'O' && square5 == 'O' && square8 == 'O') return 'O';
return '/';
}
int main() {
int playerone, playertwo;
system("cls");
cout << "tic tac toe" << endl;
cout << "-----------" << endl;
cout << "Start game (1)" << endl;
cout << "Quit game (2)" << endl;
cout << "Press 1 or 2 to proceed." << endl;
cin >> menumain;
if (menumain == 2)
{
return 0;
} else {
cout << "Player One, please enter your name: " << endl;
cin >> playerone;
system("cls");
cout << "Player Two, please enter your name: " << endl;
cin >> playertwo;
system("cls");
int playermove;
cout << "Choose a number between 1-9 to place!" << endl;
cin >> playermove;
if (playermove == 1)
square1 = oneplayer;
else if (playermove == 2)
square2 = oneplayer;
else if (playermove == 3)
square3 = oneplayer;
else if (playermove == 4)
square4 = oneplayer;
else if (playermove == 5)
square5 = oneplayer;
else if (playermove == 6)
square6 = oneplayer;
else if (playermove == 7)
square7 = oneplayer;
else if (playermove == 8)
square8 = oneplayer;
else if (playermove == 9)
square9 = oneplayer;
cout << "Tic tac toe!" << endl;
cout << "------------" << endl;
cout << " " << square1 << " | " << square2 << " | " << square3 << " "
<< endl;
cout << " " << square4 << " | " << square5 << " | " << square6 << " "
<< endl;
cout << " " << square7 << " | " << square8 << " | " << square9 << " "
<< endl;
cout << " " << endl;
cout << playerone << endl;
cout << playertwo << endl;
while (1) {
if (win() == 'X') {
cout << playerone << " wins!" << endl;
break;
}
else if (win() == 'O') {
cout << playertwo << " wins!" << endl;
break;
}
toggleplayers();
}
system("pause");
return 0;
}
}
I made a quick fix. copy, paste, compile and test. it will do the job in terms of a game. It works. It's tic tac toe. :D. Good luck with further adjustments.
These }; }; }; are just markers for myself since I edited this in a text editor and not an IDE. My suggestion, add a clause for a Draw. Because It will not do anything if there is no winner.
#include <iostream>
using namespace std;
int menumain;
int oneplayer = 'X';
char square1('1');
char square2('2');
char square3('3');
char square4('4');
char square5('5');
char square6('6');
char square7('7');
char square8('8');
char square9('9');
void toggleplayers()
{
if (oneplayer == 'X')
oneplayer = 'O';
else
oneplayer = 'X';
};
char win()
{
if (square1 == 'X' && square2 == 'X' &&square3 == 'X')
return 'X';
if (square4 == 'X' && square5 == 'X' &&square6 == 'X')
return 'X';
if (square7 == 'X' && square8 == 'X' &&square9 == 'X')
return 'X';
if (square1 == 'X' && square5 == 'X' && square9 == 'X')
return 'X';
if (square3 == 'X' && square5 == 'X' &&square7 == 'X')
return 'X';
if (square3 == 'X' && square6 == 'X' &&square9 == 'X')
return 'X';
if (square1 == 'X' && square4 == 'X' &&square7 == 'X')
return 'X';
if (square2 == 'X' && square5 == 'X' &&square8 == 'X')
return 'X';
if (square1 == 'O' && square2 == 'O' &&square3 == 'O')
return 'O';
if (square4 == 'O' && square5 == 'O' &&square6 == 'O')
return 'O';
if (square7 == 'X' && square8 == 'X' &&square9 == 'X')
return 'X';
if (square1 == 'O' && square5 == 'O' && square9 == 'O')
return 'O';
if (square3 == 'O' && square5 == 'O' &&square7 == 'O')
return 'O';
if (square3 == 'O' && square6 == 'O' &&square9 == 'O')
return 'O';
if (square1 == 'O' && square4 == 'O' &&square7 == 'O')
return 'O';
if (square2 == 'O' && square5 == 'O' &&square8 == 'O')
return 'O';
return '/';
};
int main()
{
int playerone, playertwo;
cout << "tic tac toe" << endl;
cout << "-----------" << endl;
cout << "Start game (1)" << endl;
cout << "Quit game (2)" << endl;
cout << "Press 1 or 2 to proceed." << endl;
cin >> menumain;
if (menumain == 2)
{
return 0;
}
else
{
cout << "Player One, please enter your name: " << endl;
// cin >> playerone; create char array or string and ask for an input;
cout << "Player Two, please enter your name: " << endl;
// cin >> playertwo; create char array or string and ask for an input;
cout << "Tic tac toe!" << endl;
cout << "------------" << endl;
cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;
cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;
cout << " " << square7 << " | " << square8 << " | " << square9 << " " << endl;
cout << " " << endl;
cout << playerone << endl; //here replace playerone with char array variable or string;
cout << playertwo << endl; //here replace playerone with char array variable or string;
while(1)
{
int playermove;
cout << "Choose a number between 1-9 to place!" << endl;
cin >> playermove;
if (playermove == 1)
square1 = oneplayer;
else if (playermove == 2)
square2 = oneplayer;
else if (playermove == 3)
square3 = oneplayer;
else if (playermove == 4)
square4 = oneplayer;
else if (playermove == 5)
square5 = oneplayer;
else if (playermove == 6)
square6 = oneplayer;
else if (playermove == 7)
square7 = oneplayer;
else if (playermove == 8)
square8 = oneplayer;
else if (playermove == 9)
square9 = oneplayer;
cout << "Tic tac toe!" << endl;
cout << "------------" << endl;
cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;
cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;
cout << " " << square7 << " | " << square8 << " | " << square9 << " " << endl;
cout << " " << endl;
cout << playerone << endl;
cout << playertwo << endl;
if (win() == 'X')
{
cout << playerone << " wins!" << endl;
break;
}
else if (win() == 'O')
{
cout << playertwo << " wins!" << endl;
break;
};
toggleplayers();
};
return 0;
};
};
There are many issues with your code.
user input not being allowed
Actually you do get user input, at the beginning but not afterwards.
After toggling the player, you need to prompt the player for their move:
toggleplayers();
cout << "Player " << oneplayer << ", enter your move (1 - 9): ";
cout.flush();
cin >> playermove;

Unexpected unqualified-id before 'while'

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!

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