15
1
Let's build a simulation for an aspect in the card game, which I personally know by the Dutch name 'Oorlog' (translates to 'War').
How does 'Oorlog' work?
Two decks of cards (each including two Jokers) are equally divided between the amount of players playing. Each player shuffles their own stock, put it upside down in front of them, and all players open up the first card of the stock at the same time.
The winner of that 'battle' is determined by the values of the cards following these rules: Joker/Ace defeats King; King defeats Queen; Queen defeats Jack; Jack defeats 10; 10 defeats 9; .... In addition, both 2 and 3 defeat Ace/Joker. The last rule may lead to a cycle where 2 or 3 beats Ace or Joker, Ace or Joker beats some other card, which in turn beats the 2 or 3. In this case, the 2 or 3 wins the battle.
(Suit is irrelevant in this card game.)
When two or more players have the equal highest cards, they have a 'war'. This means they put one card upside down, and then each opens up a new card from their stock, again looking who has the highest card. This continues until a single player wins the entire battle.
(All cards of that battle go to the discard pile of the player that won the battle. Then everyone opens up a new card. When a player's stock is out of cards, they turn their discard pile upside down and continue with this new stock. This continues until a player is out of all his/her cards and then the player with the highest amount of cards wins.)
Example 'battles' with three players:
- 4, 8, Jack:
Jack wins. - 7, Ace, Queen:
Ace wins. - 10, 10, King:
King wins. - 3, Joker, 2:
3 wins. - Ace, Joker, 2:
2 wins. - 3, Queen, Ace:
3 wins. - Queen, Queen, 9:
Queen & Queen are having a 'war', so it continues with two new cards: 4, 8;
8 wins. - 4, 4, 4:
All are having a 'war', so it continues with three new cards: 8, Ace, 2;
2 wins. - Jack, 5, Jack:
Jack & Jack are having a 'war', so it continues with two new cards: 5, 5;
5 & 5 are also equal, so the 'war' continues again with two new cards: 10, King;
King wins. - Joker, Joker, Ace:
All are having a 'war', so it continue with three new cards: 9, 7, 9;
9 & 9 are also equal, so the 'war' continues with two new cards: Jack, 3;
Jack wins.
So, onto the code challenge:
Input:
STDIN with an array, or a string simulating an array (your call - even if your language does support arrays). This array contains the cards of a battle in chronological order (see test cases for a clearer understanding of this).
Output:
STDOUT the index of the player that won the battle.
You can choose whether you want a zero-indexed (i.e. 0
, 1
, or 2
) or one-indexed output (i.e. 1
, 2
, 3
).
Challenge rules:
- The input will be a single array / string representing an array. So you can't have an array of arrays to simplify it. You also can't have surrogate items for cards not participating in the war.
- We use number notations for the face-cards instead of letter notation. So Ace/Joker =
1
; Jack =11
; Queen =12
; and King =13
. - In this challenge we can assume we are always playing with 3 players.
- The first three indicate the start of the 'battle'. When two or more players have a 'war' the continuing cards in the array indicate their battle (see test cases for a clearer understanding of this).
General rules:
- This is tagged code-golf, so shortest answer in bytes wins.
This doesn't mean non code-golfing languages shouldn't enter. Try to come up with an as short as possible code-golf answer for 'every' programming language. - Please mention which indexing (zero- or one-indexed) you've used for the output.
Test cases:
Test case 1: [4, 8, 11] -> 2 (or 3)
Test case 2: [7, 1, 12] -> 1 (or 2)
Test case 3: [10, 10, 13] -> 2 (or 3)
Test case 4: [3, 1, 2] -> 0 (or 1)
Test case 5: [1, 1, 2] -> 2 (or 3)
Test case 6: [3, 12, 1] -> 0 (or 1)
Test case 7: [12, 12, 9, 4, 8] -> 1 (or 2)
Test case 8: [4, 4, 4, 8, 1, 2] -> 2 (or 3)
Test case 9: [11, 5, 11, 5, 5, 10, 13] -> 2 (or 3)
Test case 10: [1, 1, 1, 9, 7, 9, 11, 3] -> 0 (or 1)
Test case 11: [13, 13, 4, 1, 3] -> 1 (or 2)
Test case 12: [13, 4, 13, 2, 3] -> 2 (or 3)
4
The English name is indeed War (minus the jokers and minus the 2-and-3-beat-ace rule).
– Martin Ender – 2016-06-03T07:11:32.803@MartinEnder also, when there is a tie, both players flip 3 cards face down, and a 4th face up. The 4th decides the winner of the round, and the face down cards are the "spoils of war". Also, doesn't the game keep going until 1 player has all the cards? I dunno if this was a local rule or not, anyone else remember this? That's how I remember playing. – Magic Octopus Urn – 2017-10-06T19:55:05.513
1@MagicOctopusUrn I remember flipping over the discard piles to continue playing until one player had it all. – Kamil Drakari – 2017-10-06T20:03:35.867
1@KamilDrakari yeah! That's the way I played too. I was in Louisiana growing up playing this. – Magic Octopus Urn – 2017-10-06T20:27:14.647
@MagicOctopusUrn My experience is from Minnesota, and since we now have 2 data points I think it's safe to say all of America is the same. – Kamil Drakari – 2017-10-06T20:49:12.120
@MagicOctopusUrn The way I played it was with one card face down for each played who was at war. So when the opening is
10, 10, 4
, the first two players laid down a face-down card AND a face-up card. And then the two face-up cards are compared again. And the winner of the war indeed took all, both face-up and face-down. Also, because it usually takes quite a while, we continued until one player had no cards left, and whomever had the most at that moment won. But you could continue until one player has all cards. – Kevin Cruijssen – 2017-10-07T08:56:05.550