From 306a7b5c008f0836d55e7032c21e563e7726e821 Mon Sep 17 00:00:00 2001 From: edm20627 Date: Tue, 20 Apr 2021 01:01:47 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E5=88=9D=E6=9C=9F=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai3-1/edm20627/go.mod | 3 +++ kadai3-1/edm20627/main.go | 26 ++++++++++++++++++ kadai3-1/edm20627/typing/typing.go | 43 ++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 kadai3-1/edm20627/go.mod create mode 100644 kadai3-1/edm20627/main.go create mode 100644 kadai3-1/edm20627/typing/typing.go 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..f0b00275 --- /dev/null +++ b/kadai3-1/edm20627/typing/typing.go @@ -0,0 +1,43 @@ +package typing + +import ( + "bufio" + "fmt" + "io" + "time" +) + +func Start(r io.Reader, w io.Writer, words []string, gameTime time.Duration) { + 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) +} + +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 +} From c20e6ea58d2c2a10af5a02130b14cc3209c03671 Mon Sep 17 00:00:00 2001 From: edm20627 Date: Tue, 20 Apr 2021 02:16:00 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E7=94=A8?= =?UTF-8?q?=E3=81=ABStart=E9=96=A2=E6=95=B0=E3=81=AB=E6=88=BB=E3=82=8A?= =?UTF-8?q?=E5=80=A4=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai3-1/edm20627/typing/typing.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kadai3-1/edm20627/typing/typing.go b/kadai3-1/edm20627/typing/typing.go index f0b00275..56b9263a 100644 --- a/kadai3-1/edm20627/typing/typing.go +++ b/kadai3-1/edm20627/typing/typing.go @@ -7,7 +7,7 @@ import ( "time" ) -func Start(r io.Reader, w io.Writer, words []string, gameTime time.Duration) { +func Start(r io.Reader, w io.Writer, words []string, gameTime time.Duration) int { var score int timeLimit := time.After(gameTime) ch := input(r) @@ -28,6 +28,7 @@ L: } } fmt.Fprintln(w, "score: ", score) + return score } func input(r io.Reader) <-chan string { From 50aa2acaf6f34c6c5811f631913c426f997de488 Mon Sep 17 00:00:00 2001 From: edm20627 Date: Tue, 20 Apr 2021 02:16:40 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=B3?= =?UTF-8?q?=E3=83=BC=E3=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kadai3-1/edm20627/typing/typing_test.go | 92 +++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 kadai3-1/edm20627/typing/typing_test.go 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) + } + }) + } +}