Blackjack 1
1

First, set the browser to Full Screen view, to give this worksheet and your code as much space as possible.

Then choose either one of the two options below:

Option 1: Start this worksheet from scratch

- First Enter your name:
- Then
  .
- All answers that you subsequently enter/edit will be auto-saved to this sheet.

Option 2: Continue, or view, previous work

- All answers that you subsequently enter/edit will be auto-saved to this sheet.
- The first time you re-load an answers sheet - within a single session on Elan - you will be asked to confirm that you understand this.

This worksheet is copyright © Richard Pawson 2025, and protected by the Creative Commons license: Attribution-NonCommercial-NoDerivatives 4.0 International. You may freely make and distribute copies of this worksheet as is, but if you modify this worksheet you may not distribute your modified version (outside your own teaching institution) without the author's permission. Please email the author to report errors or suggest improvements.

# Step 1: learning the rules of Blackjack

Watch the first video:

Now
# 9a6bf9a75f10fe272f1e3837478b3432a0a6e570fcfde07386e7d0735907aebf Elan 1.7.0 guest default_profile valid main let game be new Game(1000) call game.addPlayer(new HumanPlayer("Player A", 1000)) variable anotherRound set to true while anotherRound call playOneRound(game) call game.setMessage("Points updated. Do you want to play another round? (press y or n)") call clearKeyBuffer() call display(game) let k be waitForKey().lowerCase() if k is 'y' then call game.setMessage("") else set anotherRound to false end if end while end main procedure playOneRound(game as Game) call game.newRound() call display(game) let dealer be game.dealer each player in game.players call player.startTurn() call display(game) while player.status is Status.active call player.nextAction(dealer.faceCard) call display(game) end while end each call dealer.play() call display(game) while dealer.status is Status.active call dealer.nextAction(dealer.faceCard) call display(game) end while call game.updatePoints() end procedure procedure display(game as Game) let html be "<style>{styleSheet}</style>{htmlForGame(game)}" call displayHtml(html) call pause(1500) end procedure procedure updatePoints(dealer as Dealer, player as Player, playerOutcome as Outcome) if playerOutcome is Outcome.winDouble then call player.changePointsBy(2) call dealer.changePointsBy(-2) else if playerOutcome is Outcome.win then call player.changePointsBy(1) call dealer.changePointsBy(-1) else if playerOutcome is Outcome.lose then call player.changePointsBy(-1) call dealer.changePointsBy(1) end if end procedure function determinePlayerOutcome(dealer as Dealer, player as Player) returns Outcome # These 'let' definitions are just to make the logic that follows them simpler to read let d be dealer.status let dTotal be dealer.handTotal let p be player.status let pTotal be player.handTotal let bust be Status.bust let bj be Status.blackjack let win be Outcome.win let winDouble be Outcome.winDouble let lose be Outcome.lose let draw be Outcome.draw variable playerOutcome set to draw if p is bust then set playerOutcome to lose else if (p is bj) and (d isnt bj) then set playerOutcome to winDouble else if d is bust then set playerOutcome to win else if (d is bj) and (p is bj) then set playerOutcome to draw else if p is bj then set playerOutcome to winDouble else if d is bj then set playerOutcome to lose else if pTotal > dTotal then set playerOutcome to win else if pTotal < dTotal then set playerOutcome to lose else # strictly, this 'else' clause is redundant - as the variable was initialised to 'draw' - but added for clarity set playerOutcome to draw end if return playerOutcome end function function dealCard(random as Float) returns Card let number be (random*52).floor() let rank be rankValue.keys()[number div 4] let suit be number mod 4 return new Card() with rank set to rank, suit set to intAsSuit(suit) end function function intAsSuit(n as Int) returns Suit variable suit set to Suit.clubs if n is 1 then set suit to Suit.diamonds else if n is 2 then set suit to Suit.hearts else if n is 3 then set suit to Suit.spades end if return suit end function function htmlForGame(game as Game) returns String variable html set to "<div class='game'>" set html to html + htmlForPlayer(game.dealer) each player in game.players set html to html + htmlForPlayer(player) end each set html to html + "<div class='message'>{game.message}</div>" return html + "</div>" end function function htmlForPlayer(player as Player) returns String variable html set to "<div class='player'>" set html to html + "<div class='details'>{player.name} - {player.points} points {player.getMessage()}</div>" set html to html + "<div class='hand'>" each card in player.cards let suit be card.suit let rank be card.rank set html to html + htmlForCard(card) end each return html + "</div>" end function function htmlForCard(card as Card) returns String variable html set to "" if card.faceDown then set html to "<div class='card reversed'>" else let rank be card.rank let suit be card.suit let colour be colours[suit] let symbol be symbols[suit] set html to "<div class='card {colour}'>" let u be htmlForSpot("u", rank) let v be htmlForSpot("v", symbol) variable grid set to "" each location in gridByRank[rank] if location is "royal" then set grid to grid + htmlForSpot(location, rank) else set grid to grid + htmlForSpot(location, symbol) end if end each set html to html + "{u}{v}{grid}" end if return html + "</div>" end function function htmlForSpot(id as String, content as String) returns String return "<div class='{id}'>{content}</div>" end function function strategyA(p as Player, dealerFaceUp as Card) returns Action return Action.stand end function function strategyB(p as Player, dealerFaceUp as Card) returns Action return Action.draw end function class Game constructor(dealerStartPoints as Int) set property.dealer to new Dealer(dealerStartPoints) end constructor property dealer as Dealer property players as List<of Player> property message as String procedure newRound() call property.dealer.newHand() each player in property.players call player.newHand() end each end procedure procedure updatePoints() each player in property.players let playerOutcome be determinePlayerOutcome(property.dealer, player) call global.updatePoints(property.dealer, player, playerOutcome) end each end procedure procedure addPlayer(player as Player) call property.players.append(player) end procedure procedure setMessage(message as String) set property.message to message end procedure end class class Card property suit as Suit property rank as String property faceDown as Boolean procedure turnFaceUp() set property.faceDown to false end procedure procedure turnFaceDown() set property.faceDown to true end procedure end class abstract class Player property name as String property points as Int property cards as List<of Card> property handTotal as Int property softAce as Boolean property status as Status property hasTurn as Boolean procedure startTurn() if property.status is Status.active then set property.hasTurn to true end if end procedure procedure evaluateStatus(newCard as Card) if (cardCount() is 2) and (property.handTotal is 21) then set property.status to Status.blackjack else if (property.handTotal > 21) and (property.softAce) then set property.handTotal to property.handTotal - 10 set property.softAce to false else if property.handTotal > 21 then set property.status to Status.bust else if property.handTotal is 21 then set property.status to Status.standing end if if property.status isnt Status.active then set property.hasTurn to false end if end procedure procedure stand() set property.status to Status.standing set property.hasTurn to false end procedure procedure draw() let newCard be dealCard(random()) call property.cards.append(newCard) if newCard.rank is "A" then call addAce() else set property.handTotal to property.handTotal + rankValue[newCard.rank] end if call evaluateStatus(newCard) end procedure procedure addAce() if property.softAce then set property.handTotal to property.handTotal + 1 else set property.handTotal to property.handTotal + 11 set property.softAce to true end if end procedure function cardCount() returns Int return property.cards.length() end function procedure changePointsBy(amount as Int) set property.points to property.points + amount end procedure abstract procedure newHand() procedure newHandHelper() set property.cards to empty List<of Card> set property.handTotal to 0 set property.status to Status.active call draw() call draw() end procedure abstract function getMessage() returns String function statusAsString() returns String variable msg set to "" let status be property.status if property.hasTurn then set msg to msg + " - PLAYING" else if status is Status.standing then set msg to msg + " - STANDING" else if status is Status.blackjack then set msg to msg + " - BLACKJACK" else if status is Status.bust then set msg to msg + " - BUST" end if return msg end function abstract procedure nextAction(dealerFaceCard as Card) end class class Dealer inherits Player constructor(startingPoints as Int) set property.name to "Dealer" set property.points to startingPoints end constructor property faceCard as Card property hasPlayed as Boolean procedure play() call startTurn() let hiddenCard be property.cards[1] call hiddenCard.turnFaceUp() set property.hasPlayed to true end procedure procedure newHand() set property.hasPlayed to false call newHandHelper() set property.faceCard to property.cards[0] let hiddenCard be property.cards[1] call hiddenCard.turnFaceDown() end procedure procedure nextAction(faceCard as Card) if property.handTotal < 17 then call draw() else call stand() end if end procedure function getMessage() returns String variable msg set to "" if property.hasPlayed then set msg to statusAsString() + " - hand total: {property.handTotal}" end if return msg end function end class class HumanPlayer inherits Player constructor(name as String, startingPoints as Int) set property.name to name set property.points to startingPoints end constructor procedure newHand() call newHandHelper() end procedure procedure nextAction(dealerFaceCard as Card) variable key set to "" call clearKeyBuffer() while key is "" set key to waitForKey() if key is "d" then call draw() else if key is "s" then call stand() else set key to "" end if end while end procedure function getMessage() returns String variable msg set to statusAsString() + " - hand total: {property.handTotal}" if property.hasTurn then set msg to msg + " - press 'd' to draw, 's' to stand" end if return msg end function end class class AutomatedPlayer inherits Player constructor(name as String, decisionFunc as Func<of Player, Card => Action>, startingPoints as Int) set property.name to name set property.decisionFunc to ref decisionFunc set property.points to startingPoints end constructor property decisionFunc as Func<of Player, Card => Action> procedure newHand() call newHandHelper() end procedure procedure nextAction(dealerFaceCard as Card) let act be decisionFunc(this, dealerFaceCard) if act is Action.draw then call draw() else call stand() end if end procedure function getMessage() returns String return statusAsString() + " - hand total: {property.handTotal}" end function end class enum Action stand, draw enum Outcome undecided, lose, draw, win, winDouble enum Status active, standing, blackjack, bust enum Suit clubs, diamonds, hearts, spades constant styleSheet set to ':root {\n background-color: darkgreen;\n padding-left: 5px;\n}\n\n.game {\n padding: 5px;\n}\n\n.message, .details {\n color: white;\n font-family: Arial, Helvetica, sans-serif;\n}\n\n.hand {\n margin-top: 5px;\n height: 150px;\n padding-bottom: 10px;\n }\n \n.card {\n position: relative;\n float: left;\n background-color: white;\n width: 95px;\n height:140px;\n margin-right:10px;\n padding: 5px;\n border-radius: 5px;\n font-family: Helvetica, sans-serif; \n}\n.royal,.a,.b,.c,.d,.e,.f,.g,.h,.i,.j,.k,.l,.m,.n,.o,.p,.q,.r,.s,.t,.u,.v,.w,.x,.y,.z {position: absolute; text-align:center;}\n\n/* Standard spots */ \n .a,.b,.c,.d,.e,.f,.g,.h,.i,.l,.m,.n,.o,.p,.r,.s,.t {font-size: 30px;}\n\n /* columns */\n .d,.n,.h,.p,.f {left: 18px }\n .a,.b,.c,.l,.m,.s,.t {left: 43px;}\n .e,.o,.i,.r,.g {left: 68px}\n\n /* rows */\n .d,.b,.e {top: 0px}\n .s {top: 20px;}\n .l {top: 28px;}\n .n,.o {top: 37px;}\n .h,.a,.i {top: 57px}\n .p,.r {top: 75px;}\n .m {top: 86px;}\n .t {top: 93px;}\n .f,.c,.g {top: 114px;}\n\n/* royals */\n .royal {\n position: absolute;\n z-index: 1;\n width: 95px;\n height: 140px;\n line-height: 140px;\n font-size: 100px;\n }\n\n/* corner summary */\n .u {font-size: 15px; width: 15px; text-align: center; left: 0px; top: 2px;}\n .v {font-size: 20px; width: 15px; text-align: center; left: 0px; top: 12px;}\n\n/* suit colors */\n .red {color: red}\n .black {color: black}\n\n/* back */\n .card.reversed { background-color: rgba(0, 0, 255, 0.607);}' constant symbols set to {Suit.clubs:"&clubs;", Suit.diamonds:"&diams;", Suit.hearts:"&hearts;", Suit.spades:"&spades;"} constant rankValue set to {"2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":10, "Q":10, "K":10, "A":11} constant gridByRank set to {"A":{"royal"}, "2":{"b", "c"}, "3":{"a", "b", "c"}, "4":{"d", "e", "f", "g"}, "5":{"a", "d", "e", "f", "g"}, "6":{"d", "e", "f", "g", "h", "i"}, "7":{"d", "e", "f", "g", "h", "i", "l"}, "8":{"d", "e", "f", "g", "h", "i", "l", "m"}, "9":{"a", "d", "e", "f", "g", "n", "o", "p", "r"}, "10":{"d", "e", "f", "g", "n", "o", "p", "r", "s", "t"}, "J":{"royal"}, "Q":{"royal"}, "K":{"royal"}} constant colours set to {Suit.clubs:"black", Suit.diamonds:"red", Suit.hearts:"red", Suit.spades:"black"}
and immediately use file > auto save the code to your local file storage: because you will be making changes and want to ensure that your changes are saved.

When you run the program, the display tab will be shown in place of this worksheet, you can flip between them by clicking on the tab headings.

So, run the program now, either:

- using the mouse, by clicking on the run button at the top of the screen, or
- using the keyboard, by pressing Ctrl+r

and play exactly 10 rounds of Blackjack.

Make sure that you have grasped the following points:

- Most cards have a obvious numeric value. The Royal cards - Jack, Queen, King - each have the value 10. An Ace is special: it can be valued at 11, or at 1. An Ace valued at 11 is known as a 'soft Ace'. If a dealt card takes the hand total over 21 then a soft Ace is automatically devalued to 1.
- The aim is to get the value of your hand as close to 21 as you can - but if drawing a card causes the total to go over 21 then (unless you have a 'soft Ace') you will be 'BUST' and your turn is over.
- If the first two cards of any player total exactly 21 - which would require one Ace, and one Royal or 10 card - then you have 'BLACKJACK' - the best hand possible - so no further action is needed from you.
- When all hands have finished playing, the outcome is determined, points swapped as follows:
  - If a player is BUST then 1 point is passed from them to the dealer even the dealer is also BUST.
  - If a player has BLACKJACK then they gain 2 points from the dealer unless the dealer also has BLACKJACK, in which case it is a draw.
  - else, if the player is STANDING and the dealer has BLACKJACK the dealer gains one point from the player.
  - else, if the player is STANDING and their hand total is greater than the dealer's, the player gains one point from the dealer.
  - else, if the player is STANDING and their hand total is the same as the dealer's, it is a draw.
  - else, if the player's total is less than the dealer's, the dealer gains one point from the player.
- This program uses deck of multiple packs of cards: so there is a small chance that the same card may appear on screen more than once.

After playing 10 rounds, how many points did you, and the dealer, end up with?

If you have not already exited the program, click stop.

#### Notes

Step Completed Total hints used: /

# Step 2: start to explore the code

Watch the next video:

How many instructions are there in total within the Blackjack program (just find the largest instruction number)?

The initial keyword in an instruction defines the type of instruction. Find and list here at least five initial keywords not mentioned explicitly in the video:

#### Notes

Step Completed Total hints used: /

# Step 3: the different elements within instructions

Watch the next video:

List below the different colours that you can see being used within the code (they are all shown within the first 15 instructions):

What is the significance of the colour dark-blue, and black?

#### Notes

Step Completed Total hints used: /

# Step 4: comments

Watch the next video:

Scrolling through the code, find two comments, not including the one at the top showing the Elan version, and write down the instruction number immediately below each comment:

#### Notes

Step Completed Total hints used: /

# Step 5: identifiers

Watch the next video:

Find five identifiers used in the program - not including any that were mentioned in the video - and list them here:

#### Notes

Step Completed Total hints used: /

# Step 6: literals

Watch the next video:

Find an example of a literal number, and a literal string - not including any specifically mentioned in the video - and note their instruction numbers below:

#### Notes

Step Completed Total hints used: /

# Step 7: methods

Watch the next video:

Find five method names - not including any specifically mentioned in the video - and write them here:

#### Notes

Step Completed Total hints used: /

# Step 8: types

Which of the types mentioned in the video are defined within the standard Elan language (if necessary, watch the video again)?

#### Notes

Step Completed Total hints used: /

# Step 9: selecting simple and compound instructions

Watch the next video:

List several different examples of compound instructions

Can you select the 'header comment' (beginning '# Elan') at the top of the code?

#### Notes

Step Completed Total hints used: /

# Step 10: adding a new instruction

Watch the next video:

Your first task is to turn the program into a two-player game (not counting the dealer, played by the computer):

- Near the top of main, find this instruction: call game.addPlayer procedureName?(new HumanPlayer("Player A", 1000)arguments?)0 and immediately below it insert a new call instruction.
- Into the two editable fields type in the same code that you see in the instruction immediately above it, except that instead of "Player A", write "Player B". Note that:
  - your text will initially be black, but it will be coloured once you leave the field
  - a drop-down list will offer you some 'auto-completion' options, which can save typing and errors. You can select an option with the mouse or with the cursor ↑↓ keys and Enter.
  - You might see a message appear on the right, but you can ignore it for now, unless it is coloured red (you will also then see the word invalid or error appear) in which case you should Backspace to correct the mistake.
- When you have completed the new instruction, run the program, and take responsibility for both Player A and Player B (or, if you are pairing with another person, control one each).

When does the dealer play, now ?

#### Notes

Step Completed Total hints used: /

# Step 11: modifying an existing instruction

- Go back to the instruction that creates Player A and above itinsert a let instruction and note that this features two editable fields.
- In the first field enter name1 and in the second field type in the following code using the auto-completions to help inputString("Enter name for first player")

  What message do you see within the code editor?

  And what do you observe in the 'status panel' - at the top of the screen, just to the right of the code editor?

  Then revert the field back to name2.
- Select the instruction below this - in other words the one that you added previously - and Tab to the second field. Edit the contents, replacing the literal string "Player A" with name2 (no quotation marks because this is an identifier that holds a string, not a literal string).
- Run the program. When asked, type in your own first name, finishing with the Enter key.

  Has the name you entered made a difference?

  There is an unwanted 'side effect' on the screen - what is it?

- To fix the unwanted side effect, insert the following new instruction immediatelyafter the second player is created and added to the game: call clearPrintedText procedureName?(arguments?)0
- Now change the program so that it asks the user to enter a name for the second player, this time using the identifier name2.
- Run the program again, and confirm that you can now name both players.

What would happen if you define the second identifier as name2, but in the next line you were to write name1? (If you need to check, run the program)

And what if the the second identifier was still name2 but you used name3 in the following instruction? This time, can you even run the program to try it?

#### Notes

Step Completed Total hints used: /

# Congratulations! You have completed this worksheet

You have learned:

- How to load, run, and stop a program
- How to recognise the different kinds of instructions that make up a program, including compound instructions, which contain other instructions.
- How to navigate around the program, selecting different instructions in order to draw attention to them and as a prelude to modifying them.
- How to insert new instructions, delete instructions, and to undo changes when they didn't do what you wanted.

In the next worksheet - Blackjack 2 - you'll start to do some real programming, by defining and implementing the logic for a fully-automated Blackjack player - and then in the Blackjack 3 worksheet, pitting different automated players against each other for thousands of games, and using the outcome to refine the strategies.

In the meantime, if you have time, we recommend that you watch the following additional videos - that offer further techniques for navigating and editing instructions. Practice the techniques shown in each video, either while it is running, or immediately afterwards.

All the techniques for navigating and modifying instructions may be looked up in the Help tab. For example: show in Help