sashomaga

Check cards in poker game

Mar 28th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* • If the five cards are equal, the program outputs “Impossible”, otherwise:
  2. • if four of them are equal, the program outputs “Four of a Kind”, otherwise:
  3. • if there are three equal and the another two are also equal (example: ‘2’, ‘2’, ‘10’, ‘10’, ‘2’), the program outputs “Full House”, otherwise:
  4. • if the five are consecutive (example: ‘2’, ‘3’, ‘4’, ‘5’, ‘6’), the program outputs “Straight”, otherwise:
  5. • if three of them are equal, the program outputs “Three of a Kind”, otherwise:
  6. • if two pairs contain respectively equal numbers (example ‘A’, ‘K’, ‘A’, ‘K’, ‘J’), the program outputs “Two Pairs”, otherwise:
  7. • if only two cards are equal, the program outputs “One Pair”, otherwise;
  8. • the program outputs “Nothing”. */
  9.  
  10. function Solve(args) {
  11.         var cards = [];
  12.         for (var i = 0; i < args.length; i++) {
  13.             if (isNaN(args[i])) {
  14.                 switch (args[i]) {
  15.                     case "A": { cards[i] = 1; break; }
  16.                     case "K": { cards[i] = 13; break; }
  17.                     case "Q": { cards[i] = 12; break; }
  18.                     case "J": { cards[i] = 11; break; }
  19.                 }
  20.             } else {
  21.                 cards[i] = parseInt(args[i]);
  22.             }
  23.         }      
  24.         card = cards.sort(function (a, b) { return a - b; });
  25.         if (cards[0] == cards[1] && cards[0] == cards[2] && cards[0] == cards[3] && cards[0] == cards[4]) {
  26.             return "Impossible";
  27.         }
  28.         if (cards[0] == cards[1] && cards[0] == cards[2] && cards[0] == cards[3] || cards[4] == cards[1] && cards[4] == cards[2] && cards[4] == cards[3]) {
  29.             return "Four of a Kind";
  30.         }
  31.         if ((cards[0] == cards[1] && cards[0] == cards[2] && cards[4] == cards[3]) || (cards[4] == cards[3] && cards[4] == cards[2] && cards[0] == cards[1])) {
  32.             return "Full House";
  33.         }
  34.         if (cards.indexOf(1) == 0) {
  35.             if (cards[4] == 13 && cards[3] == 12 && cards[2] == 11 && cards[1] == 10)
  36.             {
  37.                 return "Straight";
  38.             }
  39.         }
  40.         if (cards[4] - cards[3] == 1 && cards[3] - cards[2] == 1 && cards[2] - cards[1] == 1 && cards[1] - cards[0] == 1) {
  41.             return "Straight";
  42.         }
  43.         if ((cards[0] == cards[1] && cards[0] == cards[2]) || (cards[4] == cards[3] && cards[4] == cards[2])) {
  44.             return "Three of a Kind";
  45.         }
  46.         if ((cards[1] == cards[0] || cards[1] == cards[2]) && (cards[3] == cards[2] || cards[3] == cards[4])) {
  47.             return "Two Pairs";
  48.         }
  49.         if (cards[0] == cards[1] || cards[1] == cards[2] || cards[2] == cards[3] || cards[3] == cards[4]) {
  50.             return "One Pair";
  51.         }
  52.  
  53.         return "Nothing";
  54.         console.log(cards);
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment