# Thread: Catching a Tie in War Card Game

1. ## Catching a Tie in War Card Game

Basically, the program deals 52 cards between 2 players. Each time you run the game it usually takes between 50 - 350 hands for the game to complete.

Sometimes, you'll get a game where the 2 players will get two alternating cards that beat the other player's card, so you'll get an infinite loop where player 1 wins the hand then player 2 wins the hand.

How can I catch when this happens so I can end it with a tie game?

Thanks

2. can you post your code?

3. Sometimes, you'll get a game where the 2 players will get two alternating cards that beat the other player's card, so you'll get an infinite loop where player 1 wins the hand then player 2 wins the hand.
Can you explain this a little more? I would've thought that once a card is played it can't be played again. Also, if card A beats card B, card B can't be played to beat card A. If either of those isn't true - than the game itself has an infinite loop. If those are both true, than how are you getting into this infinite loop?

4. Code:
```if player1 cardvalue equals player2 cardvalue
war
else
if player1 cardvalue > player2 cardvalue
player1 wins
else
player2 wins```
Pretty simple, but it depends on how you're keeping track of all the cards in the current war. Probably just keep pushing them on a stack, then when you finally get a winner, everything on the stack goes to them.

Note: It is theoretically possible for there to be a draw in the card game of War. Described: Split a new pack of cards in half. Player 1 gets A-K of Hearts and Clubs. Player 2 gets A-K of Diamonds and Spades. Don't shuffle them for ease of explanation.

P1: AH
... war 2,3,4H and 2,3,4D
...... 5H and 5D
......... war 6,7,8H and 6,7,8D
............ repeat the process until you run out of cards

All cards wind up in the current war stack, and there are no more cards, the game is a draw.

Quzah.

5. I'm not talking about an instance of a tie between card versus card. I have the program completely running and working. The problem is, every so often, there will be a game that never ends because each player will have 4-5 cards that they keep playing and player 1 will win, then player 2 will win, and it just keeps going without end.

The program is set up so when there is a hand that ties, both of those cards are thrown out. The deck usually ends with about 40 cards when it started with 52 cards.

6. I'm not really sure how you're running out of cards at all. Cards you win go on the bottom of your pile. Otherwise, you'd simply run out of cards anyway. There shouldn't really be a scenario where one side ends up with a limited number of cards that never changes. I suppose there's some theoretical chance of that, but I'd be really interested in a chain of cards that produced that:

A2 v rest of deck.
A wins, 2 loses
A? v rest of deck
A wins, ? probably loses
A? v rest of deck
A wins, ? probably loses
...

Eventually, the ace will either tie, which means that the remaining card will be set to call the win/loss of the war. Losing is game over, winning now gives them an additional four cards. I'm just not seeing this as being very likely. You will eventually either end up with all the cards, or you'll keep going until you lose all your cards.

Quzah.

7. Originally Posted by quzah
I'm not really sure how you're running out of cards at all. Cards you win go on the bottom of your pile. Otherwise, you'd simply run out of cards anyway. There shouldn't really be a scenario where one side ends up with a limited number of cards that never changes.
It's for a class assignment. I'm actually redoing the program because the instructor took off 10% for not catching a time game. So apparently it is possible. He's not a very good teacher, when you ask for help he just turns you to the book and tells you to figure it out.

It's not that you run out of cards, it's that each player will have just the right cards where they will just win and lose back and forth and the game will never end. I can't think of an exact set of cards that so this, but I have seen the program run into it so it is possible.

8. Here's what I think he is referring to as a tie game. As you can see, they both run out of cards at the same time but it says player 2 wins.

Code:
```      Five of Hearts = Five of Clubs                     2 - 2
Eight of Spades = Eight of Clubs                    1 - 1
Seven of Clubs = Seven of Diamonds                 0 - 0

Tie Game!
It took 222 hands,and there were 26 ties.```
Looks simple now. I'll just do if player 1 score == 0 && player 2 score == 0 then it's a tie.

9. I'm still not seeing how you would have a tie. If you run out of cards, you lose. You can't both run out at the same time. It's impossible.

Quzah.

10. Yeah you can. If you read my first post completely. If you have two cards that are the same value in one hand, I'm throwing those two cards out of the game. If both players run out at the same time because their last 3 cards were the same rank played at the same time, then it's a tie.

11. Then you're not playing standard War. Anyway, all you have to do is check to see who has the most cards at the end. If they both have zero, then it's a tie. Otherwise, the one with the cards wins.

Quzah.