/test/language/gc/hand2.e

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