CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 34 - 2D Lists


As usual, create two directories for today's class. Create a directory called lecture34 under activities, and a directory called lab34 under labs.


How many dimensions are there?

You have seen the three major sequence data types that Python provides for you: Lists, Tuples, and Dictionaries. One interesting aspect of these data types is that they can be recursive — They can hold other sequence data types! This means that we can create grids of data, cubes of data, even hypercubes of data. Luckily, creating these structures is pretty easy. Using them effectively takes some reasoning skills we will exercise today.


Lab Activity 1

It's CodeLab time again. Today, you have 3 CodeLab activities to attempt individually. You may discuss with your partner only after you have attempted these activities.


Lab Assignment 34
Tic-Tac-Toe

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 simply 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 is less than trivial, however.

Luckily, we are providing you with a lot of starter code, which should reduce the complexity of writing such a program.

Details

Download the code linked above. You are tasked with writing the body for three different functions:

  1. initialize_board(game_board)
  2. valid_choice(row, col, game_board)
  3. win(game_board, token)

The Pre and Post conditions should be sufficient for your understanding of what each function should accomplish. You may need to write some additional functions to assist your writing of the three required functions above.

Example

cssmith:lab34$ python3 tic_tac_toe.py
| - | - | - | 
-------------
| - | - | - | 
-------------
| - | - | - | 
Player 1 Turn
Which row would you like to play? 0
Which column would you like to play? 0
| X | - | - | 
-------------
| - | - | - | 
-------------
| - | - | - | 
Player 2 Turn
Which row would you like to play? 1
Which column would you like to play? 0
| X | - | - | 
-------------
| O | - | - | 
-------------
| - | - | - | 
Player 1 Turn
Which row would you like to play? 0
Which column would you like to play? 1
| X | X | - | 
-------------
| O | - | - | 
-------------
| - | - | - | 

………

| X | X | O | 
-------------
| O | X | - | 
-------------
| - | O | - | 
Player 1 Turn
Which row would you like to play? 2
Which column would you like to play? 2
| X | X | O | 
-------------
| O | X | - | 
-------------
| - | O | X | 
Congratulations Player 1 You won the game!
Would you like to play again (Y or N) N

Hint

  • In initialize_board, you are given a list. You need to append empty lists to the game_board parameter. You then have to make sure that the EMPTY element appears in every location in the BOARD_SIZE x BOARD_SIZE list.

  • valid_choice needs to make sure both row and column are in the range [0, BOARD_SIZE). It should also check to make sure the element at that grid location is currently EMPTY.

  • win should check all of the rows of the grid, all of the columns of the grid, and both the major and minor diagonal of the grid. If the specified token appears BOARD_SIZE times in one of those locations, that player has won. If you have a list of the elements from those locations, You can simply count the number of elements in those respective sequences, in order to figure out if the player won.

  • Speaking of which, it might be useful to write functions to get a row, get a column, get the major axis, and get the minor axis. These functions should return a list of the elements from the specified locations.


Challenge

Conway's Game of Life is another grid-based game. Except in the game of life, two people don't compete, life itself does. In Conway's game of life, a two dimensional grid represents a petridish. Each cell in the grid is either occupied by a bacteria, 1, or not, 0. In each turn of the game every cell in the grid changes state based on the following rules:

The file game_of_life.py contains the outline of a program that plays the game of life. The function update_petridish is undefined. The function should return a new petri dish, a two-dimensional list of 0s and 1s, that is equivalent to the input petri dish, updated according to the above rules. The following is an example of two iterations of the game of life:

0000000
0000000
0111110
0000000
0000000

0000000
0011100
0011100
0011100
0000000


Submission

When you have finished, create a tar file of your lab34 directory. To create a tar file, execute the following commands:

cd ~/cs120/labs
tar czvf lab34.tgz lab34/

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 34. Only one of your pair should submit your activity. Make sure both partners are listed in the header of your files.

Do not forget to email your partner today's files!


Last modified: Thu Nov 7 21:44:28 EST 2013