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
}
Men's T-Shirt XPirateMental XPM2-21
Halloween Pumpkin Backpack XPM2-50
Men's T-Shirt Vintage Wash Nantucket Red XSE1-34
Men's Bird Regular Olive T-Shirt XPM2-45
Men's T-Shirt Vintage Wash Nantucket Red XSE1-34
Men's Regular Lemon Color T-Shirt XPM2-28 Design
XPM2-22 T-Shirt