r/dailyprogrammer • u/fvandepitte 0 0 • Jun 21 '17
[2017-06-21] Challenge #320 [Intermediate] War (card game)
Description
You will be implementing the classic card game [War](https://en.wikipedia.org/wiki/War_(card_game)).
Gameplay
This two player game is played using a standard 52-card deck. The objective of the game is to win all the cards. The deck is divided evenly among the players, giving each a deck of face-down cards. In unison, each player reveals the top card of their deck – this is a battle – and the player with the higher card adds both cards to the bottom of their deck. If the cards are of equal value, it's war!
This process is repeated until one player runs out of cards, at which point the other player is declared the winner.
War
Both players place their next three cards face down, then a card face-up. The owner of the higher face-up card wins the war and adds all cards on the table to the bottom of their deck. If the face-up cards are again equal then the war repeats with another set of face-down/up cards, until one player's face-up card is higher than their opponent's, or both players run out of cards
If, when a war begins
- either player does not have enough cards for the war, both players reduce the number of cards to allow the war to complete (e.g. if P2 has only three cards remaining, both players play two cards down and one card up. If P2 has only one card remaining, no cards are played face-down and each player only plays one card up).
- either player has no cards remaining, the other player wins.
- both players have no cards remaining, the game is a draw (this is exceptionally rare in random games).
Post-battle/war
For consistency (so we all end up with the same result for the same input), cards used in a battle or war should be added to the bottom of the winner's deck in a particular order.
After a battle, the winner's card is added to the bottom the winner's deck first, then the loser's card.
After a war or wars, cards used in the war(s) are added to the deck first, followed by the two tying cards. "Cards used in the war(s)" is defined as follows:
0. Cards from any sub-wars (recursive, using this ordering)
- Winner's face-down cards (in the order they were drawn, first card draw is first added to bottom, etc)
- Winner's face-up card
- Loser's face-down cards (in the order they were drawn, first card draw is first added to bottom, etc)
- Loser's face-up card
Input
Input will consist of two lines of space-separated integers in [1..13]. In a standard game, the two lines will each contain 26 numbers, and will be composed of four of each integer in [1..13]. However, your program should be able to handle decks of any size and composition. The first number on a line represents the top card in the deck, last number is the bottom.
Challenge inputs
5 1 13 10 11 3 2 10 4 12 5 11 10 5 7 6 6 11 9 6 3 13 6 1 8 1
9 12 8 3 11 10 1 4 2 4 7 9 13 8 2 13 7 4 2 8 9 12 3 12 7 5 3 11 6 12 2 13 5 7 10 3 10 4 12 11 1 13 12 2 1 7 10 6 12 5 8 1
9 10 7 9 5 2 6 1 11 11 7 9 3 4 8 3 4 8 8 4 6 9 13 2 13 5 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 Output
Output "1" if P1 wins, "2" if P2 wins, and "0" if P1 and P2 tied.
Challenge outputs
120Finally
Have a good challenge idea, like /u/lpreams did?
Consider submitting it to /r/dailyprogrammer_ideas
7
u/Specter_Terrasbane Jun 21 '17 edited Jun 22 '17
Python 2
+/u/CompileBot Python
1
u/J354 Jun 21 '17 edited Jun 21 '17
In which order should the battle cards be added to the bottom of the deck?
Also, in the case of sub-wars, should the sub-war cards be added in order? I.e: sub-war 1 cards added first
And if the war is a tie, which war deck gets added first?
1
u/ct075 Jun 21 '17
In Standard ML. Similar to /u/popillol, I'm getting that player two wins the two non-tied challenge inputs using the scheme I described here. Changing it to always put the tied cards at the end causes player 1 to win both games. Am I missing something?
5
u/Godspiral 3 3 Jun 21 '17 edited Jun 21 '17
in J, potential rule differences,
player 1 cards always "on top" of player 2s
war puts cards all in one pile (with p1 on top of p2)
result is the end decks with loser's empty. A tie would loop infinitely :( (though sample tie works out)
for challenge 2,
2
u/Working-M4n Jun 21 '17 edited Jun 21 '17
JavaScript Super lazy about how the decks were declared and run, sorry!
2
u/[deleted] Jun 21 '17 edited Jun 21 '17
My solution in Python, made it into an actual game. I know it's super messy and long, but I'm a complete beginner, so have mercy. I must have misunderstood the after-war adding, because I get 1 using the second example.
I'm looking forward to other Python solutions, some people will write it in 2 lines :)
2
u/popillol Jun 21 '17 edited Jun 21 '17
Go / Golang I didn't get the same challenge outputs though. I got Player 2, Player 2, Tie. I used the system that /u/ct075 suggests for putting cards back in the deck after a war.
Playground Link only because it's kindof long
Edit: Here's a sample output of the first War I could find of [player 1 deck] vs [player 2 deck] before/after the war.
2
u/matt_delicioso Jun 22 '17 edited Jun 22 '17
C++ A little late but I implemented it to read the player's hands from a .txt file for a bit more of a challenge. I did not account for all of the conditions of the war part of the game. It also outputs the wrong winner for the test cases. but it does do ties! any feedback would be greatly appreciated!
EDIT: Formatting issue, this is my first time posting here!
1
u/silentclowd Jun 21 '17 edited Jun 21 '17
Python Similar to others my outputs were 2, 2, 0. I think there either was a misinput on your part or a misunderstanding on how cards need to be put back in the deck.
I'll give you a log of the first input right as soon as pastebin stops dying.A paste of my log here.1
u/itah Jun 22 '17
Python3 with correct output =)
Rules on the order of adding cards was a bit confusing, first I got 2, 1, 0 as result. Then I changed the order of adding cards after a normal battle (A += [b, a] instead of A += [a, b]), so now it's opposite than after a war where you add the winners cards first.
1
u/lpreams Jun 22 '17
/u/fvandepitte The challenge outputs are wrong. There was a bug in the code (below) that I used to calculate the challenge outputs. The corrects outputs should be 2 2 0
+/u/CompileBot Java
Input:
1
u/sirnamlik Jun 22 '17 edited Jun 22 '17
JAVA Solved with a bit of unnecessary OO.
main class
Deck
Card (can just be an int but done OO in case i want to expand on this)
Prints out every step the result of the last step shows who won. edit: swapped around the way the cards are added, now the output is: 2 2 0
1
u/gabyjunior 1 2 Jun 22 '17
C
The cards are put back at the bottom of the winner deck in same order as they were played, one for the winner and one for the loser alternatively.
It was easier for me because the function that implements the battle plays cards one by one, sorry for my laziness.
The program takes as arguments the number of cards for each value, the number of values and the maximum number of face down cards played during a war (4 13 3 for standard game).
1
u/thodelu Jun 22 '17
Javascript With my interpretation of how the cards will be added to the deck, the results do not match the given outputs.
https://jsfiddle.net/butv1p6L/1/
1
u/sirsleepy Jun 22 '17 edited Jun 22 '17
Python3 late to the party but here goes:
+/u/CompileBot Python3
1
u/cheers- Jun 22 '17
Javascript (Node)
wargame.js
index.js
Output (p1 -> 1 p2 -> 2 DRAW -> 3):
1
u/Gobbedyret 1 0 Jun 23 '17 edited Jun 24 '17
Python 3.6
In case of recursive wars the tying cards starting the original war are added to the bottom of the pile very last.
Since the implementation is recursive, it will crash if 1000 nested wars are waged - assuming a properly shuffled deck with diverse cards, that's practically impossible anyway.
Each deck is implemented as a deque, with the bottom of the deck being the deque's leftmost element.
+/u/CompileBot Python 3
1
u/TheThinker_SK Jun 23 '17 edited Jun 23 '17
I wrote my code in C++ and my answers were 1 1 and 0. Not sure if I have a bug but I'll try to figure it out.
Edit: Found my bug. It has to do when a war that uses less than 4 cards. I'll fix it promptly :)
Edit 2: Fixed the case were the cards are less then 4 in a war and still get 1 for the second input. Any tips would be appreciated.
1
u/SexyToad Jun 24 '17
Python3, went with a more... OOP approach to this by having player classes which keeps track of the winner's pile for me. I also used the rules for defining the pile from /u/ct075
+/u/CompileBot Python3
1
u/Ihr_Todeswunsch Jun 24 '17
Haskell
If I create my own test, I get the output I expect, but my output is different from the challenge output. Perhaps I misunderstood how you're supposed to order the cards when you're putting them back into their respective decks, and that's why when I write my own test cases, it behaves how I think it'll behave, but I'm getting different output for the challenge inputs.
Output:
1
u/Crawford_Fish Jun 25 '17 edited Jun 26 '17
Haskell I got 2 2 0
1
u/emiliodeg Jun 26 '17
PHP in action
1
u/mistahchris Jun 27 '17
That was pretty fun. Here's my quick and dirty Python solution with some tests: https://pastebin.com/XKpiA3ZX
1
u/DanaKaZ Jun 30 '17
Python 3
Scenario 2 gave me the most problems, and needs the rules for adding cards after war(s) to be precisely implemented.
+/u/CompileBot Python 3
1
u/cp1122 Jul 07 '17 edited Jul 07 '17
Python3
I'm not sure why my output is different but I think I got is semi-solved. Any tasteful critiques is welcomed.
1
u/RedHeadedMenace Jul 11 '17
Java I broke it up into three classes just for organization's sake. This is my first submission, please feel free to criticize!
Output:
1
u/Chariot Jul 18 '17
+/u/compilebot C #include <stdio.h> #include <stdlib.h> #include <string.h>
1
u/shindexro Jul 20 '17 edited Jul 20 '17
Python
1
u/mattsminor Jul 20 '17
As part of a school project, I created a cool version of War. check it out if you want. https://github.com/GorbulasGuy/War_Card_Game
1
u/[deleted] Aug 01 '17
Python 2.7 Object-Oriented approach with optional play-by-play (boring).
1
u/TimNetis Aug 22 '17
Java