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.
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.
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?
python3 verify_bot.py
YOUR_BOT_FILE_NAME.py
. Note, YOUR_BOT_FILE_NAME.py is the name
of your chat bot python file.The bot server will run in the background and allow other people to chat with your chat bot program.
python3 bot_server.py YOUR_BOT_FILE_NAME.py
.
Note, YOUR_BOT_FILE_NAME.py is the name of your chat bot python
file.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.
python3 loebner_client.py
.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.
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.
$ 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
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!
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.
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!