Rock Paper Scissors is usually a two-player game that is played by making one of the three different hand shapes. This game is named according to those shapes, i.e., rock, paper, and scissors. A closed fist indicates rock. Paper is shown by a flat hand, and scissor makes the index and middle finger in "V" shaped form.
The rules of the game are simple. The players simultaneously display a hand shape. The result of a game is either a draw or a win for any one of the players depending on the hand shapes. The rock beats the scissor but faces defeat against the paper. The paper beats the rock but faces defeat against the scissor. The scissor wins against the paper but loses against the rock. If the players make the same hand sign, it is declared a draw/a tie. The following image indicates the interactions between the shapes.
There are two different approaches to the algorithm of this game. It can choose the move randomly, or it can predict the favorable move by analyzing the patterns of the opponent's previous moves. The target of any two-player game is to gain an advantage over another player, and it becomes difficult when the opponent is truly random. But in real-life, the behavior of humans is not random. An intelligent algorithm that does a frequency analysis of moves can exploit the weakness to gain an advantage over the non-random opponents. In this article, we will discuss the random version of the rock paper scissors game between a user and an algorithm. In section 3, the randomized approach along with the programs is discussed.
1. Algorithm for rock paper scissors game
1. Both players simultaneously make a hand shape.
2. If both make the same move, declare the game a draw.
3. If the first player shows the "rock" and the second player shows the "scissor," declare the first player as the winner.
4. If the first player shows the "scissor" and the second player shows the "paper," declare the first player as the winner.
5. If the first player shows the "paper" and the second player shows the "rock," declare the first player as the winner.
6. For the other cases, declare the second player as the winner.
2. Pseudocode for rock paper scissors game
Input : Move of Player$1$ ($p_1$) and Player$2$ ($p_2$)
Output : Winner of the game
1.Procedure RockPaperScissors($p_1$, $p_2$):
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.End Procedure
3. Program & Output for Random selection approach
Allowing an algorithm to play a random move can be a simple choice. Though the outcome of randomization is naturally unpredictable, in most cases, it does not help to gain any additional advantage over the opponent. In this section, our goal is to develop a program that can randomly reply with rock, paper, or scissors to its opponent, i.e., the user. The program does not evaluate any previous move sequence to make the current choice. So the theoretical probability of winning or drawing a game is equally likely (33.33%) for the user and the algorithm. To verify this, we conducted five rounds containing 100000 games between two artificial players. The following figure depicts the result.
So, we can see from the result that the odds of each outcome are significantly around 33.33%. Below, the C, C++, Java, Python, and C# programs for completely random rock paper scissors games are given.
3.1. C Program & output for rock, paper and scissors game by playing random moves
/*************************************** alphabetacoder.com C program for simple rock paper scissor game by playing completely random move ****************************************/ #include <stdio.h> #include<stdlib.h> #include<time.h> // function display the move name void display_move(int m) { if (m == 1) printf("Rock"); else if (m == 2) printf("Paper"); else printf("Scissor"); } int main() { // feed time as random seed srand(time(0)); // declare variables int computer, user; // keep repeating until user wants to exit while (1) { // take input from user printf("Enter 1 for Rock.\n"); printf("Enter 2 for Paper.\n"); printf("Enter 3 for Scissor.\n"); printf("Enter 0 to exit.\n"); printf("Choice >> "); scanf("%d", & user); // check if player wants to exit if (user == 0) break; // input validation if (!(user >= 1 && user <= 3)) { printf("\nEnter a valid choice!\n\n"); continue; } // computer chooses random move // Genearte a random number from 1 to 3 // Number 1, 2, 3 indicates rock, // paper, scissor resepectively computer = rand() % 3 + 1; // display the move of each player // by calling function printf("\nYou played : "); display_move(user); printf("\nComputer played: "); display_move(computer); //check for the winner // rock vs paper => paper wins // rock vs scissor => rock wins // paper vs scissor => scissor wins if (computer == user) printf("\nIt is a tie!\n"); else if (computer == 1 && user == 3) printf("\nComputer wins!\n"); else if (computer == 2 && user == 1) printf("\nComputer wins!\n"); else if (computer == 3 && user == 2) printf("\nComputer wins!\n"); else printf("\nYou win!\n"); // new line printf("\n"); } return 0; }
Output
Enter 1 for Rock.
Enter 2 for Paper.
Enter 3 for Scissor.
Enter 0 to exit.
Choice >> 5
Enter a valid choice!
Enter 1 for Rock.
Enter 2 for Paper.
Enter 3 for Scissor.
Enter 0 to exit.
Choice >> 2
You played : Paper
Computer played: Scissor
Computer wins!
Enter 1 for Rock.
Enter 2 for Paper.
Enter 3 for Scissor.
Enter 0 to exit.
Choice >> 3
You played : Scissor
Computer played: Scissor
It is a tie!
Enter 1 for Rock.
Enter 2 for Paper.
Enter 3 for Scissor.
Enter 0 to exit.
Choice >> 1
You played : Rock
Computer played: Scissor
You win!
Enter 1 for Rock.
Enter 2 for Paper.
Enter 3 for Scissor.
Enter 0 to exit.
Choice >> 2
You played : Paper
Computer played: Paper
It is a tie!
Enter 1 for Rock.
Enter 2 for Paper.
Enter 3 for Scissor.
Enter 0 to exit.
Choice >> 1
You played : Rock
Computer played: Scissor
You win!
Enter 1 for Rock.
Enter 2 for Paper.
Enter 3 for Scissor.
Enter 0 to exit.
Choice >> 3
You played : Scissor
Computer played: Rock
Computer wins!
Enter 1 for Rock.
Enter 2 for Paper.
Enter 3 for Scissor.
Enter 0 to exit.
Choice >> 0
3.2. C++ Program for rock, paper and scissors game by playing random moves
/*************************************** alphabetacoder.com C++ program for simple rock paper scissor game by playing completely random move ****************************************/ #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() { // feed time as random seed srand(time(0)); // declare variables int computer, user; string move[] = {"Rock", "Paper", "Scissor"}; // keep repeating until user wants to exit while (1) { // take input from user cout << "Enter 1 for Rock." << endl; cout << "Enter 2 for Paper." << endl; cout << "Enter 3 for Scissor." << endl; cout << "Enter 0 to exit." << endl; cout << "Choice >> "; cin >> user; // check if player wants to exit if (user == 0) break; // input validation if (!(user >= 1 && user <= 3)) { cout << "\nEnter a valid choice!\n" << endl; continue; } // computer chooses random move // Genearte a random number from 1 to 3 // Number 1, 2, 3 indicates rock, // paper, scissor resepectively computer = rand() % 3 + 1; // display the move of each player // fetch move name from string array cout << "\nYou played : " << move[user - 1]; cout << "\nComputer played: " << move[computer - 1]; //check for the winner // rock vs paper => paper wins // rock vs scissor => rock wins // paper vs scissor => scissor wins if (computer == user) cout << "\nIt is a tie!" << endl; else if (computer == 1 && user == 3) cout << "\nComputer wins!" << endl; else if (computer == 2 && user == 1) cout << "\nComputer wins!" << endl; else if (computer == 3 && user == 2) cout << "\nComputer wins!" << endl; else cout << "\nYou win!" << endl; // new line cout << "\n"; } return 0; }
3.3. Java Program for rock, paper and scissors game by playing random moves
/**************************************** alphabetacoder.com Java program for simple rock paper scissor game by playing completely random move *****************************************/ import java.util.Scanner; import java.util.Random; public class Main { public static void main(String[] args) { // declare variables int computer, user; // declare an array to store the moves name String move[] = {"Rock", "Paper", "Scissor"}; // create an object of Random class Random random = new Random(); // create an object of Scanner class Scanner sc = new Scanner(System.in); // keep repeating until user wants to exit while (true) { // Display the choices System.out.println("Enter 1 for Rock."); System.out.println("Enter 2 for Paper."); System.out.println("Enter 3 for Scissor."); System.out.println("Enter 0 to exit."); System.out.print("Choice >> "); // take input from user user = sc.nextInt(); // check if player wants to exit if (user == 0) break; // input validation if (!(user >= 1 && user <= 3)) { System.out.println("\nEnter a valid choice!\n"); continue; } // computer chooses random move // Generate a random number from 1 to 3 // Number 1, 2, 3 indicate rock, // paper, scissor resepectively computer = random.nextInt(3) + 1; // display the move of each player // fetch move name from string array System.out.print("\nYou played : " + move[user - 1]); System.out.print("\nComputer played: " + move[computer - 1]); //check for the winner // rock vs paper => paper wins // rock vs scissor => rock wins // paper vs scissor => scissor wins if (computer == user) System.out.println("\nIt is a tie!"); else if (computer == 1 && user == 3) System.out.println("\nComputer wins!"); else if (computer == 2 && user == 1) System.out.println("\nComputer wins!"); else if (computer == 3 && user == 2) System.out.println("\nComputer wins!"); else System.out.println("\nYou win!"); // new line System.out.println(""); } } }
3.4. Python Program for rock, paper and scissors game by playing random moves
# ********************************************* # alphabetacoder.com # Python program for simple rock paper scissor # game by playing completely random moves # ********************************************* import random # declare a list to store move name move = ["Rock", "Paper", "Scissor"] # keep playing until user wants to exit while True: # take input from user print("Enter 1 for Rock.") print("Enter 2 for Paper.") print("Enter 3 for Scissor.") print("Enter 0 to exit.") user = int(input("Choice >> ")) # check if player wants to exit if user == 0: break # input validation if user < 1 or user > 3: print("\nEnter a valid choice!\n") continue # computer chooses random move # Genearte a random number from 1 to 3 # Number 1, 2, 3 indicates rock, # paper, scissor resepectively computer = random.randint(1, 3) # display the move of each player # fetch move name from string array print("\nYou played : ", move[user - 1]) print("Computer played: ", move[computer - 1]) # check for the winner # rock vs paper => paper wins # rock vs scissor => rock wins # paper vs scissor => scissor wins if computer == user: print("It is a tie!") elif computer == 1 and user == 3: print("Computer wins!") elif computer == 2 and user == 1: print("Computer wins!") elif computer == 3 and user == 2: print("Computer wins!") else: print("You win!") # new line print("")
3.5. C# Program for rock, paper and scissors game by playing random moves
/*************************************** alphabetacoder.com C# program for simple rock paper scissor game by playing completely random move ****************************************/ using System; namespace RockPaperScissor { class Program { static void Main(string[] args) { // declare variables int computer, user; string[] move = {"Rock", "Paper", "Scissor"}; // declare instance of class Random Random random = new Random(); // keep repeating until user wants to exit while (true) { // take input from user Console.WriteLine("Enter 1 for Rock."); Console.WriteLine("Enter 2 for Paper."); Console.WriteLine("Enter 3 for Scissor."); Console.WriteLine("Enter 0 to exit."); Console.Write("Choice >> "); user = Convert.ToInt32(Console.ReadLine()); // check if player wants to exit if (user == 0) break; // input validation if (!(user >= 1 && user <= 3)) { Console.WriteLine("\nEnter a valid choice!\n"); continue; } // computer chooses random move // Genearte a random number from 1 to 3 // Number 1, 2, 3 indicates rock, // paper, scissor resepectively computer = random.Next(1, 4); // display the move of each player // fetch move name from string array Console.WriteLine("\nYou played : " + move[user - 1]); Console.WriteLine("Computer played: " + move[computer - 1]); //check for the winner // rock vs paper => paper wins // rock vs scissor => rock wins // paper vs scissor => scissor wins if (computer == user) Console.WriteLine("\nIt is a tie!"); else if (computer == 1 && user == 3) Console.WriteLine("\nComputer wins!"); else if (computer == 2 && user == 1) Console.WriteLine("\nComputer wins!"); else if (computer == 3 && user == 2) Console.WriteLine("\nComputer wins!"); else Console.WriteLine("\nYou win!"); // new line Console.WriteLine(""); } // wait for user to press any key Console.ReadKey(); } } }