randomize acceleration

This commit is contained in:
Luke Meyers 2020-02-05 18:27:27 -08:00
parent 661244fec4
commit 78f7e4f55c
2 changed files with 16 additions and 4 deletions

17
game.go
View File

@ -1,6 +1,9 @@
package main package main
import "log" import (
"log"
"math/rand"
)
type state struct { type state struct {
teams []team teams []team
@ -12,7 +15,11 @@ func newState() state {
for i := 0; i < numTeams; i++ { for i := 0; i < numTeams; i++ {
var bots []bot var bots []bot
for j := 0; j < numBots; j++ { for j := 0; j < numBots; j++ {
bots = append(bots, bot{pos: j * (steps / numBots)}) b := bot{
id: i*numTeams + j,
pos: j * (steps / numBots),
}
bots = append(bots, b)
} }
teams = append(teams, team{ teams = append(teams, team{
bots: bots, bots: bots,
@ -32,6 +39,7 @@ type team struct {
} }
type bot struct { type bot struct {
id int
pos int pos int
v int v int
a int a int
@ -50,6 +58,7 @@ func updateState(sOld state) state {
if b.a == 0 { if b.a == 0 {
b.a = 10 b.a = 10
} }
b.a += rand.Intn(3) - 1
b.v += b.a b.v += b.a
b.pos += b.v b.pos += b.v
@ -73,7 +82,7 @@ func maybePassBaton(t *team) {
continue continue
} }
if abs(b.pos-h.pos) <= 10 { if abs(b.pos-h.pos) <= 10 {
log.Printf("pass from %p to %p!", t.baton.holder, &t.bots[i]) log.Printf("pass from %v to %v!", h.id, b.id)
t.baton.holder.v = 0 t.baton.holder.v = 0
t.baton.holder.a = 0 t.baton.holder.a = 0
t.baton.holder = &t.bots[i] t.baton.holder = &t.bots[i]
@ -99,7 +108,7 @@ func gameOver(s state) bool {
const ( const (
steps = 400 steps = 400
numBots = 10 numBots = 10
numTeams = 4 numTeams = 1
) )
func abs(n int) int { func abs(n int) int {

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"math/rand"
"time" "time"
"github.com/faiface/pixel" "github.com/faiface/pixel"
@ -20,6 +21,8 @@ func run() {
panic(err) panic(err)
} }
rand.Seed(time.Now().UnixNano())
s := newState() s := newState()
colors := teamColors(s.teams) colors := teamColors(s.teams)