aboutsummaryrefslogtreecommitdiff
path: root/RPS
diff options
context:
space:
mode:
authorHyder <hyder@hyderhadi.xyz>2025-08-23 20:45:46 +0300
committerHyder <hyder@hyderhadi.xyz>2025-08-23 20:45:46 +0300
commitb4c6db0fc67d2d3f85891b0eca767bd0fa063379 (patch)
treef5ff7c74462de24173c894e7b43847c87a2e7f52 /RPS
parentfb0c97cb976b4d2f14fc5dea9ffbb35fa3149b81 (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
Diffstat (limited to 'RPS')
-rw-r--r--RPS/rps.c111
1 files changed, 111 insertions, 0 deletions
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