Reputation: 113
I have got an assignment to develop tic tac toe game but my isValidMove
method creates trouble. It replaces the symbol even after testing isValidMove
method. Actually, it never goes and checks the 'if' condition in isValidMove
method.
public boolean executeMove(String move, Player p)
{
int row;
int col;
row = (int)(move.charAt(0)-'0');
col = (int)(move.charAt(1)-'0');
if(isValidMove(move) == false)
{
board[row-1][col-1]= p.getSymbol();
printBoard();
}
return true;
}
public boolean isValidMove(String move)
{
int row = (int)(move.charAt(0)-'0');
int col = (int)(move.charAt(1)-'0');
if(board[row-1][col-1] == ' ')
{
return true;
}
return false;
}
PS: If someone can tell me.. the program enters name of players as string but prints the address rather than printing name.. what to do?
Upvotes: 2
Views: 1660
Reputation: 14338
Seems that your executeMove()
must return true
only if the move is legally performed. But you return true
even for illegal moves.
public boolean executeMove(String move, Player p) {
int row;
int col;
row = (int)(move.charAt(0)-'0');
col = (int)(move.charAt(1)-'0');
boolean valid = isValidMove(move); // <-- remember is the move valid
if (valid) {
board[row-1][col-1] = p.getSymbol(); // <-- place symbol only if move is valid
}
printBoard();
return valid;
}
Upvotes: 1