src

Go monorepo.
Log | Files | Refs

commit 02d19d9e9b676ba8186e18cf665b1b64de5e8296
parent dc4abee56e21a9f91a9b6d14d18308170ea6abda
Author: dwrz <dwrz@dwrz.net>
Date:   Mon, 26 Dec 2022 17:13:55 +0000

Refactor minotaur automatic size

Diffstat:
Mcmd/minotaur/main.go | 20++++++++++++++++----
Mpkg/minotaur/minotaur.go | 10++++++++++
2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/cmd/minotaur/main.go b/cmd/minotaur/main.go @@ -15,8 +15,8 @@ import ( ) var ( - height = flag.Int("h", 2, "height") - width = flag.Int("w", 2, "width") + height = flag.Int("h", 0, "height") + width = flag.Int("w", 0, "width") tick = flag.Int("t", 250, "ms between minotaur movement") ) @@ -25,10 +25,10 @@ func main() { // Parse flags. flag.Parse() - if *height <= 0 { + if *height < 0 { l.Error.Fatalf("invalid height: %d", *height) } - if *width <= 0 { + if *width < 0 { l.Error.Fatalf("invalid width: %d", *width) } if *tick <= 0 { @@ -62,6 +62,18 @@ func main() { if err != nil { l.Error.Fatalf("failed to get terminal attributes: %v", err) } + size, err := t.Size() + if err != nil { + l.Error.Fatalf("failed to get terminal size: %v", err) + } + if *height == 0 { + rows := int(size.Rows) + height = &rows + } + if *width == 0 { + cols := int(size.Columns) + 1 + width = &cols + } // TODO: refactor; handle sigwinch. go func() { diff --git a/pkg/minotaur/minotaur.go b/pkg/minotaur/minotaur.go @@ -58,6 +58,16 @@ type Parameters struct { } func New(p Parameters) (*Game, error) { + // Account for two spaces per cell; horizontal passages and walls. + // Account an extra row for the right wall. + if p.Width%4 == 0 { + p.Width -= 1 + } + p.Width /= 4 + + // Account extra rows for vertical passages and walls. + p.Height /= 2 + var g = &Game{ errs: make(chan error), events: make(chan *input.Event),