MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / askForPlayerMove

Function askForPlayerMove

src/gamesbyexample/hungryrobots.py:140–181  ·  view source on GitHub ↗

Returns the (x, y) integer tuple of the place the player moves next, given their current location and the walls of the board.

(board, robots, playerPosition)

Source from the content-addressed store, hash-verified

138
139
140def askForPlayerMove(board, robots, playerPosition):
141 """Returns the (x, y) integer tuple of the place the player moves
142 next, given their current location and the walls of the board."""
143 playerX, playerY = playerPosition
144
145 # Find which directions aren't blocked by a wall:
146 q = 'Q' if isEmpty(playerX - 1, playerY - 1, board, robots) else ' '
147 w = 'W' if isEmpty(playerX + 0, playerY - 1, board, robots) else ' '
148 e = 'E' if isEmpty(playerX + 1, playerY - 1, board, robots) else ' '
149 d = 'D' if isEmpty(playerX + 1, playerY + 0, board, robots) else ' '
150 c = 'C' if isEmpty(playerX + 1, playerY + 1, board, robots) else ' '
151 x = 'X' if isEmpty(playerX + 0, playerY + 1, board, robots) else ' '
152 z = 'Z' if isEmpty(playerX - 1, playerY + 1, board, robots) else ' '
153 a = 'A' if isEmpty(playerX - 1, playerY + 0, board, robots) else ' '
154 allMoves = (q + w + e + d + c + x + a + z + 'S')
155
156 while True:
157 # Get player's move:
158 print('(T)eleports remaining: {}'.format(board["teleports"]))
159 print(' ({}) ({}) ({})'.format(q, w, e))
160 print(' ({}) (S) ({})'.format(a, d))
161 print('Enter move or QUIT: ({}) ({}) ({})'.format(z, x, c))
162
163 move = input('> ').upper()
164 if move == 'QUIT':
165 print('Thanks for playing!')
166 sys.exit()
167 elif move == 'T' and board['teleports'] > 0:
168 # Teleport the player to a random empty space:
169 board['teleports'] -= 1
170 return getRandomEmptySpace(board, robots)
171 elif move != '' and move in allMoves:
172 # Return the new player position based on their move:
173 return {'Q': (playerX - 1, playerY - 1),
174 'W': (playerX + 0, playerY - 1),
175 'E': (playerX + 1, playerY - 1),
176 'D': (playerX + 1, playerY + 0),
177 'C': (playerX + 1, playerY + 1),
178 'X': (playerX + 0, playerY + 1),
179 'Z': (playerX - 1, playerY + 1),
180 'A': (playerX - 1, playerY + 0),
181 'S': (playerX, playerY)}[move]
182
183
184def moveRobots(board, robotPositions, playerPosition):

Callers 1

mainFunction · 0.70

Calls 2

isEmptyFunction · 0.85
getRandomEmptySpaceFunction · 0.85

Tested by

no test coverage detected