0% found this document useful (0 votes)
0 views14 pages

c Programming Project

The document outlines a project for creating a Rock-Paper-Scissors game using C programming, detailing the game's rules, logic, and implementation. It includes explanations of key concepts such as player input, random number generation, and the use of the sleep() function for timing. The project also discusses potential improvements and emphasizes its educational value for beginners in programming.

Uploaded by

apilkhanal73
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)
0 views14 pages

c Programming Project

The document outlines a project for creating a Rock-Paper-Scissors game using C programming, detailing the game's rules, logic, and implementation. It includes explanations of key concepts such as player input, random number generation, and the use of the sleep() function for timing. The project also discusses potential improvements and emphasizes its educational value for beginners in programming.

Uploaded by

apilkhanal73
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/ 14

Sdadeddddddddddddddddddddddd

Wddddddddddddddddddddddddd

A A PROJECT TO DEMOSTRATE THE


ROCK, PAPER , SCISSORS GAME USING C PROGRAM

SUBMITTED BY: SUBMITTED TO :


NAME: APIL KHANAL DEPARTMENT OF ELECTRONICS AND
CLASS: BCT A COMPUTER ENGINEERING
ROLL NO : ACE081BCT017

1
THEORY
Creating a Rock-Paper-Scissors Game in C

1. Introduction

Rock-Paper-Scissors is a simple hand game played between two


players (or a player and the computer). Each player
simultaneously forms one of three shapes with their hand:

 Rock (✊)
 Paper (✋)
 Scissors (✌️)

The rules of the game are:

 Rock crushes Scissors.


 Scissors cut Paper.
 Paper covers Rock.

If both players choose the same shape, the game is a tie.

In this project, we will implement a Rock-Paper-


Scissors game in C where the user plays against the computer.
The computer's choice will be randomly generated, and the
user will input their choice.

2. Key Concepts and Logic

Game Logic

The game logic involves:

1. Player Input: The user selects one of the three options


(Rock, Paper, or Scissors).
2. Computer Choice: The computer randomly selects one of
the three options.
3. Comparison: The player's choice is compared with the
computer's choice to determine the winner based on the
rules of the game.
4. Result: The result is displayed (win, lose, or tie).

2
Random Number Generation

To simulate the computer's choice, we use the rand() function


from the C standard library (stdlib.h). The rand () function
generates a pseudo-random number, which can be mapped to
one of the three choices (Rock, Paper, or Scissors).

2.3. Conditional Statements

The comparison between the player's choice and the


computer's choice is done using conditional statements (if-
else or switch-case). These statements determine the outcome
of the game based on the rules.

2.4. Looping for Multiple Rounds

To allow the player to play multiple rounds, we use a loop


(e.g., while or do-while). The game continues until the player
decides to quit.

Theory of Using sleep() Function in C

The sleep() function in C is used to pause program execution


for a specified amount of time. It is commonly used in game
development, animations, simulations, and real-time
applications where a delay is needed.

1. How sleep() Works

 sleep() pauses execution for a specified number of


seconds.
 usleep() (microsecond sleep) pauses execution for
milliseconds or microseconds.
 The function helps create time delays in loops,
animations, or user interactions.

Syntax:

3
c
#include <unistd.h>
unsigned int sleep(unsigned int seconds);

4
Source code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

void printChoice(int choice) {


if (choice == 1) printf("Rock");
else if (choice == 2) printf("Paper");
else printf("Scissors");
}

int main() {
int playerChoice, computerChoice, rounds, i, playAgain;
int playerScore, computerScore, ties, totalWins = 0,
totalLosses = 0;

srand(time(0));

do {
playerScore = computerScore = ties = 0;

printf("\nWelcome to Rock, Paper, Scissors!\n");


printf("How many rounds do you want to play? ");
scanf("%d", &rounds);

for (i = 1; i <= rounds; i++) {

5
printf("\nRound %d\n", i);
printf("1. Rock\n2. Paper\n3. Scissors\n");
printf("Enter your choice (1-3): ");
scanf("%d", &playerChoice);

if (playerChoice < 1 || playerChoice > 3) {


printf("Invalid choice! Please choose between 1 and
3.\n");
i--;
continue;
}

computerChoice = rand() % 3 + 1;

printf("\nRock... ");
Sleep(500);
printf("Paper... ");
Sleep(500);
printf("Scissors... GO!\n");
Sleep(500);

printf("\nYou chose: ");


printChoice(playerChoice);
printf("\nComputer chose: ");
printChoice(computerChoice);
printf("\n");

6
Sleep(500);

if (playerChoice == computerChoice) {
printf("It's a tie!\n");
ties++;
} else if ((playerChoice == 1 && computerChoice == 3)
||
(playerChoice == 2 && computerChoice == 1) ||
(playerChoice == 3 && computerChoice == 2)) {
printf("You win this round!\n");
playerScore++;
} else {
printf("You lose this round!\n");
computerScore++;
}
}

printf("\nGame Over! Here are the results:\n");


printf("You won: %d rounds\n", playerScore);
printf("Computer won: %d rounds\n", computerScore);
printf("Tied rounds: %d\n", ties);

if (playerScore > computerScore) {


printf("Congratulations! You won the game!\n");
totalWins++;
} else if (playerScore < computerScore) {
printf("Better luck next time! Computer won the game.\
n");

7
totalLosses++;
} else {
printf("It's a tie overall!\n");
}

printf("\nTotal Wins: %d | Total Losses: %d\n", totalWins,


totalLosses);

printf("\nDo you want to play again? (1 for Yes, 0 for No):


");
scanf("%d", &playAgain);

} while (playAgain == 1);

printf("\nThanks for playing! See you next time.\n");

return 0;
}
OUTPUT
Welcome to Rock, Paper, Scissors!
How many rounds do you want to play? 5

Round 1
1. Rock
2. Paper
3. Scissors
Enter your choice (1-3): 2

8
Rock... Paper... Scissors... GO!

You chose: Paper


Computer chose: Rock
You win this round!

Round 2
1. Rock
2. Paper
3. Scissors
Enter your choice (1-3): 1

Rock... Paper... Scissors... GO!

You chose: Rock


Computer chose: Scissors
You win this round!

Round 3
1. Rock
2. Paper
3. Scissors
Enter your choice (1-3): 3

Rock... Paper... Scissors... GO!

You chose: Scissors

9
Computer chose: Scissors
It's a tie!

Round 4
1. Rock
2. Paper
3. Scissors
Enter your choice (1-3): 2

Rock... Paper... Scissors... GO!

You chose: Paper


Computer chose: Paper
It's a tie!

Round 5
1. Rock
2. Paper
3. Scissors
Enter your choice (1-3): 3

Rock... Paper... Scissors... GO!

You chose: Scissors


Computer chose: Rock
You lose this round!

10
Game Over! Here are the results:
You won: 2 rounds
Computer won: 1 rounds
Tied rounds: 2
Congratulations! You won the game!

Total Wins: 1 | Total Losses: 0

Do you want to play again? (1 for Yes, 0 for No): 1

Welcome to Rock, Paper, Scissors!


How many rounds do you want to play? 3

Round 1
1. Rock
2. Paper
3. Scissors
Enter your choice (1-3): 2

Rock... Paper... Scissors... GO!

You chose: Paper


Computer chose: Rock
You win this round!

Round 2
1. Rock

11
2. Paper
3. Scissors
Enter your choice (1-3): 1

Rock... Paper... Scissors... GO!

You chose: Rock


Computer chose: Scissors
You win this round!

Round 3
1. Rock
2. Paper
3. Scissors
Enter your choice (1-3): 2

Rock... Paper... Scissors... GO!

You chose: Paper


Computer chose: Scissors
You lose this round!

Game Over! Here are the results:


You won: 2 rounds
Computer won: 1 rounds
Tied rounds: 0
Congratulations! You won the game!

12
Total Wins: 2 | Total Losses: 0

Do you want to play again? (1 for Yes, 0 for No): 0

Thanks for playing! See you next time.

13
Conclusion of the Rock, Paper, Scissors Program
The Rock, Paper, Scissors game implemented in C is a
simple yet engaging program that demonstrates fundamental
programming concepts such as conditional statements,
loops, random number generation, and user input
handling.

This program allows the user to:


✔ Decide the number of rounds to play.
✔ Make a choice between Rock, Paper, and Scissors.
✔ Compete against a computer-generated random choice.
✔ Track the score and overall game results.
✔ Choose to play again or exit.

🔹 Key Takeaways

✅ Cross-platform support – Uses Sleep() for Windows and


sleep() for Linux/macOS.
✅ Looping structure – Ensures multiple rounds and replay
functionality.
✅ Randomization – Uses rand() to simulate unpredictable
computer moves.
✅ User Interaction – Provides a smooth gaming experience
with clear instructions.

🔹 Future Improvements

To further enhance the game, we could:


✔ Add a graphical interface instead of console-based
input/output.
✔ Implement AI-based decision-making to make the
computer smarter.
✔ Introduce multiplayer mode for two users to play together.
✔ Use file handling to store and display the highest scores.

Overall, this program is a great beginner-level C project that


strengthens problem-solving skills and programming logic while
offering an interactive user experience. 🚀🎮

14

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