-
Notifications
You must be signed in to change notification settings - Fork 0
/
SONAR.py
217 lines (176 loc) · 7.52 KB
/
SONAR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Sonar
import random
import sys
def drawBoard(board):
# Draw the board data structure.
hline = ' ' # initial space for the numbers down the left side of the board
for i in range(1, 6):
hline += ' ' * 9 + str(i)
# print the numbers across the top
print hline
print ' ' + '0123456789' * 6
print
# print each of the 15 rows
for i in range(15):
# single-digit numbers need to be padded with an extra space
if i < 10:
extraSpace = ' '
else:
extraSpace = ''
print extraspace,i," ",getRow(board,i)," ",i
# print the numbers across the bottom
print
print ' ' + '0123456789' * 6
print hline
def getRow(board, row):
# Return a string from the board data structure at a certain row.
boardRow = ''
for i in range(60):
boardRow += board[i][row]
return boardRow
def getNewBoard():
# Create a new 60x15 board data structure.
board = []
for x in range(60): # the main list is a list of 60 lists
board.append([])
for y in range(15): # each list in the main list has 15 single-character strings
# use different characters for the ocean to make it more readable.
if random.randint(0, 1) == 0:
board[x].append('~')
else:
board[x].append('`')
return board
def getRandomChests(numChests):
# Create a list of chest data structures (two-item lists of x, y int coordinates)
chests = []
for i in range(numChests):
chests.append([random.randint(0, 59), random.randint(0, 14)])
return chests
def isValidMove(x, y):
# Return True if the coordinates are on the board, otherwise False.
return x >= 0 and x <= 59 and y >= 0 and y <= 14
def makeMove(board, chests, x, y):
# Change the board data structure with a sonar device character. Remove treasure chests
# from the chests list as they are found. Return False if this is an invalid move.
# Otherwise, return the string of the result of this move.
if not isValidMove(x, y):
return False
smallestDistance = 100 # any chest will be closer than 100.
for cx, cy in chests:
if abs(cx - x) > abs(cy - y):
distance = abs(cx - x)
else:
distance = abs(cy - y)
if distance < smallestDistance: # we want the closest treasure chest.
smallestDistance = distance
if smallestDistance == 0:
# xy is directly on a treasure chest!
chests.remove([x, y])
return 'You have found a sunken treasure chest!'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return "Treasure detected at a distance of "+str(smallestDistance)+" from the sonar device."
else:
board[x][y] = 'O'
return 'Sonar did not detect anything. All treasure chests out of range.'
def enterPlayerMove():
# Let the player type in her move. Return a two-item list of int xy coordinates.
print "Where do you want to drop the next sonar device? (0-59 0-14) (or type quit)"
while True:
move = input()
if move.lower() == 'quit':
print "Thanks for playing!"
sys.exit()
move = move.split()
if len(move) == 2 and move[0].isdigit() and move[1].isdigit() and isValidMove(int(move[0]), int(move[1])):
return [int(move[0]), int(move[1])]
print "Enter a number from 0 to 59, a space, then a number from 0 to 14."
def playAgain():
# This function returns True if the player wants to play again, otherwise it returns False.
print "Do you want to play again? (yes or no)"
return input().lower().startswith('y')
def showInstructions():
print "Instructions:"
print " You are the captain of the Simon, a treasure-hunting ship. Your current mission"
print " is to find the three sunken treasure chests that are lurking in the part of the"
print " ocean you are in and collect them."
print " To play, enter the coordinates of the point in the ocean you wish to drop a"
print " sonar device. The sonar can find out how far away the closest chest is to it."
print " For example, the d below marks where the device was dropped, and the 2's"
print " represent distances of 2 away from the device. The 4's represent"
print " distances of 4 away from the device."
print " 444444444"
print " 4 4"
print " 4 22222 4"
print " 4 2 2 4"
print " 4 2 d 2 4"
print " 4 2 2 4"
print " 4 22222 4"
print " 4 4"
print " 444444444"
print " Press enter to continue..."
raw_input()
print "For example, here is a treasure chest (the c) located a distance of 2 away"
print "from the sonar device (the d):"
print "22222"
print "c 2"
print "2 d 2"
print "2 2"
print "22222"
print "The point where the device was dropped will be marked with a 2."
print "The treasure chests don't move around. Sonar devices can detect treasure"
print "chests up to a distance of 9. If all chests are out of range, the point"
print "will be marked with O"
print "If a device is directly dropped on a treasure chest, you have discovered"
print "the location of the chest, and it will be collected. The sonar device will"
print "remain there."
print "When you collect a chest, all sonar devices will update to locate the next"
print "closest sunken treasure chest."
print "Press enter to continue..."
raw_input()
print
print "S O N A R !"
print
print "Would you like to view the instructions? (yes/no)"
if raw_input().lower().startswith('y'):
showInstructions()
while True:
# game setup
sonarDevices = 16
theBoard = getNewBoard()
theChests = getRandomChests(3)
drawBoard(theBoard)
previousMoves = []
while sonarDevices > 0:
# Start of a turn:
# sonar device/chest status
if sonarDevices > 1: extraSsonar = 's'
else: extraSsonar = ''
if len(theChests) > 1: extraSchest = 's'
else: extraSchest = ''
print "You have ",sonarDevices," sonar device",extraSsonar," left.",len(theChests)," traesure chest",extraSchest," remaining"
x, y = enterPlayerMove()
previousMoves.append([x, y]) # we must track all moves so that sonar devices can be updated.
moveResult = makeMove(theBoard, theChests, x, y)
if moveResult == False:
continue
else:
if moveResult == 'You have found a sunken treasure chest!':
# update all the sonar devices currently on the map.
for x, y in previousMoves:
makeMove(theBoard, theChests, x, y)
drawBoard(theBoard)
print(moveResult)
if len(theChests) == 0:
print "You have found all the sunken treasure chests! Congratulations and good game!"
break
sonarDevices -= 1
if sonarDevices == 0:
print "We\'ve run out of sonar devices! Now we have to turn the ship around and head"
print "for home with treasure chests still out there! Game over."
print " The remaining chests were here:"
for x, y in theChests:
print x, y
if not playAgain():
sys.exit()