player simulates a person playing the game of tennis.
(name string, court chan int)
| 37 | |
| 38 | // player simulates a person playing the game of tennis. |
| 39 | func player(name string, court chan int) { |
| 40 | // Schedule the call to Done to tell main we are done. |
| 41 | defer wg.Done() |
| 42 | |
| 43 | for { |
| 44 | // Wait for the ball to be hit back to us. |
| 45 | ball, ok := <-court |
| 46 | if !ok { |
| 47 | // If the channel was closed we won. |
| 48 | fmt.Printf("Player %s Won\n", name) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | // Pick a random number and see if we miss the ball. |
| 53 | n := rand.Intn(100) |
| 54 | if n%13 == 0 { |
| 55 | fmt.Printf("Player %s Missed\n", name) |
| 56 | |
| 57 | // Close the channel to signal we lost. |
| 58 | close(court) |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | // Display and then increment the hit count by one. |
| 63 | fmt.Printf("Player %s Hit %d\n", name, ball) |
| 64 | ball++ |
| 65 | |
| 66 | // Hit the ball back to the opposing player. |
| 67 | court <- ball |
| 68 | } |
| 69 | } |