Tic Tac Toe

Write your question here.
Im new to C++, and i am trying to build a game of tic tac toe. i have been using this website as a template.http://www.cplusplus.com/forum/beginner/55728/. i understand all of the code except the part where you assign each player to either 'X' or 'O'. to be more specific it is this code in particular that is confusing me.
1
2
3
4
5
6
7
  //Set Player Marker, Player 1 uses X and Player 2 uses O
			char PlayerMarker;
			if (PlayerTurn = 1){
				PlayerMarker = 'X';
			}else{
				PlayerMarker = 'O';
			}
Last edited on
There's actually a mistake here. What they meant to write was if(PlayerTurn == 1) instead of if(PlayerTurn = 1).

At a glance, this if control structure is located in some loop. With each iteration of the loop, the player turn changes. Depending on whose turn it is, the character PlayerMarker is set to the current player's symbol, and is used later on to be assigned to one of the characters that represent the game board when the current player makes a move.
Last edited on
Thank you for the help. I fixed a few more things, but now it will not switch from x to o.
Obviously it is not finished, but it only plugs in the 'X' character and not the O. Please take a look at this and tell me what i need to fix.

#include <iostream>
#include <string>
using namespace std;





int main() {

char menuChoice;

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





cout << " WOULD YOU LIKE TO PLAY TIC TAC TOE?" << endl;
cout << " Play(y)" << endl;
cout << " Quit(n)" << endl;

cin >> menuChoice;

bool gameOn = false;
bool validinput;


if (menuChoice == 'y') {

do {

cout << " " << square1 << " | " << square2 << " | " << square3 << endl;
cout << " -----+-----+-----" << endl;
cout << " " << square4 << " | " << square5 << " | " << square6 << endl;
cout << " -----+-----+-----" << endl;
cout << " " << square7 << " | " << square8 << " | " << square9 << endl;
cout << " -----+-----+-----" << endl;


int playerturn = 1;

char playermarker;
if (playerturn == 1) {
playermarker = 'X';
}
else {
playermarker = 'O';
}

char CurrentMove;
cout << " Enter the number of the square you want to take" << endl;
cin >> CurrentMove;
validinput = true;

if (CurrentMove == '1' && square1 == '1') {
square1 = playermarker;
}
else if (CurrentMove == '2' && square2 == '2') {
square2 = playermarker;
}
else if (CurrentMove == '3' && square3 == '3') {
square3 = playermarker;
}
else if (CurrentMove == '4' && square4 == '4') {
square4 = playermarker;
}
else if (CurrentMove == '5' && square5 == '5') {
square5 = playermarker;
}
else if (CurrentMove == '6' && square6 == '6') {
square6 = playermarker;
}
else if (CurrentMove == '7' && square7 == '7') {
square7 = playermarker;
}
else if (CurrentMove == '8' && square8 == '8') {
square8 = playermarker;
}
else if (CurrentMove == '9' && square9 == '9') {
square9 = playermarker;
}
else {
cout << " Invalid Input, Try Again" << endl;
validinput = false;
}



} while (validinput);


}while (!gameOn);

system("pause");
return 0;
}
In the future, please use code tags:
http://www.cplusplus.com/articles/jEywvCM9/

It makes it that much more likely that people will want to help you out.

Here's what you have:
1
2
3
4
5
6
7
8
9
int playerturn = 1;

char playermarker;
if (playerturn == 1) {
playermarker = 'X';
}
else {
playermarker = 'O';
}


All of this exists within the scope of your primary loop. The integer playerturn will fall out of scope when the loop finishes an iteration, and will then be reinstantiated. Any changes you make to this variable will not be reflected once you're in the next iteration since you're dealing with a totally different variable, which is always initialized to 1, and therefore playermarker will always be set to 'X'. Basically, you'll have to move the playerturn variable outside of the scope of the loop, so that any changes made to it will carry over to each iteration.
Last edited on
I put the variable before the loop and i am still getting the same results.
Topic archived. No new replies allowed.