Game Development Reference
In-Depth Information
16 - AI Simulation
253. turn = 'X'
254. else:
255. turn = 'O'
256. print('The ' + turn + ' will go first.')
257.
258. while True:
259. drawBoard(mainBoard)
260. scores = getScoreOfBoard(mainBoard)
261. print('X has %s points. O has %s points' %
(scores['X'], scores['O']))
262. input('Press Enter to continue.')
263.
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
277. else:
278. turn = otherTile
279.
280. # Display the final score.
281. drawBoard(mainBoard)
282. scores = getScoreOfBoard(mainBoard)
283. print('X scored %s points. O scored %s points.' %
(scores['X'], scores['O']))
284.
285. if not playAgain():
286. sys.exit()
How the AISim1.py Code Works
The AISim1.py program is the same as the original Reversi program, except that the call to
getPlayerMove() has been replaced with a call to getComputerMove() . There have
been some other changes to the text that is printed to the screen to make the game easier to
follow.
When you run the AISim1.py program, all you can do is press Enter for each turn until the
game ends. Run through a few games and watch the computer play itself. Since both the X
and O players are using the same algorithm, it really is just a matter of luck to see who wins.
The X player will win half the time, and the O player will win half the time.
Search WWH ::




Custom Search