Greetings,
Since my last question about C++, I actually learned quite an amount. I am now comfortable with classes, just not so comfortable with pointers and references yet.
Please note, I am not asking you nesceserally to solve my problem, I'm asking why I'm not getting the wished result.
here are some code snippets that that should help me explain the problem: Im making a console based tic-tac-toe game(recently started learning C++, this imo is a good way for it)
main.cpp:
//Here I initalise 1 variable and an array int move; char board[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '}; First question: Is there a better way to define empty spaces in a char array? The reason i'm doing it like this is, I want my tictactoe board to contain an empty space when an X or O has not been set yet.
main.cpp:
if(referee.validateMove(board, move)) { board[move] = player2.getToken(); displayBoard(board); } What happens here is easily understood but i'll explain anyway;
A call to the validateMove method from the object referee is made, and passed 2 paramaters, the move that the player has selected (in between 0-8). and the tictactoe board.
Referee.cpp
bool Referee::validateMove(const char (&board)[9], int& move) { if(board[move] != 'X' || 'O') { return true; } else { return false; } } Here is where the main trouble is, and my last question arises. As seen before, I passed an array of chars with a size of 9.
The reference to the move variable works well(should i use a reference or pointer?) and it contains the wished result.
At this point, say player 1 just made its move and placed a token in position 2 I now want to place my token as PLAYER 2 on position 2
When I debug with visual studio, I get the following:
0x0024faa8 " O ÌÌÌÌÌÌÌÌÌÌÌ" This is when i hold my mouse on the board parameter. So the board does know, it is occupied.
Why however does the validateMove method always return true, and is board[move] never equal to O or X?
Thank you for taking the time to read my problem. If you have a question, or I explained something in a stupid matter, you see invalid naming conventions, please notify me. I am a student, and I want to pick up as much as possible.
This problem is resolved. Thanks for those who have answered. Highly appreciate it.