Skip to content

Commit

Permalink
Tetsing the standard game class.
Browse files Browse the repository at this point in the history
  • Loading branch information
archy-bold committed Jan 6, 2022
1 parent 16e6f77 commit 13c75c2
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 6 deletions.
19 changes: 16 additions & 3 deletions game/game.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package game

import (
"fmt"
"strings"
)

Expand Down Expand Up @@ -70,7 +71,12 @@ func (g *game) Play(word string) (bool, error) {
g.grid[g.attempts] = row
g.attempts++

return word == g.answer, nil
if word == g.answer {
g.complete = true
return true, nil
}

return false, nil
}

func (g *game) HasEnded() bool {
Expand All @@ -82,6 +88,9 @@ func (g *game) GetScore() (int, int) {
}

func (g *game) GetLastPlay() []GridCell {
if g.attempts == 0 {
return nil
}
return g.grid[g.attempts-1]
}

Expand All @@ -90,13 +99,17 @@ func (g *game) OutputForConsole() string {
}

func (g *game) OutputToShare() string {
return outputGridToShare(g.grid, g.attempts, len(g.grid))
score := fmt.Sprint(g.attempts)
if !g.complete && g.HasEnded() {
score = "X"
}
return outputGridToShare(g.grid, score, len(g.grid))
}

// CreateGame creates a game for the given answer and number of allowed tries
func CreateGame(answer string, tries int) Game {
// TODO include valid entries
grid := make([][]GridCell, tries)
grid := make(Grid, tries)

return &game{false, 0, answer, grid}
}
198 changes: 198 additions & 0 deletions game/game_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package game

import (
"testing"

"github.com/stretchr/testify/assert"
)

var (
gameTapirStart = &game{false, 0, "tapir", make(Grid, 6)}
gameTapirFinished = &game{true, 4, "tapir", Grid{
{GridCell{"g", STATUS_WRONG}, GridCell{"r", STATUS_INCORRECT}, GridCell{"o", STATUS_WRONG}, GridCell{"u", STATUS_WRONG}, GridCell{"p", STATUS_INCORRECT}},
{GridCell{"p", STATUS_INCORRECT}, GridCell{"r", STATUS_INCORRECT}, GridCell{"a", STATUS_INCORRECT}, GridCell{"n", STATUS_WRONG}, GridCell{"k", STATUS_WRONG}},
{GridCell{"s", STATUS_WRONG}, GridCell{"p", STATUS_INCORRECT}, GridCell{"a", STATUS_INCORRECT}, GridCell{"r", STATUS_INCORRECT}, GridCell{"e", STATUS_WRONG}},
{GridCell{"t", STATUS_CORRECT}, GridCell{"a", STATUS_CORRECT}, GridCell{"p", STATUS_CORRECT}, GridCell{"i", STATUS_CORRECT}, GridCell{"r", STATUS_CORRECT}},
nil,
nil,
}}
gameAtStart = &game{false, 0, "at", make(Grid, 1)}
gameAtFinished = &game{false, 1, "at", Grid{
{GridCell{"t", STATUS_INCORRECT}, GridCell{"a", STATUS_INCORRECT}},
}}
)

var createGameTests = map[string]struct {
answer string
tries int
expected *game
}{
"5 letter, 6 tries": {"tapir", 6, &game{false, 0, "tapir", Grid{nil, nil, nil, nil, nil, nil}}},
"3 letter, 3 tries": {"bat", 3, &game{false, 0, "bat", Grid{nil, nil, nil}}},
// "5 letter, uppercase": {"TAPIR", 6, &game{false, 0, "tapir", Grid{nil, nil, nil, nil, nil, nil}}},
}

func Test_CreateGame(t *testing.T) {
for tn, tt := range createGameTests {
g := CreateGame(tt.answer, tt.tries)

assert.Equalf(t, tt.expected, g, "Expected game to match for test '%s'", tn)
}
}

var gamePlayTests = map[string]struct {
g *game
tries []string
expected []bool
expectedErr string
expectedGrid Grid
}{
"5-letter, won": {
g: gameTapirStart,
tries: []string{"group", "prank", "spare", "tapir"},
expected: []bool{false, false, false, true},
expectedGrid: gameTapirFinished.grid,
},
"2-letter, lost": {
g: gameAtStart,
tries: []string{"ta"},
expected: []bool{false},
expectedGrid: gameAtFinished.grid,
},
}

func Test_game_Play(t *testing.T) {
for tn, tt := range gamePlayTests {
// Copy first
g := &game{tt.g.complete, tt.g.attempts, tt.g.answer, make(Grid, len(tt.g.grid))}
for i, row := range tt.g.grid {
copy(g.grid[i], row)
}
for i, word := range tt.tries {
res, _ := g.Play(word)

// Make the assertions
assert.Equalf(t, tt.expected[i], res, "Expected play outcome to match for test '%s'", tn)
assert.Equalf(t, tt.expected[i], g.complete, "Expected complete to match for test '%s'", tn)
assert.Equalf(t, tt.expectedGrid[i], g.grid[i], "Expected grid row to match for test '%s'", tn)
assert.Equalf(t, i+1, g.attempts, "Expected attempts to match for test '%s'", tn)
}
}
}

var gameHasEndedTests = map[string]struct {
g *game
expected bool
}{
"5-letter start": {gameTapirStart, false},
"5-letter finished": {gameTapirFinished, true},
"2-letter start": {gameAtStart, false},
"2-letter finished": {gameAtFinished, true},
}

func Test_game_HasEnded(t *testing.T) {
for tn, tt := range gameHasEndedTests {
assert.Equalf(t, tt.expected, tt.g.HasEnded(), "Expected result to match for test '%s'", tn)
}
}

var gameGetScoreTests = map[string]struct {
g *game
expectedScore int
expectedOf int
}{
"5-letter start": {gameTapirStart, 0, 6},
"5-letter finished": {gameTapirFinished, 4, 6},
"2-letter start": {gameAtStart, 0, 1},
"2-letter finished": {gameAtFinished, 1, 1},
}

func Test_game_GetScore(t *testing.T) {
for tn, tt := range gameGetScoreTests {
score, of := tt.g.GetScore()

assert.Equalf(t, tt.expectedScore, score, "Expected score to match for test '%s'", tn)
assert.Equalf(t, tt.expectedOf, of, "Expected of to match for test '%s'", tn)
}
}

var gameGetLastPlayTests = map[string]struct {
g *game
expected []GridCell
}{
"5-letter start": {gameTapirStart, nil},
"5-letter finished": {gameTapirFinished, gameTapirFinished.grid[3]},
"2-letter start": {gameAtStart, nil},
"2-letter finished": {gameAtFinished, gameAtFinished.grid[0]},
}

func Test_game_GetLastPlay(t *testing.T) {
for tn, tt := range gameGetLastPlayTests {
assert.Equalf(t, tt.expected, tt.g.GetLastPlay(), "Expected result to match for test '%s'", tn)
}
}

var gameOutputForConsoleTests = map[string]struct {
g *game
expected string
}{
"5-letter start": {
g: gameTapirStart,
expected: "\n-------\n-------\n",
},
"5-letter finished": {
g: gameTapirFinished,
expected: "\n-------\n" +
"|g" + COLOUR_RESET + COLOUR_YELLOW + "r" + COLOUR_RESET + "o" + COLOUR_RESET + "u" + COLOUR_RESET + COLOUR_YELLOW + "p" + COLOUR_RESET + "|\n" +
"|" + COLOUR_YELLOW + "p" + COLOUR_RESET + COLOUR_YELLOW + "r" + COLOUR_RESET + COLOUR_YELLOW + "a" + COLOUR_RESET + "n" + COLOUR_RESET + "k" + COLOUR_RESET + "|\n" +
"|s" + COLOUR_RESET + COLOUR_YELLOW + "p" + COLOUR_RESET + COLOUR_YELLOW + "a" + COLOUR_RESET + COLOUR_YELLOW + "r" + COLOUR_RESET + "e" + COLOUR_RESET + "|\n" +
"|" + COLOUR_GREEN + "t" + COLOUR_RESET + COLOUR_GREEN + "a" + COLOUR_RESET + COLOUR_GREEN + "p" + COLOUR_RESET + COLOUR_GREEN + "i" + COLOUR_RESET + COLOUR_GREEN + "r" + COLOUR_RESET + "|\n" +
"-------\n",
},
"2-letter start": {
g: gameAtStart,
expected: "\n----\n----\n",
},
"2-letter finished": {
g: gameAtFinished,
expected: "\n----\n|" + COLOUR_YELLOW + "t" + COLOUR_RESET + COLOUR_YELLOW + "a" + COLOUR_RESET + "|\n----\n",
},
}

func Test_game_OutputForConsole(t *testing.T) {
for tn, tt := range gameOutputForConsoleTests {
assert.Equalf(t, tt.expected, tt.g.OutputForConsole(), "Expected result to match for test '%s'", tn)
}
}

var gameOutputToShareTests = map[string]struct {
g *game
expected string
}{
"5-letter start": {
g: gameTapirStart,
expected: "Wordle 0/6\n\n\n",
},
"5-letter finished": {
g: gameTapirFinished,
expected: "Wordle 4/6\n\n" +
"⬜🟨⬜⬜🟨\n" +
"🟨🟨🟨⬜⬜\n" +
"⬜🟨🟨🟨⬜\n" +
"🟩🟩🟩🟩🟩\n\n",
},
"2-letter start": {
g: gameAtStart,
expected: "Wordle 0/1\n\n\n",
},
"2-letter finished": {
g: gameAtFinished,
expected: "Wordle X/1\n\n🟨🟨\n\n",
},
}

func Test_game_OutputToShare(t *testing.T) {
for tn, tt := range gameOutputToShareTests {
assert.Equalf(t, tt.expected, tt.g.OutputToShare(), "Expected result to match for test '%s'", tn)
}
}
5 changes: 3 additions & 2 deletions game/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func outputGridForConsole(grid [][]GridCell, length int) string {
return str
}

func outputGridToShare(grid [][]GridCell, score int, of int) string {
str := fmt.Sprintf("Wordle %d/%d\n\n", score, of)
func outputGridToShare(grid [][]GridCell, score string, of int) string {
// TODO output game number here
str := fmt.Sprintf("Wordle %s/%d\n\n", score, of)
for _, row := range grid {
if len(row) == 0 {
break
Expand Down
8 changes: 7 additions & 1 deletion game/unknown.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package game

import "fmt"

type UnknownGame struct {
complete bool
attempts int
Expand Down Expand Up @@ -49,7 +51,11 @@ func (g *UnknownGame) OutputForConsole() string {
}

func (g *UnknownGame) OutputToShare() string {
return outputGridToShare(g.grid, g.attempts, len(g.grid))
score := fmt.Sprint(g.attempts)
if !g.complete && g.HasEnded() {
score = "X"
}
return outputGridToShare(g.grid, score, len(g.grid))
}

// CreateGame creates a game for the given answer and number of allowed tries
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/archy-bold/wordle-go

go 1.12

require github.com/stretchr/testify v1.6.1
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 13c75c2

Please sign in to comment.