Skip to content

Instantly share code, notes, and snippets.

@jbutcher5
Created September 30, 2023 17:21
Show Gist options
  • Save jbutcher5/b5ba624d09b42075b57b04547e8b913d to your computer and use it in GitHub Desktop.
Save jbutcher5/b5ba624d09b42075b57b04547e8b913d to your computer and use it in GitHub Desktop.
A rock paper scissors game with modular arithmetic
#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