Bob turner
Bob turner

Reputation: 21

Adding a loop to my game

I have a game that's running perfectly. I want to put a line of code that asks the player if they want to play again at the end of the game. I would also like to keep a score system for every player and computer win.

I'm having trouble with the input = Integer.parseInt(sc.nextInt()); line

import java.util.Scanner;

public class Sticks {

    public static boolean whoStart(String choice) {
        int ran = (int) (Math.random() * 2 + 1);

        String ht = "";
        switch (ran) {
            case 1:
                ht = "head";
                break;
            case 2:
                ht = "tails";
        }
        if (ht.equals(choice.toLowerCase())) {
            System.out.println("you start first");
            return true;
        } else {
            System.out.println("computer starts first");
            return false;
        }
    }

    public static int playerTurn(int numberOfSticks) {
        System.out.println(" \nthere are " + numberOfSticks + " sticks ");
        System.out.println("\nhow many sticks do you wanna take? 1 or 2?");
        Scanner in = new Scanner(System.in);
        int sticksToTake = in.nextInt();
        while ((sticksToTake != 1) && (sticksToTake != 2)) {
            System.out.println("\nyou can only take 1 or 2 sticks");
            System.out.println("\nhow many sticks do you wanna take?");
            sticksToTake = in.nextInt();
        }
        numberOfSticks -= sticksToTake;
        return numberOfSticks;
    }

    public static int computerTurn(int numberOfSticks) {
        int sticksToTake;
        System.out.println("\nthere are " + numberOfSticks + " sticks ");
        if ((numberOfSticks - 2) % 3 == 0 || (numberOfSticks - 2 == 0)) {
            sticksToTake = 1;
            numberOfSticks -= sticksToTake;
        } else {
            sticksToTake = 2;
            numberOfSticks -= sticksToTake;
        }
        System.out.println("\ncomputer took " + sticksToTake + " stick ");
        return numberOfSticks;
    }

    public static boolean checkWinner(int turn, int numberOfSticks) {

         int score = 0;
         int input;
         int B = 1;
         int Y=5, N=10;

        if ((turn == 1) && (numberOfSticks <= 0)) {
            System.out.println("player lost");
            return true;
        }
        if ((turn == 2) && (numberOfSticks <= 0)) {
            System.out.println("player won");
            score++;
            return true;
        }

        System.out.println("Your score is "+ score);
        System.out.println("Do you want to play again? Press (5) for Yes / (10) for No");

        // ----- This line -----
        input = Integer.parseInt(sc.nextInt());

        if (input == Y) {
          B = 1;
          System.out.println("Rock, Paper, Scissors");
        } else if (input == N) {
            System.exit(0);
            System.out.println("Have A Good Day!");
        }
    }

    public static void main(String args[]) {
        int turn;
        int numberOfSticks = 21;
        Scanner in = new Scanner(System.in);
        System.out.println("choose head or tails to see who starts first");
        String choice = in.next();
        if (whoStart(choice) == true) {
            do {
                turn = 1;
                numberOfSticks = playerTurn(numberOfSticks);
                if (checkWinner(turn, numberOfSticks) == true) {
                    break;
                };

                turn = 2;
                numberOfSticks = computerTurn(numberOfSticks);
                checkWinner(turn, numberOfSticks);
            } while (numberOfSticks > 0);
        } else {
            do {
                turn = 2;
                numberOfSticks = computerTurn(numberOfSticks);
                if (checkWinner(turn, numberOfSticks) == true) {
                    break;
                };
                turn = 1;
                numberOfSticks = playerTurn(numberOfSticks);
                checkWinner(turn, numberOfSticks);
            } while (numberOfSticks > 0);
        }
    }
}

Upvotes: 1

Views: 62

Answers (2)

Unlocked
Unlocked

Reputation: 648

Just put everything in a while(true) loop and use a break; if they choose no. Something like:

static int playerPoints = 0;

public static void main(String args[]) {
    int turn;
    int numberOfSticks = 21;
    Scanner in = new Scanner(System.in);
    while(true){

        ...

        System.out.println("You have " + playerPoints + " points!")
        System.out.println("Do you want to play again?");
        if (!in.nextLine().toLowerCase().equals("yes")){
            break;
        }
    }
}

Edit: ZenLulz's answer is better than this one, mainly because it encourages better programming practice. Mine works but isn't the best way to solve the issue.

Upvotes: 1

J&#228;mes
J&#228;mes

Reputation: 7245

The title of your question almost answered you what you need to add: a loop!

I suggest you to refactor your function main and extract all your game logic from it to be stored within a dedicated function for the sake of the readability. Let's call it startGame().

Your main is going to become shorter and can represent a good location to introduce this loop, such as:

public static void main(String[] a) {
    boolean isPlaying = true;
    Scanner in = new Scanner(System.in);

    while(isPlaying) {
        startGame();

        // Your message to continue or stop the game
        if(in.next().equalsIgnoreCase("No")) {
            isPlaying = false;
        }
    }
}

I recommend you to use a boolean that is checked in your while loop rather than using a break statement, as it brings a better control flow in your application.

Upvotes: 2

Related Questions