mirror of
https://github.com/Crocmagnon/gopher-the-number.git
synced 2024-11-21 23:48:03 +01:00
Implement a first working version of the game
This commit is contained in:
parent
7e4c8cece9
commit
ffc92dc861
3 changed files with 95 additions and 0 deletions
28
lib.go
28
lib.go
|
@ -1,5 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
func CheckGuess(guess, random int) (string, bool) {
|
func CheckGuess(guess, random int) (string, bool) {
|
||||||
if guess == random {
|
if guess == random {
|
||||||
return "Good job!", true
|
return "Good job!", true
|
||||||
|
@ -9,3 +16,24 @@ func CheckGuess(guess, random int) (string, bool) {
|
||||||
}
|
}
|
||||||
return "Nope, lower.", false
|
return "Nope, lower.", false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoopUntilFound(writer io.Writer, reader io.Reader, random int) {
|
||||||
|
scanner := bufio.NewScanner(reader)
|
||||||
|
win := false
|
||||||
|
count := 0
|
||||||
|
for !win {
|
||||||
|
fmt.Fprint(writer, "your guess: ")
|
||||||
|
scanner.Scan()
|
||||||
|
value := scanner.Text()
|
||||||
|
guess, _ := strconv.Atoi(value)
|
||||||
|
var msg string
|
||||||
|
msg, win = CheckGuess(guess, random)
|
||||||
|
fmt.Fprintf(writer, "\n%v\n", msg)
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
if count == 1 {
|
||||||
|
fmt.Fprintln(writer, "You got it right in 1 try.")
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(writer, "You got it right in %d tries.\n", count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
57
lib_test.go
57
lib_test.go
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -49,3 +51,58 @@ func assertCheckGuess(t testing.TB, guess, random int, wantStatus string, wantWo
|
||||||
t.Errorf("got %v want %v", won, wantWon)
|
t.Errorf("got %v want %v", won, wantWon)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoopUntilFound(t *testing.T) {
|
||||||
|
t.Run("found on first try", func(t *testing.T) {
|
||||||
|
output := bytes.Buffer{}
|
||||||
|
input := strings.NewReader("37\n")
|
||||||
|
random := 37
|
||||||
|
LoopUntilFound(&output, input, random)
|
||||||
|
|
||||||
|
got := output.String()
|
||||||
|
want := "your guess: \nGood job!\nYou got it right in 1 try.\n"
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q want %q", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("found on second try", func(t *testing.T) {
|
||||||
|
output := bytes.Buffer{}
|
||||||
|
input := strings.NewReader("50\n37\n")
|
||||||
|
random := 37
|
||||||
|
LoopUntilFound(&output, input, random)
|
||||||
|
|
||||||
|
got := output.String()
|
||||||
|
want := "your guess: \nNope, lower.\nyour guess: \nGood job!\nYou got it right in 2 tries.\n"
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q want %q", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("found after many tries", func(t *testing.T) {
|
||||||
|
output := bytes.Buffer{}
|
||||||
|
inputs := []string{"50", "25", "32", "37", ""}
|
||||||
|
input := strings.NewReader(strings.Join(inputs, "\n"))
|
||||||
|
random := 37
|
||||||
|
LoopUntilFound(&output, input, random)
|
||||||
|
|
||||||
|
got := output.String()
|
||||||
|
wants := []string{
|
||||||
|
"your guess: ",
|
||||||
|
"Nope, lower.",
|
||||||
|
"your guess: ",
|
||||||
|
"Try higher.",
|
||||||
|
"your guess: ",
|
||||||
|
"Try higher.",
|
||||||
|
"your guess: ",
|
||||||
|
"Good job!",
|
||||||
|
"You got it right in 4 tries.",
|
||||||
|
"",
|
||||||
|
}
|
||||||
|
want := strings.Join(wants, "\n")
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q want %q", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
10
main.go
10
main.go
|
@ -1 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
random := rand.Intn(100)
|
||||||
|
LoopUntilFound(os.Stdout, os.Stdin, random)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue