Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display keyboard #9

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 0.0.8
VERSION = 0.1.0
SOURCE = ./...

.DEFAULT_GOAL := build
Expand Down
51 changes: 50 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
absent evaluation = iota
present
correct
unknown
)

var (
Expand Down Expand Up @@ -87,6 +88,15 @@ type wordle struct {
// evaluations is a slice of slices, representing an evaluation of each character of each guess.
evaluations [maxGuesses][wordLength]evaluation

// keyboardChars is a slice of all keyboard characters
// Because Go does not preserve the order of maps, keyboardChars
// stores the keyboard key characters in the order in which they
// typically appear on many keyboards.
keyboardChars []rune

// keyboard is a map of alphabetical keyboard keys to an evaluation of each key character
keyboard map[rune]evaluation

// guessIndex is the current guess index.
guessIndex int

Expand All @@ -102,6 +112,29 @@ func (w *wordle) displaySolution() {
w.write("\n")
}

func (w *wordle) displayKeyboard() {
w.write("\n")

for _, char := range w.keyboardChars {
if char == 'A' || char == 'Z' {
w.write("\n")
}

switch w.keyboard[char] {
case correct:
w.displayGreenTile(char)
case present:
w.displayYellowTile(char)
case absent:
w.displayGrayTile(' ')
default:
w.displayGrayTile(char)
}
}

w.write("\n")
}

func (w *wordle) displayGrid() {
for i, guess := range w.guesses {
for j, guessLetter := range guess {
Expand Down Expand Up @@ -192,10 +225,19 @@ func (w *wordle) run() {

if len(guess) == len(solution) {
w.guesses[w.guessIndex] = guess
w.evaluations[w.guessIndex] = w.evaluateGuess(guess)
evaluations := w.evaluateGuess(guess)

w.evaluations[w.guessIndex] = evaluations

for i, eval := range evaluations {
w.keyboard[[]rune(string(guess[i]))[0]] = eval
}

w.displayGrid()
}

w.displayKeyboard()

if guess == solution {
break
}
Expand Down Expand Up @@ -232,6 +274,13 @@ func newWordle(word string, in io.Reader, out io.Writer) *wordle {
w.guesses[i] = emptyGuess
}

w.keyboardChars = []rune("QWERTYUIOPASDFGHJKLZXCVBNM")
// Seed the initial keyboard with each character having an unknown evaluation.
w.keyboard = map[rune]evaluation{}
for _, char := range w.keyboardChars {
w.keyboard[char] = unknown
}

return w
}

Expand Down
107 changes: 107 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ import (
)

func TestRun(t *testing.T) {
emptyChar := fmt.Sprintf("\033[40m\033[1;37m %s \033[m\033[m", emptyGuessChar)
emptyRow := strings.Join([]string{
emptyChar,
emptyChar,
emptyChar,
emptyChar,
emptyChar,
"\n",
}, "")
emptyRows := strings.Join([]string{
emptyRow,
emptyRow,
emptyRow,
emptyRow,
emptyRow,
}, "")

tests := []struct {
word string
inputs []string
Expand Down Expand Up @@ -42,6 +59,36 @@ func TestRun(t *testing.T) {
"\033[42m\033[1;30m C \033[m\033[m",
"\033[42m\033[1;30m H \033[m\033[m",
"\n",
emptyRows,
"\n",
"\033[40m\033[1;37m Q \033[m\033[m",
"\033[40m\033[1;37m W \033[m\033[m",
"\033[42m\033[1;30m E \033[m\033[m",
"\033[40m\033[1;37m R \033[m\033[m",
"\033[40m\033[1;37m T \033[m\033[m",
"\033[40m\033[1;37m Y \033[m\033[m",
"\033[40m\033[1;37m U \033[m\033[m",
"\033[40m\033[1;37m I \033[m\033[m",
"\033[40m\033[1;37m O \033[m\033[m",
"\033[40m\033[1;37m P \033[m\033[m",
"\n",
"\033[42m\033[1;30m A \033[m\033[m",
"\033[40m\033[1;37m S \033[m\033[m",
"\033[40m\033[1;37m D \033[m\033[m",
"\033[40m\033[1;37m F \033[m\033[m",
"\033[40m\033[1;37m G \033[m\033[m",
"\033[42m\033[1;30m H \033[m\033[m",
"\033[40m\033[1;37m J \033[m\033[m",
"\033[40m\033[1;37m K \033[m\033[m",
"\033[40m\033[1;37m L \033[m\033[m",
"\n",
"\033[40m\033[1;37m Z \033[m\033[m",
"\033[40m\033[1;37m X \033[m\033[m",
"\033[42m\033[1;30m C \033[m\033[m",
"\033[40m\033[1;37m V \033[m\033[m",
"\033[42m\033[1;30m B \033[m\033[m",
"\033[40m\033[1;37m N \033[m\033[m",
"\033[40m\033[1;37m M \033[m\033[m",
}, ""),
}, {
word: "BEATS",
Expand All @@ -53,6 +100,36 @@ func TestRun(t *testing.T) {
"\033[43m\033[1;30m S \033[m\033[m",
"\033[43m\033[1;30m T \033[m\033[m",
"\n",
emptyRows,
"\n",
"\033[40m\033[1;37m Q \033[m\033[m",
"\033[40m\033[1;37m W \033[m\033[m",
"\033[40m\033[1;37m E \033[m\033[m",
"\033[40m\033[1;37m \033[m\033[m",
"\033[43m\033[1;30m T \033[m\033[m",
"\033[40m\033[1;37m Y \033[m\033[m",
"\033[40m\033[1;37m \033[m\033[m",
"\033[40m\033[1;37m I \033[m\033[m",
"\033[40m\033[1;37m O \033[m\033[m",
"\033[40m\033[1;37m P \033[m\033[m",
"\n",
"\033[40m\033[1;37m A \033[m\033[m",
"\033[43m\033[1;30m S \033[m\033[m",
"\033[40m\033[1;37m D \033[m\033[m",
"\033[40m\033[1;37m F \033[m\033[m",
"\033[40m\033[1;37m G \033[m\033[m",
"\033[40m\033[1;37m H \033[m\033[m",
"\033[40m\033[1;37m J \033[m\033[m",
"\033[40m\033[1;37m K \033[m\033[m",
"\033[40m\033[1;37m L \033[m\033[m",
"\n",
"\033[40m\033[1;37m Z \033[m\033[m",
"\033[40m\033[1;37m X \033[m\033[m",
"\033[40m\033[1;37m C \033[m\033[m",
"\033[40m\033[1;37m V \033[m\033[m",
"\033[42m\033[1;30m B \033[m\033[m",
"\033[40m\033[1;37m N \033[m\033[m",
"\033[40m\033[1;37m M \033[m\033[m",
}, ""),
}, {
word: "BOOTY",
Expand All @@ -64,6 +141,36 @@ func TestRun(t *testing.T) {
"\033[40m\033[1;37m S \033[m\033[m",
"\033[40m\033[1;37m E \033[m\033[m",
"\n",
emptyRows,
"\n",
"\033[40m\033[1;37m Q \033[m\033[m",
"\033[40m\033[1;37m W \033[m\033[m",
"\033[40m\033[1;37m \033[m\033[m",
"\033[40m\033[1;37m \033[m\033[m",
"\033[40m\033[1;37m T \033[m\033[m",
"\033[40m\033[1;37m Y \033[m\033[m",
"\033[40m\033[1;37m U \033[m\033[m",
"\033[40m\033[1;37m \033[m\033[m",
"\033[40m\033[1;37m O \033[m\033[m",
"\033[40m\033[1;37m P \033[m\033[m",
"\n",
"\033[40m\033[1;37m \033[m\033[m",
"\033[40m\033[1;37m \033[m\033[m",
"\033[40m\033[1;37m D \033[m\033[m",
"\033[40m\033[1;37m F \033[m\033[m",
"\033[40m\033[1;37m G \033[m\033[m",
"\033[40m\033[1;37m H \033[m\033[m",
"\033[40m\033[1;37m J \033[m\033[m",
"\033[40m\033[1;37m K \033[m\033[m",
"\033[40m\033[1;37m L \033[m\033[m",
"\n",
"\033[40m\033[1;37m Z \033[m\033[m",
"\033[40m\033[1;37m X \033[m\033[m",
"\033[40m\033[1;37m C \033[m\033[m",
"\033[40m\033[1;37m V \033[m\033[m",
"\033[40m\033[1;37m B \033[m\033[m",
"\033[40m\033[1;37m N \033[m\033[m",
"\033[40m\033[1;37m M \033[m\033[m",
}, ""),
}}

Expand Down