diff --git a/kadai3-1/edm20627/go.mod b/kadai3-1/edm20627/go.mod new file mode 100644 index 00000000..0333a74b --- /dev/null +++ b/kadai3-1/edm20627/go.mod @@ -0,0 +1,3 @@ +module github.com/edm20627/gopherdojo-studyroom/kadai3-1/edm20627 + +go 1.15 diff --git a/kadai3-1/edm20627/main.go b/kadai3-1/edm20627/main.go new file mode 100644 index 00000000..1bfd36b0 --- /dev/null +++ b/kadai3-1/edm20627/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "os" + "time" + + "github.com/edm20627/gopherdojo-studyroom/kadai3-1/edm20627/typing" +) + +var words = []string{ + "go", + "java", + "ruby", + "php", + "javascript", + "python", + "kotlin", + "swift", + "c", +} + +var gameTime = 20 * time.Second + +func main() { + typing.Start(os.Stdin, os.Stdout, words, gameTime) +} diff --git a/kadai3-1/edm20627/typing/typing.go b/kadai3-1/edm20627/typing/typing.go new file mode 100644 index 00000000..56b9263a --- /dev/null +++ b/kadai3-1/edm20627/typing/typing.go @@ -0,0 +1,44 @@ +package typing + +import ( + "bufio" + "fmt" + "io" + "time" +) + +func Start(r io.Reader, w io.Writer, words []string, gameTime time.Duration) int { + var score int + timeLimit := time.After(gameTime) + ch := input(r) + fmt.Fprintln(w, "game start!!") + +L: + for _, word := range words { + fmt.Fprintln(w, word) + fmt.Fprint(w, ">") + select { + case answer := <-ch: + if word == answer { + score++ + } + case <-timeLimit: + fmt.Fprintf(w, "\ntime out!!\n") + break L + } + } + fmt.Fprintln(w, "score: ", score) + return score +} + +func input(r io.Reader) <-chan string { + ch := make(chan string) + go func() { + s := bufio.NewScanner(r) + for s.Scan() { + ch <- s.Text() + } + close(ch) + }() + return ch +} diff --git a/kadai3-1/edm20627/typing/typing_test.go b/kadai3-1/edm20627/typing/typing_test.go new file mode 100644 index 00000000..16bf9997 --- /dev/null +++ b/kadai3-1/edm20627/typing/typing_test.go @@ -0,0 +1,92 @@ +package typing_test + +import ( + "bytes" + "strings" + "testing" + "time" + + "github.com/edm20627/gopherdojo-studyroom/kadai3-1/edm20627/typing" +) + +var words = []string{ + "go", + "java", + "ruby", + "php", + "javascript", + "python", + "kotlin", + "swift", + "c", +} + +func TestStart(t *testing.T) { + cases := []struct { + name string + gameTime time.Duration + answer []string + score int + }{ + { + name: "success", + gameTime: 20 * time.Second, + answer: []string{ + "go", + "java", + "ruby", + "php", + "javascript", + "python", + "kotlin", + "swift", + "c", + }, + score: 9, + }, + { + name: "2 typos", + gameTime: 20 * time.Second, + answer: []string{ + "typo1", + "typo2", + "ruby", + "php", + "javascript", + "python", + "kotlin", + "swift", + "c", + }, + score: 7, + }, + { + name: "timeout", + gameTime: 0 * time.Second, + answer: []string{ + "go", + "java", + "ruby", + "php", + "javascript", + "python", + "kotlin", + "swift", + "c", + }, + score: 0, + }, + } + + for _, c := range cases { + + t.Run(c.name, func(t *testing.T) { + input := bytes.NewBufferString(strings.Join(c.answer, "\n")) + output := new(bytes.Buffer) + score := typing.Start(input, output, words, c.gameTime) + if c.score != score { + t.Errorf("expected %d, but got %d", c.score, score) + } + }) + } +}