I'm creating a chess game with java.
As you know when you start out the chess game you have two of each "Captains" (sorry I'm not sure what the term is) I have created the following switch case to create the graphical layout of the figures:
switch (j) {
case 1 || 8 : Rook tower = new Rook(""); return tower.getBrik();
case 2 || 7 :
case 3 || 6 : Bishop bishop = new Bishop(""); return bishop.getBrik();
case 4 : King king = new King(""); return king.getBrik();
case 5 : Queen queen = new Queen(""); return queen.getBrik();
}
Where the getBrik() method is a Node that returns an imageview.
Now as you can see my case 2 and 3 are my failed attempt to do two cases in one.
Is this even possible and if so how?
Because of fall through (execution continues to the next case
statement unless you put a break;
at the end, or of course, as in your case, a return
), you can just put the cases under each other:
...
case 1:
case 8:
Rook tower = new Rook("");
return tower.getBrik();
case 3:
case 6:
Bishop bishop = new Bishop("");
return bishop.getBrik();
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With