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