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. This will be a console-based game, utilizing prompt() to get the player's choice and confirm() to ask if they want to play again.

Objectives

  • icon Implementing game logic if statements and loops
divider

Activity Tasks

  • icon Create a new project named 2-7-rock-paper.js.
  • icon Complete each task individually.

Task 1: Initialize Game Data

2-7-rock-paper.js
let wins = 0;
let losses = 0;
let round = 1;
let playing = true;
let player;
let computer;
/*
Rules:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
*/
console.log("--- 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.
2-7-rock-paper.js
15 collapsed lines
let wins = 0;
let losses = 0;
let round = 1;
let playing = true;
let player;
let computer;
/*
Rules:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
*/
console.log("--- Rock, Paper, Scissors ---")
while (playing) {
console.log("Round " + round);
console.log(`W: ${wins} - L: ${losses}\n`);
player = prompt("(rock, paper, scissors):").toLowerCase();
// Pick computer hand
let random = Math.floor(Math.random() * 3); // 0 to 2
if (random == 0) {
computer = "rock";
}
else if (random == 1) {
computer = "paper";
}
else {
computer = "scissors";
}
console.log(`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.
2-7-rock-paper.js
15 collapsed lines
let wins = 0;
let losses = 0;
let round = 1;
let playing = true;
let player;
let computer;
/*
Rules:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
*/
console.log("--- Rock, Paper, Scissors ---")
while (playing) {
console.log("Round " + round);
console.log(`W: ${wins} - L: ${losses}\n`);
player = prompt("(rock, paper, scissors):").toLowerCase();
// Pick computer hand
let random = Math.floor(Math.random() * 3); // 0 to 2
if (random == 0) {
computer = "rock";
}
else if (random == 1) {
computer = "paper";
}
else {
computer = "scissors";
}
console.log(`Computer chooses ${computer}!`);
// Regarding the win condition:
// It's okay to break a condition over multiple lines if it get too long.
if (player == computer) {
console.log("-TIE-");
}
else if (
(player == "rock" && computer == "scissors") ||
(player == "paper" && computer == "rock") ||
(player == "scissors" && computer == "paper")
) {
console.log("-YOU WIN-");
wins++;
}
else {
console.log("-YOU LOSE-");
losses++;
}
round++;
// confirm() returns true or false depending on the answer
playing = confirm("Play again?");
}
//

Task 4: Show Final Score

2-7-rock-paper.js
60 collapsed lines
let wins = 0;
let losses = 0;
let round = 1;
let playing = true;
let player;
let computer;
/*
Rules:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
*/
console.log("--- Rock, Paper, Scissors ---")
while (playing) {
console.log("Round " + round);
console.log(`W: ${wins} - L: ${losses}\n`);
player = prompt("(rock, paper, scissors):").toLowerCase();
// Pick computer hand
let random = Math.floor(Math.random() * 3); // 0 to 2
if (random == 0) {
computer = "rock";
}
else if (random == 1) {
computer = "paper";
}
else {
computer = "scissors";
}
console.log(`Computer chooses ${computer}!`);
// Regarding the win condition:
// It's okay to break a condition over multiple lines if it get too long.
if (player == computer) {
console.log("-TIE-");
}
else if (
(player == "rock" && computer == "scissors") ||
(player == "paper" && computer == "rock") ||
(player == "scissors" && computer == "paper")
) {
console.log("-YOU WIN-");
wins++;
}
else {
console.log("-YOU LOSE-");
losses++;
}
round++;
// confirm() returns true or false depending on the answer
playing = confirm("Play again?");
}
console.log("\n--- Final Score ---");
console.log(`Wins: ${wins} - Losses: ${losses}`);
console.log("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? [y/N] y
Round 2
W: 1 - L: 0
(rock, paper, scissors): paper
Computer chooses paper!
-TIE-
Play again? [y/N] y
Round 3
W: 1 - L: 0
(rock, paper, scissors): scissors
Computer chooses rock!
-YOU LOSE-
Play again? [y/N] 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