dimanche 9 février 2020

How to combine the switch statement and if case?

I am new to java and trying to program a function that checks if the moves are valid on a board game.

B1 has A2 and C2 as valid moves.

D1 has C2 and E2, however if C2 is occupied by B1 then D1 only has E2 as a valid move.

F1 has E2 and G2, again if E2 is occupied by D1, F1 only has G2 as a valid move.

Lastly H1 only has G2 as a valid move, and no other valid move if G2 is occupied by F1.

Same rule applies to B1 if C2 is occupied by D1.

I thought of combining the switch statement with if but I do not know how to do it.

This is my code so far:

public static boolean isValidMove(int dim, String[] players, char figure, String origin, String dest) {
        if ((dim < 4) && (dim > 26)) throw new IllegalArgumentException ("NO!");
        if (players == null) throw new IllegalArgumentException ("NO!");
        if ((figure != 'F') && (figure != 'H')) throw new IllegalArgumentException ("NO!");
        switch (origin) {
            case "B1":
                dest = "A2";
                dest = "C2";
                break;
            case "D1":
                dest = "C2";
                dest = "E2";
                break;
            case "F1":
                dest = "E2";
                dest = "G2";
                break;
            case "H1":
                dest = "G2";
                break;
            }

            return true;


        }

Origin Strings are B1, D1, F1 and H1.

Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire