Hardware Reference
In-Depth Information
his just shows the function itself without any test harness. Test harness is the software term
used by software engineers to describe temporary code that exists only to test functions in
isolation. It is an important part of the process of developing a project to be able to test func-
tions in isolation from the rest of the code. his is because you can then feed the function
with all sorts of conditions that might not happen very often when the function is run as
part of the normal code.
he wipeBoard function simply puts spaces in every position in the board. Here the statement
for square in range(0, len(board) ) :
generates the loop variable square as just a sequence of numbers. Rather than just put the
last number in the range as 16, you have used the len() function, which works out the
length of the list. his means that it will work with any size board and hence any length list.
You might think the canMove() function is not necessary, but one situation that
checkWin() cannot cope with is when there are no more spaces left on the board - which is
why you need canMove() . Initially the move variable is set to be false. hen it visits each
square on the board; if it sees a blank space, it sets the move variable to true. It doesn't matter
if this variable is set to be true once or ifteen times, and it is often simpler in programming to
let things run on like this rather than try to stop when you ind the irst instance of a blank
square.
A Two-Player Game
Now it is time to put the code pieces together and make a program that can run a two-player
game. hat is, there are two human players, each taking a turn to enter his or her move. he
computer keeps track of the moves, prints out the board after each move and checks if one
player has won or if it is a draw. here is, however, just one more thing you need to consider
before proceeding.
When representing the board as a list, the elements of the list, as always, start of with the
irst item position number of zero. Most noncomputer people are not happy with a number
zero, so as a sop to them you shall be representing the positions on the board as numbers 1
to 9. herefore, whenever a number is input from a player, the program has to adjust this
number to match the internal representation of the position. True, this involves only adding
or subtracting one from the number, but you need to remember to do this each time a num-
ber is presented to a player. As a reminder of the numbering system the board list is ini-
tialised to represent the number of each square before being wiped prior to beginning the
game. his is shown in the two-player version of the game, shown in Listing 3-5.
Search WWH ::




Custom Search