-1

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();     

       }
    }
}
  • 2
    Please read: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) --- "*`if (this.player.equals("White")) { setPlayer("Black"); printTurn(); }`*" - Make it an `else if (this.player.equals("White")) { ... }` and you should be good to go. – Turing85 Nov 02 '21 at 15:37
  • Your `isValidPosition` method would be much more easily written as `int value = Integer.parseInt(input); return value >= 1 && value <= 32;`. – Andy Turner Nov 02 '21 at 15:40

2 Answers2

1

The problem you're having in nextTurn is that you're changing the player from Black to White; and then checking the player to see if it is White.

The trivial fix for that is to change the if to else if. But I think the code is just over-complicated:

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();
    }
}

Neither of these methods really need the if. In the first case, just concatenate the player name:

public void printTurn (){
  System.out.println("It's " + this.player + "s turn");
}

In the second case, you just need to pick the other player, set that, then call printTurn():

public void nextTurn (){
  String next = this.player.equals("Black") ? "White" : "Black";
  setPlayer(next);
  printTurn();
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
-1

Try it like this

public void nextTurn (){
    if(this.player.equals("Black")){
        setPlayer("White");
        printTurn();
        return;
    }
    if (this.player.equals("White")) {
        setPlayer("Black");
        printTurn();
    }
}

This way, it returns from the method if the first condition matches.

Ivan Jadric
  • 34
  • 1
  • 4
  • It solved the problem, thanks! I will accept this solution as soon as stackoverflow allows me to. I do have a question though. There shouldnt be any difference between multiple if statements and a else if, right? Only one should be true, how come that the else if matters in this case? – Quincy van Deursen Nov 02 '21 at 15:47
  • In your case, you changed the state with `setPlayer("White")`, which made your other if `true` as well. That is why the second pring occured. – Ivan Jadric Nov 02 '21 at 16:37