Game Development Reference
In-Depth Information
Making the Computer Play Itself Several Times
But what if we created a new algorithm? Then we could set this new AI against the one
implemented in getComputerMove() , and see which one is better. Let's make some
changes to our program. Click on File and then Save As , and save this file as AISim2.py so
that we can make changes without affecting AISim1.py . At this point, AISim1.py and
AISim2.py have the same code. We will make changes to AISim2.py and save that file so
that AISim2.py has the new changes and AISim1.py has the original code.
Add the following code. The additions are in bold, and some lines have been removed.
When you are done changing the file, save it as AISim2.py .
If this is confusing, you can always download the AISim2.py source code from the topic's
website at http://inventwithpython.com/chapter16.
AISim2.py
This code can be downloaded from http://inventwithpython.com/AISim2.py
If you get errors after typing this code in, compare it to the topic's code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al@inventwithpython.com
246. print('Welcome to Reversi!')
247.
248. xwins = 0
249. owins = 0
250. ties = 0
251. numGames = int(input('Enter number of games to run: '))
252.
253. for game in range(numGames):
254. print('Game #%s:' % (game), end=' ')
255. # Reset the board and game.
256. mainBoard = getNewBoard()
257. resetBoard(mainBoard)
258. if whoGoesFirst() == 'player':
259. turn = 'X'
260. else:
261. turn = 'O'
262.
263. while True:
264. if turn == 'X':
265. # X's turn.
266. otherTile = 'O'
267. x, y = getComputerMove(mainBoard, 'X')
268. makeMove(mainBoard, 'X', x, y)
269. else:
270. # O's turn.
271. otherTile = 'X'
272. x, y = getComputerMove(mainBoard, 'O')
273. makeMove(mainBoard, 'O', x, y)
274.
275. if getValidMoves(mainBoard, otherTile) == []:
276. break
Search WWH ::




Custom Search