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

Function getPossibleDstMoves

src/gamesbyexample/checkers.py:125–180  ·  view source on GitHub ↗

Get all possible destination moves from srcSpace. Returns a tuple of two lists. The first is a list of spaces the checker at srcSpace can move to, the second is a list of spaces the checker at srcSpace jumps to after capturing a checker.

(board, srcSpace)

Source from the content-addressed store, hash-verified

123
124
125def getPossibleDstMoves(board, srcSpace):
126 """Get all possible destination moves from srcSpace.
127
128 Returns a tuple of two lists. The first is a list of spaces the
129 checker at srcSpace can move to, the second is a list of spaces
130 the checker at srcSpace jumps to after capturing a checker."""
131 assert board.get(srcSpace) != EMPTY
132
133 checker = board[srcSpace] # The checker at srcSpace.
134 possibleDstMoves = [] # Possible places the checker can move to.
135 possibleDstCaptures = [] # Possible places the checker can jump to.
136
137 # Set up variables for various spaces adjacent/near srcSpace:
138 column = srcSpace[0] # Columns are letters.
139 row = int(srcSpace[1]) # E.g. convert string '1' to int 1.
140 downLeftSpace = prevCol(column) + str(row + 1)
141 downRightSpace = nextCol(column) + str(row + 1)
142 upLeftSpace = prevCol(column) + str(row - 1)
143 upRightSpace = nextCol(column) + str(row - 1)
144 down2Left2Space = prevCol(prevCol(column)) + str(row + 2)
145 down2Right2Space = nextCol(nextCol(column)) + str(row + 2)
146 up2Left2Space = prevCol(prevCol(column)) + str(row - 2)
147 up2Right2Space = nextCol(nextCol(column)) + str(row - 2)
148
149 # See where the checker at this space can move:
150 if checker in ('x', 'X', 'O'): # x, X, and O can move down.
151 # Check the two spaces below this checker:
152 if board.get(downLeftSpace) == EMPTY:
153 possibleDstMoves.append(downLeftSpace)
154 if board.get(downRightSpace) == EMPTY:
155 possibleDstMoves.append(downRightSpace)
156
157 # Check the two spaces below this checker if you can capture:
158 if ((board.get(downLeftSpace) in otherCheckers(checker))
159 and (board.get(down2Left2Space) == EMPTY)):
160 possibleDstCaptures.append(down2Left2Space)
161 if ((board.get(downRightSpace) in otherCheckers(checker))
162 and (board.get(down2Right2Space) == EMPTY)):
163 possibleDstCaptures.append(down2Right2Space)
164
165 if checker in ('o', 'X', 'O'): # o, X, and O can move up.
166 # Check the two spaces above this checker:
167 if board.get(upLeftSpace) == EMPTY:
168 possibleDstMoves.append(upLeftSpace)
169 if board.get(upRightSpace) == EMPTY:
170 possibleDstMoves.append(upRightSpace)
171
172 # Check the two spaces below this checker if you can capture:
173 if ((board.get(upLeftSpace) in otherCheckers(checker))
174 and (board.get(up2Left2Space) == EMPTY)):
175 possibleDstCaptures.append(up2Left2Space)
176 if ((board.get(upRightSpace) in otherCheckers(checker))
177 and (board.get(up2Right2Space) == EMPTY)):
178 possibleDstCaptures.append(up2Right2Space)
179
180 return (possibleDstMoves, possibleDstCaptures)
181
182

Callers 2

askForPlayerMoveFunction · 0.70
makeMoveFunction · 0.70

Calls 3

prevColFunction · 0.70
nextColFunction · 0.70
otherCheckersFunction · 0.70

Tested by

no test coverage detected