I am trying to make a Tic Tac Toe program and when I have to make something to check if the block is taken or not, I'm going to use an int. When the block is '0', it means it is not taken and can continue putting X or O there. When it is '1', it is taken and nothing can be put there thereafter. But the problem is, when I try to declare each block's variable as '0' to start off, it gives me the following errors.
error: ISO C++ forbids itialization of member 'eightHasBeenTaken'
error: making 'eightHasBeenTaken' static
error: ISO C++ foribds in-class initalization of non-const static member 'eightHasBeenTaken'
You cant initialize membervariables of a class inside the class definition. You will have do that in the constructor or create an other function (wich could be handy for re-starting the game).
Just a suggestion: I would use bool instead of integer, since it can only hold 0 (false) or 1 (true).
Hey, I started with boolean too. It's just that it had the same problem, so i tried integer.
P.S. Thanks, i'll try it.
EDIT : Another problem - when I start it, it keeps saying that my number is invalid and nothing happens, even though I type in a number 1 to 9. My code for that section is -
If choice is not equal to 1 or choice is not equal to 2 or etc. etc. then the number is invalid.
true || false = true
There can only be one of the conditions false, since choice can only be one number at a time. So, because of the above, the if-statement will always be executed.
You could use && instead, but (if choice is an integer) I would use if( !(choice>=1 && choice<=8)).
Also, I wouldnt use a construction where the function calls itself. I suggest to place the whole thing in a while-loop instead.
board[0] & board[4] & board[8] == 'O'
(board[0] is usually not '\0' [it would depend on your implementation]. I'll assume the board is full or that you use ' ' as empty.)
1 & board[4] & board[8] == 'O'
(board[4] is, like board[0], not '\0')
1 & 1 & board[8] == 'O'
(1&x==x. This is a fact of boolean algebra.) 1 & 1 & board[8] == 'O'
board[8] == 'O'
Just compare each thing to what you need. When you're writing, don't think it like "is A and B and C equal to D?". Think it like "is A equal to D and A equal to D and C equal to D?"
By the way, your check routine could be little more procedural.
I should use for-loops. If the boards are like this:
0 1 2
3 4 5
6 7 8
You could use someting like this (pseudo-code):
1 2 3 4 5 6 7 8 9 10 11 12 13
//first check for 012-345-678
for (int i=0;i<=6;i+=3){
bool allX=true;
bool allO=true;
for (int j=0;j<=2;j++){
if (board[i+j]!='X') allX=false;
if (board[i+j]!='O') allO=false;
}
if (allX) //player 1 wins
if (allO) //player 2 wins
}
//the same for 036-147-258, first loop upgrade with one, second with three
//you couls also use loops for 048 and 642, but that wont make it a lot shorter
I think it's easier to use a twodimensional array for the boards.