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

Function main

src/gamesbyexample/mancala.py:29–70  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

27
28
29def main():
30 print('''Mancala, by Al Sweigart al@inventwithpython.com
31
32The ancient two-player, seed-sowing game. Grab the seeds from a pit on
33your side and place one in each following pit, going counterclockwise
34and skipping your opponent's store. If your last seed lands in an empty
35pit of yours, move the opposite pit's seeds into your store. The
36goal is to get the most seeds in your store on the side of the board.
37If the last placed seed is in your store, you get a free turn.
38
39The game ends when all of one player's pits are empty. The other player
40claims the remaining seeds for their store, and the winner is the one
41with the most seeds.
42
43More info at https://en.wikipedia.org/wiki/Mancala
44''')
45 input('Press Enter to begin...')
46
47 gameBoard = getNewBoard()
48 playerTurn = '1' # Player 1 goes first.
49
50 while True: # Run a player's turn.
51 # "Clear" the screen by printing many newlines, so the old
52 # board isn't visible anymore.
53 print('\n' * 60)
54 # Display board and get the player's move:
55 displayBoard(gameBoard)
56 playerMove = askForPlayerMove(playerTurn, gameBoard)
57
58 # Carry out the player's move:
59 playerTurn = makeMove(gameBoard, playerTurn, playerMove)
60
61 # Check if the game ended and a player has won:
62 winner = checkForWinner(gameBoard)
63 if winner == '1' or winner == '2':
64 displayBoard(gameBoard) # Display the board one last time.
65 print('Player ' + winner + ' has won!')
66 sys.exit()
67 elif winner == 'tie':
68 displayBoard(gameBoard) # Display the board one last time.
69 print('There is a tie!')
70 sys.exit()
71
72
73def getNewBoard():

Callers 1

mancala.pyFile · 0.70

Calls 5

checkForWinnerFunction · 0.85
getNewBoardFunction · 0.70
displayBoardFunction · 0.70
askForPlayerMoveFunction · 0.70
makeMoveFunction · 0.70

Tested by

no test coverage detected