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

Function doPlayerMove

src/gamesbyexample/barca.py:199–237  ·  view source on GitHub ↗

Ask the player for their move, and if it is valid, carry it out on the board.

(player, board)

Source from the content-addressed store, hash-verified

197
198
199def doPlayerMove(player, board):
200 """Ask the player for their move, and if it is valid, carry it out
201 on the board."""
202 validMoves = getPieceMovements(player, board)
203 assert len(validMoves) > 0
204
205 validMovesInA1 = []
206 for x, y in validMoves.keys():
207 validMovesInA1.append(xyToA1(x, y))
208 print(player + ', select piece to move (or QUIT):', ', '.join(validMovesInA1))
209 while True:
210 # Keep asking the player until they select a valid piece:
211 response = input('> ').upper()
212 if response == 'QUIT':
213 print('Thanks for playing!')
214 sys.exit()
215 if response in validMovesInA1:
216 moveFrom = A1ToXy(response)
217 break # Player has selected a valid piece.
218 print('Please select one of the given pieces.')
219
220 moveToInA1 = []
221 for x, y in validMoves[moveFrom]:
222 moveToInA1.append(xyToA1(x, y))
223 moveFromStr = getAnimalStr(board, moveFrom[0], moveFrom[1])
224 print('Select where to move this {}: {}'.format(moveFromStr, ', '.join(moveToInA1)))
225 while True:
226 # Keep asking the player until they select a valid move:
227 response = input('> ').upper()
228 if response == 'QUIT':
229 print('Thanks for playing!')
230 sys.exit()
231 if response in moveToInA1:
232 moveTo = A1ToXy(response)
233 break # Player has selected a valid space to move to.
234 print('Please select one of the given spaces.')
235
236 # Carry out the player's move:
237 movePiece(moveFrom, moveTo, board)
238
239
240def movePiece(moveFrom, moveTo, board):

Callers 1

mainFunction · 0.70

Calls 5

getPieceMovementsFunction · 0.85
xyToA1Function · 0.85
A1ToXyFunction · 0.85
getAnimalStrFunction · 0.85
movePieceFunction · 0.85

Tested by

no test coverage detected