@@ -39,7 +39,10 @@ def playAgain():
3939 return input ().lower ().startswith ('y' )
4040
4141def makeMove (board , letter , move ):
42- board [move ] = letter
42+ if isSpaceFree (board ,move ):
43+ board [move ] = letter
44+ else :
45+ raise Exception ("makeMove: the field is not empty!" )
4346
4447def isWinner (bo , le ):
4548 # Given a board and a player's letter, this function returns True if that player has won.
@@ -64,7 +67,7 @@ def getBoardCopy(board):
6467
6568def isSpaceFree (board , move ):
6669 # Return true if the passed move is free on the passed board.
67- return board [move ] == ' '
70+ return board [move ]. isdigit ()
6871
6972def getPlayerMove (board ):
7073 # Let the player type in his move.
@@ -82,7 +85,7 @@ def chooseRandomMoveFromList(board, movesList):
8285 if isSpaceFree (board , i ):
8386 possibleMoves .append (i )
8487
85- if len (possibleMoves ) != 0 :
88+ if len (possibleMoves ) > 0 :
8689 return random .choice (possibleMoves )
8790 else :
8891 return None
@@ -130,53 +133,57 @@ def isBoardFull(board):
130133 return False
131134 return True
132135
133-
134- print ('Welcome to Tic Tac Toe!' )
135-
136- while True :
137- # Reset the board
138- theBoard = [' ' ] * 10
139- playerLetter , computerLetter = inputPlayerLetter ()
140- turn = whoGoesFirst ()
141- print ('The ' + turn + ' will go first.' )
142- gameIsPlaying = True
143-
144- while gameIsPlaying :
145- if turn == 'player' :
146- # Player's turn.
147- drawBoard (theBoard )
148- move = getPlayerMove (theBoard )
149- makeMove (theBoard , playerLetter , move )
150-
151- if isWinner (theBoard , playerLetter ):
136+ def main ():
137+ print ('Welcome to Tic Tac Toe!' )
138+ random .seed ()
139+ while True :
140+ # Reset the board
141+ theBoard = [' ' ] * 10
142+ for i in range (9 ,0 ,- 1 ):
143+ theBoard [i ] = str (i )
144+ playerLetter , computerLetter = inputPlayerLetter ()
145+ turn = whoGoesFirst ()
146+ print ('The ' + turn + ' will go first.' )
147+ gameIsPlaying = True
148+
149+ while gameIsPlaying :
150+ if turn == 'player' :
151+ # Player's turn.
152152 drawBoard (theBoard )
153- print ( 'Hooray! You have won the game!' )
154- gameIsPlaying = False
155- else :
156- if isBoardFull (theBoard ):
153+ move = getPlayerMove ( theBoard )
154+ makeMove ( theBoard , playerLetter , move )
155+
156+ if isWinner (theBoard , playerLetter ):
157157 drawBoard (theBoard )
158- print ('The game is a tie !' )
159- break
158+ print ('Hooray! You have won the game !' )
159+ gameIsPlaying = False
160160 else :
161- turn = 'computer'
162-
163- else :
164- # Computer's turn.
165- move = getComputerMove (theBoard , computerLetter )
166- makeMove (theBoard , computerLetter , move )
167-
168- if isWinner (theBoard , computerLetter ):
169- drawBoard (theBoard )
170- print ('The computer has beaten you! You lose.' )
171- gameIsPlaying = False
161+ if isBoardFull (theBoard ):
162+ drawBoard (theBoard )
163+ print ('The game is a tie!' )
164+ break
165+ else :
166+ turn = 'computer'
167+
172168 else :
173- if isBoardFull (theBoard ):
169+ # Computer's turn.
170+ move = getComputerMove (theBoard , computerLetter )
171+ makeMove (theBoard , computerLetter , move )
172+
173+ if isWinner (theBoard , computerLetter ):
174174 drawBoard (theBoard )
175- print ('The game is a tie! ' )
176- break
175+ print ('The computer has beaten you! You lose. ' )
176+ gameIsPlaying = False
177177 else :
178- turn = 'player'
179-
180- if not playAgain ():
181- break
182-
178+ if isBoardFull (theBoard ):
179+ drawBoard (theBoard )
180+ print ('The game is a tie!' )
181+ break
182+ else :
183+ turn = 'player'
184+
185+ if not playAgain ():
186+ break
187+
188+ if __name__ == "__main__" :
189+ main ()
0 commit comments