]> git repositories - rock-paper-scissors-in-c.git/commitdiff
first commit main
authorbochard <mail@tenkyuu.dev>
Tue, 24 Jun 2025 11:11:55 +0000 (19:11 +0800)
committerbochard <mail@tenkyuu.dev>
Tue, 24 Jun 2025 11:11:55 +0000 (19:11 +0800)
README.md [new file with mode: 0644]
main.c [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
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 (file)
index 0000000..8da6d0a
--- /dev/null
+++ b/main.c
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+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