The Elves begin to set up camp on the beach. To decide whose tent gets to be closest to the snack storage, a giant Rock Paper Scissors tournament is already in progress.
God the question’s description is always pure gold…now let’s jump right in.
We are given a string, our “strategy guide” that represents a rock, paper, scissors turn decision. The string is divided into two columns, the first represents the first player’s decision and the second ours. For example:
We are also told the following: For player 1 - A = Rock, B = Paper, C = Scissors For player 2 - X = Rock, Y = Paper, Z = Scissors We are also given a score table for the outcome and the decision we made, for example, a win is 6 points and choosing paper is 2 points, etc…
Like previous questions, we’ll start by parsing our input
We can go (see what I did there?) with several options to represent our game but in my opinion, an array of tuples is the simplest option
The make function allocates a piece of memory in a certain, specified size for our array
We are asked to provide our total score if we play exactly as instructed in the strategy guide.
Let’s think about this for a bit, there are several ways we can solve this, we can use a bunch of
if statements or some fancy pattern matching, since go does not have pattern matching and I don’t want to write a ton of
if statements we will go with a hybrid approach.
We will create 3 different mappings:
- Represents the points we get for our choice e.g rock, paper, or scissors
- Winning state, meaning If we choose X what does the other player need to choose for us to Win
- Tie state, essentially the same as .2
We don’t take into account the losing state since its essentially a no-op (0 points)
Building on top of these maps and our parsing logic, we can now solve the first part with the following code
At each loop iteration we first add the points based on our choice
score += scores[my] then we check if
his move is what we need based on our player choice, to win or get a tie, and if it is we add the necessary points to our total score.
In part two the sneaky elves switch things up a bit.
Instead of our column representing our moves, it represents the turn outcome where X = lose, Y = tie, and Z = win and we need to choose our choice accordingly.
For example, let’s look at the first turn
A Y, the new meaning of this pair is “player one chose Rock, and the game ended in a tie” building on this information we can create new mappings, the new mappings will be between player 1 choice and the choice player 2 need to make to get to a certain state e.g winning, losing, tie, etc…
Since it’s pretty similar to part 1, we will jump right ahead and look at part 2 as a whole
For each desired state we check what move we need to do based on player 2 choice and pass it down to the
That’s it we are all done with paper, rock, scissors and I must admit that I didn’t think it can be so confusing 🤣
You can find the complete code here Thanks for reading!
This post is number 3 of a 13 posts series > Learning Go.