Game Development Reference
In-Depth Information
We are going to make three new programs, each based on the Reversi program in the last
chapter. We will make changes to reversi.py to create AISim1.py . Next we will make
changes to AISim1.py to create AISim2.py . And finally, we will make changes to AISim2.py
to for AISim3.py . You can either type these changes in yourself, or download them from the
topic's website at the URL http://inventwithpython.com/chapter16.
Making the Computer Play Against Itself
Save the old reversi.py file as AISim1.py by clicking on File and then Save As , and then
entering AISim1.py for the file name and clicking Ok . This will create a copy of our
Reversi source code as a new file that we can make changes to, while leaving the original
Reversi game the same (we may want to play it again). Change the following code in
AISim1.py :
266. move = getPlayerMove(mainBoard, playerTile)
To this (the change is in bold):
266. move = getComputerMove (mainBoard, playerTile)
And run the program. Notice that the game still asks you if you want to be X or O, but it
will not ask you to enter in any moves. When we replaced getPlayerMove() , we no
longer call any code that takes this input from the player. We still press Enter after the
original computer's moves (because of the input('Press Enter to see the
computer\'s move.') on line 285), but the game plays itself!
Let's make some other changes to AISim1.py . All of the functions we defined for Reversi
can stay the same. But replace the entire main section of the program (line 246 and on) to
look like the following code. Some of the code has remained, but most of it has been
altered. But all of the lines before line 246 are the same as in Reversi in the last chapter.
You can also avoid typing in the code by downloading the source from the URL
http://inventwithpython.com/chapter16.
AISim1.py
This code can be downloaded from http://inventwithpython.com/AISim1.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. while True:
249. # Reset the board and game.
250. mainBoard = getNewBoard()
251. resetBoard(mainBoard)
252. if whoGoesFirst() == 'player':
Search WWH ::




Custom Search