top of page

Goblin Hunt >>> Lesson 4: Refining our game


Your code should look like this so far. Programming languages (unlike spoken ones!) are very strict with grammar and punctuation. So make sure you haven't missed out any bracket, or colon etc.


We already have a game that works but there are improvements we can make. Let us change what the player sees after they make a guess. Currently the player will see a message informing them whether they have won or lost. Let us reveal the open cupboard door, based on the player's choice, showing either the goblin in the cupboard or an empty cupboard depending on whether the player found the goblin or not. Let us re-write the if...else statements as follows:

if chosen_position == goblin_position:

print("Well done, your win")

print(cupboard_door*2 + "|O|" + cupboard_door*2)

else:

print("Sorry. You lose! Better luck next time.")

We have added a second print() function in the case when the player wins. Let's look at this in detail. Inside the brackets there are three strings we are adding together, separated by the + sign. Recall that for now, we have put the goblin in the third of five cupboards. The player guessed correctly so we want to show the third cupboard door open, showing the goblin, open-mouthed, inside! This is the "|O|" string in the middle of the print() function. The other cupboards, the first two and the last two remain closed. The first cupboard_door*2 in the print() function displays the first two closed doors and the second occurrence of cupboard_door*2 shows the fourth and fifth.

Why is this a very bad way of doing things?

What if we had to hide the goblin in the second cupboard now, or any other one for that matter? The way we have written our code so far is specific for the goblin being in the third position. We should never do this when programming. Instead we want to replace the two occurrences of *2 in the print() function with something that is robust enough so that it works wherever the goblin is. Here's an alternative (don't miss any brackets out! But you can keep writing the long print() line in a single line if you wish):

if chosen_position == goblin_position:

print("Well done, your win")

print(cupboard_door*(goblin_position-1) + "|O|"

+ cupboard_door*(nu_of_doors-goblin_position))

else:

print("Sorry. You lose! Better luck next time.")

Whichever position the goblin is in, we want to display one fewer cupboard doors that remain closed, which is why we replaced the first *2 with *(goblin_position-1). And the number of closed cupboard doors after the one containing the goblin will always be (nu_of_doors-goblin_position). This now works for any goblin position. Try it out by changing the value of the goblin_position variable. [An astute student of this micro-course asked why there are no spaces before and after * in these lines of code whereas we put spaces around * earlier in the program. These spaces, and those around other signs such as +, = and ==, are optional and you can include them if you feel they make the code more readable. However please note that the indentation of the lines after the if and else statements is not optional, and must always be the same]

We can do something very similar for the case when the player does not find the goblin. In this case we want to show that the door the player has opened is empty. We can use this 'picture' to illustrate this: "|^|":

if chosen_position == goblin_position:

print("Well done, your win")

print(cupboard_door*(goblin_position-1) + "|O|"

+ cupboard_door*(nu_of_doors-goblin_position))

else:

print("Sorry. You lose! Better luck next time.")

print(cupboard_door*(chosen_position-1) + "|^|"

+ cupboard_door*(nu_of_doors-chosen_position))

Try running the program a few times with different guesses, and also change the value of goblin_position in the program (for now only choose values between 1 and 5 as we only have 5 doors.) This will help you make sure your program works as expected.

In the final lesson we will complete the game by randomising the position of the goblin to make the game more fun. Please email me on info@codetoday.co.uk if you get stuck or have any other questions. Feel free to copy and paste your program in the email.

18 views

Recent Posts

See All

Python Coding for Young People

CT-Unlimited-FinalRAW-transparent.png

Codetoday Unlimited is for the curious teenager or preteen keen to learn proper Python coding. Stephen's courses start from the basics and carry on to intermediate and advanced levels.

Python Coding for Adults

The Python Coding Place is Stephen's platform full of courses and other resources for beginners and intermediate learners. The focus is on clarity and Stephen's unique communication style.

bottom of page