Skip to content

Commit

Permalink
minor refactoring; cmd/go-wordles -> game (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored Sep 13, 2022
1 parent b6e8726 commit c8d0cf0
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 91 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ default: run
all: test

build:
go build ./cmd/go-wordle
go build .

dist:
gox -ldflags="-s -w -X main.version=${VERSION}" \
gox -ldflags="-s -w -X github.com/jedib0t/go-wordle/game.version=${VERSION}" \
-os="linux darwin windows" \
-arch="amd64" \
-output="./dist/{{.Dir}}_{{.OS}}_{{.Arch}}" \
./cmd/go-wordle
.

fmt:
go fmt $(shell go list ./...)
Expand All @@ -31,7 +31,7 @@ lint:
golint -set_exit_status $(shell go list ./...)

run:
go run ./cmd/go-wordle
go run .

test: fmt tidy lint vet build
go test -cover -coverprofile=.coverprofile $(shell go list ./...)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ page.

If you want to build your own using GoLang:

* `go get -u github.com/jedib0t/go-wordle/cmd/go-wordle`
* `go install github.com/jedib0t/go-wordle`
* `go-wordle`

If you want to run from source:
Expand Down
2 changes: 1 addition & 1 deletion cmd/go-wordle/actions.go → game/actions.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package game

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion cmd/go-wordle/flags.go → game/flags.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package game

import (
"flag"
Expand Down
77 changes: 33 additions & 44 deletions cmd/go-wordle/game.go → game/game.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package game

import (
"strings"
Expand All @@ -22,39 +22,10 @@ var (
hints []string
)

func getUserInput() {
char, key, err := keyboard.GetSingleKey()
if err != nil {
logErrorAndExit("failed to get input: %v", err)
}

switch key {
case keyboard.KeyEsc, keyboard.KeyCtrlC:
handleActionQuit()
case keyboard.KeyCtrlD:
handleActionDecrementAttempts()
case keyboard.KeyCtrlI:
handleActionIncrementAttempts()
case keyboard.KeyCtrlR:
handleActionReset()
case keyboard.KeyBackspace, keyboard.KeyBackspace2:
handleActionBackSpace()
case keyboard.KeyEnter:
if inputCharStatus {
handleActionAttemptStatus()
} else {
handleActionAttempt()
}
default:
if inputCharStatus {
handleActionInputStatus(char)
} else {
handleActionInput(char)
}
}
}

func play() {
// Play starts the game.
func Play() {
defer cleanup()
generateWordles(*flagNumWordles)
cliAttempts := strings.Split(*flagAttempts, ",")
currAttempts = make([]wordle.Attempt, len(wordles))
hints = wordle.CombineHints(wordles...)
Expand Down Expand Up @@ -106,16 +77,34 @@ func play() {
wg.Wait()
}

func renderAsync(chStop chan bool, wg *sync.WaitGroup) {
defer wg.Done()
timer := time.Tick(time.Second / time.Duration(*flagRefreshRate))
for {
select {
case <-chStop: // render one final time and return
render(wordles, hints, currAttempts)
return
case <-timer: // render as part of regular cycle
render(wordles, hints, currAttempts)
func getUserInput() {
char, key, err := keyboard.GetSingleKey()
if err != nil {
logErrorAndExit("failed to get input: %v", err)
}

switch key {
case keyboard.KeyEsc, keyboard.KeyCtrlC:
handleActionQuit()
case keyboard.KeyCtrlD:
handleActionDecrementAttempts()
case keyboard.KeyCtrlI:
handleActionIncrementAttempts()
case keyboard.KeyCtrlR:
handleActionReset()
case keyboard.KeyBackspace, keyboard.KeyBackspace2:
handleActionBackSpace()
case keyboard.KeyEnter:
if inputCharStatus {
handleActionAttemptStatus()
} else {
handleActionAttempt()
}
default:
if inputCharStatus {
handleActionInputStatus(char)
} else {
handleActionInput(char)
}
}
}
Expand Down
16 changes: 2 additions & 14 deletions cmd/go-wordle/header_footer.go → game/header_footer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package game

import (
"fmt"
Expand All @@ -20,25 +20,13 @@ const (
// U+259x ▐ ░ ▒ ▓ ▔ ▕ ▖ ▗ ▘ ▙ ▚ ▛ ▜ ▝ ▞ ▟

titleText1 = `
▞ ▛▀▀▀▀▀▀▀▀▀▀▀▀▀▜ ▚
░ ▒ ▓ █ ▌ W O R D L E ▐ █ ▓ ▒ ░
▚ ▙▄▄▄▄▄▄▄▄▄▄▄▄▄▟ ▞
`
titleTextBig = `
░░ ░░ ░░░░░░ ░░░░░░ ░░░░░░ ░░ ░░░░░░░
▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒ ▒▒
▒▒ ▒ ▒▒ ▒▒ ▒▒ ▒▒▒▒▒▒ ▒▒ ▒▒ ▒▒ ▒▒▒▒▒
▓▓ ▓▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓
███ ███ ██████ ██ ██ ██████ ███████ ███████
`
titleTextScary = `
▄▄▌ ▐ ▄▌ ▄▄▄ ·▄▄▄▄ ▄▄▌ ▄▄▄ .
██· █▌▐█ ▄█▀▄ ▀▄ █·██· ██ ██• ▀▄.▀·
██▪▐█▐▐▌▐█▌.▐▌▐▀▀▄ ▐█▪ ▐█▌██ ▪ ▐▀▀▪▄
▐█▌██▐█▌▐█▌.▐▌▐█•█▌██. ██ ▐█▌ ▄▐█▄▄▌
▀▀▀▀ ▀▪ ▀█▄▀▪.▀ ▀▀▀▀▀▀• .▀▀▀ ▀▀▀
`
titleText = titleTextScary
titleText = titleText1
)

var (
Expand Down
2 changes: 1 addition & 1 deletion cmd/go-wordle/helpers.go → game/helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package game

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion cmd/go-wordle/keyboard.go → game/keyboard.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package game

import (
"fmt"
Expand Down
18 changes: 1 addition & 17 deletions cmd/go-wordle/main.go → game/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package main
package game

import (
"fmt"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
Expand Down Expand Up @@ -46,17 +44,3 @@ func cleanup() {
exitHandler()
}
}

func logErrorAndExit(msg string, a ...interface{}) {
_, _ = fmt.Fprintf(os.Stderr, "ERROR: "+strings.TrimSpace(msg)+"\n", a...)
cleanup()
os.Exit(-1)
}

func main() {
defer cleanup()

generateWordles(*flagNumWordles)

play()
}
21 changes: 18 additions & 3 deletions cmd/go-wordle/render.go → game/render.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main
package game

import (
"fmt"
"strings"
"sync"
"time"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
Expand Down Expand Up @@ -37,6 +38,20 @@ var (
renderMutex = sync.Mutex{}
)

func renderAsync(chStop chan bool, wg *sync.WaitGroup) {
defer wg.Done()
timer := time.Tick(time.Second / time.Duration(*flagRefreshRate))
for {
select {
case <-chStop: // render one final time and return
render(wordles, hints, currAttempts)
return
case <-timer: // render as part of regular cycle
render(wordles, hints, currAttempts)
}
}
}

func render(wordles []wordle.Wordle, hints []string, currAttempts []wordle.Attempt) {
renderMutex.Lock()
defer renderMutex.Unlock()
Expand Down Expand Up @@ -197,9 +212,9 @@ func renderKeyboardLegend(wordles []wordle.Wordle) string {
}

func renderKeyboardShortcuts() string {
shortcuts := "escape/ctrl+c to quit; ctrl+r to restart"
shortcuts := "quit: <ESC>/<Ctrl+C> | restart: <Ctrl+R>"
if *flagSolve {
shortcuts = "escape/ctrl+c to quit"
shortcuts = "quit: <ESC>/<Ctrl+C>"
}
shortcuts = text.AlignCenter.Apply(shortcuts, 56)
return colorHints.Sprint(shortcuts)
Expand Down
12 changes: 11 additions & 1 deletion cmd/go-wordle/utils.go → game/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main
package game

import (
"fmt"
"os"
"strings"

"github.com/jedib0t/go-pretty/v6/text"
"github.com/jedib0t/go-wordle/wordle"
)
Expand Down Expand Up @@ -52,3 +56,9 @@ func isGameOver(wordles []wordle.Wordle) bool {
}
return true
}

func logErrorAndExit(msg string, a ...interface{}) {
_, _ = fmt.Fprintf(os.Stderr, "ERROR: "+strings.TrimSpace(msg)+"\n", a...)
cleanup()
os.Exit(-1)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807
github.com/jedib0t/go-pretty/v6 v6.3.3
github.com/jedib0t/go-pretty/v6 v6.3.8
github.com/stretchr/testify v1.7.4
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 h1:jdjd5e68T4R/j4PWxfZqcKY8KtT9oo8IPNVuV4bSXDQ=
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807/go.mod h1:Xoiu5VdKMvbRgHuY7+z64lhu/7lvax/22nzASF6GrO8=
github.com/jedib0t/go-pretty/v6 v6.3.3 h1:shEWoyXqldeP54byATY3IczSfMC1b/UziOISaSxcvMQ=
github.com/jedib0t/go-pretty/v6 v6.3.3/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI=
github.com/jedib0t/go-pretty/v6 v6.3.8 h1:p5eZqLFMEGr7CC+9915lC4Dk7Gub6mH7NE35jDhkJsQ=
github.com/jedib0t/go-pretty/v6 v6.3.8/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/jedib0t/go-wordle/game"

func main() {
game.Play()
}

0 comments on commit c8d0cf0

Please sign in to comment.