Search code examples
javawhile-loopjava.util.scannercoin-flipping

Why my loop doesnt work properly in my coin toss game?


I've started a bachelor degree in a computer science related field so I ought to code and everything is quite new for me, but I guess we all started from scratch.

I'm having a hard time making my code function the way it should. I have to program a flip-coin game....create random number (even/odd), using user input and then the user should play as long as he wants, therefore I created a while-loop and it doesn't seem to work property. I already tried to put my code inside it and it didn't work neither. My IDE is also telling me that i never user the value assigned to my scanner.nextInt(), which is UserEingabe. I'm quite sure is something quite easy to solve for many of u but I'm struggling a bit. Thanks in advance for the help.

Code: Main class

class CoinObject {

    public static void main(String[] args) {

    Coin coinObject = new Coin();
    coinObject.throwCoin();
    }
}

Second class:

import java.util.Scanner;
public class Coin {

    public void throwCoin(){

        Scanner scanner = new Scanner(System.in);
        System.out.println("Erraten sie, ob Kopf oder Zahl oben liegt:");
        System.out.println("Kopf=0");
        System.out.println("Zahl=1");
        int UserEingabe = scanner.nextInt();
        int randomNumber = (int) Math.random();

        String yes = "yes";
        String no = "no";
        int spiele = 1;
        int victories = 1;
        String play = scanner.next();

// if the input = the random #, cool!, otherwise false :)
            if (UserEingabe == randomNumber){
                System.out.println("Sie haben richtig geraten");
                System.out.println("Moechten Sie weiter spielen (yes/no)");
                play = scanner.next();

            } else {

                System.out.println("Sie haben falsch geraten");
                System.out.println("Moechten Sie weiter spielen (yes/no)");
                play = scanner.next();

            } if (UserEingabe != 0 || UserEingabe != 1){
                System.out.println("falsche Eingabe, versuchen Sie wieder");
                UserEingabe = scanner.nextInt();
            }
// the loop will be repeat it as long as the player wants to play
        while (play != no){
            UserEingabe = scanner.nextInt();
            if (play == yes){
                System.out.println("Sie haben " + spiele + "Spiele gespielt und " + victories + "Spiele gewonnen");

                victories=victories +1;
                spiele = spiele+1;
            }
        }
    }
}

Solution

  • CoinObject (Main method)

    public class CoinObject {
    
                public static void main (String[] args) {
    
    
                Coin coinObject = new Coin();
                coinObject.initializeGame();
                }
            }
    

    Coin (game logic)

    import java.util.Random;
    import java.util.Scanner;
    public class Coin {
    
    
        // In this block we initialize the global varibales, that can be used and modified by all the methods in the class
        Scanner scanner = new Scanner(System.in);
        private int games = 0;
        private int victories = 0;
        private int rightInputGuess = 0;
        private int wrongInputGuess = 0;
        private int[] items = new int[]{0,1};
        private Random rand = new Random();
    
    
          // This method creates the gameheader, meaning it creates a random number and passes it to the game method
          public void initializeGame() {
              System.out.println("Erraten sie, ob Kopf oder Zahl oben liegt:");
              System.out.println("Kopf=0");
              System.out.println("Zahl=1");
              int randomNumber = rand.nextInt(items.length);
    
              if (randomNumber == 1) {
                  rightInputGuess = 1;
                  wrongInputGuess = 0;
              } else if (randomNumber == 0) {
                  rightInputGuess = 0;
                  wrongInputGuess = 1;
              }
    
              playGame(randomNumber);
          }
    
          // This method is the actual game logic
          // It takes the generates randomNumber as parameter.
          // if the user types something else as 0 or 1 he will be asked to try to guess the number again.
          public void playGame(int randomNumber) {
    
              int userInput = scanner.nextInt();
              String play;
    
    
              if (userInput == rightInputGuess){
                  System.out.println("Sie haben richtig geraten");
                  System.out.println("Moechten Sie weiter spielen (yes/no)");
                  play = scanner.next();
    
                  if(play.equals("yes")) {
                      victories=victories +1;
                      games = games+1;
                      initializeGame();
                  }
                  else if (play.equals("no")){
                      victories=victories +1;
                      games = games+1;
                      System.out.println("Sie haben " + games + " Spiele gespielt und " + victories + " Spiele gewonnen");
                  }
    
              } else if (userInput == wrongInputGuess){
    
                  System.out.println("Sie haben falsch geraten");
                  System.out.println("Moechten Sie weiter spielen (yes/no)");
                  play = scanner.next();
    
                  if(play.equals("yes")) {
                      games = games+1;
                      initializeGame();
                  }
                  else if (play.equals("no")){
                      games = games+1;
                      System.out.println("Sie haben " + games + " Spiele gespielt und " + victories + " Spiele gewonnen");
                  }
    
              } else if (userInput != 0 || userInput != 1){
                  System.out.println("falsche Eingabe, versuchen Sie wieder");
                  // The playGame method is called with the current randomNumber.
                  // If the user types something else as 0 or 1 he gets the chance to type a valid guess
                  playGame(randomNumber);
              }
          }
    
    }
    

    This fulfils the requirements of your game. Should the user try some invalid input that is not '0' or '1', he'll get a chance to type another input and guess the current random number.