-
-
Save faiface/d5e4c797a50da5899048e2adb8660b78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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