Lab 11: Lists for TicTacToe

Quiz: Zybooks 8.28 Lab 11 List Comparison Quiz


Design the Structure for a Tic-Tac-Toe Program

Given the declarations and functions shown below, design the overall structure to play a Tic-tac-toe game, writing down your group's solution in the shared slides: bit.ly/tictactoedesign


Declarations:

board = ['_','_','_', # 1 2 3 (though stored 0-based)

'_','_','_', # 4 5 6

'_','_','_'] # 7 8 9

move_number = 0

Functions (while not shown, when implemented as code the functions will all get a board as a parameter):

display_board() # Take pieces list and display board as a 3x3

computer_move() # Choose a random open spot on the board and place 'X' there

human_move() # Prompt for a board location 1..9 to place 'O'

there_is_a_win() # Returns True if there are 3 pieces in a row, False otherwise

You should be focussing on designing the overall program structure, not writing code. This should be an outline of what to do. Feel free to add other loops, variables, or functions to your design. Your first approach might be something like:

display board()

computer move()

human move()

see if there is a win


This is a reasonable first step, but it only allows for a single computer move followed by a single human move. If the computer were to win, it still allows a human move before checking, and after a win it doesn't display the final board. So maybe your second iteration might be something like:

display board()

computer move()

display board()

human move()

display board()

computer move()

display board()

human move()

display board()

computer move()

display board()

human move()

display board()


This version now displays the initial board and then allows three moves each for the computer and then the human. It doesn't, however, check for a win anywhere, and the repetitive nature of it suggests that there may be a better way to write it.


Think about questions such as: What goes first? What next? How do you use a loop to make repeated moves? Compared to other steps, when do you display the board?


Do you have what you think is a good design? If so, start implementing it in Replit at Lab 11: Tic Tac Toe design. (The task for today is just the design part using the slides. You don't have to implement the code just yet, but it is there in case you have time to start on it.)


Playing the game should look like what is shown below:

Welcome to Tic-tac-toe!

Computer starts with "X" and human plays "O"

0. Board:

_ _ _ 1 2 3

_ _ _ 4 5 6

_ _ _ 7 8 9

Computer places X in square 7

1. Board:

_ _ _ 1 2 3

_ _ _ 4 5 6

X _ _ 7 8 9

Place O in square: 5

2. Board:

_ _ _ 1 2 3

_ O _ 4 5 6

X _ _ 7 8 9

Computer places X in square 9

3. Board:

_ _ _ 1 2 3

_ O _ 4 5 6

X _ X 7 8 9

Place O in square: 8

4. Board:

_ _ _ 1 2 3

_ O _ 4 5 6

X O X 7 8 9

Computer places X in square 6

5. Board:

_ _ _ 1 2 3

_ O X 4 5 6

X O X 7 8 9

Place O in square: 3

6. Board:

_ _ O 1 2 3

_ O X 4 5 6

X O X 7 8 9

Computer places X in square 1

7. Board:

X _ O 1 2 3

_ O X 4 5 6

X O X 7 8 9

Place O in square: 4

8. Board:

X _ O 1 2 3

O O X 4 5 6

X O X 7 8 9

Computer places X in square 2

9. Board:

X X O 1 2 3

O O X 4 5 6

X O X 7 8 9

Thanks for playing