Game Development Reference
In-Depth Information
10 - Tic Tac Toe
117. if isWinner(copy, playerLetter):
118. return i
119.
120. # Try to take one of the corners, if they are free.
121. move = chooseRandomMoveFromList(board, [1, 3, 7, 9])
122. if move != None:
123. return move
124.
125. # Try to take the center, if it is free.
126. if isSpaceFree(board, 5):
127. return 5
128.
129. # Move on one of the sides.
130. return chooseRandomMoveFromList(board, [2, 4, 6, 8])
131.
132. def isBoardFull(board):
133. # Return True if every space on the board has been
taken. Otherwise return False.
134. for i in range(1, 10):
135. if isSpaceFree(board, i):
136. return False
137. return True
138.
139.
140. print('Welcome to Tic Tac Toe!')
141.
142. while True:
143. # Reset the board
144. theBoard = [' '] * 10
145. playerLetter, computerLetter = inputPlayerLetter()
146. turn = whoGoesFirst()
147. print('The ' + turn + ' will go first.')
148. gameIsPlaying = True
149.
150. while gameIsPlaying:
151. if turn == 'player':
152. # Player's turn.
153. drawBoard(theBoard)
154. move = getPlayerMove(theBoard)
155. makeMove(theBoard, playerLetter, move)
156.
157. if isWinner(theBoard, playerLetter):
158. drawBoard(theBoard)
159. print('Hooray! You have won the game!')
160. gameIsPlaying = False
161. else:
162. if isBoardFull(theBoard):
163. drawBoard(theBoard)
164. print('The game is a tie!')
165. break
166. else:
167. turn = 'computer'
168.
169. else:
Search WWH ::




Custom Search