Skip to content

Instantly share code, notes, and snippets.

@faiface
Created June 30, 2017 20:02
Show Gist options
  • Save faiface/d5e4c797a50da5899048e2adb8660b78 to your computer and use it in GitHub Desktop.
Save faiface/d5e4c797a50da5899048e2adb8660b78 to your computer and use it in GitHub Desktop.
type Proboj interface {
ReadPlayer(r io.Reader) interface{}
ActPlayer(i int, turn interface{})
WriteState(w io.Writer)
Update()
}
func RunProboj(proboj Proboj, players []string) {
turnChans := make([]chan interface{}, len(players))
playerIns := make([]io.Writer, len(players))
for i, player := range players {
ch := make(chan interface{})
cmd := exec.Command(player)
in, out := cmd.StdinPipe(), cmd.StdoutPipe()
cmd.Start()
proboj.WriteState(in)
go func() {
for {
ch <- proboj.ReadPlayer(out)
}
}()
turnChans[i] = ch
playerIns[i] = in
}
for {
proboj.Update()
for i := range turnChans {
select {
case turn := <-turnChans[i]:
proboj.ActPlayer(i, turn)
proboj.WriteState(playerIns[i])
default: // zakomentovane je turn-based, inak real-time
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment