Game Development Reference
In-Depth Information
161. else:
162. if isBoardFull(theBoard):
163. drawBoard(theBoard)
164. print('The game is a tie!')
165. break
If the player did not win with his last move, then maybe his last move filled up the entire
board and we now have a tie. In this else-block, we check if the board is full with a call to
the
isBoardFull()
function. If it returns
True
, then we should draw the board by
calling
drawBoard()
and tell the player a tie has occurred. The
break
statement will
break us out of the
while
loop we are in and jump down to line 186.
Running the Computer's Turn
166. else:
167. turn = 'computer'
If the player has not won or tied the game, then we should just set the
turn
variable to
'computer'
so that when this
while
loop loops back to the start it will execute the
code for the computer's turn.
169. else:
If the
turn
variable was not set to
'player'
for the condition on line 151, then we
know it is the computer's turn and the code in this else-block will execute. This code is very
similar to the code for the player's turn, except the computer does not need the board
printed on the screen so we skip calling the
drawBoard()
function.
170. # Computer's turn.
171. move = getComputerMove(theBoard,
computerLetter)
172. makeMove(theBoard, computerLetter, move)
This code is almost identical to the code for the player's turn on line 154 and 155.
174. if isWinner(theBoard, computerLetter):
175. drawBoard(theBoard)
176. print('The computer has beaten you! You
lose.')
177. gameIsPlaying = False