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

Function main

src/gamesbyexample/hexapawn.py:21–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19
20
21def main():
22 print("""Hexapawn, by Al Sweigart al@inventwithpython.com
23 A pawn-only chess variant where you must try to move one of your
24 pawns to the opposite end of the board. You also win if you block
25 your opponent from making a move.
26
27 Pawns can advance one space at a time (if they are not blocked by
28 an opponent's pawn), and can capture pawns that are diagonally
29 in front of them.
30 """)
31
32 width, height = askForBoardSize()
33 board = getNewBoard(width, height)
34 turn = O_PLAYER
35 while True: # Main game loop.
36 displayBoard(board)
37
38 # Check if the player is blocked and can't make any moves:
39 validMoves = getValidMoves(turn, board)
40 if len(validMoves) == 0:
41 print(turn.upper(), 'is blocked and cannot move!')
42 if turn == X_PLAYER:
43 print('O has won!')
44 elif turn == O_PLAYER:
45 print('X has won!')
46 print('Thanks for playing!')
47 sys.exit()
48
49 # Carry out the player's move:
50 doPlayerMove(turn, board)
51 if checkIfPlayerReachedEnd(turn, board):
52 displayBoard(board)
53 print(turn.upper(), 'has won!')
54 print('Thanks for playing!')
55 sys.exit()
56
57 if turn == X_PLAYER:
58 turn = O_PLAYER
59 elif turn == O_PLAYER:
60 turn = X_PLAYER
61
62
63def askForBoardSize():

Callers 1

hexapawn.pyFile · 0.70

Calls 6

askForBoardSizeFunction · 0.85
checkIfPlayerReachedEndFunction · 0.85
getNewBoardFunction · 0.70
displayBoardFunction · 0.70
getValidMovesFunction · 0.70
doPlayerMoveFunction · 0.70

Tested by

no test coverage detected