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.
Related
I'm trying to make a program to play Tic Tac Toe. I have it start asking if you would like to play, but when I press Y my program doesn't start running and just closes. I'm not to sure what is causing this. Here is my code :
#include "pch.h"
#include <iostream>
using namespace std;
char square[10] = { '0','1','2','3','4','5','6','7','8','9'};
void showWelcomeMenu();
void showMenu();
int winner();
void board();
int startGame();
int quitApp();
int startGame()
{
int player = 1, i, choice;
char mark;
do
{
board();
player = (player % 2) ? 1 : 2;
cout << "Player " << player << ", enter space number: ";
cin >> choice;
mark = (player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')
square[9] = mark;
else
{
cout << " Invalid move ";
player--;
cin.ignore();
cin.get();
}
i = winner();
player++;
} while (i == -1);
board();
if (i == 1)
cout << "==>\aPlayer " << --player << " wins! ";
else
cout << "==>\aDraw";
cin.ignore();
cin.get();
return 0;
}
void showWelcomeMenu()
{
char answer;
bool quit = false;
cout << "Welcome to Tic Tac Toe!\n" << endl << endl;
cout << "Would you like to play? (Y/N)" << endl << endl;
cin >> answer;
if (answer == 'Y')
{
startGame();
}
else if (answer != 'Y')
{
exit(0);
}
}
int winner()
{
if (square[1] == square[2] && square[2] == square[3])
return 1;
else if (square[4] == square[5] && square[5] == square[6])
return 1;
else if (square[7] == square[8] && square[8] == square[9])
return 1;
else if (square[1] == square[4] && square[4] == square[7])
return 1;
else if (square[2] == square[5] && square[5] == square[8])
return 1;
else if (square[3] == square[6] && square[6] == square[9])
return 1;
else if (square[3] == square[5] && square[5] == square[7])
return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
&& square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8'
&& square[9] != '9')
return 0;
else
return -1;
}
int quitApp()
{
exit(0);
}
void board()
{
system("cls");
cout << "\n";
cout << "Tic Tac Toe";
cout << "\n";
cout << "Player 1 (X) : Player 2 (0)" << endl << endl;
cout << "\n";
cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
cout << " | | " << endl << endl;
}
int main()
{
showWelcomeMenu();
quitApp();
}
Any help would be appreciated! Thank you. Sorry if this has been answered! I couldn't find it.
The code is working perfectly for me.
Please recheck it.
Maybe you are providing wrong input. like using y instead of Y (case sensitive)
To ensure that code works despite of case you can modify condition as
if (answer == 'Y' || answer == 'y')
{
startGame();
}
moreover , I'll suggest you to simplify conditions from that do while loop.
eg.
if (choice == 1 && square[1] == '1')
square[1] = mark;
should be replace by
if (choice == 1)
square[1] = mark;
same for other conditions that decide which cell to mark.
why? because choice is an integer checking it against charecter is not necessary(actually can cause bugs due to implicit type casting)
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;
I am taking my first coding class this year in school (High school/C++), and this is my first real attempt at coding anything other than book exercises.
I just wanted to make a simple Tic Tac Toe game for my first project by myself (not school related, just for fun), but I have been struggling to fix some errors for the past few days.
First, I have been wondering if there is a way for me to return a variable to my 'Checkerboard' function so I don't have to call it after every move. Right now I have to call the 'Checkerboard' after every move, but I'm wondering if there is a way to return the placement of the X or O right to the original function so a new board does not pop up after every move.
Next, when I am using the 'CheckMove' functions my two "if" statements will not run one after the other (i,e. if I type a number that has already been used and then a number outside of the scope 0-10, my program does not run correctly). My program runs fine if I only do one of these things, but as soon as I do them back to back it fails to work properly.
Finally, I cannot figure out why my code will not stop running when a winner is declared (see 'CheckWinner function).
On a side note, as this is my first code any other suggestions on how to improve it would be highly appreciated.
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
void Checkerboard(char SpaceNumber[]);
void CheckMoveX(int Player[], int Choice, char SpaceNumber[]);
void CheckMoveO(int Player[], int Choice, char SpaceNumber[]);
int CheckWinner(char SpaceNumber[], int CheckWin);
void main()
{
char SpaceNumber[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int Player[2] = {1, 2}, Choice, CheckWin=0;
cout << " Tic Tac Toe\n\n";
cout << "Player 1: 'X' --- Player 2: 'O'\n\n";
Checkerboard(SpaceNumber);
do {
cout << "\nPlayer " << Player[0] << " pick a number to place your mark...";
cin >> Choice;
CheckMoveX(Player, Choice, SpaceNumber);
CheckWinner(SpaceNumber, CheckWin);
cout << "\nPlayer " << Player[1] << " pick a number to place your mark..."; //The rest of the main function is a duplicate of the first half, but it is for player 2.
cin >> Choice;
CheckMoveO(Player, Choice, SpaceNumber);
CheckWinner(SpaceNumber, CheckWin);
}while (CheckWinner != 0); //Program isn't ending when winner occurs.
_getch();
}
void Checkerboard(char SpaceNumber[])
{
cout << setw(3) << SpaceNumber[0] << setw(3) << "|" << setw(3) << SpaceNumber[1] << setw(3) << "|" << setw(3) << SpaceNumber[2] << endl;
cout << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << endl;
cout << setw(3) << SpaceNumber[3] << setw(3) << "|" << setw(3) << SpaceNumber[4] << setw(3) << "|" << setw(3) << SpaceNumber[5] << endl;
cout << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << setw(2) << "-" << endl;
cout << setw(3) << SpaceNumber[6] << setw(3) << "|" << setw(3) << SpaceNumber[7] << setw(3) << "|" << setw(3) << SpaceNumber[8] << endl;
}
void CheckMoveX(int Player[], int Choice, char SpaceNumber[]) //I cannot seem to have the first/second "if" statements run back to back.
{
do {
if (Choice > 0 && Choice < 10)
{
if (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O') //This loop ensures that the same square cannot be picked twice.
{
do {
cout << "This number has already been chosen. Please pick a different number...";
cin >> Choice;
} while (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O');
}
SpaceNumber[Choice - 1] = 'X';
Checkerboard(SpaceNumber);
}
if (Choice < 1 || Choice > 9)//Forces the user to pick a number on the board.
{
do {
cout << "Invalid choice. Please pick another number...";
cin >> Choice;
} while (Choice < 1 || Choice > 9);
SpaceNumber[Choice - 1] = 'X';
Checkerboard(SpaceNumber);
}
} while (SpaceNumber[Choice - 1] != 'X');
}
void CheckMoveO(int Player[], int Choice, char SpaceNumber[]) //Duplicate of CheckMoveO
{
if (Choice > 0 && Choice < 10)
{
if (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O')
{
do {
cout << "This number has already been chosen. Please pick a different number...";
cin >> Choice;
} while (SpaceNumber[Choice - 1] == 'X' || SpaceNumber[Choice - 1] == 'O');
}
SpaceNumber[Choice - 1] = 'O';
Checkerboard(SpaceNumber);
}
if (Choice < 1 || Choice > 9)
{
do {
cout << "Invalid choice. Please pick another number...";
cin >> Choice;
} while (Choice < 1 || Choice > 9);
SpaceNumber[Choice - 1] = 'O';
Checkerboard(SpaceNumber);
}
}
int CheckWinner(char SpaceNumber[], int CheckWin)
{
if (SpaceNumber[0] == 'X' && SpaceNumber[3] == 'X' && SpaceNumber[6] == 'X') //Not sure how to do this without if/else statements.
{
CheckWin = 0;
cout << "Player 1 Wins!";
return 0;
}
else if (SpaceNumber[1] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[7] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins!";
return CheckWin;
}
else if (SpaceNumber[2] == 'X' && SpaceNumber[5] == 'X' && SpaceNumber[7] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins";
return 0;
}
else if (SpaceNumber[0] == 'X' && SpaceNumber[1] == 'X' && SpaceNumber[2] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins!";
return 0;
}
else if (SpaceNumber[3] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[5] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins!";
return 0;
}
else if (SpaceNumber[6] == 'X' && SpaceNumber[7] == 'X' && SpaceNumber[8] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins!";
return 0;
}
else if (SpaceNumber[0] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[8] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins";
return 0;
}
else if (SpaceNumber[2] == 'X' && SpaceNumber[4] == 'X' && SpaceNumber[6] == 'X')
{
CheckWin = 0;
cout << "Player 1 Wins";
return 0;
}
//End Player 1 Win. Begin Player 2 Win (Duplicate Code).
else if (SpaceNumber[0] == 'O' && SpaceNumber[3] == 'O' && SpaceNumber[6] == 'O') //Not sure how to do this without if/else statements.
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[1] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[7] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[2] == 'O' && SpaceNumber[5] == 'O' && SpaceNumber[7] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins";
return 0;
}
else if (SpaceNumber[0] == 'O' && SpaceNumber[1] == 'O' && SpaceNumber[2] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[3] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[5] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[6] == 'O' && SpaceNumber[7] == 'O' && SpaceNumber[8] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins!";
return 0;
}
else if (SpaceNumber[0] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[8] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins";
return 0;
}
else if (SpaceNumber[2] == 'O' && SpaceNumber[4] == 'O' && SpaceNumber[6] == 'O')
{
CheckWin = 0;
cout << "Player 2 Wins";
return 0;
}
//End player 2 win; begin cout for tie game.
else
return 1;
}
1) You're not saving the value of CheckWinner to CheckWin
Replace CheckWinner(SpaceNumber, CheckWin); with CheckWin = CheckWinner(SpaceNumber, CheckWin);
2) You need to check if (CheckWin != 0) instead of (CheckWinner != 0)
3) You're not checking if the game ended before taking the input for the next move.
Replace your do.. while block in your main function with this
do {
CheckWin = CheckWinner(SpaceNumber, CheckWin);
if (CheckWin!=0)
{
cout << "\nPlayer " << Player[0] << " pick a number to place your mark...";
cin >> Choice;
CheckMoveX(Player, Choice, SpaceNumber);
}
CheckWin = CheckWinner(SpaceNumber, CheckWin);
if (CheckWin != 0 )
{
cout << "\nPlayer " << Player[1] << " pick a number to place your mark...";
cin >> Choice;
CheckMoveO(Player, Choice, SpaceNumber);
}
}while (CheckWin != 0);
That should solve your problem. Let me know if you have some other issue with the code.
You're not calling CheckWinner in your while condition, you are comparing the address of CheckWinner which will never be zero.
You have a lot of duplicate code. Rather than two CheckMove functions, just have one and pass in an 'X' or 'O' to compare with. You can do the same thing with CheckWinner (which has an incorrect subscript in the 3rd comparison).
For your second question, the problem in CheckMove is that in the do/while loop you get the input value but don't validate it.
There are two reasons why your program doesn't stop when you have a winner: The first is that after 'X' moves you check for a winner but don't check the result of that before having 'O' move. The second is that your comparison, CheckWinner != 0 is checking the address of a function for zero (which will never happen), rather than your CheckWin variable. This would work better if CheckWinner returned the winner rather than setting a global variable.
I'm not sure what you're asking with your first question.
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!
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] == '.') {