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

Function checkForWinner

src/gamesbyexample/mancala.py:180–210  ·  view source on GitHub ↗

Looks at board and returns either '1' or '2' if there is a winner or 'tie' or 'no winner' if there isn't. The game ends when a player's pits are all empty; the other player claims the remaining seeds for their store. The winner is whoever has the most seeds.

(board)

Source from the content-addressed store, hash-verified

178
179
180def checkForWinner(board):
181 """Looks at board and returns either '1' or '2' if there is a
182 winner or 'tie' or 'no winner' if there isn't. The game ends when a
183 player's pits are all empty; the other player claims the remaining
184 seeds for their store. The winner is whoever has the most seeds."""
185
186 player1Total = board['A'] + board['B'] + board['C']
187 player1Total += board['D'] + board['E'] + board['F']
188 player2Total = board['G'] + board['H'] + board['I']
189 player2Total += board['J'] + board['K'] + board['L']
190
191 if player1Total == 0:
192 # Player 2 gets all the remaining seeds on their side:
193 board['2'] += player2Total
194 for pit in PLAYER_2_PITS:
195 board[pit] = 0 # Set all pits to 0.
196 elif player2Total == 0:
197 # Player 1 gets all the remaining seeds on their side:
198 board['1'] += player1Total
199 for pit in PLAYER_1_PITS:
200 board[pit] = 0 # Set all pits to 0.
201 else:
202 return 'no winner' # No one has won yet.
203
204 # Game is over, find player with largest score.
205 if board['1'] > board['2']:
206 return '1'
207 elif board['2'] > board['1']:
208 return '2'
209 else:
210 return 'tie'
211
212
213# If the program is run (instead of imported), run the game:

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected