Add multiple teams.

This commit is contained in:
Luke Meyers 2020-02-05 00:06:07 -08:00
parent d5f457ea91
commit d4d78078f8
1 changed files with 71 additions and 48 deletions

119
main.go
View File

@ -26,14 +26,11 @@ func run() {
start := time.Now() start := time.Now()
for !w.Closed() && !s.won { for !w.Closed() && !s.gameOver {
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!")
}
} }
} }
@ -43,78 +40,94 @@ func main() {
func render(s state, w *pixelgl.Window, d time.Duration) { func render(s state, w *pixelgl.Window, d time.Duration) {
b := w.Bounds() b := w.Bounds()
i := imdraw.New(nil) im := imdraw.New(nil)
offset := b.Size().X / steps hOffset := b.Size().X / steps
vOffset := b.Size().Y / (numTeams + 1)
for _, bot := range s.bots { for i, t := range s.teams {
if bot.active { for j, bot := range t.bots {
i.Color = pixel.RGB(0, 1, 0) if &t.bots[j] == t.baton.holder {
} else { im.Color = pixel.RGB(0, 1, 0)
i.Color = pixel.RGB(1, 0, 0) } else {
im.Color = pixel.RGB(1, 0, 0)
}
from := pixel.V(b.Min.X+25, b.Min.Y+float64(i+1)*vOffset)
pos := from.Add(pixel.V(float64(bot.pos)*hOffset, 0))
im.Push(pos)
im.Clear()
im.Circle(50, 0)
im.Draw(w)
} }
from := pixel.V(b.Min.X+25, b.Center().Y)
pos := from.Add(pixel.V(float64(bot.pos)*offset, 0))
i.Push(pos)
i.Clear()
i.Circle(50, 0)
i.Draw(w)
} }
} }
type state struct { type state struct {
bots []bot teams []team
won bool gameOver bool
} }
func newState() state { func newState() state {
var bots []bot var teams []team
for i := 0; i < numBots; i++ { for i := 0; i < numTeams; i++ {
bots = append(bots, bot{pos: i * (steps / numBots)}) var bots []bot
for j := 0; j < numBots; j++ {
bots = append(bots, bot{pos: j * (steps / numBots)})
}
teams = append(teams, team{
bots: bots,
baton: baton{holder: &bots[0]},
})
} }
bots[0].active = true
return state{ return state{
bots: bots, teams: teams,
} }
} }
type team struct {
bots []bot
baton baton
won bool
}
type bot struct { type bot struct {
pos int pos int
active bool }
type baton struct {
holder *bot
} }
func updateState(sOld state) state { func updateState(sOld state) state {
s := sOld s := sOld
var active *bot for _, t := range s.teams {
for i := range s.bots { b := t.baton.holder
if !s.bots[i].active { b.pos++
continue maybePassBaton(t)
}
active = &s.bots[i]
} }
active.pos++ for _, t := range s.teams {
maybePassBaton(active, &s) if won(*t.baton.holder, s) {
if won(*active, s) { s.gameOver = true
s.won = true }
} }
return s return s
} }
func maybePassBaton(b *bot, s *state) { func maybePassBaton(t team) {
for i, bb := range s.bots { for i, b := range t.bots {
if b == &bb { h := t.baton.holder
if h == &b {
continue continue
} }
if bb.pos-b.pos == 1 { if b.pos-h.pos == 1 {
b.active = false t.baton.holder = &t.bots[i]
s.bots[i].active = true log.Println("pass!")
return return
} }
} }
@ -124,7 +137,17 @@ func won(b bot, s state) bool {
return b.pos == steps return b.pos == steps
} }
func gameOver(s state) bool {
for _, t := range s.teams {
if t.won {
return true
}
}
return false
}
const ( const (
steps = 150 steps = 150
numBots = 5 numBots = 5
numTeams = 2
) )