back

Maze Challenge

In order to convince you to brush up on your socket programming skills, I've devised a not-particularly-devious set of challenges from which you may earn up to one bonus point. These challenges involve moving one or more clients around a maze hosted on a remote server (gs5015.sp.cs.cmu.edu, port 13126). (Server source code -- if you find some bugs, let me know. Example client code, though you'll learn more if you don't just copy-paste it.)

If you need to brush up on networking skils, try the 15-441 page -- I think they are supposed to teach this stuff.

The maze

Your client, upon connecting to the server, will be placed in a large ASCII maze similar in structure to this one:

#################
### ######## ####
##              #
########## #### #
#     #         #
#####   ###  ####
#################

Any non-space character is a wall, any space (' ') character a passage. The null character ('\0') will not appear.

The challenges

Your client will be placed in the maze randomly upon connection, and will only be able to see those cells in the maze that are adjacent to it. Thus, all the challenges involve writing a client to explore the maze. Note that you will be able to glean up to One Extra Credit Point by completing the challenges -- this is a significant boost to your grade!

Challenge 1

The first challenge is to pick up gems. When a client enters the maze, a random empty tile is selected. When the client reaches this tile, it gets a gem and a new random empty tile is selected. If your client manages to collect 10 gems, you will have succeeded at challenge one.

Challenge 1 is worth 0.2 Extra Credit Points.

Challenge 2

Challenge two is a bit trickier. You need to connect to the server with two (or more) clients, navigate them all to the same place, and have one of them issue the 'f' ("I've found the others") move command. If you are successful, you will be recorded as completing challenge two. Otherwise, all your clients (those bearing your andrewid) will be disconnected.

(By the way, please don't connect under other people's id's and spam erroneous 'f' moves to disconnect them -- it isn't very nice.)

Challenge 2 is worth 0.3 Extra Credit Points.

Protocol

The client and server communicate by sending short messages over a persistent TCP connection. All messages follow the same generic format:

012...(length+2)
typelengthpayload

The first two bytes are a header which specify a message type and (unsigned) message length (not including header). These are followed by a 0 to 255 byte payload.

Handshake

After opening the connection and before doing anything else, the client should send the server a handshake message containing the client's name (use your andrewid, so I know who to give points to):

01
'H'length(name)name

View

The server will, when it is ready for a client to move, send that client a view of the maze:

012-11
'V'9abcdefghi

The characters in the view message correspond to the contents of the maze cells northwest of, north of, northeast of, west of, at, east of, southwest of, south of, and southeast of the client's current position. So the characters "abcdefghi" would correspond to a neighborhood that looked like this:

a b c
d e f
g h i

...with the client currently standing at 'e'. Characters that are ' ' (ASCII space) indicate open areas. Other characters are walls. The 5th received character will always be a space, as clients cannot stand in walls.

Move

Upon receiving a view message, a client should consider its options and send a move command to the server:

012
'M'1command

Where the move command is one of six letters:

lettermeaning
'N'Move north
'S'Move south
'E'Move east
'W'Move west
'.'Don't move
'f'Declare "found" (see challenge 2)

Moving into a wall is bad and will cause your client to be disconnected from the server.

Tell

Sometimes, the server will decide to tell your client something. It will do this with a tell message.

01
'T'length(message)message

Tell messages contain a bare string (without NULL termination) that should be printed to the terminal or otherwise displayed to the end user.

Gem

When a client connects, it is randomly placed in the maze. Also, a special square in the maze is selected such that when the client steps into this square it gets a gem. Immediately after a move that brings the client into this square it will receive a gem message:

01
'G'0

This zero-length message just lets you know that you've picked up a gem and that another square has been randomly selected. Thus, if you are doing some sort of structured gem search (for challenge 1), you can reset your search.