You are on page 1of 4

Page 1 of 4

CENG1004 – Introduction to Programming


Spring 2023
Homework#1
Due Date: 30.03.2023 23:59
Write a Python program named TreasureHunting that plays the game of finding a treasure
hidden at a location on the 2D grid, where the coordinates are integers between 1 and 10,
where the player can make at most ten guesses.

• Import the random module to generate random integer numbers.


random.randint(a, b) returns a random integer N such that a <= N <= b.
For example:

import random
# generates random integer number between 1 and 100
x = random.randint(1,100)
print(x)

Functions:
• Define a function absolute(n) that returns the absolute value of n.
• Define a function get_location() that generates a random integer number
between 1 and 10, inclusive, and returns it.
• Define a function get_distance(x, y, guess_x, guess_y) that takes four
parameters: x, y, guess_x and guess_y. The function calculates the Manhattan
distance between two points: (x, y) and (guess_x, guess_y) by using the
absolute() function. The function returns the calculated distance.
𝑀𝑎𝑛ℎ𝑎𝑡𝑡𝑎𝑛 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 = |𝑥1 − 𝑥2 | + |𝑦1 − 𝑦2 |

• Define a function get_advice(distance) that takes a parameter distance.


o If distance is 0, the function returns the string "You have found the treasure!".
o If distance is less than or equal to 3, the function returns the string "The
treasure is very close.".
o If distance is less than or equal to 6, the function returns the string "The
treasure is somewhat close.".
o Otherwise, the function returns the string "The treasure is not close.".
Page 2 of 4

Instructions:
• Generate the location of the treasure by calling the get_location() function for x and y
coordinates.

• Write a while loop that continues until number of guesses is less than 10. During each
iteration of the loop, do the following:

o Ask the user to enter their guess for the x and y coordinates of the treasure.
o Compute the Manhattan distance between the user's guess and the true
location of the treasure by calling the get_distance() function and passing in
the appropriate parameters.
o Provide advice to the user based on the calculated distance by calling the
get_advice() function and passing in the calculated distance.
o If the user has not found the treasure, print the remaining number of guesses.
o If the user has found the treasure, end the game by breaking out of the while
loop.

• If the user fails to find the treasure within 10 guesses, print a message that reveals the
true location of the treasure.

Make sure that after the user has correctly found the location, the execution of the loop-body
will skip even if there are more rounds remaining.

Sample Output:

Enter x coordinate (between 1 and 10): 7


Enter y coordinate (between 1 and 10): 3
The treasure is somewhat close.
>> remaining guess: 9

Enter x coordinate (between 1 and 10): 5


Enter y coordinate (between 1 and 10): 4
The treasure is not close.
>> remaining guess: 8

Enter x coordinate (between 1 and 10): 8


Enter y coordinate (between 1 and 10): 1
The treasure is very close.
>> remaining guess: 7

Enter x coordinate (between 1 and 10): 9


Enter y coordinate (between 1 and 10): 1
You have found the treasure!
Page 3 of 4

Enter x coordinate (between 1 and 10): 1


Enter y coordinate (between 1 and 10): 5
The treasure is not close.
>> remaining guess: 9

Enter x coordinate (between 1 and 10): 9


Enter y coordinate (between 1 and 10): 4
The treasure is somewhat close.
>> remaining guess: 8

Enter x coordinate (between 1 and 10): 9


Enter y coordinate (between 1 and 10): 6
The treasure is somewhat close.
>> remaining guess: 7

Enter x coordinate (between 1 and 10): 9


Enter y coordinate (between 1 and 10): 8
The treasure is not close.
>> remaining guess: 6

Enter x coordinate (between 1 and 10): 8


Enter y coordinate (between 1 and 10): 3
The treasure is very close.
>> remaining guess: 5

Enter x coordinate (between 1 and 10): 8


Enter y coordinate (between 1 and 10): 2
The treasure is very close.
>> remaining guess: 4

Enter x coordinate (between 1 and 10): 8


Enter y coordinate (between 1 and 10): 4
The treasure is very close.
>> remaining guess: 3

Enter x coordinate (between 1 and 10): 7


Enter y coordinate (between 1 and 10): 6
The treasure is somewhat close.
>> remaining guess: 2

Enter x coordinate (between 1 and 10): 7


Enter y coordinate (between 1 and 10): 3
The treasure is very close.
>> remaining guess: 1

Enter x coordinate (between 1 and 10): 7


Enter y coordinate (between 1 and 10): 2
The treasure is very close.
>> remaining guess: 0

Sorry, you did not find the treasure. The true location was 8 , 1
Page 4 of 4

Notes:

• The program should use proper indentation and include comments to make the code
more readable and understandable.
• Late submissions will not be allowed!
• All work on homeworks must be done individually.
Submit:

• You must submit your homeworks via the online learning system. (Homeworks sent by
e-mail will not be accepted.)
• Name your file as HW_homeworkNo_StudentId_Name_Surname.ipynb or
HW_homeworkNo_StudentId_Name_Surname.py

Colab >> File >> Download >> Choose [Download .ipynb] or [Download .py] as shown
the following:

Lack of proper application of the above guidelines may result loss of points.

You might also like