944,188 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1319
  • Java RSS
Oct 1st, 2007
0

Check for winner and tie

Expand Post »
Wanted to verify that this actually does check for all wins- if so I need to a statement that a tie does still exist and where to add it...Opinions and suggestions please.. method displayHandName() writes out the hand found.




Java Syntax (Toggle Plain Text)
  1.  
  2. public static void determineWinner(int[] finalHands, int[][] hands) throws
  3. IOException
  4. {
  5. int maxHand = 0; // The best hand value seen so far.
  6. int winner = 0; // The winner.
  7. // Look at each player's hand to find the winner.
  8. for (int i = 0; i < NUM_PLAYERS; i++) {
  9. // The current player has the best hand so far.
  10. if (finalHands[i] > maxHand) {
  11. maxHand = finalHands[i];
  12. winner = i;
  13. }
  14. // The current player is tied with the best hand so far...
  15. else if (finalHands[i] == maxHand)
  16. // ... but wins on the tiebreak.
  17. if (hands[i][0] > hands[winner][0]) {
  18. maxHand = finalHands[i];
  19. winner = i;
  20. }
  21. }
  22. System.out.println("Hit return to see who won...");
  23. stdin.readLine();
  24. System.out.println("----------------------------------------------
  25. \n");
  26. System.out.println("PLAYER " + (winner+1) + ", YOU WIN!!!");
  27. displayHandName(maxHand);
  28. }
Similar Threads
Reputation Points: 20
Solved Threads: 0
Newbie Poster
SirJames is offline Offline
5 posts
since Jul 2007
Oct 1st, 2007
0

Re: Check for winner and tie

We can't tell if it works if we don't know the game or rules. What determines a winner?
Reputation Points: 92
Solved Threads: 51
Practically a Posting Shark
Phaelax is offline Offline
856 posts
since Mar 2004
Oct 2nd, 2007
0

Re: Check for winner and tie

1. Poker is the game...
2. Checking for the best of hands to determine winner.
3. Ties are possible...but I do not think my code handes the tie (ie. two straights of the same rank- different suits)

Here is determine hands if needed...

Java Syntax (Toggle Plain Text)
  1. public static void displayHand(int[] hand)
  2. {
  3. // Display each card.
  4. for (int card = 0; card < HAND_SIZE; card++) {
  5. System.out.print(" " + (card+1) + ": ");
  6. // Display the card's numerical value.
  7. switch (hand[card] / 4) {
  8. case 14 : System.out.print("Ace");
  9. break;
  10. case 13 : System.out.print("King");
  11. break;
  12. case 12 : System.out.print("Queen");
  13. break;
  14. case 11 : System.out.print("Jack");
  15. break;
  16. default: System.out.print(hand[card] / 4);
  17. }
  18. System.out.print(" of ");
  19. // Display the card's suit.
  20. switch (hand[card] % 4) {
  21. case 0 : System.out.println("hearts");
  22. break;
  23. case 1 : System.out.println("clubs");
  24. break;
  25. case 2 : System.out.println("diamonds");
  26. break;
  27. case 3 : System.out.println("spades");
  28. break;
  29. }
  30. }
  31. System.out.println();
  32. }

Thanks
Reputation Points: 20
Solved Threads: 0
Newbie Poster
SirJames is offline Offline
5 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Inventory part 5
Next Thread in Java Forum Timeline: RunTime error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC