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
}
Peace and Love Gator Tumbler, 32oz
Winter Scene Holiday Women's Shorts
Heart of Roses Unisex Heavy Blendâ„¢ Crewneck Sweatshirt
Canvas Tote Bag Lucky Money $ Bag for Good Luck and Fortune
Ling's Stainless Steel Double Insulated Gator Tumbler, 32oz (0.95 Liters)
Unisex Zip Hoodie (XPM2-48)
Steppin' Out Women's Sneakers