From c6663b51defac60e42056e076902b7f6aa68eeb3 Mon Sep 17 00:00:00 2001 From: bochard Date: Tue, 24 Jun 2025 19:11:55 +0800 Subject: [PATCH 1/1] first commit --- README.md | 3 +++ main.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 README.md create mode 100644 main.c diff --git a/README.md b/README.md new file mode 100644 index 0000000..e6af66c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +### Rock, Paper, Scissors Game in C + +practice project using C programming language. \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..8da6d0a --- /dev/null +++ b/main.c @@ -0,0 +1,75 @@ +#include +#include +#include + +int getComputerChoice(); +int getUserChoice(); +void checkWinner(int userChoice, int computerChoice); + +int main(){ + srand(time(NULL)); + + printf("*** ROCK PAPER SCISSORS GAME ***\n"); + + int userChoice = getUserChoice(); + int computerChoice = getComputerChoice(); + + switch(userChoice){ + case 1: + printf("You chose ROCK!\n"); + break; + case 2: + printf("You chose PAPER!\n"); + break; + case 3: + printf("You chose SCISSORS!\n"); + break; + } + + switch(computerChoice){ + case 1: + printf("Computer chose ROCK!\n"); + break; + case 2: + printf("Computer chose PAPER!\n"); + break; + case 3: + printf("Computer chose SCISSORS!\n"); + break; + } + + checkWinner(userChoice, computerChoice); + + return 0; +} + +int getComputerChoice(){ + return (rand() % 3) + 1; +} + +int getUserChoice(){ + int choice = 0; + + do{ + printf("Choose an option\n"); + printf("1. ROCK\n"); + printf("2. PAPER\n"); + printf("3. SCISSORS\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + }while(choice < 1 || choice > 3); + + return choice; +} + +void checkWinner(int userChoice, int computerChoice){ + if(userChoice == computerChoice){ + printf("It's a TIE!\n"); + }else if((userChoice == 1 && computerChoice == 3) || + (userChoice == 2 && computerChoice == 1) || + (userChoice == 3 && computerChoice == 2)){ + printf("You WIN!\n"); + }else{ + printf("You LOSE!\n"); + } +} \ No newline at end of file -- 2.39.5