Catching a Tie in War Card Game

This is a discussion on Catching a Tie in War Card Game within the C Programming forums, part of the General Programming Boards category; Basically, the program deals 52 cards between 2 players. Each time you run the game it usually takes between 50 ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    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. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    can you post your code?

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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
    P2: AD
    ... 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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    Quote Originally Posted by quzah View Post
    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. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    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.
    Last edited by surefire; 02-15-2010 at 06:57 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    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. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Event driven game engine
    By abachler in forum Game Programming
    Replies: 9
    Last Post: 12-01-2009, 06:03 AM
  2. Card shuffle game
    By newbie30 in forum C Programming
    Replies: 5
    Last Post: 08-13-2009, 05:19 PM
  3. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 11:06 AM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  5. War with Iraq - Read this article if you're interested
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-26-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21