Skip to content
Advertisement

Check if a char is in a 2D array in java

I have been working on my homework of creating a simple chess program.

The chessboard is a 2D array, named board. The pieces are all single chars like R,Q,K, and empty space is filled with a ‘ ‘ char, so a space.

public static char[][] board;

The method get should return the char or whatever is there on the coordinate (x,y).

 public char get(int x, int y) {
        if(x < board[0].length && x >= 0 && y < board.length && y >= 0){
            return board[x][y];
        }
        else
            return '';
    }

And the problem is that my program of evaluating who is the winner is not working as expected.

This program checks whether the king of each team is still in the chessboard, so the board array.

It should return 0 if both are still alive, 1, if only the white king is alive, and 2 if only the black king is alive.

I have made a program, or at least tried to make one, that goes through each coordinate and checks if there is a character ‘K’, which represents the white king, and ‘S’, the black king.

I have set the boolean kingIsAlive to false, so that if there is no K or S found in the array, it remains false.

Though, my code at the bottom, with the if and else that returns 0,1 or2, has the error, that kingWhiteIsAlive is always false and kingBlackIsAlive is always false.

So, I think my program of turning the kingIsAlive boolean to true is not working at all….

The errors I got is:

White should have won => expected: <1> but was: <-1>

No one should have won => expected: <0> but was: <1>

And after a couple of hours trying, I gave up and decided to ask here.

public int decideWinner(){
    boolean kingWhiteIsAlive = false;
    boolean kingBlackIsAlive = false;

    for(int y = 0; y < board.length;y++){
        for(int x = 0;x < board[0].length;x++){
            if(get(x,y) == 'K'){
                kingWhiteIsAlive = true;
            }
        }
    }
    for(int j = 0; j < board.length;j++){
        for(int i = 0;i < board[0].length;i++){
            if(get(i,j) == 'S'){
                kingBlackIsAlive = true;
            }
        }
    }
    if(kingWhiteIsAlive && kingBlackIsAlive){
        return 0;
    }
    else if(kingWhiteIsAlive && !kingBlackIsAlive){
        return 1;
    }
    else if(!kingWhiteIsAlive && kingBlackIsAlive){
        return 2;
    }
    else
        return -1;
} 

return -1 is for a test case that there are no kings from both teams.

Advertisement

Answer

I’ve tested your code & it’s running perfectly, perhaps the problem is in your board initialization?

Try to debug your code by showing the actual content of the array at the beginning of decideWinner function.

I’ve used this initialization for the board, if it might help.

public void initBoard() {
  board = new char[8][8];
  // Init pawns
  for (int j = 0; j < board.length; j++) {
    board[j][1] = 'P';
    board[j][6] = 'P';
  }
  // Rooks
  board[0][0] = 'R';
  board[7][0] = 'R';
  board[0][7] = 'R';
  board[7][7] = 'R';

  // Knights
  board[1][0] = 'N';
  board[6][0] = 'N';
  board[1][7] = 'N';
  board[6][7] = 'N';

  // Bishops
  board[2][0] = 'B';
  board[5][0] = 'B';
  board[2][7] = 'B';
  board[5][7] = 'B';

  // Queens
  board[3][0] = 'Q';
  board[4][7] = 'Q';

  // Kings
  board[4][0] = 'K'; // White
  board[3][7] = 'S'; // Black

  // Empty space
  for (int y = 0; y < board.length; y++) {
    for (int x = 2; x < 6; x++) {
      board[y][x] = ' ';
    }
  }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement