Hardware Reference
In-Depth Information
his contains three functions you have not seen before - play() , swapPlayer() and
getMove() . Look at the simplest one irst, swapPlayer() : All this does is return a player's
symbol, which is not the symbol passed into it. Although this might seem like a trivial thing
to do, this action is required at many diferent parts of the code, which is why it warrants
being built into a function.
he getMove() function might look more complex than you might expect. his is because
there is a lot of code designed to make sure that a valid number is entered by the user. If you
could trust the user to enter a valid square number, this would all be unnecessary - but you
can't, so it is necessary.
Chapter 2 uses the raw_input function but only to input a name; here you want to input a
number. So irst you have to check that it is a number that has been entered and then that the
number represents a square that doesn't already have a symbol in it. herefore the structure
used in this function is a while loop that keeps repeating until a valid number has been
entered. After getting something from the user you must check that it is a number; this can be
done with the int() function, which turns a text input into a number (an integer in this case).
If this can't be done because the user has typed in some alphabetical or punctuation characters,
the program will crash and print out an error message, and you don't want that to happen.
herefore you use a new function structure try , which allows the program to check if the code
is going to throw an error; if so, the program jumps to the except label, where you set the vari-
able to an out-of-range number so that it can be rejected later on in the function. hen you test
if the variable is within the range of the board. Finally, you check to see if that space is actually
free to take a symbol. Only when this inal test is passed do you actually make the move on the
board and set the variable correct_number to true, which stops the while loop from repeat-
ing. Otherwise, a hopefully helpful error message is relayed to the user.
he play() function orchestrates the whole game. First the board is printed out, which, as
mentioned before, contains the square numbering identiication information. hen you
enter an endless loop which repeatedly plays a game. It starts of by wiping the board and
setting the irst player as X. he individual game is contained in the while loop that keeps
on going until there is a winner or there are no more blank squares to use. he player's move
is then made and the board printed again, and then you swap the player for the next move.
he code then loops around to the while function, but as you have already swapped the
player in anticipation of the next round, when you call the checkWin() function as part of
the while tests you must call it for the player who has just gone - hence the use of the
swap_player() function in the calling parameter of the checkWin() function. When one
player has won or the free squares have run out, the while loop stops, and the code tests for
a win; if a win is found, the winning player is printed. However, if there is not a win, the
game must have been a draw, so that is printed out. hen a new game is started. When the
users want to quit the game they should press the Ctrl and C keys together.
Search WWH ::




Custom Search