• No results found

Now that we know about how we want the program to work, let's look at what each line does.

The Start of the Program

1. # Tic Tac Toe 2.

The first couple of lines are a comment and importing the random module so we can use the randint() function in our game.

Printing the Board on the Screen

5. def drawBoard(board):

6. # This function prints out the board that it was passed. 7.

8. # "board" is a list of 10 strings representing the board (ignore index 0)

9. print(' | |')

10. print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])

11. print(' | |')

12. print('---')

13. print(' | |')

14. print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])

15. print(' | |')

16. print('---')

17. print(' | |')

18. print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])

19. print(' | |')

This function will print out the game board, marked as directed by the board parameter. Remember that our board is represented as a list of ten strings, where the string at index 1 is the mark on space 1 on the Tic Tac Toe board. (And remember that we ignore the string at index 0, because the spaces are labeled with numbers 1 to 9.) Many of our functions will work by passing the board as a list of ten strings to our functions. Be sure to get the spacing right in the strings, otherwise the board will look funny when it is printed on the screen.

Just as an example, here are some values that the board parameter could have (on the left side of the table) and what the drawBoard() function would print out (on the right):

Table 10-1: Examples of values of board and output from drawBoard(board) calls.

board value drawBoard(board) output

[' ', ' ', ' ', ' ', 'X', 'O', ' ', 'X', ' ', 'O'] | | X | | O | | ---

| | X | O | | | --- | | | | | | [' ', 'O', 'O', ' ', ' ', 'X', ' ', ' ', ' ', ' '] | | | | | | --- | | | X | | | --- | | O | O | | | [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] | | | | | |

--- | | | | | | --- | | | | | | [' ', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'] | | X | X | X | | --- | | X | X | X | | --- | | X | X | X | | ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] | | 7 | 8 | 9 | | ---

| | 4 | 5 | 6 | | --- | | 1 | 2 | 3 | |

The second to last board filled with X's could not possibly have happened (unless the X player skipped all of the O player's turns!) And the last board has strings of digits instead of X and O, which are invalid strings for the board. But the drawBoard() function doesn't care. It just prints the board parameter that it was passed. Computer programs only do exactly what you tell them, even if you tell them the wrong things to do. We will just make sure these invalid strings are not put into the passed list in the first place.

Letting the Player be X or O

21. def inputPlayerLetter():

22. # Let's the player type which letter they want to be.

23. # Returns a list with the player's letter as the first item, and the computer's letter as the second.

24. letter = ''

25. while not (letter == 'X' or letter == 'O'):

26. print('Do you want to be X or O?')

27. letter = input().upper()

The inputPlayerLetter() is a simple function. It asks if the player wants to be X or O, and will keep asking the player (with the while loop) until the player types in an X or O. Notice on line 26 that we automatically change the string returned by the call to input() to uppercase letters with the upper() string method.

The while loop's condition contains parentheses, which means the expression inside the parentheses is evaluated first. If the letter variable was set to 'X', the expression would evaluate like this:

while not ('X' == 'X' or 'X' == 'O'):

while not (True or False):

while not (True):

while not True:

while False:

As you can see, if letter has the value 'X' or 'O', then the loop's condition will be False and lets the program execution continue.

29. # the first element in the list is the player's letter, the second is the computer's letter.

30. if letter == 'X':

31. return ['X', 'O']

32. else:

33. return ['O', 'X']

This function returns a list with two items. The first item (that is, the string at index 0) will be the player's letter, and the second item (that is, the string at index 1) will be the computer's letter. This if-else statement chooses the appropriate list to return.

Deciding Who Goes First

35. def whoGoesFirst():

36. # Randomly choose the player who goes first. 37. if random.randint(0, 1) == 0:

38. return 'computer'

39. else:

40. return 'player'

The whoGoesFirst() function does a virtual coin flip to determine who goes first, the computer or the player. Instead of flipping an actual coin, this code gets a random number of either 0 or 1 by calling the random.randint() function. If this function call returns a 0, the whoGoesFirst() function returns the string 'computer'. Otherwise, the function returns

the string 'player'. The code that calls this function will use the return value to know who will make the first move of the game.

Asking the Player to Play Again

42. def playAgain():

43. # This function returns True if the player wants to play again, otherwise it returns False.

44. print('Do you want to play again? (yes or no)')

45. return input().lower().startswith('y')

The playAgain() function asks the player if they want to play another game. The function returns True if the player types in 'yes' or 'YES' or 'y' or anything that begins with the letter Y. For any other response, the function returns False. The order of the method calls on line 45 is important. The return value from the call to the input() function is a string that has its lower() method called on it. The lower() method returns another string (the lowercase string) and that string has its startswith() method called on it, passing the argument 'y'. 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.

Related documents