Check for winner and tie

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2007
Posts: 5
Reputation: SirJames is an unknown quantity at this point 
Solved Threads: 0
SirJames SirJames is offline Offline
Newbie Poster

Check for winner and tie

 
0
  #1
Oct 1st, 2007
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.




  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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 802
Reputation: Phaelax is on a distinguished road 
Solved Threads: 39
Phaelax Phaelax is offline Offline
Practically a Posting Shark

Re: Check for winner and tie

 
0
  #2
Oct 1st, 2007
We can't tell if it works if we don't know the game or rules. What determines a winner?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 5
Reputation: SirJames is an unknown quantity at this point 
Solved Threads: 0
SirJames SirJames is offline Offline
Newbie Poster

Re: Check for winner and tie

 
0
  #3
Oct 2nd, 2007
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...

  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC