16.
Rock-Paper-Scissors
Scenario: A two-player or player-vs-computer game.
Pseudo-code:
1. Input:
o Player chooses rock, paper, or scissors.
o Computer randomly selects an op on (if single-player
mode).
2. Rules:
o Rock beats scissors, scissors beat paper, paper beats rock.
o Declare the winner based on the rules.
3. Repeat: Allow mul ple rounds and keep score.
Key Concepts in C:
Use rand() for computer's choice.
switch or if-else for game rules.
Loop to repeat rounds and maintain scores.
CASE STUDY DONE BY :-
Name :- P Konark Sharma
Registra on Number :- 2024018698
Name :- Y S R Ushendra
Registra on Number :- 2024040314
CODE IN WRITTEN FORMAT :-
#include <stdio.h>
int main() {
int playerScore = 0, computerScore = 0, rounds = 0;
char playerChoice;
int computerChoice;
// Predefined computer choices for demonstra on purposes
int computerChoices[] = {0, 1, 2}; // 0: rock, 1: paper, 2: scissors
int choiceIndex = 0;
// Game loop
do {
prin ("Choose rock (r), paper (p), or scissors (s): ");
scanf(" %c", &playerChoice);
// Simulate computer's choice using a fixed sequence
computerChoice = computerChoices[choiceIndex % 3]; // cycle through 0, 1, 2
choiceIndex++;
// Display computer's choice
prin ("Computer chose: ");
if (computerChoice == 0) {
prin ("rock\n");
} else if (computerChoice == 1) {
prin ("paper\n");
} else {
prin ("scissors\n");
}
// Determine the winner
if ((playerChoice == 'r' && computerChoice == 2) ||
(playerChoice == 'p' && computerChoice == 0) ||
(playerChoice == 's' && computerChoice == 1)) {
prin ("You win this round!\n");
playerScore++;
} else if ((playerChoice == 'r' && computerChoice == 1) ||
(playerChoice == 'p' && computerChoice == 2) ||
(playerChoice == 's' && computerChoice == 0)) {
prin ("Computer wins this round!\n");
computerScore++;
} else if (playerChoice == 'r' || playerChoice == 'p' || playerChoice == 's') {
prin ("It's a e!\n");
} else {
prin ("Invalid choice. Please choose 'r', 'p', or 's'.\n");
con nue; // Skip incremen ng rounds for invalid input
rounds++;
prin ("Score: You: %d | Computer: %d\n", playerScore, computerScore);
// Ask to play again
prin ("Do you want to play another round? (y/n): ");
char playAgain;
scanf(" %c", &playAgain);
if (playAgain != 'y') {
break; // Exit the loop if player does not want to play again
} while (1);
// Final score
prin ("Final Score: You: %d | Computer: %d\n", playerScore, computerScore);
if (playerScore > computerScore) {
prin ("Congratula ons! You are the overall winner!\n");
} else if (computerScore > playerScore) {
prin ("The computer is the overall winner. Be er luck next me!\n");
} else {
prin ("It's a e overall!\n");
return 0;
BREAKDOWN OF THE CODE :-
This is where the code variables has been defined.
playerChoice; is the choice choosen by the player
and computerChoice; is the choice choosen by the computer
This is where the rock, paper & scissors are defined
This is where we built the rounds mechanism with the use of the looping
structure.
Then we made computer choose something from rock, paper & scissor but we
created a sequence for it which obviously player doesn’t know
And lastly displaying the computer’s choice.
Here, is where the declara on of the winner is established.
As the rules given in the case study the winner would be declared through that
method only.
And if a miss-clicks happen the registered program will automa cally give
player the chance to choose from the same op on that are valid.
And lastly there is the declara on of the rounds played and the winner of the
each round.
This is where we ask the player if they want to play more (round system).
And the answer should be in (y/n).
If player wishes to end the game (a er selec ng n), then the final result will be
declared and the overall winner of the game.
If the score of both the player and the computer is equal than it will be
declared as a e.
OUTPUT OF THE FOLLOWING CODE OF ROCK,PAPER & SCISSOR:
As you can see in the
output that :
1. Computer ask user
input,
2. Declara on of the
computer’s choice,
3. Declara on of the
winner of the par cular
round,
4. Shows the score of
both player and
computer,
5. Ask’s the player if he
want to play another
round,
6. Then this loops
con nue un l the player
is sa sfied,
7. A er comple on,
Overall Grading is shown.