I am making a chess game which has the method nextTurn. If the current player is black next turn should return white and vice versa. When i call this method it returns two turns instead of one. I cant figure out why. Is there someone who knows why?
The output i expect is: its blacks turn its whites turn
import jdk.jfr.internal.test.WhiteBox;
public class CheckersHelper {
private String player;
private String[] zetten;
public CheckersHelper() {
this.player = "Black";
this.printTurn();
}
public void setPlayer(String player) {
this.player = player;
}
public void printTurn (){
if(this.player.equals("Black")){
System.out.println("It's Blacks turn");
}
if (this.player.equals("White")){
System.out.println("It's Whites turn");
}
}
public void nextTurn (){
if(this.player.equals("Black")){
setPlayer("White");
printTurn();
}
if (this.player.equals("White")) {
setPlayer("Black");
printTurn();
}
}
public Boolean isValidPosistion (String input){
for (int i = 1; i <= 32; i++) {
if (Integer.valueOf(input) == i) {
return true;
}
}
return false;
}
}
Below the main method:
public class Opgave2 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
CheckersHelper helper = new CheckersHelper();
helper.nextTurn();
}
}
}