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

Function askForPlayerMove

src/gamesbyexample/ultimatetictactoe.py:168–207  ·  view source on GitHub ↗

Asks the player which space on which small board to move on. The focusX and focusY values determine which small board the player can move on, but if they are both None the player can freely choose a small board. Returns the (x, y) of the small board the next player plays on.

(player, board, focusX, focusY)

Source from the content-addressed store, hash-verified

166
167
168def askForPlayerMove(player, board, focusX, focusY):
169 """Asks the player which space on which small board to move on.
170 The focusX and focusY values determine which small board the player
171 can move on, but if they are both None the player can freely choose
172 a small board. Returns the (x, y) of the small board the next player
173 plays on.
174 """
175 # Check if the player can freely select any small board:
176 if focusX is None and focusY is None:
177 # Let the player pick which board they want to move on:
178 print(player + ': Enter the BOARD you want to move on.')
179 validBoardsToSelect = []
180 for xyTuple, smallBoard in board.items():
181 if getWinner(smallBoard) is None:
182 validBoardsToSelect.append(xyTuple)
183 selectedBoard = enter1Through9(validBoardsToSelect)
184 focusX = selectedBoard % 3
185 focusY = selectedBoard // 3
186
187 # Select the space on the focused small board:
188 smallXDesc = ['left', 'middle', 'right'][focusX]
189 smallYDesc = ['top', 'middle', 'bottom'][focusY]
190 print(player, 'moves on the', smallYDesc, smallXDesc, 'board.')
191 validSpacesToSelect = []
192 for xyTuple, tile in board[(focusX, focusY)].items():
193 if tile == EMPTY_SPACE:
194 validSpacesToSelect.append(xyTuple)
195 selectedSpace = enter1Through9(validSpacesToSelect)
196 x = selectedSpace % 3
197 y = selectedSpace // 3
198
199 board[(focusX, focusY)][(x, y)] = player
200
201 # Figure out the small board that the next player must move on:
202 if getWinner(board[x, y]) is None:
203 return (x, y)
204 else:
205 # If the small board has a winner or is tied, the next player
206 # can move on any small board:
207 return (None, None)
208
209
210def enter1Through9(validMoves):

Callers 1

mainFunction · 0.70

Calls 2

getWinnerFunction · 0.85
enter1Through9Function · 0.85

Tested by

no test coverage detected