Richard Wiener - Generic Data Structures and Algorithms in Go: An Applied Approach Using Concurrency, Genericity and Heuristics Published by Apress
In this book listing 1.15 Chess had a deadlock. Here is my updated version of the code.
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
move := make(chan int)
quit := make(chan bool)
go player("Bobby Fischer", move, quit)
go player("Boris Spassky", move, quit)
move <- 1
<-quit
<-quit
fmt.Println("over")
}
func player(name string, move chan int, quit chan bool) {
for turn := range move {
n := rand.Intn(100)
if n <= 5 && turn >= 5 {
fmt.Printf("Player %s was check mated and loses\n", name)
close(move)
} else {
fmt.Printf("Player %s has moved. Turn %d\n", name, turn)
turn++
time.Sleep(500 * time.Millisecond)
move <- turn
}
}
quit <- true
}