Skip to content
Advertisement

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

Advertisement

Answer

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!
Advertisement