0% found this document useful (0 votes)
922 views

Rock Paper Scissors

The document describes how to build a Rock Paper Scissors game using C++. It includes sections on the problem statement, design, implementation, coding, output and conclusion. The program allows a user to play against a computer opponent by selecting rock, paper or scissors and determines a winner based on the typical rules of the game.

Uploaded by

Satvik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
922 views

Rock Paper Scissors

The document describes how to build a Rock Paper Scissors game using C++. It includes sections on the problem statement, design, implementation, coding, output and conclusion. The program allows a user to play against a computer opponent by selecting rock, paper or scissors and determines a winner based on the typical rules of the game.

Uploaded by

Satvik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C++ PROJECT 1

Rock Paper Scissors Game using C++

Satvik Alla

Sri Chandana Rupireddy

Sai Sujan Reddy Bheemireddy

Sacred Heart University

Course: Introduction to Structure Programming CS 500-E

Professor: Sai Charan Samudrala

March 12, 2024


C++ PROJECT 2

CONTENTS

 About

 Problem statement

 Introduction

 Design

 Implementation

 Coding

 Output

 Conclusion
C++ PROJECT 3

1. About

Rock Paper Scissor is a game played using the hands of 2 players. Using hand gestures, the

players then select one from Rock, Paper, or Scissors. The winner of the game is decided

according to the game rules. We tried to implement the game using C++ code where the user will

be one player and the computer will be the other player. The code is written so that the computer

asks the user to select one from Rock, Paper, or Scissors and then picks up a random choice from

Rock, Paper, or Scissors. The code then determines the winner and prints it on the screen. The

code is also written in a way where it can correct the errors.

2. Problem Statement

- Purpose: This program simulates the popular game of Rock Paper Scissors.

- Problem-Solving: It provides entertainment and is a practice exercise for learning C++

programming concepts. The goal of this game is to implement a classic rock-paper-scissors game

between a user and a computer opponent. The aim is to provide the user with an interactive and

entertaining experience of the game. This code requires the user to input a choice of his own,

then the computer generates a random number, and then based on the game rules the winner

needs to be determined.

3. Introduction

- Game Description: Rock Paper Scissors is a hand game usually played between two people,

where each player simultaneously forms one of three shapes with an outstretched hand: rock,

paper, or scissors. The winner is determined by the rules: rock crushes scissors, scissors cut

paper, paper covers rock.

- Motivation: The motivation for building this program includes practicing C++ programming

skills, understanding basic game logic, and implementing user input/output functionalities. Rock
C++ PROJECT 4

Paper Scissors is a hand gesture game played between 2 players and in the problem, we are

introducing the computer as the opponent and the user as the player. The player is promoted to

choose the choice of his own from Rock or Paper or Scissors and then the computer chooses one

from them using a random function and the winner is then determined and printed on the screen.

4. Design

The program has been designed using 2 user-defined functions 1). getComputer_choice and 2).

determineWinner. A library function main() is also used in the program. Apart from 2 user-

defined functions and a library function, this program used a “rand” function to generate a

random number.

a) getComputer_choice:

This function is used to generate a random number using ‘rand()’ method and maps it to one of

the choices among the rock paper or scissors. The ‘srand(time(0))’ call seeds the random

number generator with the current time to ensure different sequences on each run.

b) determineWinner (string player_choice, string computer_choice):

This function takes the choices of the player/user and computer as input and compares them with

each other to determine the winner. The parameters of the comparison are determined in the

function. Based on the parameters the functions return the result as a string: tie, player win or

computer win.

c) Main();
C++ PROJECT 5

The main() function is the entry point of the program. It prompts the user to enter their choice

and ensures valid input by converting it to the lower case and validating against the allowed

choices. The computer’s choice is then obtained, and the winner is determined and displayed.

5. Implementation

 The code is a hand gesture game where two users play using their hands. Here we are

substituting one of the players with the computer and the other player is the user. Here

user needs to input the choice of his own to the computer from Rock, Paper, and Scissors.

 Using the ‘rand()’ function the computer generates a random number in the

‘getComputer_choice()’ function. In the function an arithmetic operation ‘int

random_number = rand() % 3;’ has been used. The random number generated has been

divided by 3 and the remainder is stored in the variable ‘random_number’. The

reminders are in the range of 0-2 (including) and every value is assigned with Rock,

Paper or Scissor in the function and that value is returned as the ‘getComputer_choice’

 In the ‘determineWninner’ function a range of comparisons have been made as shown

in the below lines of code to determine the winner. The computer’s choice is compared to

the player’s value and then compared with each other in this function. Then the code

decides the winner with the greatest value. This function prints the winner on the screen

and prints ‘It’s a Tie’ if the choices of both the computer and the user are the same.

 The ‘main()’ function is used to take inputs and print outputs in the program. The main

function takes input from the user as the user choice, if the desired input is not given by

the user the ‘main()’ function prints an error message to the user encouraging them to

choose between Rock, Paper, or Scissor. This also converts the user choice into lowercase

letters using ‘c = tolower(c);’ method to compare the user choice with the computer
C++ PROJECT 6

choice. Since ‘C++’ is a case-sensitive programming language the use of ‘c =

tolower(c);’ is much more important to convert the user choice into lowercase letters

6. Coding

#include <iostream>

#include <ctime>

using namespace std;

// Function to get the computer's choice

string getComputerChoice() {

srand(time(0)); // Seed the random number generator

int randNum = rand() % 3; // Generate a random number between 0 and 2

// Map the random number to rock, paper, or scissors

switch (randNum) {

case 0:

return "rock";

case 1:

return "paper";

case 2:

return "scissors";

return ""; // To suppress compiler warnings


C++ PROJECT 7

// Function to determine the winner

string determineWinner(string playerChoice, string computerChoice) {

if (playerChoice == computerChoice)

return "It's a tie!";

else if ((playerChoice == "rock" && computerChoice == "scissors") ||

(playerChoice == "paper" && computerChoice == "rock") ||

(playerChoice == "scissors" && computerChoice == "paper"))

return "You win!";

else

return "Computer wins!";

int main() {

string playerChoice;

cout << "Welcome to Rock, Paper, Scissors!" << endl;

cout << "Enter your choice (rock, paper, or scissors): ";

cin >> playerChoice;

// Convert player's choice to lowercase

for (char &c : playerChoice)


C++ PROJECT 8

c = tolower(c);

// Validate player's choice

while (playerChoice != "rock" && playerChoice != "paper" && playerChoice != "scissors") {

cout << "Invalid choice. Please enter rock, paper, or scissors: ";

cin >> playerChoice;

for (char &c : playerChoice)

c = tolower(c);

string computerChoice = getComputerChoice();

cout << "Computer chooses: " << computerChoice << endl;

string result = determineWinner(playerChoice, computerChoice);

cout << result << endl;

return 0;

7. Output

- Expected Output: The program will display the result of each round, including whether the user

wins, loses, or is a tie. It will also provide an option to quit the game.

8. Conclusion
C++ PROJECT 9

- Achievements: The program successfully implements the Rock Paper Scissors game in C++,

allowing users to play against the computer.

- Limitations/Improvements: It is a basic implementation and could be improved by adding

features like keeping track of scores, implementing a graphical user interface, or adding more

complex gameplay options.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy