-- 18-04-2002 A V Peterson -- -- This module is submitted as part of the assessment for -- COMP1100. -- Its completion is substanitally my own work. -- (signed) Joseph Curtis 3952239 -- class CARD_HAND -- the cards held by a single player creation make feature {NONE} the_hand : ARRAY[CARD] -- the cards held aces : INTEGER -- the number of aces held feature {ANY} make is -- Create an empty hand. do !!the_hand.make(1, 0) aces := 0 end -- make add_card(c : CARD) is -- Add card c to the hand. local do the_hand.add_last(c) if c.rank = 1 then aces := aces + 1 end end -- add_card clear is -- Discard all the cards in the player's hand. do the_hand.clear aces := 0 end -- clear value : INTEGER is -- This function goes through a hand and calculates the value of it. --If there are any aces it will decide whether to use then as 1's or --11's according to the total value of the hand local i:INTEGER val:INTEGER -- temporary value of hand temp_aces:INTEGER -- temporary number of aces do temp_aces:=aces from -- loop through the cards and get the values i:=the_hand.lower until i>the_hand.upper loop val := val + the_hand.item(i).value -- add the value of --the card to the total value i:=i+1 end if (temp_aces > 0) and (val > 21) then -- check to see if --the total value is over 21, if it is then check to see --if there are any aces from -- loop for the number of aces i:=1 until i>temp_aces or val < 22 -- loop until either the aces --have been used up or the value of the hand reduces --to below 22 loop temp_aces := temp_aces - 1 -- for every ace, take one --off the ace counter and then change the value of --that ace from11 to 1 val := val - 10 i:=i+1 end end Result := val -- send the value to the result of the function end -- value display is -- Display the cards in the hand and the value of the hand. local i : INTEGER do from i := the_hand.lower until i > the_hand.upper loop the_hand.item(i).display; io.put_spaces(2) i := i + 1 end -- loop io.put_string(" Value = ") io.put_integer(value) io.put_new_line end -- display end -- class CARD_HAND