#---------------------------------------------------------------------- # Today: we chase hurkles! This is a variant of an old text-mode # computer game from the 1970s. A hurkle is a happy beast, that hides # somewhere in a grid of rows and columns, and the player has a small # number of guesses to figure out where the hurkle is hiding. When a # guess is incorrect, the hurkle will tell the player which direction # to go (North, NorthWest, West, SouthWest, etc.). The game stops when # either the hurkle has been found or the player runs out of guesses. # # This version copyright (C) February 2020, Dr. William T. Verts #---------------------------------------------------------------------- import random,math Size = 20 # Size of the playing area is Size x Size cells Guesses = int(math.log(Size, 2)) + 1 # Number of guesses dependent on playing area size #---------------------------------------------------------------------- # Get an integer from the user that is between 0 and Size-1 (inclusive) # and also prevent illegal data entries from crashing the program. #---------------------------------------------------------------------- def GetInput (Message): # Message is a string to be printed N = -1 while (N < 0) or (N >= Size): try: N = int(input(Message)) except: N = -1 return N #---------------------------------------------------------------------- # Main game play. This entire block of code should probably be inside # its own function such as Main() #---------------------------------------------------------------------- print ("The hurkle is on a ", Size, " by ", Size, " grid.") print ("Enter values between 0 and ", Size-1) print ("You have ", Guesses, " guesses") HurkleR = random.randrange(Size) HurkleC = random.randrange(Size) # Uncomment the next "cheat" line when debugging the code. #print ("The hurkle is at ", HurkleR, HurkleC) Found = False Guess = 0 while (Guess < Guesses) and not Found: # Go to the next guess Guess = Guess + 1 print ("Enter Guess #", Guess) # Enter a guess R = GetInput("Enter the row --- ") C = GetInput("Enter the column --- ") # Check to see if the hurkle has been found. # If not, tell the user which way to go. if (HurkleR < R) and (HurkleC < C): print ("Go NorthWest") elif (HurkleR < R) and (HurkleC == C): print ("Go North") elif (HurkleR < R) and (HurkleC > C): print ("Go NorthEast") elif (HurkleR == R) and (HurkleC < C): print ("Go West") elif (HurkleR == R) and (HurkleC > C): print ("Go East") elif (HurkleR > R) and (HurkleC < C): print ("Go SouthWest") elif (HurkleR > R) and (HurkleC == C): print ("Go South") elif (HurkleR > R) and (HurkleC > C): print ("Go SouthEast") else: Found = True if Found: print ("You found me!") else: print ("I got away!") print ("I was at ", HurkleR, HurkleC)