Add win condition.
This commit is contained in:
parent
bc8d911b87
commit
891c2f435e
41
main.go
41
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/faiface/pixel"
|
"github.com/faiface/pixel"
|
||||||
|
@ -25,11 +26,14 @@ func run() {
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
for !w.Closed() {
|
for !w.Closed() && !s.won {
|
||||||
w.Clear(colornames.Peru)
|
w.Clear(colornames.Peru)
|
||||||
s = updateState(s)
|
s = updateState(s)
|
||||||
render(s, w, time.Since(start))
|
render(s, w, time.Since(start))
|
||||||
w.Update()
|
w.Update()
|
||||||
|
if s.won {
|
||||||
|
log.Println("You win!")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,6 +66,7 @@ func render(s state, w *pixelgl.Window, d time.Duration) {
|
||||||
|
|
||||||
type state struct {
|
type state struct {
|
||||||
bots []bot
|
bots []bot
|
||||||
|
won bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newState() state {
|
func newState() state {
|
||||||
|
@ -81,25 +86,27 @@ type bot struct {
|
||||||
func updateState(sOld state) state {
|
func updateState(sOld state) state {
|
||||||
s := sOld
|
s := sOld
|
||||||
|
|
||||||
|
var active *bot
|
||||||
for i := range s.bots {
|
for i := range s.bots {
|
||||||
updateBot(&s.bots[i], sOld)
|
if !s.bots[i].active {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
active = &s.bots[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
active.pos++
|
||||||
|
maybePassBaton(active, &s)
|
||||||
|
if won(*active, s) {
|
||||||
|
s.won = true
|
||||||
|
}
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateBot(b *bot, s state) {
|
func maybePassBaton(b *bot, s *state) {
|
||||||
if !b.active {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
b.pos++
|
|
||||||
maybePassBaton(b, s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func maybePassBaton(b *bot, s state) {
|
|
||||||
for i, bb := range s.bots {
|
for i, bb := range s.bots {
|
||||||
if b.pos == bb.pos {
|
if b == &bb {
|
||||||
continue // same bot
|
continue
|
||||||
}
|
}
|
||||||
if bb.pos-b.pos == 1 {
|
if bb.pos-b.pos == 1 {
|
||||||
b.active = false
|
b.active = false
|
||||||
|
@ -109,4 +116,8 @@ func maybePassBaton(b *bot, s state) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const steps = 500
|
func won(b bot, s state) bool {
|
||||||
|
return b.pos == steps
|
||||||
|
}
|
||||||
|
|
||||||
|
const steps = 150
|
||||||
|
|
Loading…
Reference in New Issue