/test/language/gc/hand2.e
Specman e | 184 lines | 123 code | 12 blank | 49 comment | 15 complexity | e735193fb714300d47b79ecb3107e0c0 MD5 | raw file
1-- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries. 2-- See the Copyright notice at the end of this file. 3-- 4expanded class HAND2 5 6create {ANY} 7 make 8 9feature {} 10 make 11 do 12 create rank.make(1, 5) 13 create suit.make(1, 5) 14 end 15 16feature {ANY} -- Queries 17 is_3_aces: BOOLEAN 18 local 19 i: INTEGER; k: INTEGER 20 do 21 -- index 22 -- count of aces 23 from 24 until 25 i = 5 26 loop 27 i := i + 1 28 if rank @ i = 1 then 29 k := k + 1 30 end 31 -- if 32 end 33 -- loop 34 Result := k = 3 35 end 36 37 is_straight: BOOLEAN 38 -- true if no pairs and all cards are 39 -- within 5 ranks of each other 40 local 41 i, j: INTEGER; a, b: INTEGER; flag: BOOLEAN 42 do 43 -- to break cycle 44 from 45 until 46 i = 4 or else flag 47 loop 48 i := i + 1 49 from 50 j := i 51 until 52 j = 5 or else flag 53 loop 54 j := j + 1 55 a := rank @ i 56 b := rank @ j 57 -- correct for ace high 58 if a = 1 and b >= 10 then 59 a := 14 60 elseif b = 1 and a >= 10 then 61 b := 14 62 end 63 -- if 64 -- reject pairs 65 if a = b then 66 flag := True 67 elseif a - 4 > b then 68 -- reject if not within 5 ranks 69 flag := True 70 elseif b - 4 > a then 71 flag := True 72 end 73 -- if 74 end 75 -- loop 76 end 77 -- loop 78 Result := not flag 79 end 80 81 is_flush: BOOLEAN 82 local 83 i: INTEGER; flag: BOOLEAN 84 do 85 -- to break cycle 86 from 87 i := 1 88 until 89 i = 5 or else flag 90 loop 91 i := i + 1 92 if suit @ i /= suit @ 1 then 93 flag := True 94 end 95 -- if 96 end 97 -- loop 98 Result := not flag 99 end 100 101 is_full_house_or_4: BOOLEAN 102 -- true if no more than two ranks 103 local 104 i: INTEGER; a, b, c: INTEGER; flag: BOOLEAN 105 do 106 -- to break cycle 107 a := rank @ 1 108 -- b := 0 -- (b rank not yet found) 109 from 110 i := 1 111 until 112 i = 5 or else flag 113 loop 114 i := i + 1 115 c := rank @ i 116 if c /= a and c /= b then 117 -- found new rank, c 118 if b /= 0 then 119 flag := True 120 -- break 121 -- already have 2 ranks 122 else 123 b := c -- b, 2nd rank, is the new one, c 124 end 125 -- if 126 end 127 -- if 128 end 129 -- loop 130 Result := not flag 131 end 132 133 is_straight_flush: BOOLEAN 134 do 135 Result := is_straight or else is_flush 136 end 137 138 wins: BOOLEAN 139 -- wins means beats 3 kings 140 do 141 Result := is_3_aces or else is_straight or else is_flush or else is_full_house_or_4 or else is_straight_flush 142 end 143 144feature {ANY} -- Operations 145 deal (card1, card2, card3, card4, card5: INTEGER) 146 local 147 i: INTEGER; card: ARRAY[INTEGER] 148 do 149 card := {ARRAY[INTEGER] 1, << card1, card2, card3, card4, card5 >> } 150 from 151 until 152 i = 5 153 loop 154 i := i + 1 155 rank.put((card @ i - 1) \\ 13 + 1, i) 156 suit.put((card @ i - 1) // 13 + 1, i) 157 end 158 -- loop 159 end 160 161feature {} -- Implementation 162 rank: ARRAY[INTEGER] -- 1=ace, 13=king 163 164 suit: ARRAY[INTEGER] -- 1=club, 2=diamond, 3=heart, 4=spade 165 166end -- class HAND2 167-- 168-- ------------------------------------------------------------------------------------------------------------------------------ 169-- Copyright notice below. Please read. 170-- 171-- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, 172-- as published by the Free Software Foundation; either version 2, or (at your option) any later version. 173-- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty 174-- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have 175-- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free 176-- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 177-- 178-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE 179-- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE 180-- 181-- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN 182-- 183-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr 184-- ------------------------------------------------------------------------------------------------------------------------------