()
| 17 | COMPUTER = 'O' |
| 18 | |
| 19 | def main(): |
| 20 | print('Reversegam, by Al Sweigart al@inventwithpython.com') |
| 21 | print('Place your tiles around your opponent\'s tiles to turn') |
| 22 | print('them into your tiles. Try to get the most tiles. The game') |
| 23 | print('ends when the board is full.') |
| 24 | print() |
| 25 | |
| 26 | mainBoard = getNewBoard() # Start a new board. |
| 27 | isPlayersTurn = True |
| 28 | while True: # Main game loop. |
| 29 | humanCantMove = getValidMoves(mainBoard, HUMAN) == [] |
| 30 | computerCantMove = getValidMoves(mainBoard, COMPUTER) == [] |
| 31 | if humanCantMove and computerCantMove: |
| 32 | break # Neither player can move, so quit. |
| 33 | |
| 34 | if isPlayersTurn and not humanCantMove: |
| 35 | # Human player's turn: |
| 36 | displayBoard(getBoardWithValidMoves(mainBoard, HUMAN)) |
| 37 | move = askForPlayerMove(mainBoard) |
| 38 | if move == 'QUIT': |
| 39 | print('Thanks for playing!') |
| 40 | sys.exit() |
| 41 | else: |
| 42 | makeMove(mainBoard, HUMAN, move[0], move[1]) |
| 43 | elif not isPlayersTurn and not computerCantMove: |
| 44 | |
| 45 | displayBoard(mainBoard) |
| 46 | print() |
| 47 | input('Press Enter to see the computer\'s move.') |
| 48 | x, y = getComputerMove(mainBoard) |
| 49 | print('The computer moved on {}{}.'.format(COLS[x], ROWS[y])) |
| 50 | print() |
| 51 | makeMove(mainBoard, COMPUTER, x, y) |
| 52 | |
| 53 | isPlayersTurn = not isPlayersTurn |
| 54 | |
| 55 | # Display the final board and score. |
| 56 | displayBoard(mainBoard) |
| 57 | print('Good game!') |
| 58 | |
| 59 | |
| 60 | def getNewBoard(): |
no test coverage detected