Back

Activity 2.7 Rock, Paper, Scissors

divider

Introduction

In this activity, you will use loops and selection statements to create the classic game of Rock, Paper, Scissors.

Objectives

  • icon Implementing game logic if statements and loops
divider

Activity Tasks

  • icon Create a new project named 2-7-Rock-Paper-Scissors.
  • icon Complete each task individually.

Task 1: Initialize Game Data

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int wins = 0;
int losses = 0;
int round = 1;
boolean playing = true;
String player;
String computer;
/*
Rules:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
*/
System.out.println("--- Rock, Paper, Scissors ---");
}
}

Task 2: Begin Core Game Loop

  • icon Display the round number and Wins/Losses.
  • icon User inputs rock, paper, or scissors.
  • icon Computer generates a random hand.
Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
16 collapsed lines
Scanner input = new Scanner(System.in);
int wins = 0;
int losses = 0;
int round = 1;
boolean playing = true;
String player;
String computer;
/*
Rules:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
*/
System.out.println("--- Rock, Paper, Scissors ---");
while (playing) {
System.out.println("Round " + round);
System.out.println("W: " + wins + " - L: " + losses + "\n");
System.out.print("(rock, paper, scissors): ");
player = input.nextLine().toLowerCase();
// Pick computer hand
int random = (int)(Math.random() * 3); // 0 to 2
if (random == 0) {
computer = "rock";
}
else if (random == 1) {
computer = "paper";
}
else {
computer = "scissors";
}
System.out.println("Computer chooses " + computer + "!");
}
}
}
//

Task 3: Implement Win/Loss Conditions

  • icon If player and computer pick the same hand: tie.
  • icon There are three possible win conditions.
  • icon If the player neither ties nor wins, they lose.
Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
17 collapsed lines
Scanner input = new Scanner(System.in);
int wins = 0;
int losses = 0;
int round = 1;
boolean playing = true;
String player;
String computer;
/*
Rules:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
*/
System.out.println("--- Rock, Paper, Scissors ---");
while (playing) {
20 collapsed lines
System.out.println("Round " + round);
System.out.println("W: " + wins + " - L: " + losses + "\n");
System.out.print("(rock, paper, scissors): ");
player = input.nextLine().toLowerCase();
// Pick computer hand
int random = (int)(Math.random() * 3); // 0 to 2
if (random == 0) {
computer = "rock";
}
else if (random == 1) {
computer = "paper";
}
else {
computer = "scissors";
}
System.out.println("Computer chooses " + computer + "!");
// Regarding the win condition:
// It's okay to break a condition over multiple lines if it get too long.
if (player.equals(computer)) {
System.out.println("-TIE-");
}
else if (
(player.equals("rock") && computer.equals("scissors")) ||
(player.equals("paper") && computer.equals("rock")) ||
(player.equals("scissors") && computer.equals("paper"))
) {
System.out.println("-YOU WIN-");
wins++;
}
else {
System.out.println("-YOU LOSE-");
losses++;
}
round++;
System.out.print("Play again? (yes/no) ");
String choice = input.nextLine();
if (choice.equals("no")) {
playing = false;
}
}
}
}
//

Task 4: Show Final Score

Program.java
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
16 collapsed lines
Scanner input = new Scanner(System.in);
int wins = 0;
int losses = 0;
int round = 1;
boolean playing = true;
String player;
String computer;
/*
Rules:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
*/
System.out.println("--- Rock, Paper, Scissors ---");
while (playing) {
47 collapsed lines
System.out.println("Round " + round);
System.out.println("W: " + wins + " - L: " + losses + "\n");
System.out.print("(rock, paper, scissors): ");
player = input.nextLine().toLowerCase();
// Pick computer hand
int random = (int)(Math.random() * 3); // 0 to 2
if (random == 0) {
computer = "rock";
}
else if (random == 1) {
computer = "paper";
}
else {
computer = "scissors";
}
System.out.println("Computer chooses " + computer + "!");
// Regarding the win condition:
// It's okay to break a condition over multiple lines if it get too long.
if (player.equals(computer)) {
System.out.println("-TIE-");
}
else if (
(player.equals("rock") && computer.equals("scissors")) ||
(player.equals("paper") && computer.equals("rock")) ||
(player.equals("scissors") && computer.equals("paper"))
) {
System.out.println("-YOU WIN-");
wins++;
}
else {
System.out.println("-YOU LOSE-");
losses++;
}
round++;
System.out.print("Play again? (yes/no) ");
String choice = input.nextLine();
if (choice.equals("no")) {
playing = false;
}
}
System.out.println("\n--- Final Score ---");
System.out.println("Wins: " + wins + " - Losses: " + losses);
System.out.println("Thanks for playing!");
}
}
//
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
--- Rock, Paper, Scissors ---
Round 1
W: 0 - L: 0
(rock, paper, scissors): rock
Computer chooses scissors!
-YOU WIN-
Play again? (yes/no) y
Round 2
W: 1 - L: 0
(rock, paper, scissors): paper
Computer chooses paper!
-TIE-
Play again? (yes/no) y
Round 3
W: 1 - L: 0
(rock, paper, scissors): scissors
Computer chooses rock!
-YOU LOSE-
Play again? (yes/no) n
--- Final Score ---
Wins: 1 - Losses: 1
Thanks for playing!
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. If you wanted to play best-of-5 instead of a continuous game, how would you change the while loop condition?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete