Created
May 30, 2012 01:34
-
-
Save matthavener/2832339 to your computer and use it in GitHub Desktop.
poker kata (matt and xerik)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe "highcard", -> | |
| it "3 beats 2", -> | |
| expect(beats(3, 2)).toBe true | |
| it "2 doesnotbeat 3", -> | |
| expect(beats(2,3)).toBe false | |
| it "J beats 10", -> | |
| expect(beats("J",10)).toBe true | |
| it "Q beats J", -> | |
| expect(beats("Q","J")).toBe true | |
| it "A beats K", -> | |
| expect(beats("A","K")).toBe true | |
| it "K beats Q", -> | |
| expect(beats("K","Q")).toBe true | |
| it "highcard hand wins",-> | |
| expect(beatsHand([4,2],[3,2])).toBe true | |
| it "facecard highcard hand wins", -> | |
| expect(beatsHand(["A", 2], [10, 3])).toBe true | |
| describe "pairs", -> | |
| it "pair beats highcard", -> | |
| expect(beatsHand([2,2], [3,2])).toBe true | |
| it "pairs use highcard", -> | |
| expect(beatsHand([3,3], [2,2])).toBe true | |
| it "pairs with 5 cards", -> | |
| expect(beatsHand([2,2], [1,2,3,4,5])).toBe true | |
| it "pairs both with 5 cards", -> | |
| expect(beatsHand([1,2,2,3,4], [1,2,3,4,5])).toBe true | |
| it "highcard pair wins", -> | |
| expect(beatsHand([1,3,3,2,4], [1,2,2,3,4])).toBe true | |
| it "highcard equalpair wins", -> | |
| expect(beatsHand([1,2,2,3,4], [1,2,2,3,5])).toBe false | |
| it "highcard equalpair uses lower card", -> | |
| expect(beatsHand([1,2,4,5,5], [1,2,3,5,5])).toBe true | |
| it "facecard pair", -> | |
| expect(beatsHand(["A", "A"], [2,2])).toBe true | |
| it "twopair beats pair", -> | |
| expect(beatsHand(['A','A'],[2,2,3,3])).toBe false | |
| it "twopair equal uses highcard", -> | |
| expect(beatsHand([3,4,4,5,5],[2,4,4,5,5])).toBe true | |
| describe "threeofakind", -> | |
| it "simple 3ofakind", -> | |
| expect(beatsHand([3,3,3], [4,4])).toBe true | |
| describe "straight", -> | |
| it "straights beat ace",-> | |
| expect(beatsHand([6,2,3,4,5],["A"])).toBe true | |
| to_num = (card) -> | |
| if (card == "J") | |
| return 11 | |
| if(card == "Q") | |
| return 12 | |
| if(card == "K") | |
| return 13 | |
| if(card == "A") | |
| return 14 | |
| card | |
| beats = (player1, player2) -> | |
| to_num(player1) > to_num(player2) | |
| highCard = (hand) -> | |
| (Math.max (hand.map (h) -> to_num(h))...) | |
| nexists = (hand, n) -> | |
| hands = hand.sort().reverse() | |
| output = {} | |
| for h in hand | |
| if (!output[h]) | |
| output[h] = 1 | |
| else if(output[h] < n-1) | |
| output[h]++ | |
| else | |
| return [h, c for c in hand when h != c] | |
| return [false, hand] | |
| pair = (hand) -> | |
| return nexists(hand,2) | |
| threes = (hand) -> | |
| return nexists(hand,3) | |
| straight = (hand) -> | |
| handrs = hand.sort().reverse() | |
| corrhandrs = [handrs[0]..handrs[handrs.length-1]] | |
| if (handrs == corrhandrs && hand.length == 5) | |
| return handrs[0] | |
| return false | |
| beatsHand=(hand1,hand2)-> | |
| hand1 = (to_num(a) for a in hand1) | |
| hand2 = (to_num(a) for a in hand2) | |
| str1 = straight(hand1) | |
| str2 = straight(hand2) | |
| if (str1 != str2) | |
| return str1 > str2 | |
| [trips1, hand1] = threes(hand1) | |
| [trips2, hand2] = threes(hand2) | |
| [fp1, hand1] = pair(hand1) | |
| [fp2, hand2] = pair(hand2) | |
| [sp1, hand1] = pair(hand1) | |
| [sp2, hand2] = pair(hand2) | |
| if(trips1 != trips2) | |
| return trips1 > trips2 | |
| if (sp1 != sp2) | |
| return (sp1 > sp2) | |
| if (fp1 != fp2) | |
| return fp1 > fp2 | |
| highCard(hand1) > highCard(hand2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment