Im making a 2 Player connect4 game with RMI.My method checkWin() to check if a player has won is not working. If a player has won it will return true
, else false
. The method is correct, however its not working. The following are my classes being used in this method:
Server
class,
Client
class,
Piece
class (A client has a piece to themself).
A Piece
has a Color
and it has a get and set color methods.
Code:
// ...Rest of code...
public void actionPerformed(ActionEvent e) {
int column = Integer.parseInt(e.getActionCommand());
try {
if(myTurn){
if(server.updateBoard(column)){
if(win(column)){
server.showWinner();
}
server.updateCurrentPlayer();
server.updateTitle();
}
// ....Rest of code...}
@Override
public synchronized boolean updateBoard(int col) throws RemoteException {
if(col >= 0 && col < columns){
for(int row = rows - 1;row >=0;row--){
if(this.board[row][col]==null){
this.board[row][col] = currentPlayer.getPiece();
player1.updateGui(currentPlayer.getPiece(), col, this.board);
player2.updateGui(currentPlayer.getPiece(), col, this.board);
currentPlayer.setBoard(this.board);
return true;
}
}
}
return false;
}
@Override
public synchronized boolean checkWin(int col) throws RemoteException {
//Vertical Check
int row = 0;
while (row < rows && this.board[row][col] == null) {
row++;
}
int streak = 1;
for (int i = row + 1; i < rows; i++) {
if (this.board[i][col] == currentPlayer.getPiece()) {
streak++;
if (streak == 4) {
return true; // Win condition met
}
} else {
streak = 1;
break; // Streak is broken
}
}
//...Rest of code...}
I have done some testing by cross checking a piece thats just being placed on the board to both the players' piece and they both dont match. Why is that when it is a player's piece that is being placed on the board in the updateBoard()
method. Also if I call board[][].getColor()
and board[][]
is null
it will get into a never ending process or a loop. The updateBoard()
method works because both clients GUI updates and my Piece
class does implement Serializable
. Also currentPlayer
is just either player1
or player2
depending on whos turn it is.
I tried to use board[][].getColor() == currentPlayer.getColor()
instead but it throws me into a never ending process or a loop.
Your code is missing some interesting pieces, especially the declarations of currentPlayer.getPiece()
and board
.
But from the fact that you can compare this.board[row][col] == null
I deduct that board
is an array of some form of objects. Therefore it is very very likely that the comparison this.board[i][col] == currentPlayer.getPiece()
is wrong because it compares object identities rather than contents. Even more suspicious if either board
or the result of currentPlayer.getPiece()
are transferred via RMI.
Please implement proper equals
and hashcode
methods (they always have to go together!), probably best that you use an IDE tool or some pattern to create their code. And use this.board[i][col].equals(currentPlayer.getPiece())
instead so you get a correct comparison.