1. | Write a suite of programs that run in parallel and interact to play the "Paper, Scissors, Rock" game. In this game, two players secretly choose either paper, scissors, or rock. They then reveal their choice. A referee decides who wins as follows: Paper beats rock (by covering it). Rock beats scissors (by blunting it). Scissors beats paper (by cutting it). Matching choices draw. [Page 531]The winning player gets a point. In a draw, no points are awarded. Your program should simulate such a game, allowing the user to choose how many iterations are performed, observe the game, and see the final score. Here's an example of a game: $ ./play 3 ...play three iterations. Paper, Scissors, Rock: 3 iterations Player 1: ready Player 2: ready Go Players [1] Player 1: Scissors Player 2: Rock Player 2 wins Go Players [2] Player 1: Paper Player 2: Rock Player 1 wins Go Players [3] Player 1: Paper Player 2: Paper Players draw. Final score: Player 1: 1 Player 2: 1 Players Draw $ _ You should write three programs, which operate as follows: One program is the main program, which fork/execs one referee process and two player processes. It then waits until all three terminate. It should check that the command-line parameter that specifies the number of turns is legal and pass it to the referee process as a parameter to exec (). One program is a referee program, which plays the role of the server. This program should prepare a socket and then listen for both players to send the string "READY", which means that they're ready to make a choice. It should then tell each player to make a choice by sending them both the string "GO". Their responses are then read, and their scores are calculated and updated. This process should be repeated until all of the turns have been taken, at which point the referee should send both players the string "STOP", which causes them to terminate. One program is a player program, which plays the role of the client. This program is executed twice by the main program, and should start by connecting to the referee's socket. It should then send the "READY" message. When it receives the "GO" message back from the referee, it should make a choice and send it as a string to the referee. When it receives the string "STOP", it should kill itself. [Page 532]These programs will almost certainly share some functions. To do a good job, create a makefile that separately compiles these common functions and links them into the executables that use them. Do not avoid sending strings by encoding them as one-byte numberssending strings is part of the exercise. [level: medium] |
3. | Rewrite Exercise 1 to allow the players to reside on different machines on the network. Each component of the game should be able to start separately. [level: hard] This is an example of how 3 will work. ...execute this command on vanguard. $ ./referee 5000 ...use local port 5000. ...execute this command on csservr2. $ ./player vanguard.5000 ...player is on a remote port. ...execute this command on wotan. $ ./player vanguard.5000 ...player is on a remote port. |