-- 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 A4_GAME -- Simplified version of the card game blackjack or twenty-one. -- Played interactively with one player against the dealer (computer). -- Game rules defined in a4_game_rules.txt. creation make feature {ANY} the_deck : DECK -- standard deck of cards dealer : CARD_PLAYER -- dealer is constrained by dealer rules player : CARD_PLAYER -- player may choose whether to receive more cards bet : REAL -- amount of bet (default is 1.00) make is -- Create a blackjack game local name : STRING do introduction bet := 1 -- set the value of the bet -- create and shuffle deck of cards !!the_deck.make the_deck.shuffle -- create the players !!dealer.make("dealer") io.put_string("Your name? ") io.read_line; name := clone(io.last_string) !!player.make(name) play_game end -- make introduction is -- an introductory message do io.put_string("Welcome to the COMP1100 blackjack game.%N") io.put_string("The aim of the game is to have a hand of cards%N") io.put_string("with a higher value (up to 21) than the dealer's%N") io.put_string("hand. Hands with value > 21 are worthless.%N") io.put_new_line io.put_string("The value of an individual card with rank 2 - 10%N") io.put_string("is just the rank of the card. Jacks, queens and%N") io.put_string("kings (denoted J, Q, and K) all have the value 10.%N") io.put_string("Aces (denoted A) can have the value 11 or 1%N") io.put_string("depending on which value gives you the most%N") io.put_string("favorable hand. The suit of a card (denoted S,%N") io.put_string("H, D, C for spade, heart, diamond and club) does%N") io.put_string("not matter.%N") io.put_new_line end -- introduction play_game is -- Play the game; number of hands played is the player's choice. local another_hand : BOOLEAN -- whether to continue game answer : STRING -- 'y' or 'n' do from another_hand := true until not another_hand loop io.put_string("%NPlay another hand? [y/n] ") io.read_line; answer := clone(io.last_string) if (answer.count > 0) and then (answer.item(1) = 'y') then play_hand -- display outcome with new scores io.put_string("%NOutcome of hand : %N") dealer.display player.display io.put_new_line -- discard hands player.hand.clear dealer.hand.clear else another_hand := false end -- if end -- loop end -- play_game play_hand is -- This procedure goes through the steps for playing a hand of --blackjack. The cards are dealt and the user can decide when --they want to stop recieving the cards. The dealer revieves his --cards and the outcome of the hand is decided depending of the --values of the two hands. local another_card : BOOLEAN -- Stores whether the player wants another --card dealer_another_card : BOOLEAN -- Stores whether the dealer wants --another card answer : STRING -- User input do the_deck.deal_card -- deal first card to player player.get_card(the_deck.last_card) the_deck.deal_card -- deal first card to dealer dealer.get_card(the_deck.last_card) the_deck.deal_card -- deal second card to player player.get_card(the_deck.last_card) dealer.display -- display the cards in hands player.display from -- User card dealing loop another_card := true until not another_card -- continue the loop if the user wants another --card loop io.put_string("Another card? [y/n] ") -- does the user want --another card io.read_line answer := clone(io.last_string) if (answer.count > 0) and (answer.item(1) = 'y') then -- deals --the user another card if they wish another_card := true the_deck.deal_card player.get_card(the_deck.last_card) player.hand.display else another_card := false -- otherwise set the another_card --variable to false so the loop will end end end from -- Dealer card choosing loop dealer_another_card := true until not dealer_another_card loop if (dealer.hand.value < 17) then -- if dealer's hand value is --less than 17 then get another card the_deck.deal_card dealer.get_card(the_deck.last_card) else dealer_another_card := false end end -- Scoring if(player.hand.value /= dealer.hand.value) then if (player.hand.value < 22) then -- if both values are under 22 if(dealer.hand.value < 22) then if(player.hand.value > dealer.hand.value) then -- if the --player's value is greater than the dealer's player.update_score(bet) -- add the bet on to the player's --score dealer.update_score(-bet) -- take the bet off the dealer's --score else -- if the dealer's score is higher than the player's score player.update_score(-bet) -- take the bet off the player's --score dealer.update_score(bet) -- add the bet on to the --dealer's score end else -- if only the player's score is under 22 player.update_score(bet) dealer.update_score(-bet) end else if(dealer.hand.value < 22) then -- if only the dealer's score --is under 22 player.update_score(-bet) dealer.update_score(bet) end end end end -- play_hand end -- class A4_GAME