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

Function makeMove

src/gamesbyexample/checkers.py:243–281  ·  view source on GitHub ↗

Carry out the move and return a new board data structure.

(board, srcSpace, dstSpace)

Source from the content-addressed store, hash-verified

241
242
243def makeMove(board, srcSpace, dstSpace):
244 """Carry out the move and return a new board data structure."""
245 board = copy.copy(board) # We'll modify a copy of the board object.
246 srcColumn, srcRow = srcSpace[0], int(srcSpace[1])
247 dstColumn, dstRow = dstSpace[0], int(dstSpace[1])
248
249 if abs(srcRow - dstRow) >= 1:
250 # The checker is making a normal or jump move:
251 board[dstSpace] = board[srcSpace]
252 board[srcSpace] = EMPTY
253
254 if abs(srcRow - dstRow) == 2:
255 # Erase the checker that was captured in the jump:
256 if dstColumn < srcColumn and dstRow < srcRow:
257 board[prevCol(srcColumn) + str(srcRow - 1)] = EMPTY
258 elif dstColumn < srcColumn and dstRow > srcRow:
259 board[prevCol(srcColumn) + str(srcRow + 1)] = EMPTY
260 elif dstColumn > srcColumn and dstRow < srcRow:
261 board[nextCol(srcColumn) + str(srcRow - 1)] = EMPTY
262 elif dstColumn > srcColumn and dstRow > srcRow:
263 board[nextCol(srcColumn) + str(srcRow + 1)] = EMPTY
264
265 # See if we need to promote this checker:
266 if board[dstSpace].islower() and (dstRow == 1 or dstRow == 8):
267 print(board[dstSpace].upper(), 'has been promoted!')
268 board[dstSpace] = board[dstSpace].upper() # Promote checker.
269
270 # See if this checker can do another jump after jumping:
271 dstMoves, dstCaptures = getPossibleDstMoves(board, dstSpace)
272 if dstCaptures != [] and abs(srcRow - dstRow) == 2:
273 displayBoard(board)
274 while True: # Keep asking until valid input is entered.
275 print('Enter the double jump to make:')
276 print(' '.join(dstCaptures))
277 doubleJumpMove = input('> ').upper().strip()
278 if doubleJumpMove in dstCaptures:
279 break # Exit loop when a valid space is entered.
280 return makeMove(board, dstSpace, doubleJumpMove)
281 return board
282
283
284def hasLost(board, player):

Callers 1

mainFunction · 0.70

Calls 4

prevColFunction · 0.70
nextColFunction · 0.70
getPossibleDstMovesFunction · 0.70
displayBoardFunction · 0.70

Tested by

no test coverage detected