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 Vintage Wash Saltwater T-Shirt Rainbow Piano Keyboard XPM2-46 Waiting So Long
Men's Vintage Wash Saltwater T-Shirt Rainbow Piano Keyboard XPM2-46 Waiting So Long
Halloween Jack-O-Lantern Pumpkin Baby Bodysuit XPM2-51
XPirateMental XPM2-20
Synths Samplers Sequencers Men's Vintage Wash T-Shirt Navy XPirateMental XPM2-43
Men's Regular Lemon Color T-Shirt XPM2-28 Design
Synths Samplers Sequencers Men's Vintage Wash T-Shirt Navy XPirateMental XPM2-43