Game Development Reference
In-Depth Information
There is no loop, because we assume that if the user entered anything besides a string
that begins with 'y' , they want to stop playing. So, we only ask the player once.
Placing a mark on the Board
47. def makeMove(board, letter, move):
48. board[move] = letter
The makeMove() function is very simple and only one line. The parameters are a list
with ten strings named board , one of the player's letters (either 'X' or 'O' ) named
letter , and a place on the board where that player wants to go (which is an integer from
1 to 9 ) named move .
But wait a second. You might think that this function doesn't do much. It seems to
change one of the items in the board list to the value in letter . But because this code is
in a function, the board variable will be forgotten when we exit this function and leave the
function's scope.
Actually, this is not the case. This is because lists are special when you pass them as
arguments to functions. This is because you pass a reference to the list and not the list itself.
Let's learn about the difference between lists and list references.
List References
Try entering the following into the shell:
>>> spam = 42
>>> cheese = spam
>>> spam = 100
>>> spam
100
>>> cheese
42
This makes sense from what we know so far. We assign 42 to the spam variable, and
then we copy the value in spam and assign it to the variable cheese . When we later
change the value in spam to 100 , this doesn't affect the value in cheese . This is because
spam and cheese are different variables that store different values.
But lists don't work this way. When you assign a list to a variable with the = sign, you
are actually assigning a reference to the list. A reference is a value that points to some bit
of data, and a list reference is a value that points to a list. Here is some code that will
make this easier to understand. Type this into the shell:
Search WWH ::




Custom Search