- 16 Dec, 2025 *
In a previous post, I mentioned I’m working on a series of terminal games using the C language to solidify concepts I’m learning in programming.
Originally I was going to do one game a day. I’ve been falling behind. I’m finding I can’t perfectly recreate my code from the day before and have been committed to drilling myself until I can. It took me three days to be able to recreate Guess the Number without having to look up the solution I came up with earlier, but I was finally able to move on to Rock Paper Scissors the other day. Today I was able to stumble through it only looking at my notes rather than the full code.
I found a few different approaches in various tutorials on the In…
- 16 Dec, 2025 *
In a previous post, I mentioned I’m working on a series of terminal games using the C language to solidify concepts I’m learning in programming.
Originally I was going to do one game a day. I’ve been falling behind. I’m finding I can’t perfectly recreate my code from the day before and have been committed to drilling myself until I can. It took me three days to be able to recreate Guess the Number without having to look up the solution I came up with earlier, but I was finally able to move on to Rock Paper Scissors the other day. Today I was able to stumble through it only looking at my notes rather than the full code.
I found a few different approaches in various tutorials on the Internet and decided I wanted to use an enumerated data structure for the player choice and a random number generator for the computer’s choice.
typedef enum {
ROCK,
PAPER,
SCISSORS
} Choice;
Choice generateComputerChoice() {
return (Choice)(rand() % 3);
}
Choice getPlayerChoice() {
int choice;
printf("Choose:\n1. Rock\n2. Paper\n3. Scissors\n");
scanf("%d", &choice);
return (Choice)(choice - 1);
}
Nothing fancy, but it made more sense to my ADHD brain than some of the other methods I stumbled on.
I’m reading Chapter 6 in Stepanov and Rose’s From Mathematics to General Programming right now, and they’re covering abstractions in arithmetic. That’s book-ending the data abstraction methods I’m reading about in Structure and Interpretation of Computer Programs and helping me to better understand how loops work, not only in programming but in arithmetic.
Rock → Paper → Scissors → Rock
↑ ↓
└───────────────────────────┘
That’s the general cycle of the game, which I had to represent using if-else statements:
int determineWinner(Choice player, Choice computer) {
if (player == computer) {
return 0;
}
if ((player + 1) % 3 == computer) {
return -1;
}
return 1;
}
const char* choiceToString(Choice c) {
if (c == ROCK) return "Rock";
if (c == PAPER) return "Paper";
return "Scissors";
}
Enter cyclic groups.
In arithmetic, a finite group is called cyclic if it has an element a such that for any element b, there is an integer n where b = an. Every element in the group can be generated by raising one particular element to different powers. In this game, ‘rock’ is a generator a–apply the ‘next beats’ operation once and we get paper, twice gives scissors, and three times loops back to rock.
That doesn’t map one-to-one with an if-else loop, but I can see the same general pattern in what I’ve written and what Stepanov and Rose have defined as a cyclic group. This is an integers mod 3 group. Instead of checking nine individual if statements, like I was trying to do before I recognized the pattern, I realized there was a cyclical structure. I only need three cases: tie, player wins by one step in the cycle, or the computer wins.

(It ain’t much, but it’s honest work.)
If I wanted to extend the code to include five choices (so it would become RPS Lizard Spock) then the general approach scales. I’d switch to mod 5 and adjust the winning condition to handle each choice beating two others instead of one. Something like (player + 1) % 5 == computer || (player + 3) % 5 == computer.
This is why I’m not focusing on one subject at a time. Everything I’m learning is reinforcing each other. Writing this code helped me to understand groups, and learning about groups helped me to write this code.
Maybe by the time I write my next post I’ll have started to grok monoids and can tie it in to player choice in interactive fiction and hierarchical data structures in SICP.