Blackjack

In this exercise you will:

Play a few hands of Blackjack using a ready-made Elan program

Before you start this exercise, your teacher should have advised you:

Load and Run the program

From the Elan page, select File then Load and use the file-selector dialog to find the file Blackjack.elan. Your screen should then look something like this (the text might be slightly different due to version changes):

Note:

Assuming everything is as described above you will now be able to run the program. Click on the Run button - the triangular icon shown at the top of the screen.

(Note that the button next to it looks similar but with a 'bug' inside it. You will learn about how to use that later. If you pressed this button it would also run the program, but it may run slower because it is having to do more work. So always run with the plain Run button until you understand how and when to use Debug.)

Upon running notice that:

Playing Blackjack

In the program you are the only player, playing against the dealer. Each of you has been dealt an initial 'hand' of two cards. You can see both of your own cards and you can also see one of the dealer's two cards, known as the dealer's 'face card'. Alongside your hand you can see the total value of the hand.

Note:

So play out the hand by responding, each time you are asked with s or d and then pressing Enter to finish inputting the value. (You can also try entering a different character. What happens?). Continue until either you have decided to stand in which case status will change from 'playing' to 'standing' - or you go bust. When either of these occurs then after a short delay the dealer will then start playing the dealer's hand - until it either stands or goes bust. Again after a short delay the outcome of the round will be determined, and the scores updated. The following shows one possible such scenario:

Note:

The program now offers you the option to 'Enter p for play again, or x for exit'. Enter 'p' and play a total of 10 rounds. When finished, make a note of both the dealer's score and your own.

Exploring the program

Terminate the program, either by entering x when you are asked if you want to play again, or just by pressing the square Stop button at the top of the screen at any point. Then click somewhere within the code editor so that its background goes white again, indicating that it now has the browser's 'focus'.

Scrolling up to the top of the code (if necessary) you will see the word main on its own line. It might be highlighted in pale blue as shown below (indicating that this is the part of the code with the current 'focus') or that highlight might currently be elsewhere in the code - it doesn't matter for now:

+main1 call playGameprocedureName?(?)2 end main

Note:

If you click with the mouse on any keyword then the whole instruction will be highlighted in pale blue. Try doing this on the following keywords all of which should be visible without having to scroll:

Note:

Within individual instructions you will also see text in different colours. For example:

Instruction numbers

Notes:

Outlining

Find the button at the top of the screen labelled +/- and click it once to show the 'outline' view of the code - where each container instruction is collapsed to its top line, and then shown with a + symbol in front of it. Double-clicking this + will expand just that one instruction although the expanded view may contain other instructions that are still collapsed. You can expand the whole code again by clicking the +/- button at the top (you might need to click it twice if the view contains a mixture of expanded and collapsed instructions.

Create an automated Blackjack players

First two automated players

Make PlayerA always Stand when asked; PlayerB always Draw. Run a simulated game between them.

+function getActionForPlayerAname?(hand as Hand, dealerFaceUp as Cardparameter definitions?) returns ActionType?30 return Action.drawexpression?31 end function +function getActionForPlayerBname?(hand as Hand, dealerFaceUp as Cardparameter definitions?) returns ActionType?32 return Action.standexpression?33 end function

Run the simulation

+main1 call simulateGameprocedureName?(?)2 end main

Run the simulation 1000 times:

Mimic the dealer's own strategy

+function getActionForPlayerAname?(hand as Hand, dealerFaceUp as Cardparameter definitions?) returns ActionType?30 let thresholdname? be 17expression?31 return if hand.total > threshold then Action.stand
else
Action.draw
expression?
32
end function

Introduce a condition

Try varying the threshold up or down

Take account of soft-ace

+function getActionForPlayerAname?(hand as Hand, dealerFaceUp as Cardparameter definitions?) returns ActionType?28 let thresholdname? be if hand.softAce then 18
else
12
expression?
29
return if hand.total >= threshold then Action.stand
else
Action.draw
expression?
30
end function

Explore whether the dealers face card makes a difference

Work through each of the face card values and capture the scores

Use the dealer face card to vary the two thresholds

Optimise the thresholds - using machine learning

Running overnight

You have 20 values each in the range 12-20, so 180 calculations, each of 10,000 games