diff options
| author | Hyder <hyder@hyderhadi.xyz> | 2025-08-23 20:45:46 +0300 |
|---|---|---|
| committer | Hyder <hyder@hyderhadi.xyz> | 2025-08-23 20:45:46 +0300 |
| commit | b4c6db0fc67d2d3f85891b0eca767bd0fa063379 (patch) | |
| tree | f5ff7c74462de24173c894e7b43847c87a2e7f52 | |
| parent | fb0c97cb976b4d2f14fc5dea9ffbb35fa3149b81 (diff) | |
Rock,Paper,Scissor game in C, i guess the code is so messy but im trying to do it like OOP style of objects and methods...etc
| -rw-r--r-- | README | 1 | ||||
| -rw-r--r-- | RPS/rps.c | 111 |
2 files changed, 112 insertions, 0 deletions
@@ -13,3 +13,4 @@ CONTENTS - Binary(28-bit) to decimal converter, in ROOT/BTD/28bitToDecimal.c - Palindrome words checker exercise, in ROOT/Palindrome/Palindrome.c - Sorting numbers with (selection sort), in ROOT/SelectionSort/SelectionSort.c +- Made a simple Rock, Paper, Scissor game, in ROOT/RPS/rps.c diff --git a/RPS/rps.c b/RPS/rps.c new file mode 100644 index 0000000..4256674 --- /dev/null +++ b/RPS/rps.c @@ -0,0 +1,111 @@ +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> + +struct Game { + int wins; + int losses; + int draws; + void (*conclusion)(struct Game *self, int decider, char *user_input); + void (*showScore)(struct Game *self); +}; + +void Conclusion(struct Game *self, int decider, char *user_input) { + if (decider >= 1 && decider <= 34) { + printf("Rock is picked by computer\n"); + if (strcmp(user_input, "paper") == 0) { + printf("You win!\n"); + self->wins++; + } + else if (strcmp(user_input, "scissor") == 0) { + printf("You lose!\n"); + self->losses++; + } + else if (strcmp(user_input, "rock") == 0) { + printf("Draw\n"); + self->draws++; + } + else { + printf("Invalid input\n"); + } + } + else if (decider >= 35 && decider <= 66) { + printf("Paper is picked by computer\n"); + if (strcmp(user_input, "paper") == 0) { + printf("Draw\n"); + self->draws++; + } + else if (strcmp(user_input, "scissor") == 0) { + printf("You win!\n"); + self->wins++; + } + else if (strcmp(user_input, "rock") == 0) { + printf("You lose\n"); + self->losses++; + } + else { + printf("Invalid input\n"); + } + } + else if (decider >= 67 && decider <= 100) { + printf("Scissor is picked by computer\n"); + if (strcmp(user_input, "paper") == 0){ + printf("You lose!\n"); + self->losses++; + } + else if (strcmp(user_input, "scissor") == 0) { + printf("Draw\n"); + self->draws++; + } + else if (strcmp(user_input, "rock") == 0) { + printf("You win!\n"); + self->wins++; + } + else { + printf("Invalid input\n"); + } + } +} + +void Show_Score(struct Game *self) { + printf("Wins: %d | Losses: %d | Draws: %d\n", self->wins, self->losses, self->draws); +} + +void Game_init(struct Game *game) { + game->conclusion = Conclusion; + game->wins = 0; + game->showScore = Show_Score; + game->losses = 0; + game->draws = 0; +} + + + +int main() +{ + struct Game mygame; + Game_init(&mygame); + srand(time(NULL)); + char user_input[10]; + printf("Pick Rock, Paper Or Scissor... (write Exit to quit, Score to see score)\n"); + while (fgets(user_input, sizeof(user_input), stdin) != NULL) { + user_input[strcspn(user_input, "\n")] = '\0'; + for (int i = 0;user_input[i] != '\0';i++) { + user_input[i] = tolower(user_input[i]); + } + if (strcmp(user_input, "exit") == 0) { + printf("Exited\n"); + mygame.showScore(&mygame); + break; + } + else if (strcmp(user_input, "score") == 0) { + mygame.showScore(&mygame); + continue; + } + printf("YOU PICKED: %s\n", user_input); + mygame.conclusion(&mygame, rand() % 100, user_input); + } + return 0; +}
\ No newline at end of file |
