150 likes | 310 Views
GBK Programming 2 2009-10. Jordan Johnson. Today’s plan. Greeting Networking and the Sea Battle game Clean-up. Multi-player sea battle: Networking. player 2. server. player 1. Requirements: Player program ( client ) “Game master” program ( server ) Protocol to govern communications.
E N D
GBK Programming 22009-10 Jordan Johnson
Today’s plan Greeting Networking and the Sea Battle game Clean-up
Multi-player sea battle:Networking player 2 server player 1 • Requirements: • Player program (client) • “Game master” program (server) • Protocol to govern communications
Multi-player sea battle: Example Protocol “Give me a ship of length 5.” player 2 (place 5) server player 1 • Placing ships: • Server requests a ship • Player places a ship • Repeat until all ships are placed.
Multi-player sea battle: Example Protocol player 2 ((1 1) (2 1) (3 1) (4 1) (5 1)) server player 1 “Here it is: (1,1), (2,1), ...” • Placing ships: • Server requests a ship • Player places a ship • Repeat until all ships are placed.
Multi-player sea battle:Example Protocol “Give me a ship of length 4.” player 2 (place 4) server player 1 ...and so on... • Placing ships: • Server requests a ship • Player places a ship • Repeat until all ships are placed.
Battle Protocol: Placing Ships • Summary of messages required: • Server Client: (place N), where N {2,3,4,5} • Client Server: listof[coord-pair] • where a coord-pair is a list of two coords, • and a coord is a nat[<20]. • Server Client: • All ships are placed? ‘your-turn OR ‘enemy-turn • Player’s ships are all placed? ‘ok • Player sent a bogus ship? (try-again N) • Ships left to place? Back to...(place N) • The players are mostly independent.
Battle protocol: Shooting Represents the cell a player is shooting at Responses to shots (if a ship was sunk, length was its length) coord = nat[<20] length = int[2..5] “win” messages mean the player’s last shot won the game.
Implementing the Universe(Server-side data) • Data required for the placement phase:
Implementing the Universe (Server-side data) • Data required for the battle phase:
Data Definitions A pre-battle is a (make-pre-battle iworldiworld listof[len] listof[len] board board) (define-struct pre-battle (p1 p2 len1* len2* bd1 bd2)) representing the phase of the game where p1 and p2 are connected, have placed the ships on their boards bd1 & bd2, and have yet to place ships of the lengths specified in len1* and len2*. • Data definitions for the universe (server): A universe is either • empty, or • an iworld, or • a pre-battle, or • a battle.
Data Definitions ; A battle is a ; (make-battle iworldiworld; board board ; status) (define-struct pre-battle (p1 p2 bd1 bd2 status)) ; where status is either ; ‘p1-turn or ‘p2-turn or ; ‘game-over. ; This represents a state in the ; turn-based portion of the ; game: bd1 and bd2 store the ; current state of the ships; ; status tells whose turn it is ; (or whether the game’s ended). • Data definitions for the universe (server): A universe is either • empty, or • an iworld, or • a pre-battle, or • a battle.
To-do List (for the universe): • on-new handler: • hookup : universe world bundle • on-disconnect handler: • hangup : universe world bundle • on-msg handler: • receive : universe world bundle • Add helper functions as needed.
Reminders Write out your templates. In the handlers, decide what kind of universe you have, and call a helper function for the appropriate kind. Use helper functions to process lists. Write test cases before functions.
Clean-up • Before you leave, please . . . • Log out. • Push in your chair. • Make sure you’ve got everything. • Make sure all trash ends up in the trash can.