Asks the player for a move and returns a (x, y) tuple of integer indexes for the place they want to put their tile. Also returns (None, None) if they want to pass on their turn.
(player, board)
| 93 | |
| 94 | |
| 95 | def askForPlayerMove(player, board): |
| 96 | """Asks the player for a move and returns a (x, y) tuple of integer |
| 97 | indexes for the place they want to put their tile. Also returns |
| 98 | (None, None) if they want to pass on their turn.""" |
| 99 | print('It is ' + player + '\'s turn.') |
| 100 | while True: # Keep looping until the player enters a valid move: |
| 101 | print('Enter a move (such as B3) or PASS or QUIT:') |
| 102 | response = input('> ').upper() |
| 103 | |
| 104 | if response == 'QUIT': |
| 105 | print('Thanks for playing!') |
| 106 | sys.exit() |
| 107 | |
| 108 | if response == 'PASS': |
| 109 | return (None, None) # No move should be made. |
| 110 | |
| 111 | # Make sure the player entered a valid move like 'B3' or 'D14'. |
| 112 | # The response[0] character will always be a letter, and the |
| 113 | # next one or more characters must be a number. |
| 114 | if len(response) >= 2 and response[1:].isdecimal(): |
| 115 | # Make sure the letter they entered is on the board: |
| 116 | # The letter 'A' has an ASCII value of 65. |
| 117 | if 'A' <= response[0] < chr(65 + BOARD_WIDTH): |
| 118 | # Make sure the number they entered is on the board: |
| 119 | if 1 <= int(response[1:]) <= BOARD_HEIGHT: |
| 120 | # Get the integer indexes for their move: |
| 121 | moveX = ord(response[0]) - 65 |
| 122 | moveY = int(response[1:]) - 1 |
| 123 | |
| 124 | if board[(moveX, moveY)] != EMPTY_SPACE: |
| 125 | print('There is already a piece there.') |
| 126 | continue |
| 127 | break # Player has entered a valid move. |
| 128 | |
| 129 | # If any of the previous checks failed, make the player enter |
| 130 | # their move again: |
| 131 | print('That is not a valid space on this board.') |
| 132 | return (moveX, moveY) |
| 133 | |
| 134 | |
| 135 | def isWinner(player, board): |