CPSC120A
Fundamentals of Computer Science I

Lab 29

Networking

Use the command line to create a new directory called lab29 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

NetworkConnection Module

Networking can sometimes be a very big chore. This is especially true if you have to use the low level protocols for communications. However, there are a lot of reasons to use networking in your programs. You might need to communicate with other running programs, or maybe you just need data from the Internet.

The network_connection.py module holds 2 main functions: accept(port) and connect(address, port). These two functions allow you to create a server listening on a specific port, and connect to a server listening on that port respectively.

More detailed documentation is available here. Also, make sure you pay attention to the lecture for more detailed information.

Networked Nim

Nim is an acient game where players take turns picking up stones. The stones are arranged in some fashion, and each turn the player must remove 1, 2, or 3 stones from the collection. The player that picks up the last stone loses. In this activity, you will write a networked version of the game Nim.

Details

Write a function called play_nim(num_stones) in a file called nim_server.py. This function takes an integer as a parameter, which represents the number of stones the game starts with. Your function should create two server connections on different ports, one for each player in the game.

You also need to write a function called play_nim(address, port) in a file called nim_client.py. This function takes two parameters, a string address, and an integer port. It should connect to the address and port specified in the parameters, and allow the client to play one of the players of the game.

Example

On Computer 1

$ python3 nim_server.py &
Serving a nim game on ports 9001 and 9002
$ python3 nim_client.py
Connecting to server on lab-mcsp-00 port 9001
Welcome to the Nim game Player 1!  10 Stones remain.
Player 1's Turn!
How many stones do you want to take? 3
Player 1 took 3 stones!  7 Stones remain.
Player 2's Turn!
Player 2 took 3 stones!  4 Stones remain.
Player 1's Turn!
How many stones do you want to take? 3
Player 1 took 3 stones!  1 Stones remain.
Player 2's Turn!
Player 2 took 1 stones!  0 Stones remain.
Player 1 wins!
Thanks for playing!
$

On Computer 2

$python3 nim_client.py
Connecting to server on lab-mcsp-00 port 9002
Welcome to the Nim game Player 2!  10 Stones remain.
Player 1's Turn!
Player 1 took 3 stones!  7 Stones remain.
Player 2's Turn!
How many stones do you want to take? 3
Player 2 took 3 stones!  4 Stones remain.
Player 1's Turn!
Player 1 took 3 stones!  1 Stones remain.
Player 2's Turn!
How many stones do you want to take? 1
Player 2 took 1 stones!  0 Stones remain.
Player 1 wins!
Thanks for playing!
$

Hint

The Nim server needs to keep track of the number of stones remaining. This can just be an integer counter for this program.

The Nim server should accept two connections: one on port 9001, and one on port 9002.

The Nim server should receive a message from player 1 first, then a message from player 2. Between these receives, the server should send messages to each player describing the state of the game.

After establishing the connection to the server, the client should receive from the server until the server says it is the client's turn. Then the client should send an integer containing the player's move to the server.

Make sure all the players stop once the game is won!

 

Challenge

A two player game of Nim is fine, but the strategies are decently well defined. Rewrite the programs to allow for \(N\) different players. Each game of nim eliminates one of the players. You continue until only one player is left standing.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.