Find the outcome of a game of War
When I was in elementary school, there was a "Rock-Paper-Scissors"-ish game we'd play during assemblies, when waiting for our teacher, at recess etc. We called it "War". After some searching however, it turns out this is a much simpler variant of the "Shotgun Game" (according to WikiHow). I'm going to call it "War" since the rules are slightly different:
2 people sit across from each other. The goal of the game is to "kill" the other player. Each turn, you can play one of 3 moves:
Reload: You have a gun that holds a single shot. It must be reloaded before it can be fired each time. Reloading when you already have ammo is legal, but does nothing. A reload was symbolized by tapping your temples with both hands. Each player starts with 0 ammo.
Guard: The only safe move. If you're shot while guarding, you don't die. Guarding was symbolized by crossing your arms over your chest.
Fire: Fire your gun. To successfully fire, you must have reloaded since the last shot. If your opponent is reloading, you win. If they also fire, and you both have ammo, it's a draw. If they're guarding, you wasted the ammo. While firing without ammo is a legal move, it does nothing and leaves you vulnerable like reloading. Firing was symbolized by pointing at the other player.
It was played similar to RPS, in that each player simultaneously throws down their choice (we tapped our legs twice in between turns to keep in rhythm with each other, but that's not important to the challenge).
The Challenge:
Your task is to find the outcome of a game of War. It can be a function or full program.
Input
The option each player chose each turn will be represented by a character/string:
r: reload
g: guard
f: fire
Input will be a list of pairs, a delimited/undelimited string, or anything else along these lines.
An example input in Python could be [("r", "g"), ("f", "r")]
, meaning on the first turn the first player reloaded, and the second player guarded. On the second turn, the first player fires, while the second player reloads. Player one wins this game. The same input could optionally be represented as "r g f r"
, "rgfr"
, "rg fr"
"rg-fr"
...
You can assume the following:
Input will match your chosen format, and that it will only contain valid characters.
Someone will die within 100 turns.
You cannot however assume that the turns end when someone dies.
Output
A value indicating who won (or, who won first*
). You can chose what to output for each scenario, but must account for the following:
Player 1 wins
Player 2 wins
They kill each other (draw)
Each outcome must have a district value, and must always be the same for each scenario.
As an example: you could output 1
when player 1 wins, 2
when player 2 wins, and 0
in the event of a draw. You must then always output 1
when player 1 wins, 2
when player 2 wins, and 0
in the event of a draw.
It can be returned, or printed to the stdout. Trailing whitespace is fine.
Just so it's clear, the only scenario that leads to a draw is if both players fire, and both have ammo.
*
Since in this challenge, turns may continue after someone dies, it's possible more than 1 player may win eventually. You need to find who won first according to the input.
Test Cases (assuming 1
when P1 wins, 2
when P2 wins, and 0
for a draw):
"rg fr" => 1 (P1 shot P2 while they were reloading)
"rg ff" => 1 (They both shot, but only P1 had ammo)
"rr ff" => 0 (Both had ammo and shot each other)
"rr ff rr fg" => 0 (Both had ammo and shot each other. Everything after the first win is ignored)
"rr fg rf" => 2 (P2 shot P1 while they were reloading)
"rf gg rr fg rr fr" => 1
(P2 tried to shoot but didn't have any ammo, then they both guarded, then they both reloaded, then P2 blocked a shot, then they both reloaded again [but P2 still only has 1 ammo!], then P1 shoots P2 while they're reloading.
"rr gf fr rf gg rg ff" => 1
^ Player 1 wins here. The rest to the right has no effect on the output
This is code golf, so the smallest number of bytes wins!
Note, as the test cases show, you must handle "dumb" moves. It's perfectly valid for a player to try to shoot when they don't have ammo, or reload 2 turns in a row (and only accumulate a single ammo).
{"rff","rgf"}
? \$\endgroup\$