Commit 17a3e3a1 authored by Boris Mühmer's avatar Boris Mühmer
Browse files

re-structured project again and added CLI arguments



Signed-off-by: default avatarBoris Mühmer <boris@muehmer.de>
parent 96fefbef
Loading
Loading
Loading
Loading

cmd/wator/main.go

0 → 100644
+137 −0
Original line number Diff line number Diff line
package main

import (
	"flag"
	"fmt"
	"strconv"
	"time"

	"golang.org/x/crypto/ssh/terminal"
	"repositories.muehmer.net/bsmrgo/wator"
)

func main() {
	// check terminal size
	width, height := 0, 0
	var err error
	if terminal.IsTerminal(0) {
		width, height, err = terminal.GetSize(0)
		if err != nil {
			panic(err)
		}
		fmt.Printf("Terminal: %d * %d\n", width, height)
	}

	// setup environment
	w := &wator.Wator{
		Debug:             false, //true,
		Width:             72,    //32,
		Height:            36,    //14,
		InitialFishes:     400,   //200,
		InitialSharks:     40,    //20,
		BreedFish:         3,
		BreedShark:        30,
		StarveShark:       2,
		SleepMilliseconds: 50 * time.Millisecond,
	}

	if width > 0 {
		w.Width = width
	}
	if height > 5 {
		w.Height = height - 5
	}

	flag.BoolVar(&w.Debug, "debug", false, "enable debug mode")
	flag.IntVar(&w.Width, "width", w.Width, "wator width")
	flag.IntVar(&w.Height, "height", w.Height, "wator height")
	flag.IntVar(&w.InitialFishes, "fish-count", 400, "initial number of fish")
	flag.IntVar(&w.InitialSharks, "shark-count", 40, "initial number of sharks")
	flag.IntVar(&w.BreedFish, "fish-breed", 3, "chronos for fish to breed")
	flag.IntVar(&w.BreedShark, "shark-breed", 30, "chronos for sharks to breed")
	flag.IntVar(&w.StarveShark, "shark-starve", 2, "chronos for sharks to starve")
	//flag.IntVar(&w.MaxAgeFishes, "fish-max-age", )
	flag.Parse()

	initialize(w)
	print(w)
	repl(w)
}

func repl(w *wator.Wator) {
	cycles := read()
	if cycles == 0 {
		return
	}
	for i := 0; i < cycles; i++ {
		if i > 0 {
			time.Sleep(w.SleepMilliseconds)
		}
		evaluate(w)
		print(w)
	}
	repl(w)
}

func initialize(w *wator.Wator) {
	if err := w.Populate(); err != nil {
		panic(err)
	}
}

func print(w *wator.Wator) {
	fmt.Printf("\033[H\033[2J") // TBD: remove this hack
	if err := w.Display(); err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", w)
}

func evaluate(w *wator.Wator) {
	if err := w.Next(); err != nil {
		panic(err)
	}
}

func read() int {
	cycles := 1
	done := false
	for !done {
		fmt.Printf("Command: ")
		var s string
		n, err := fmt.Scanf("%s", &s)
		if n == 0 {
			s = "n"
			err = nil
		}
		if err != nil {
			panic(err)
		}
		if num, err := strconv.Atoi(s); (err == nil) && (num > 0) && (num <= 100000) {
			done = true
			cycles = num
		}
		if !done {
			switch s {
			case "?", "h", "help":
				usage()
			case "n", "next":
				done = true
			case "q", "quit":
				cycles = 0
				done = true
			default:
				fmt.Printf("Unknown command \"%s\"\n", s)
			}
		}
	}
	return cycles
}

func usage() {
	fmt.Println("--- Commands ---")
	fmt.Println("?, h, help: show this usage information")
	fmt.Println("n, next   : calculate next iteration")
	fmt.Println("<number>  : do <number> iterations")
	fmt.Println("q, quit   : terminate wator")
}
+3 −1
Original line number Diff line number Diff line
module repositories.muehmer.net/bsmrgo/wator

go 1.12
go 1.13

require golang.org/x/crypto v0.0.0-20200117160349-530e935923ad

go.sum

0 → 100644
+8 −0
Original line number Diff line number Diff line
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg=
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=