Hardware Reference
In-Depth Information
Here, the only changes from the earlier listings are to the play() and generateMove()
functions, but the program is shown in its entirety. he play() function is changed to keep a
count of the number of moves that have been made, and this number is passed to the generate
Move() function. If the move number is anything other than one, the move-picking strategy is
the same as before. However, if it is the irst move the computer has made, it responds in a dif-
ferent way. here are four lists containing the corners and the corresponding opposite corners,
along with the side squares and the corresponding adjacent squares to play in response. Note
that these adjacent squares are also corners, but this is of no importance here. Also, every side
square has two adjacent corner squares, but it doesn't matter which one is picked. Finally, if the
human player opens with the centre square, the computer just picks one of the corners.
he way this works is that for each move and its response, a list is generated. hen the move
list is stepped through one square at a time, looking for the human player's symbol in
each square. If it is found, the corresponding element of the response list is passed to the
prefMove() function. As this element is a list whose length is only one, it gets picked.
here is no point writing a new function when an existing function will suice. he same is
done for the side moves. Note as this only is performed on the irst move, there is only one
X symbol to ind. his plays a pretty mean game that I think can't be beaten.
Over to You
Well, it is now over to you; it's time to add your own improvements and embellishments. You
might want to improve on how the board looks by printing more characters and giving it more
space. On the other hand, you might want it to print quickly and revert to the simpler board
you started with. You might not want to print out the board after each move, but only when
both you and the computer have moved. he numbering of the board is conventional, but per-
haps you want to change this to match the locations of the numbers on the numeric keypad on
your keyboard - that is, if you have one. hat would mean the top-left square would be 7, and
lower-right one would be 3. You can change it in the input section simply by using a list as a
lookup table; don't forget to also use another lookup table to translate when the computer
prints out its move. (A lookup table is a predeined conversion between one number and
another. It is simply a list where the input value is the index of the list and the output value; the
one you want to associate with the input value is the contents of that list element.)
You will notice that the computer is polite, and it always allows you to go irst. Look into
making changes to allow the computer to play irst. I feel that X should always go irst, but
you might look into ofering a choice of symbols to the opening player as well. You can try
and change the playing strategy into giving other classes of squares irst priority in place of
the corners. If you are more experienced, you might like to look up the minimax algorithm
that computes all possible moves. To do that, you have to use a recursive algorithm, which
involves a function that calls itself. hese sorts of things can be very powerful but diicult to
get your head around. Perhaps that is too much at this stage; you might be better of leaving
that until you have inished the topic.
Search WWH ::




Custom Search