Lab 12: TicTacToe Board Compare

Quiz:

Start with the Lab 12 Quiz: Counting in List at Zybooks 8.29 This quiz asks you to traverse a list and count how many of the elements are not '.' (a period), as shown in the following examples:

Your Task

Write your code in 8.30 Lab 12 Tic Tac Toe Board Compare.

Implement function match_lists( board, pattern) to step through the pattern elements and return -1 if the board and the pattern don't match on the first 9 elements, and return the number at the end of the pattern if the board and the pattern do match.

  • Pattern list elements '.' can be ignored since they match anything in the corresponding board element.

  • Pattern list elements 'X', 'O', or '_'(blank) must match the corresponding board element exactly.

For example we might have the following board and pattern:

These are stored internally as:

The pattern has an extra element which is the number to be returned if the first nine elements match. Since these two lists do match, the function in this case would return 9. For a second example, shown below, the two do not match (because the board has 'X' in position 7 but the pattern requires a blank there), so the call to the function would return -1.

Lab Activity Background Explanation:

The next step in our TicTacToe game is to identify board situations we care about and indicate what move should be made in that situation. For our work today the board move positions are numbered 1..9, even though the board is stored as a list, with the list elements indexed 0..8.

The above diagram shows how the board is stored as a list of 9 strings 'X', 'O', or '_'that we conceptually think of as a 3x3 board, even though it is stored internally as a single list of 9 elements indexed 0..8.

Now let's think about what the best move would be for 'X' (the computer) to make when the board is either of the following situations:

In both cases the right move to make would be for 'X' to move into position 9, the bottom-right square. To capture the information on the right move to make in various situations we use a set of stored patterns. These patterns are lists similar to the board list, except the patterns use not only 'X', 'O', and '_' but also use a '.'character which means we don't care what is in that spot. Each pattern represents a board situation and what move 'X' (the computer) should make in that situation, captured as a number tacked on to the end of the list. We compare the board against these patterns and once we find one that matches, we make the move indicated by the number stored in that pattern.

For example consider the following pattern that matches both of the boards shown above, where we have two 'X' pieces in the bottom row , and the bottom-right position is empty, and the other board pieces can be anything:

['.','.','.',

'.','.','.',

'X','X','_', 9]

The pattern indicates that if a board matches with this pattern, then the right move for the computer ('X') to make is into the bottom-right square in position 9, leading to an immediate win for X. Alternatively consider the case where the board looks like this:

Moving into the bottom-left square at position 7 sets up a win on the next move for the computer ('X'). The pattern shown below captures this:

['X','.','.',

'_','.','.',

'_','X','_', 7]

(If you are thinking deeply about this you might have realized that the order of the patterns is important. For instance we have to first look at the patterns that check for an immediate win for 'X', and next have the patterns that block a win for 'O' on the next move.)