-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Functionality to play a game with a given word.
- Loading branch information
1 parent
9a1658c
commit 7551752
Showing
4 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type CellStatus int | ||
|
||
const ( | ||
STATUS_EMPTY CellStatus = 0 | ||
STATUS_CORRECT CellStatus = 1 | ||
STATUS_INCORRECT CellStatus = 2 | ||
STATUS_WRONG CellStatus = 3 | ||
) | ||
|
||
type GridCell struct { | ||
Letter string | ||
Status CellStatus | ||
} | ||
|
||
type Grid = [][]GridCell | ||
|
||
type Game interface { | ||
Play(word string) (bool, error) | ||
HasEnded() bool | ||
GetScore() (int, int) | ||
OutputForConsole() string | ||
} | ||
|
||
type game struct { | ||
complete bool | ||
attempts int | ||
answer string | ||
grid Grid | ||
} | ||
|
||
func (g *game) Play(word string) (bool, error) { | ||
// TODO check the word length here and error if too long/short | ||
|
||
// Create the row for the grid | ||
parts := strings.Split(word, "") | ||
answerParts := strings.Split(g.answer, "") | ||
row := make([]GridCell, len(parts)) | ||
numCorrect := 0 | ||
for i, chr := range parts { | ||
var status CellStatus | ||
if chr == answerParts[i] { | ||
status = STATUS_CORRECT | ||
numCorrect++ | ||
} else if stringInSlice(chr, answerParts) { | ||
status = STATUS_INCORRECT | ||
} else { | ||
status = STATUS_WRONG | ||
} | ||
|
||
row[i] = GridCell{chr, status} | ||
} | ||
|
||
// Update the game | ||
g.grid[g.attempts] = row | ||
g.attempts++ | ||
|
||
return word == g.answer, nil | ||
} | ||
|
||
func (g *game) HasEnded() bool { | ||
return g.complete || g.attempts == len(g.grid) | ||
} | ||
|
||
func (g *game) GetScore() (int, int) { | ||
return g.attempts, len(g.grid) | ||
} | ||
|
||
func (g *game) OutputForConsole() string { | ||
str := "\n" + strings.Repeat("-", len(g.answer)+2) + "\n" | ||
for _, row := range g.grid { | ||
if len(row) == 0 { | ||
break | ||
} | ||
|
||
str += "|" | ||
for _, cell := range row { | ||
switch cell.Status { | ||
case STATUS_CORRECT: | ||
str += COLOUR_GREEN | ||
case STATUS_INCORRECT: | ||
str += COLOUR_YELLOW | ||
} | ||
str += cell.Letter + COLOUR_RESET | ||
} | ||
str += "|\n" | ||
} | ||
str += strings.Repeat("-", len(g.answer)+2) + "\n" | ||
|
||
return str | ||
} | ||
|
||
// TODO include valid entries | ||
func CreateGame(answer string, tries int) Game { | ||
grid := make([][]GridCell, tries) | ||
|
||
return &game{false, 0, answer, grid} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
func stringInSlice(a string, list []string) bool { | ||
for _, b := range list { | ||
if b == a { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters