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 mous开发者_StackOverflow中文版e 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.
Your condition boils down to this:
if((board[move] != 'X') || ('O'))
'O'
will always evaluate to true
in this case, so you always pass the condition. What you want is this:
if((board[move] != 'X') && (board[move] != 'O'))
To verify, this code runs fine for me:
#include <iostream>
bool validateMove(const char (&board)[9], int move)
{
if((board[move] != 'X') && (board[move] != 'O'))
{
std::cout << "True" << std::endl;
return true;
}
else
{
std::cout << "False" << std::endl;
return false;
}
}
int main()
{
char board[9] = {' ',' ','O',' ','X',' ',' ',' ',' ',};
validateMove(board, 0);
validateMove(board, 2);
validateMove(board, 4);
}
The output is:
True
False
False
The reason you're displaying extra junk at the end is that your "board" isn't a nul terminated string. If you also want to use it for display purposes, you'll need to declare it board[10], and add a '\0' at the end. From a design point of view, however, I would recommend against this. I would, in fact, define a Board class, with a << operator to display it, so that I could change the representation at will, without having to modify the framework. If the representation within the board class is a char[9], and the board values are the actual characters, then you could simply memcpy the board into a char[10], append the missing '\0', and output that. Be ready to adopt a more complex solution later, however, if you find that char[3][3] is a better representation, or that it is preferable to use, say 0, -1 and 1 as the status of a position on the board. (I'd probably have the board class responsible for determining the legality of a move, too.)
And while I'm at it: your validateMove function should be:
return board[move] == 'X' || board[move] == '0';
or perhaps even simpler:
return board[move] == ' ';
You were ||'ing with '0', which, converted to bool, is always true. And an if which returns true or false is unnecessary complication and clutter, and suggests that the author either doesn't understand the concept of 'bool', or is intentionally trying to obfuscate the code.
精彩评论