If a deck gets empty this way, we finish the game, if not we go into round two. We continue this algorithm until the played cards are different.
If they are, we choose the winner and attribute the pot to the winners deck. Implemented in JavaScript, the algorithm looks like this:. The Goal Let's go back to basics with this simple card game: war! Even though there is no Deck object, we will still need to create a standard card playing card deck and shuffle it. A standard deck has 13 cards for each of the four suits: an Ace, cards numbered , a Jack, a Queen, and a King.
Note the unusual for declaration. We are going to make 13 cards per suit, but each card will have a value from 2 to This is because Aces are high in this game higher than Kings , and we want to compare values easily.
Also, note the Shuffle method that we haven't implemented yet. I previously wrote a post on the Fisher-Yates card shuffling algorithm and that's what we're going to use to shuffle the cards.
Finally, we need to handle the GetShortName method. So we need a method to assign this short name to each card, like so:. With the card, player, and "deck" in place, we can finally create the last and most complex object in our model: the game itself. In this modeling practice, like several of the others, the game itself is represented by an object.
The properties of this object are the things required to play a game. In real life, what we would need to play a game is simple: two players, and a deck of cards. However, we also need to end games if they become infinite, so we need to keep track of the number of turns elapsed. Remember that our model makes the deck a property of the Player object, and so it will not be declared in the Game object.
Now we need to think about the steps involved in playing a turn in War. Here's what I came up with. The end-of-game step is also relatively easy, so that's next. In the real world, end-of-game happens whenever a player is out of cards.
Decision Point : In Part 1, we discussed the possibility of infinite games of War. In our model, we want to avoid said infinite games, and so we'll forcibly end the game after turns have elapsed the reasoning for this particular number will be in Part 3 of this series. Playing a turn in War would be simple, if it weren't for the whole War mechanic. You can choose to play the game manually, conducting each battle one at a time and seeing the results. Alternately, you can choose to have the program conduct all battles until a winner is determined.
The abstract base class, CardGame , contains generic fields, properties, and methods, which could be reused to create many types of card games. CardGame class contains methods for creating a new deck of 52 cards, shuffling a set of cards see code below and dealing cards to each player. The WarCardGame class inherits the CardGame class, and adds additional fields, properties, and methods, specific to the game of War.
WarCardGame class adds methods for starting a game, executing a battle see code below , and determining the type of battles — single, double, etc. LINQ class. During the battle, cards are moved between the three temporary Dictionaries.
LINQ is also used to reverse the order of the cards and to concatenate the contents of Dictionaries. See thundergnat variation offsite png image. I've assumed that if a player runs out of cards during a 'war', then the other player automatically wins the game. The Bicycle card company's rules don't appear to cover this eventuality.
Create account Log in. Toggle navigation. Page Discussion Edit History.
0コメント