p054 CM;P1;P2;H1;H2;b © Solve Project Euler Problem 54 © using global variable © © Card Matrix like or © © D. Edwards , 9-10-2012 P1„score_hand ¨ ,1› H1 „ 0 ¯15‡CM P2„score_hand ¨ ,1› H2 „ 0 15‡CM ©H1,('I8,X1' Œfmt P1),(WIN[b+ŒIO;]),('I8,X1' Œfmt P2),H2 © See how many hands Player 1 won. 'Player one won' (+/b„P1>P2) 'hands.' ------------------------------------------------------------ s„score_hand cards;N;S;i;Nd;‘;EQ © : five card string of number and suite pairs © Return an integer score of the hand. © © Format for : '[n1][suite1] [n2][suite2] ....' © © Score is based on a basic value for rank of the hand © with added points for the high cards of the hand, © starting with the primary hand and followed by the © tie breaker cards © High Card: Highest value card. © One Pair: Two cards of the same value. © Two Pairs: Two different pairs. © Three of a Kind: Three cards of the same value. © Straight: All cards are consecutive values. © Flush: All cards of the same suit. © Full House: Three of a kind and a pair. © Four of a Kind: Four cards of the same value. © Straight Flush: All cards are consecutive values of same suit. © © D. Edwards , 9-10-2012 © Get a vector of numbers and suites N„(14½1 0 0)/cards S„(14½0 1 0)/cards © Sort the cards i„'23456789TJQKA'”N N„N[i] S„S[i] Nd„c2dec ¨ N © Vector of decimal card #s ‘„(¯1‡Nd)-1‡Nd © Difference between subsequent cards EQ„+/N°.=N © # of card repeats © Check for Straight Flush :if (5=+/S=†S) ^ 4=+/1=‘ s„8E6 © Four of a Kind :elseif 4=+/4=EQ s„7E6 © Full House :elseif (3=+/3=EQ) ^ 2=+/2=EQ s„6E6 N„N[”EQ] © Flush - All one suite :elseif 5=+/S=†S s„5E6 © Straight :elseif 4=+/1=‘ s„4E6 © Three of a kind :elseif 3=+/3=EQ s„3E6 N„N[”EQ] © Two Pair :elseif 4=+/2=EQ s„2E6 N„N[”EQ] © One Pair :elseif 2=+/2=EQ s„1E6 N„N[”EQ] :else s„0 :end s „ s + c2dec N ------------------------------------------------------------ d„c2dec h; Œio © Convert base 15 number with card characters to a decimal integer. © © D. Edwards , 9-10-2012 Œio„0 d„ 16ƒ'0123456789TJQKA'¼h ------------------------------------------------------------ ]display WIN .…----. ‡ | | WIN | '-----' ------------------------------------------------------------ ]display tpok .…----------------------------. ‡2D 3D 4D 5D AC KD QD JD TD 8S| |3D 3D 4D 5D 6S 2D 2D AS KS QS| |3D 3D 3D 4H 5H AS 2D KS 2D 2D| '-----------------------------'