ProductsLogo
LogoStudy Documents
LogoAI Grader
LogoAI Answer
LogoAI Code Checker
LogoPlagiarism Checker
LogoAI Paraphraser
LogoAI Quiz
LogoAI Detector
PricingBlogAbout Us
logo

Simulating Blackjack Game

Verified

Added on  2019/09/30

|7
|1690
|109
Report
AI Summary
The program is designed to simulate a card game, specifically Blackjack. It includes four main modules: (1) `random_pack_generator` - generates a random pack of cards, (2) `read_pack_from_file` - reads a pre-existing pack of cards from a file, (3) `assess_hand` - evaluates the value of a given hand, and (4) `deal_card` - deals a new card to a player's hand. The program also includes error handling for cases where files cannot be opened.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Pontoon Program
PROGRAM DESIGN – pseudocode
(With line numbers)
001 *** Design of pack of cards.
002 *** This will include:
003 *** Each card in the pack as an element of an array;
004 *** The index of the next card in the pack.
005 *** Design of each hand of cards.
006 *** This will include:
007 *** Each card in the hand as an element of an array;
008 *** The number of cards in the hand;
009 *** A boolean indicating whether aces are in the hand;
010 *** A boolean indicating whether the hand is a Pontoon;
011 *** The max. point count of the hand (<22 if possible);
012 *** The min. point count of the hand (each ace is 1).
013 *** Pontoon Program
014 BEGIN
015 set games played to 0
016 CALL initialise_pack (returns pack of cards)
017 LOOP
018 CALL initial_deal (passing pack, returns the 2 hands)
019 increment games played
020 IF games played > 8
021 THEN
022 CALL random_pack_generator (returns shuffled pack)
023 ENDIF
024 CALL human_plays (passing Human’s hand & pack,
returns ‘human bust’ flag)
025 IF human is bust
026 THEN
027 DISPLAY “You lose”
028 ELSE
029 CALL computer_plays (passing Computer’s hand & pack)
030 IF minimum point count of Computer’s hand > 21
031 THEN
032 DISPLAY “You win – Computer is bust”
033 ELSE
034 CALL compare_scores (passing the two hands)
035 IF winner is human
036 THEN
037 DISPLAY “You win – congratulations”
038 ELSE
039 DISPLAY “You lose”
040 ENDIF

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
041 DISPLAY “Computer held: “
042 CALL display_cards (Computer’s hand)
043 ENDIF
044 ENDIF
045 increment games played
046 CALL restore_pack (passing pack & the 2 hands)
047 DISPLAY “Play another game?”
048 READ answer
049 ENDLOOP when human does not want another game
050 CALL write_pack_to_file (passing pack)
051 END
052 initialise_pack:
053 BEGIN
054 READ user choice (from radio button)
055 IF user choses random generation
056 THEN
057 CALL random_pack_generator (returns pack of cards)
058 ELSE *** user choses to read pack from file
059 CALL read_pack_from_file
(returns pack of cards & success indicator)
060 IF cards could not be successfully read from file
061 THEN
062 CALL random_pack_generator (returns pack of cards)
063 ENDIF
064 ENDIF
065 set index of next card in pack to the first card in pack
066 RETURN pack of cards
067 END
068 initial deal:
069 *** Input is pack of cards
070 BEGIN
071 add first card in pack to Human’s hand
072 add second card in pack to Computer’s hand
073 add third card in pack to Human’s hand
074 add fourth card in pack to Computer’s hand
075 add 4 to index of next card in pack
076 set number of cards in Human’s hand to 2
077 set number of cards in Computer’s hand to 2
078 CALL assess_hand (passing Human’s hand)
079 CALL assess_hand (passing Computer’s hand)
080 RETURN Human’s hand, Computer’s hand & updated pack
081 END
Document Page
082 human_plays:
083 *** Inputs are Human’s hand & pack
084 BEGIN
085 LOOP
086 DISPLAY “Your hand”
087 CALL display_cards (Human’s hand)
088 DISPLAY “Stick or twist?”
089 READ response (from radio button)
090 IF response is ‘stick’
AND [maximum point count of hand < 16
AND cards held < 5]
091 THEN
092 DISPLAY “You have less than 16 points - must twist”
093 set response to ‘twist’
094 ENDIF
095 IF response is ‘twist’
096 THEN
097 CALL deal_card (passing Human’s hand & the pack)
098 CALL display_cards (passing Human’s hand)
099 IF minimum value of cards in human’s hand is > 21
100 THEN
101 DISPLAY “You are bust”
102 ENDIF
103 ENDIF
104 ENDLOOP when human allowed to stick OR when human is bust
105 RETURN Human’s hand, updated pack
& flag indicating whether Human is bust
106 END
107 computer_plays:
108 *** Inputs are Computer’s hand & pack
109 BEGIN
110 LOOP while computer does not have pontoon
AND computer has less than 5 cards
AND [maximum value of hand is < 17
OR minimum value of hand is <12]
111 CALL deal_card (passing Computer’s hand & the pack)
112 DISPLAY “Computer twists”
113 ENDLOOP
114 RETURN updated Computer’s hand & the pack
115 END
Document Page
116 compare_scores:
117 *** Inputs are the two hands of cards
118 BEGIN
119 IF computer has pontoon
120 THEN
121 winner is computer
122 ELSE
123 IF human has pontoon
124 THEN
125 winner is computer
126 ELSE
127 IF computer has 5 card trick
128 THEN
129 winner is computer
130 ELSE
131 IF human has 5 card trick
132 THEN
133 winner is human
134 ELSE
135 IF maximum point count of Human’s hand >
maximum point count of Computer’s hand
136 THEN
137 winner is human
138 ELSE
139 winner is computer
140 ENDIF
141 ENDIF
142 ENDIF
143 ENDIF
144 ENDIF
145 RETURN winner
146 END
147 display_cards:
148 *** Input is hand of cards
149 BEGIN
150 LOOP for each card in hand
151 DISPLAY the card’s suit
152 DISPLAY the card’s value
153 *** A for Ace, J for Jack, Q for Queen, K for King
154 *** otherwise the numeric value
155 ENDLOOP
156 END

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
157 restore_pack:
158 *** Inputs are the two hands & the pack of cards
159 BEGIN
160 move all cards earlier than the index card
to the back of the pack
161 clear all cards from each hand
162 set the number of cards in each hand to zero
163 RETURN the updated pack and the two hands
164 END
165 write_pack_to_file:
166 *** Input is pack of cards
167 BEGIN
168 try to open cards.txt for writing
169 IF cannot open cards.txt
170 THEN
171 DISPLAY “Unable to store pack of cards on file”
172 ELSE
173 set counter to 1
174 LOOP for each card in pack
175 WRITE card to cards.txt file
176 WRITE space to cards.txt file
177 increment counter
178 IF counter = 13
179 THEN
180 WRITE newline to file
181 set counter to 1
182 ENDIF
183 ENDLOOP
184 close cards.txt file
185 ENDIF
186 END
187 random_pack_generator:
188 BEGIN
189 LOOP for each card in the pack
190 add a card to the pack, in the order Ace, Two, …, King,
with suits in the order Hearts, Clubs, Diamonds, Spades
191 ENDLOOP
192 LOOP for each card in the pack except the last card
193 randomly generate the index of another card in the pack
194 swap the two cards
195 ENDLOOP
196 RETURN pack of cards
197 END
Document Page
198 read_pack_from_file:
199 BEGIN
200 set success indicator to ‘unsuccessful’
201 try to open cards.txt file
202 IF unable to open cards.txt file
203 THEN
204 DISPLAY “Cannot read pack of cards from file.”
205 DISPLAY “Pack of cards will be generated randomly.”
206 set success indicator to ‘unsuccessful’
207 ELSE
208 clear the pack of cards
209 clear error_encountered flag
210 LOOP until all cards read from file
OR until success indicator is ‘unsuccessful’
211 READ card from cards.txt
212 IF card read is not valid
213 THEN
214 set success indicator to ‘unsuccessful’
215 ELSE
216 add card to pack
217 increment card_count
218 IF card is a duplicate of one already in the pack
219 THEN
220 set success indicator to ‘unsuccessful’
221 ENDIF
222 ENDIF
223 ENDLOOP
224 close cards.txt file
225 ENDIF
226 RETURN pack of cards & success indicator
227 END
228 assess_hand:
229 *** Input is one of the hands
230 BEGIN
231 set minimum point count to 0
232 set aces held to FALSE
233 set pontoon held to FALSE
234 IF there are 2 cards in the hand
AND one card is an Ace and the other card
is either Jack, Queen or King
235 THEN
236 set aces held to TRUE
237 ENDIF
238 LOOP for each card in hand
239 IF card value is > 10
240 THEN
241 add 10 to minimum point count of hand
242 ELSE
243 add card value to minimum point count of hand
Document Page
244 ENDIF
245 IF card is ace
246 THEN
247 set aces_held to TRUE
248 ELSE
249 add card value to minimum point count of hand
250 ENDIF
251 ENDLOOP
252 set maximum point count of hand to minimum point count of
hand
253 IF aces are held OR maximum point count of hand < 12
254 THEN
255 add 10 to maximum point count of hand
256 ENDIF
257 RETURN updated hand
258 END
259 deal_card:
260 *** Inputs are one of the hands & the pack
261 BEGIN
262 add next card in pack to cards in hand
263 increment count of cards held in hand
264 increment index of next card in pack
265 CALL assess_hand (passing the hand)
266 RETURN updated hand & pack
267 END
End of Program Design (pseudocode)
1 out of 7
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]