user12929063
user12929063

Reputation:

Rockpapergame with rounds and counter variables - java

I am creating a rock paper project which has the following requirement:


Continually plays rounds of rock, paper, scissors until one of the players wins three rounds. At that point, the program outputs the winner and the number of rounds it took them to win. If there is no winner after 10 rounds, the competition is declared a tie


Something seems to be missing which I am not quite able to understand or notice. How would I make my game stop after the rounds and declare a winner?

Note: No arrays, external libraries other than scanner, or any built-in methods of java allowed

This is my attempt:

import java.util.Scanner;
public class Rockpaper{
public static void main(String[]args){
    Scanner keyboard = new Scanner(System.in);
    String player1 = keyboard.next();
    String player2 = keyboard.next();
    String player = "Player1";
    int round = 1;
    boolean go = true;
    boolean win = false;
    while(go){
    if (player1.equals("r") && player2.equals("p")){
        win = true;
    }
    else if (player1.equals("p") && player2.equals("r")){
        win = true;
    }
    else if (player1.equals("p") && player2.equals("s")){
        win = true;
    }
    else if (player1.equals("s") && player2.equals("p")){
        win = true;
    }
    else if (player1.equals("s") && player2.equals("r")){
        win = true;
    }
    else if (player1.equals("r") && player2.equals("s")){
        win = true;
    }
    else if (player1.equals("r") && player2.equals("r")){
        win = false;
    }
    else if (player1.equals("p") && player2.equals("p")){
        win = false;
    }
    else if (player1.equals("s") && player2.equals("s")){
        win = false;
    }
    if (round < 5){
        System.out.println(win+" after "+round+" rounds!");
        go = false;
        }else{
            System.out.println("Tie - No winner after "+round+" rounds!");
        }
    if (player.equals("Player1"){
        Player = "Player2";
    }else{
        Player = "Player1";
        }
      }
   }
}

The problem I see is that there needs to be a separate variable that counts each of the win possibilities, for example, "win1" which would count the player1 win possibility and "win2" that would count the player2 wins. I am not quite sure about the rounds variable that would initially start counting the rounds up to 10 which is the maximum. Sample input/output:

enter image description here

Upvotes: 1

Views: 1010

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79395

Problems with your code:

  1. Not using separate variables for individual players.
  2. Not putting input statements inside the loop as a result of which the input statements run only once.
  3. Not changing the value of the variable, round but using its value in the condition, if (round < 5) which will always evaluate true if the value of round is not increased.

Solution

import java.util.Scanner;

public class Rockpaper {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        int round = 1;
        boolean go = true;
        int player1Score = 0;
        int player2Score = 0;

        while (go && round <= 10) {
            String player1 = keyboard.next();
            String player2 = keyboard.next();
            if (player1.equals("r") && player2.equals("p")) {
                player2Score++;
            } else if (player1.equals("p") && player2.equals("r")) {
                player1Score++;
            } else if (player1.equals("p") && player2.equals("s")) {
                player2Score++;
            } else if (player1.equals("s") && player2.equals("p")) {
                player1Score++;
            } else if (player1.equals("s") && player2.equals("r")) {
                player2Score++;
            } else if (player1.equals("r") && player2.equals("s")) {
                player1Score++;
            }
            if (player1Score >= 3) {
                System.out.println("Player1 wins " + " after " + round + " rounds!");
                go = false;
            }
            if (player2Score >= 3) {
                System.out.println("Player2 wins " + " after " + round + " rounds!");
                go = false;
            }
            round++;
        }

        if (round > 10) {
            System.out.println("Tie - No winner after " + (round - 1) + " rounds!");
        }
    }
}

First sample run:

p
r
r
s
s
s
r
r
p
r
Player1 wins  after 5 rounds!

Second sample run:

p
p
p
r
r
r
s
s
p
p
s
s
s
s
p
p
r
p
s
p
Tie - No winner after 10 rounds!

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Currently you read the input only once, before the loop:

String player1 = keyboard.next();
String player2 = keyboard.next();

After every match, you should ask if players should continue playing. If so, then you must ask for their input again. This is, just move the "playerX" variable declaration and initialization inside the loop:

//comment/remove these
//String player1 = keyboard.next();
//String player2 = keyboard.next();

//inside the loop
while(go){
    String player1 = keyboard.next();
    String player2 = keyboard.next();

    if (player1.equals("r") && player2.equals("p")){
        /* rest of your code */

}

Also, this section:

if (round < 5){
    System.out.println(win+" after "+round+" rounds!");
    go = false;
    }else{
        System.out.println("Tie - No winner after "+round+" rounds!");
    }
if (player.equals("Player1"){
    Player = "Player2";
}else{
    Player = "Player1";
    }
  }

It seems odd for two things:

  1. round is never increased.
  2. The else after round < 5 will be always executed, wrongly stating that there's a tie.
  3. Reassigning Player variable for asking user input is not necessary. Instead, you could use 2 variables to store names of your players that are initialized before the game begins.

One more thing: instance of Scanner is Closeable, so each time you use it to read user input, you make sure that the instance is closed after is not needed anymore, in this case, at the end of the program.

More hints:

  • Reduce several if/else with the same output to a single if evaluation
  • You could make use of methods to ease game result.

With all this in mind, your code may look like this:

import java.util.Scanner;

public class RockPaperScizzorGame {

    public static int getGameResult(String player1Move, String player2Move) {
        int result = 0; //assume the game will be a tie
        //player 2 wins
        if (player1Move.equals("r") && player2Move.equals("p")
            || player1.equals("p") && player2.equals("s")
            || player1.equals("s") && player2.equals("r")
                ) {
            result = 2;
        }
        //player 1 wins
        if (player1.equals("p") && player2.equals("r")
            || player1.equals("s") && player2.equals("p")
            || player1.equals("r") && player2.equals("s")) {
            result = 1;
        }
        //return the result: 0, 1 or 2
        return result;
    }

    public static void main (String[] args) {
        try (Scanner keyboard = new Scanner(System.in)) {
            String player1Name = "Player 1";
            String player2Name = "Player 2";

            int round = 0;
            boolean go = true;

            int winsPlayer1 = 0;
            int winsPlayer2 = 0;

            while (go) {
                System.out.println("Make your move " + player1Name + ": ");
                String player1Move = keyboard.next();
                System.out.println("Make your move " + player2Name + ": ");
                String player2Move = keyboard.next();
                int gameResult = getGameResult(player1Move, player2Move);
                switch(gameResult) {
                    case 1:
                        winsPlayer1++;
                        break;
                    case 2:
                        winsPlayer2++;
                        break;
                }
                round++;
                if (winsPlayer1 == 3) {
                    System.out.println(player1Name + " won after " + round + " rounds!");
                    go = false;
                } else if (winsPlayer2 == 3) {
                    System.out.println(player2Name + " won after " + round + " rounds!");
                    go = false;
                } else {
                    if (round == 5 && winsPlayer1 < 3 && winsPlayer2 < 3) {
                        System.out.println("Tie - No winner after "+round+" rounds!");
                    go = false;
                }
            }
        } catch (IOException e) {
            System.out.println("Issues when trying to accept user input.");
            e.printStacktrace();
        }
    }
}

You can improve the code even more:

  • Use more methods to ease the code in main method.
  • Since your main loop depends more on a counter rather than a boolean flag, you may use a for loop rather than a while.
  • You may ask for user input for the name of the players.
  • You may create a class to encapsulate data of your players: name, currentMove, number of wins.

Upvotes: 2

Related Questions