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 36 - Loebner and More File I/O


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


It is finally time to figure out who won our chat competition! On top of that, we will also play around with more File I/O, geared towards Game of Life.


In-class Activity 1
Loebner Competition

Modify Chat Bot Output Format

All chat bot programs must have the same output format so that it is not possible to distinguish between bot and human based on format.

  1. Modify your chat bot python program's output so that it conforms to the following standard:
    1. The bot should prompt for user input first
    2. The program should not print anything before the first prompt
    3. The bot should use the prompt text "> " (without the quotes)
    4. The bot should not prefix bot output with anything

    For example, the output of the program should look like:

    > hello bot
    hello human
    > can you pass a Turing test?
    no, do you want to play chess?
    
  2. Download the python program verify_bot.py and put it in the same directory as your chat bot python file.
  3. From the directory containing your bot program, run the verify bot program by typing python3 verify_bot.py YOUR_BOT_FILE_NAME.py. Note, YOUR_BOT_FILE_NAME.py is the name of your chat bot python file.
  4. Wait until everybody's bot file is successfully verified before proceeding.

Run Bot Server

The bot server will run in the background and allow other people to chat with your chat bot program.

  1. Download the python program bot_server.py and put it in the same directory as your chat bot python file.
  2. From the directory containing your bot program, run the bot server by typing python3 bot_server.py YOUR_BOT_FILE_NAME.py. Note, YOUR_BOT_FILE_NAME.py is the name of your chat bot python file.
  3. Wait until everybody's bot server is running before proceeding.

Run Client

The client program will communicate with the server to determine who you will chat with and will connect you to the bot or person you will chat with.

  1. Download the python program loebner_client.py and put it in the same directory as your chat bot python file.
  2. From the directory containing your bot program, run the client by typing python3 loebner_client.py.
  3. Wait until everybody's has connected to the server.
  4. Chat.
  5. When the chat is over, evaluate who or what you were chatting with.

Lab Assignment 36
Conway's Game of Life

In the 1970's, John Conway invented a "zero-player game" known as the game of life. The game was intended to model behavior of bacteria, but is an incredibly interesting demonstration of emergent behavior and chaos theory.

The board in the Game of Life can be represented as a 2-dimensional list, where each cell in the list can be in one of two states: on or off. We will use the values 1 and 0 respectively to represent these states. The player interacts with the game by specifying the starting state of the individual cells in the game. The rules of the game dictate how the board evolves over time.

The way this was accomplished in the challenge problem last week was by specifying a static, 2-dimensional list in the code file. That is not very user friendly. Instead, it would be better to specify a file to represent this initial state. We will need code to read and write these state files to accomplish this. And that sounds like a decent enough place to start.

Details

Create a function read_game_state(file_name) in a file called game_of_life.py. This function takes a single parameter: a string of a file name to read the initial game state from. Your function should return a 2-dimensional list of integers in the range [0, 1].

The first line of the input file is going to be a specification of the number of rows in the board and the number of columns in the board, separated by a single space. These tell you the number of lines to follow, and the length of each line.

Create a function called write_game_state(file_name, game_board). This function takes two parameters: a string of the file to save the board to, and a 2-dimensional list of integers in the range [0, 1]. This function should write a file of the above specified format.

You should also write a function print_game(board), which takes a 2-dimensional list representing a Game of Life game board, and prints the game board to the terminal. This will allow you to easily debug your file input function.

Example

$ cat initial_state.in
5 7
0000000
0000000
0011100
0000000
0000000
$ python3
>>> import game_of_life
>>> board = read_game_state("initial_state.in")
>>> print(board)
[[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
>>> print_game(board)
0000000
0000000
0011100
0000000
0000000
>>> board[2][3] = 0
>>> write_game_state("second_state.out", board)
>>> exit()
$ cat second_state.out
5 7
0000000
0000000
0010100
0000000
0000000

Hint

  • Write a function join_list(row), which takes a list of integers as a parameter and returns a string, all of the elements from row concatenated together. You can then use this in both print_game and write_game_state.

  • Write a function explode(row_string). This function takes a binary string as a parameter, and returns a list of the digits of the binary string as integers. This will simplify your code for read_game_state a great deal.

  • Remember, readline returns a full line from the file as a string, that includes the new line character.

  • Don't forget, You can specify the alternate parameter end="" to the print function, to make print not output a new line character.

  • As always, don't forget to close your files after you read and write them!

 

Challenge

Visualizing the current state of the game board is a bit challenging using 0's and 1's through the command line terminal. However, you know how to create a graphical window to represent the game board. In the graphical window, you can use a solid black square to represent a cell that is "on", and a un-filled square to represent an "off" cell.

Create a new function called display_game(board), which creates a graphical representation of the Game of Life board. The board should fill as much of the window as possible, but still show all of the cells in the board.

Once you have the graphical display set up, you can allow the user to modify the current state of the board by clicking on the cells.


Submission

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

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

To submit your activity, go to cseval.roanoke.edu. You should see an available assignment called Lab Assignment 36. 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