A bit more for golang evolver

This commit is contained in:
Neale Pickett 2018-08-08 23:42:50 +00:00
parent 975c4111e9
commit ac5bc4115d
1 changed files with 10 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import (
"time" "time"
"strings" "strings"
"io/ioutil" "io/ioutil"
"encoding/json"
) )
type sensor struct { type sensor struct {
@ -17,6 +18,7 @@ type sensor struct {
} }
type Tank struct { type Tank struct {
dna []int
color string color string
sensors [10]sensor sensors [10]sensor
program []string program []string
@ -134,6 +136,7 @@ func program_of_dna(dna []int) []string {
n -= 720 n -= 720
// Stack. // Stack.
// XXX: This doesn't seem right, we shouldn't see a ton of "} } }" at the end
ret = append(ret, "{") ret = append(ret, "{")
stacks = append(stacks, (n + 1) % (len(dna) - i)) stacks = append(stacks, (n + 1) % (len(dna) - i))
} }
@ -147,7 +150,7 @@ func program_of_dna(dna []int) []string {
} }
func tank_of_dna(dna []int) Tank { func tank_of_dna(dna []int) Tank {
ret := Tank{} ret := Tank{dna: dna}
ret.color, dna = color_of_dna(dna) ret.color, dna = color_of_dna(dna)
for i := 0; i < len(ret.sensors); i += 1 { for i := 0; i < len(ret.sensors); i += 1 {
ret.sensors[i], dna = sensor_of_dna(dna) ret.sensors[i], dna = sensor_of_dna(dna)
@ -176,6 +179,9 @@ func (t Tank) Write(num int, dir string) {
) )
} }
twrite(num, "program", strings.Join(t.program, " ")) twrite(num, "program", strings.Join(t.program, " "))
s, _ := json.Marshal(t.dna)
twrite(num, "dna", string(s))
} }
func init() { func init() {
@ -183,7 +189,7 @@ func init() {
} }
func main() { func main() {
d := make_dna(20) d := make_dna(200)
p := program_of_dna(d) t := tank_of_dna(d)
fmt.Println(p) t.Write(0, ".")
} }