This is the second post of many in the Deck and Card/War Game series. In this post, we’re going to think about War’s flow and how it relates to various levels of existence and systems of grouping.
The first clear part of War is there there is simple comparison. Basically, player1 and player2 have cards and you compare them. Then, whoever has the higher card, gets the other player’s card. Looking at that in a different way though, once those cards are turned face-up (since in War, the deck is face down), neither of the cards belong to the deck’s of their respective players. In other words, you’ve created a new deck with a special layout: side by side. This might be a bit confusion, however it makes sense to not call the cards once played belonging to someone because a player will get both cards. We’ll say that one player gets both cards now, since that makes more sense and you’ll see why additionally in a moment in terms of war.
The second clear part of War is War. In war, you submit your first three cards on the deck to be offered up and then a fourth card so you can see who wins all of the cards. The three cards each player puts up are really no one’s cards, because while they are face down, it hardly matter’s whose cards they were originally. In this way, they’re put into what is called a winners deck. Along with the three cards from each player in a war, the two original trick cards as well as the war deciding trick cards are put into the winners deck. You don’t actually have a stack of cards those, the separate deck is a simple way of grouping cards that may switch decks.
The complicated part of war to program is that it may happen again because of the war. Double wars and triple wars aren’t uncommon so they have to be accounted for. So, you have to use a special structure called a do-while loop, at least, in java they provide it. In PHP, you’d have to do this another way however, the idea of this is to do the war once and then continue doing wars if need be, but exit the loop otherwise. This solution is elegant and it uses a native structure supported by Java.
So in this post, we’ve established that you need to group any cards that may switch decks as non-owned cards once outside of the deck. Additionally, wars are annoying because more wars can happen after them.
Hopefully, the next post will have some code with it.