As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab30 $ cd lab30
Write a function called create_nxn_matrix(size)
, which takes a
single integer as a parameter. Your function should return a \(size \times
size\) matrix of \(0\)'s.
Write a function called sum_column(a_matrix, col_index)
, which
takes a 2-dimensional list and a positive integer as parameters. Your
function should return the sum of all of the values along the specified
column of the list.
Tic-tac-toe is a classic pen and paper game that you have likely been playing since grade school. You simply create a 3x3 grid, and take turns writing your associated symbol in the grid. Typically, player one is assigned the 'X' symbol, and player two is assigned the 'O' symbol. The winner is the first player to assign their symbol three times in a row, either horizontally, vertically, or diagonally.
The game is incredibly simple to learn and play. As a matter of fact, you can buy a book that demonstrates the optimal way to play a game of tic-tac-toe. Writing a program that lets two people play tic-tac-toe not that trivial. Writing a fully functioning tic-tac-toe game is probably not feasible in 2 hours. So instead we will write some key functions so that you can finish the whole game later.
It will be very difficult to visualize the game unless you write a function that will print the grid list formatted nicely.
Create a function called print_board(game_board)
in a
file called tic_tac_toe.py. The function has a single
parameter, a 3 x 3 list of characters. This function does
not return any values. Instead, it prints the board to the command
line.
Function Call | Expected Output |
---|---|
print_board([['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']]) | -|-|- ----- -|-|- ----- -|-|- |
print_board([['X', '-', '-'], ['-', 'O', 'X'], ['O', '-', '-']]) | X|-|- ----- -|O|X ----- O|-|- |
You will need nested loops for this activity. One for loop will be for outputting a single row of the game board. For each row, you need to add a "|" character after each element except for the last element in the list. An easy way to do this is to use a string accumulator to accumulate all of the charaters in a row.
The other, outer, loop will iterate over each of the rows. It will rely in the inner for loop to accumulate the string for the row. After you have accumulated an entire row of the game board, you just want to print the accumulated string. Then, print a string of "-" characters to separate the rows. Be sure the function doesn't print the separator row after the last row.
To create a tic-tac-toe game you will also need a function to determine whether someone has won the game. The rules for victory are simple: a player wins if they have 3 of their tokens in a row either horizontally, vertically, or diagonally.
Create a function called won_game(game_board,
player_token)
in a file called tic_tac_toe.py. The
function has two parameters: a 3 x 3 list of characters and
a player_token. The function returns a boolean
value, True if the player_token won the game
and False otherwise.
Function Call | Expected Output |
---|---|
won_game([['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']], 'X') | False |
won_game([['X', '-', '-'], ['-', 'O', 'X'], ['O', '-', '-']], 'X') | False |
won_game([['X', '-', 'O'], ['-', 'O', 'X'], ['O', '-', 'X']], 'O') | True |
Determining if a player won can be broken down into a few cases:
Row: A row of the game board is a Python list.
So you can use the list count
method to determine
if the player won a row. To determine if the player won any
row, create a for loop that iterates over every row of the
board.
Column: Columns are a bit more difficult.
Write a function called get_column(game_board,
col_num)
which takes the game board and a column number.
It should return a list of the values from the specified column.
You can then use count
again to determine if the
player won a column.
Diagonal: Diagonals are a little more
challenging. Write two functions
called get_major_diagonal(game_board)
and get_minor_diagonal(game_board)
. These should
return lists, the values from the specified diagonal. You can
then use count to determine whether the player won a diagonal.
The player won the game if any of these checks are True.
Fantastic! You have completed the majority of the difficult parts of the game. For today's challenge, finish the game. The game should allow two players to alternate making moves onto the board. It should stop when one of the players has won.