Evil Alien

Somewhere beneath you, in deepest, blackest space, works Elron, the Evil Alien. You’ve managed to deactivate all but his short-range weapons, but he can still make his ship invisible. You know he’s somewhere within the three-dimensional grid you can see on your ship screen, but where?

You have four space bombs. Each one can be exploded at a position in the grid specified by three numbers between 0 and 9. Can you blast the Evil Elron out of space before he creeps up and captures you?

The code

# Import:
# * the `random` module so we can generate random numbers,
from random import randint

print("EVIL ALIEN")

# set the size of the grid
size = 10

# set the size of the goes
goes = 4

# Create a flag to keep track if we've won
won_game = False

# Elron's position is fixed by these lines, 
# which select 3 numbers between 0 and the size of the grid
x = randint(0, size)
y = randint(0, size)
d = randint(0, size)

# start of a loop which tells the computer to repeat the 
# next lines G times
for i in range(goes):
    
    # this section asks you for your 3 numbers and stores them in x1, y1 and d1
    x1 = int(input("X position (0 to 9): "))
    y1 = int(input("Y position (0 to 9): "))
    d1 = int(input("D position (0 to 9): "))
    
    # checks if you were right and jumps to 300 if you were
    if x== x1 and y == y1 and d == d1:
        won_game = True
        break
    
    # your guesses are compared with Elron's position and a report printed
    print('SHOT WAS ')
    if y1 > y:
        print('NORTH')
    if y1 < y:
        print('SOUTH')
    if x1 > x:
        print('EAST')
    if x1 < x:
        print('WEST')

    print()
    
    if d1 > x:
        print('TOO FAR')
    if d1 < x:
        print('NOT FAR ENOUGH')
    # end of loop. returns for another go until 'goes' remain.
    
# prints if you've used up all your goes
if not won_game:
    print('YOUR TIME HAS RUN OUT!!')
else:
    print('*BOOM* YOU GOT HIM!')

How to make the game harder

This program has been written so that you can easily change the difficulty by changing the size of the grid. To do this, put a different value for s in line 10.

If you increase the grid size you will need more space bombs to give you a fair chance of blasting Elron. Do this by changing the value of goes in line 14.

Puzzle corner

Can you work out how to change the program so that the computer asks you for a difficulty number which it can out into size instead of size being fixed? (Tip: limit the value of size to between 6 and 30 and use int(size//3) for the value of goes in line 14.)