top of page

# Goblin Hunt >>> Lesson 5: Finishing off our game

Almost there. The game is working reasonably well now but the goblin stays in the same position. We ideally want the goblin to be in a random position each time we run the game. We therefore want to change the line that assigns a value to goblin_position to somehow assign a random value.

Now, programmers don't always remember all the commands available, nor do they know them all. What does a programmer do when she or he wants to find out how to do something? Google it. Try typing 'python random number' in Google and you will get several options for how to obtain random numbers in Python. The result we want is given in what shows up as the second entry in the list (at time of writing) on a site called stackoverflow.com - a popular site for programmers to ask other programmers for help.

So far we have used functions that are referred to as built-in functions because they are always present whenever we run Python. Python also has many libraries, or packages, that contain additional functions relating to specific topics. We will use one such package called random that provides several functions to deal with random numbers (as you might have guessed from its name.) To let Python know we need to use this package we need to add the following line at the very top of the program:

import random

We can now replace the line that is assigning a value to goblin_position with the following:

goblin_position = random.randint(1, nu_of_doors)

randint() is a function (it has brackets after it) which gives us a random number between the two numbers (or integers) listed inside the brackets, in this case between 1 and nu_of_doors which is currently 5. Because randint() is a function that belongs to the package random, we need to tell Python this and we do so by typing random.randint. Those of you who might go on to learn more programming will see this use of the dot . a lot.

Try running the game several times now. Each time the goblin will be hiding in a random position and we have a game! You can also make the output a bit better by adding \n inside strings (which means inside the quotation marks "") wherever you would like to have an empty line displayed, as we have done in an earlier lesson. Good places to do this might be at the start of the string in the input() function asking the player to guess where the goblin is, and at the start of the two results strings telling the player whether he or she won or lost. You can also change the value of nu_of_doors to make the game harder or easier.

Congratulations! You have written your first computer program. At the bottom of this post you can see the full program for your reference.

It's not perfect. Try typing in a number which is greater than the value of nu_of_doors when asked to guess the position of the goblin and you will see that the game doesn't behave as you would expect it to. We call this a bug in our program. We could extend the program to fix this and other issues if we wanted to, and add functionality by letting the player have multiple turns, keeping a score and more. But the level of complexity we have so far is sufficient for this basic, brief course.

Hope you have enjoyed this brief course. We have hardly scratched the surface of what is possible with computer programming but hopefully this micro-course has given you a taste of what programming is and how we go about constructing a program. Feel free to experiment with the code you've written. Follow us on Twitter @codetoday_ to find out more about programming and about what we offer for both children and adults.

Email me on info@codetoday.co.uk with any questions and queries on this course. Copy-paste your code in the email if you're stuck somewhere.