CSci 1901: Lab 13, April 19

Lab 13: Tic-Tac-Toe in Python

Related files:

In this lab, you will develop a game of tic-tac-toe using Python. The purpose of this lab is to illustrate the use of lists in Python, as well as many other features of the language. Provided below is a list of useful procedures for this lab, as well as links to code examples from John and language references:

Useful functions and syntax for this lab:

Remember:

What to Do:

  1. Step 1: Checking a list for non '-' values (1 pt)

    Write a function, all_have_value(lst), that takes in a list and returns True if all elements have a value other than '-'.

    >>> all_have_value(['x','o','x'])
    True
    >>> all_have_value([-1])
    True
    >>> all_have_value([1, '-', 2, 'o', 8, 10])
    False
    >>> all_have_value([])
    True
    

  2. Step 2: Check a list for equality (1 pt)

    Write a function, all_equal(lst), that takes in a list and checks to see if all elements in the list are equal. The function should return True is all the elements are not '-' and equal, False otherwise.

    >>> all_equal(['x', 'x', 'x'])
    True
    >>> all_equal(['x', 'o', 'o'])
    False
    >>> all_equal(['-','-','-'])
    False
    >>> all_equal([])
    True
    >>> all_equal(['x','x','x','x','x'])
    True
    

  3. Step 3: Select rows and columns from a 2 dimensional array (2 pts)

    A 2D array can be represented in Python as a nested list, in which each element is a list. Write two procedures, select_row(array, i) and select_col(array, i) that take in a 2D array and the index of the row (or column) to be selected. The procedure should return the row (or column) associated with that index. Recall that list indexes start at 0, and you may assume that the index passed i is a valid index for the given list.

    >>> array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    >>> select_row(array, 0)
    [1, 2, 3]
    >>> select_row(array, 2)
    [7, 8, 9]
    >>> select_col(array, 0)
    [1, 4, 7]
    >>> select_col(array, 2)
    [3, 6, 9]
    >>> select_col([[1,2],[3,4],[5,6],[7,8]], 1)
    [2, 4, 6, 8]
    

  4. Step 4: Select the main diagonal and the counterdiagonal from a square 2 dimensional array (2 pts)

    Write two procedures, select_main_diag(array) and select_counter_diag(array) that take in a 2D square array and return a list with the elements of the main diagonal (or, respectively, the counterdiagonal). Assume the array passed is a square. The main diagonal consists of the elements connecting the upper left to the lower right, the counterdiagonal consists of the elements connecting the upper right to the lower left corner.

    >>> array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    >>> select_main_diag(array)
    [1, 5, 9]
    >>> select_counter_diag(array)
    [3, 5, 7]
    >>> select_main_diag([[1,2,3,4],[5,6,7,8]],[9,10,11,12],[13,13,15,16])
    [1, 6, 11, 16]
    

  5. Step 5: Find the winner (2 pts)

    Write a procedure, winner(board), that takes a 2D list representing a tic-tac-toe board and determines if there is a winner, who it is, or if the game ends on a draw.

    Use the procedures written above to check if either Player 1 or Player 2 has filled an entire row or column or diagonal. Return 'x' or 'o' if player 1 or player 2 is a winner, 'Draw' if the board is full and neither player won, or False if there is no winner and the board is not full.

    You may assume the board is 3x3 and uses '-' to represent an open position, 'x' for player 1 and 'o' for player 2.

    >>> winner([['-','-','-'], ['-','-','-'], ['-','-','-']])
    False
    >>> winner([['x','x','x'], ['-','-','-'], ['-','-','-']])
    'x'
    >>> winner([['o', '-','-'], ['-', 'o','-'], ['-','-','o']])
    'o'
    >>> winner([['-','-','x'], ['-','-','x'], ['-','-', 'x']])
    'x'
    >>> winner([['x','o','x'], ['x','o','o'], ['o','x','x']])
    "Draw"
    >>> winner([['x','o','x'], ['x','o','o'], ['o','-','x']])
    False
    

  6. Step 6: Draw the Board (1 pt)

    You must draw the board so that the players can see it before they make their moves. Write a procedure, display_board(board), that takes a 2D list representing a tic-tac-toe board and draws the board using text.

    >>> my_board = [['x', 'o', 'x'], ['x','o','o'], ['o','-','x']]
    >>> display_board(my_board)
    
      x  |  o  |  x  
    -----+-----+-----
      x  |  o  |  o 
    -----+-----+-----
      o  |  -  |  x  
    
    

  7. Step 7: Play the Game! (1 pt)

    The bulk of the play_game() procedure has been provided to you. This procedure will create a new board, initialize the variables needed, call your display_board procedure, and allow the user to select the row and column to make their move.

    Once the user enters their selection, you must verify that the position is open and not taken by either player. If it is open, you should mark the move on the board and switch players for the next move. If the position is already taken, display a message to the player and let that player choose again.

    Take some time to look through play_game() to understand exactly what it does. This will make it much easier to figure out what your code should do, and what it should not.

    Call play_game() and find out who has the superior tic-tac-toe skills!

Bonus Questions

  1. 2 points -- Write a procedure make-board(n) to create and initialize to '-' a board of size n.
  2. 2 points -- Modify the function play_game by adding one argument, n, which specifies the size of the board. Modify the other functions as needed, so that games on different size boards can be played.
Congratulations on completing Lab 13!
Lab 13 total: 10 pts Do you want to expand your program to play 3D tic-tac-toe? Do you want to see how to add AI to the game, so the computer can play against you? Look at what JT Olds, a junior student, did using Python and a cool graphics package in Python. Go to his web page for a short description of his work and for the Python code.

Purpose of this Lab
What you should learn from this lab:

  • Gain experience working in Python.
  • Understand how to use and manipulate lists.
  • Learn how to use data flow control in Python (if/then, while, etc).

Copyright: © 2007 by the Regents of the University of Minnesota
Department of Computer Science and Engineering. All rights reserved.
Comments to: Maria Gini
Changes and corrections are in red.