Skip to content

Commit

Permalink
Use bufio.Scanner and print color results
Browse files Browse the repository at this point in the history
  • Loading branch information
Dannflower committed Feb 14, 2022
1 parent 11a19e2 commit c8d323e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/Dannflower/godle

go 1.17

require (
github.com/fatih/color v1.13.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
61 changes: 50 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package main

import (
"bufio"
"errors"
"fmt"
"math/rand"
"os"
"strings"
"time"

"github.com/fatih/color"
)

const (
Expand All @@ -17,8 +21,11 @@ const (
wrongPosition
)

var scanner *bufio.Scanner

func init() {
rand.Seed(int64(time.Now().Nanosecond()))
scanner = bufio.NewScanner(os.Stdin)
}

func main() {
Expand Down Expand Up @@ -55,18 +62,18 @@ func handleMenuInput() {

for {

var input string

fmt.Print("Command: ")
fmt.Scanln(&input)
scanner.Scan()

switch input {
switch scanner.Text() {
case "p":
// Start new game
play()
printMenu()
case "r":
// Display rules, loop back to start of input
printRules()
printMenu()
case "q":
fmt.Println("Thanks for playing!")
os.Exit(0)
Expand All @@ -77,26 +84,30 @@ func handleMenuInput() {
}
}

func printRules() {
fmt.Println("The rules are...")
scanner.Scan()
}

func handleWin() {

fmt.Println("You got it! Hit enter to return to the menu.")
fmt.Scanln()
scanner.Scan()
}

// Start the core game loop.
func play() {

answer := selectWord()

fmt.Println("Answer: " + answer)
var guesses []string
var results [][]int

for {

guess := ""

fmt.Print("Guess: ")
fmt.Scanln(&guess)
scanner.Scan()

guess := scanner.Text()
result, err := compareRunes(convertToRunes(guess), convertToRunes(answer))

if err != nil {
Expand All @@ -105,7 +116,9 @@ func play() {

} else {

fmt.Println(result)
guesses = append(guesses, guess)
results = append(results, result)
printGuessResult(guesses, results)

// Player has won!
if guess == answer {
Expand All @@ -117,6 +130,32 @@ func play() {
}
}

// Prints the results of the last guess and all previous guesses
// with runes color coded depending on whether they are in the word,
// not in the word, or in the word but the wrong location.
func printGuessResult(guesses []string, results [][]int) {

for i, guess := range guesses {

capGuess := strings.ToUpper(guess)
colorResult := ""

for j, r := range capGuess {

switch results[i][j] {
case notInWord:
colorResult += color.HiBlackString(string(r))
case wrongPosition:
colorResult += color.YellowString(string(r))
case correctPosition:
colorResult += color.GreenString(string(r))
}
}

fmt.Println(colorResult)
}
}

// Converts the given string into a slice of runes.
func convertToRunes(word string) []rune {

Expand Down

0 comments on commit c8d323e

Please sign in to comment.