|
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:
my_element in my_list
-
Returns: True when my_element is in the list, False otherwise.for my_element in my_list:
block
- Iterates through my_list
, assigning
each element to my_element
and then executing
block
.range(my_integer)
- Creates a list of
integers from 0 to my_integer
. This is useful
in combination with for/in.my_list.count(my_element)
-
Returns: Number of times my_element
appears in
my_list
.my_list.append(my_element)
-
Appends my_element
to the end of
my_list
. Does not return a value.
Modifies the original list.x = val
- Sets x
to be equal to val
. Does not return a value.x == val
- Checks if
x
is equal to val
. Returns
True or False. Does not modify the value of x
.Remember:
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
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
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]
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]
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
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
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!
make-board(n)
to create
and initialize to '-' a board of size n
.
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.
Purpose of this Lab |
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.