0

I have a project in a CS class where we make Othello in an MVC format, I've tried making an is legal move in it, but from what I've seen it only looks upward on the board.

I've tried putting it into an array with all the possible moves you can make in it, but that either ended up with an error, or with it saying that every space was a possible move.

This is the code, board is a 2d string array that represents the board of othello, isOffBoard checks if the position is off the board. getSquare checks if the position inputted is a piece of the person playing or not. The direction class is mainly just a 2d array with coordinates to add to make the position go in a specific direction.

public boolean isLegalMove(int[] pos) {
    if (this.board[pos[0]][pos[1]].equals("")) {
        for(int[] direction : Directions.points) {
            int[] newPos = pos;
            vector(direction, newPos);

            if (isOffBoard(newPos) == true || getSquare(newPos) == 1 || getSquare(newPos) == 0) {
                continue;
            }

            while (isOffBoard(newPos) == false && getSquare(newPos) == -1 && getSquare(newPos) != 0) {
                vector(direction, newPos);
                if (getSquare(pos) == getSquare(newPos)) {
                    System.out.println("Legal Move: " + pos[0] + " " + pos[1]);
                    return true;
                }
            }
        }
    }
    return false;
}
  • 1
    `getSquare`? `.equals("")`? `1`/`-1`? Hard to tell what is going on there. This could really use a [mre]. And come to think of it, a question. – teapot418 Apr 21 '23 at 15:35
  • Anyway, you should find a non-empty sequence of enemy stones and one of yours in some direction. I don't see the "one of yours" bit. `getSquare(pos)` should be empty as per the initial `if`. Seriously, please make a [mre] or take a debugger to the code. Also the final `newPos` may very well be off board. – teapot418 Apr 21 '23 at 15:38

1 Answers1

0

You have written: int[] newPos = pos; then both variables point to the same array. You should write newPos = pos.clone().

vector(direction, newPos); here will change the common array (pos and newPos).

An if (getSquare(pos) == getSquare(newPos)) { will likely to be always true.

You should tell us what is the error. And which cases are involved.

You also wrote: getSquare(newPos) == -1 && getSquare(newPos) != 0which is redondant.

If you make the table pos constant, it should point to an empty cell. Then

if (getSquare(pos) == getSquare(newPos)) Is wrong. The condition should be if(isOffBoard(newPos) == false && getSquare(newPos) == 1) not something which could be synonymous of if(getSquare(newPos) == 0). In an other word, the condition for a legal move is 0, -1, -1, -1... 1. And here we are testing the 1 cell.

Frédéric LOYER
  • 1,064
  • 5
  • 10
  • 0 is the same as an empty square, 1 is the same square as the person playing in the moment, and -1 is the opposite piece of someone who is playing. Ill post the test cases in a second – Nathaniel Crivelli Apr 21 '23 at 16:12
  • At the very start of the game, a white can only be placed right below the 4 filled squares at the beginning, and not anywhere else around it. This also is the same for black where it can place with 2 opposite pieces and no same piece around it. – Nathaniel Crivelli Apr 21 '23 at 16:21
  • Try to avoid modifying the `pos` table in your program. This mean, copying the `pos` table instead of sharing the reference. The condition `if (getSquare(pos) == getSquare(newPos))` should be `if(getSquare(newPos)==1)` since at `pos` it MUST be 0 (if you get rid of the first issue). – Frédéric LOYER Apr 21 '23 at 16:34
  • I have forgetten the `isOffBoard(newPos) == false` which is missing just after a `vector` call. – Frédéric LOYER Apr 21 '23 at 16:41
  • I have just give you the hint to have 2 different `pos` and `newPos` table. (with the `clone` method) – Frédéric LOYER Apr 21 '23 at 16:55