Game Development Reference
In-Depth Information
might be a bit confusing, because you may forget which item is for X and which item is
for O. Instead the function returns a dictionary with keys 'X' and 'O' whose values are
the scores.
Getting the Player's Tile Choice
129. def enterPlayerTile():
130. # Let's the player type which tile they want to be.
131. # Returns a list with the player's tile as the first
item, and the computer's tile as the second.
132. tile = ''
133. while not (tile == 'X' or tile == 'O'):
134. print('Do you want to be X or O?')
135. tile = input().upper()
This function asks the player which tile they want to be, either 'X' or 'O' . The for
loop will keep looping until the player types in 'X' or 'O' .
137. # the first element in the tuple is the player's
tile, the second is the computer's tile.
138. if tile == 'X':
139. return ['X', 'O']
140. else:
141. return ['O', 'X']
The enterPlayerTile() function then returns a two-item list, where the player's
tile choice is the first item and the computer's tile is the second. We use a list here instead
of a dictionary so that the assignment statement calling this function can use the multiple
assignment trick. (See line 252.)
Determining Who Goes First
144. def whoGoesFirst():
145. # Randomly choose the player who goes first.
146. if random.randint(0, 1) == 0:
147. return 'computer'
148. else:
149. return 'player'
The whoGoesFirst() function randomly selects who goes first, and returns either the
string 'computer' or the string 'player' .
Search WWH ::




Custom Search