Skip to content

Commit

Permalink
Introduced strategies to play automatically.
Browse files Browse the repository at this point in the history
Character frequency strategy.
Refactored the code a lot.
  • Loading branch information
archy-bold committed Jan 5, 2022
1 parent afd19a7 commit a5def44
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 218 deletions.
21 changes: 18 additions & 3 deletions game.go → game/game.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main
package game

import (
"strings"
)

// CellStatus represents whether a cell is in a state of empty, correct, incorrect or wrong
type CellStatus int

const (
Expand All @@ -13,17 +14,26 @@ const (
STATUS_WRONG CellStatus = 3
)

// GridCell represents a cell within a game grid
type GridCell struct {
Letter string
Status CellStatus
}

type Grid = [][]GridCell
// Grid represents a game grid
type Grid [][]GridCell

// Game represents a game that can be played
type Game interface {
// Play plays a word in the game
Play(word string) (bool, error)
// HasEnded returns whether the game has ended, whether with success or failure
HasEnded() bool
// GetScore gets the running or final score for the game
GetScore() (int, int)
// GetLastPlay returns the result of the last play
GetLastPlay() []GridCell
// OutputForConsole returns a string representation of the game for the command line
OutputForConsole() string
}

Expand Down Expand Up @@ -71,6 +81,10 @@ func (g *game) GetScore() (int, int) {
return g.attempts, len(g.grid)
}

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

func (g *game) OutputForConsole() string {
str := "\n" + strings.Repeat("-", len(g.answer)+2) + "\n"
for _, row := range g.grid {
Expand All @@ -95,8 +109,9 @@ func (g *game) OutputForConsole() string {
return str
}

// TODO include valid entries
// 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)

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

const (
NUM_LETTERS = 5
NUM_ATTEMPTS = 6

COLOUR_RESET = "\033[0m"
COLOUR_GREEN = "\033[32m"
COLOUR_YELLOW = "\033[33m"
)

func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
10 changes: 0 additions & 10 deletions helpers.go

This file was deleted.

Loading

0 comments on commit a5def44

Please sign in to comment.