@@ -39,7 +39,10 @@ def playAgain():
39
39
return input ().lower ().startswith ('y' )
40
40
41
41
def 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!" )
43
46
44
47
def isWinner (bo , le ):
45
48
# Given a board and a player's letter, this function returns True if that player has won.
@@ -64,7 +67,7 @@ def getBoardCopy(board):
64
67
65
68
def isSpaceFree (board , move ):
66
69
# Return true if the passed move is free on the passed board.
67
- return board [move ] == ' '
70
+ return board [move ]. isdigit ()
68
71
69
72
def getPlayerMove (board ):
70
73
# Let the player type in his move.
@@ -82,7 +85,7 @@ def chooseRandomMoveFromList(board, movesList):
82
85
if isSpaceFree (board , i ):
83
86
possibleMoves .append (i )
84
87
85
- if len (possibleMoves ) != 0 :
88
+ if len (possibleMoves ) > 0 :
86
89
return random .choice (possibleMoves )
87
90
else :
88
91
return None
@@ -130,53 +133,57 @@ def isBoardFull(board):
130
133
return False
131
134
return True
132
135
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.
152
152
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 ):
157
157
drawBoard (theBoard )
158
- print ('The game is a tie !' )
159
- break
158
+ print ('Hooray! You have won the game !' )
159
+ gameIsPlaying = False
160
160
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
+
172
168
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 ):
174
174
drawBoard (theBoard )
175
- print ('The game is a tie! ' )
176
- break
175
+ print ('The computer has beaten you! You lose. ' )
176
+ gameIsPlaying = False
177
177
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