Created
September 30, 2023 17:21
-
-
Save jbutcher5/b5ba624d09b42075b57b04547e8b913d to your computer and use it in GitHub Desktop.
A rock paper scissors game with modular arithmetic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define MOD(x, k) ((x) % (k) + ((k))) % (k) | |
int main() { | |
int p1, p2; | |
puts( | |
"Welcome to Rock, Paper, Scissors\n" | |
"0 - Rock\n" | |
"1 - Paper\n" | |
"2 - Scissors"); | |
printf("Player 1, Enter your choice.\n>"); | |
scanf("%d", &p1); | |
printf("Player 2, Enter your choice.\n>"); | |
scanf("%d", &p2); | |
if (!(p1 - p2)) { | |
printf("The game was a tie.\n"); | |
return main(); | |
} | |
printf("Player %c won the game\n", (MOD(p1 - p2, 3) + 48)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment