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

Function makeMove

src/gamesbyexample/mancala.py:141–177  ·  view source on GitHub ↗

Modify the board data structure so that the player 1 or 2 in turn selected pit as their pit to sow seeds from. Returns either '1' or '2' for whose turn it is next.

(board, playerTurn, pit)

Source from the content-addressed store, hash-verified

139
140
141def makeMove(board, playerTurn, pit):
142 """Modify the board data structure so that the player 1 or 2 in
143 turn selected pit as their pit to sow seeds from. Returns either
144 '1' or '2' for whose turn it is next."""
145
146 seedsToSow = board[pit] # Get number of seeds from selected pit.
147 board[pit] = 0 # Empty out the selected pit.
148
149 while seedsToSow > 0: # Continue sowing until we have no more seeds.
150 pit = NEXT_PIT[pit] # Move on to the next pit.
151 if (playerTurn == '1' and pit == '2') or (
152 playerTurn == '2' and pit == '1'
153 ):
154 continue # Skip opponent's store.
155 board[pit] += 1
156 seedsToSow -= 1
157
158 # If the last seed went into the player's store, they go again.
159 if (pit == playerTurn == '1') or (pit == playerTurn == '2'):
160 # The last seed landed in the player's store; take another turn.
161 return playerTurn
162
163 # Check if last seed was in an empty pit; take opposite pit's seeds.
164 if playerTurn == '1' and pit in PLAYER_1_PITS and board[pit] == 1:
165 oppositePit = OPPOSITE_PIT[pit]
166 board['1'] += board[oppositePit]
167 board[oppositePit] = 0
168 elif playerTurn == '2' and pit in PLAYER_2_PITS and board[pit] == 1:
169 oppositePit = OPPOSITE_PIT[pit]
170 board['2'] += board[oppositePit]
171 board[oppositePit] = 0
172
173 # Return the other player as the next player:
174 if playerTurn == '1':
175 return '2'
176 elif playerTurn == '2':
177 return '1'
178
179
180def checkForWinner(board):

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected