Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kadai3 1 edm20627 #55

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kadai3-1/edm20627/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/edm20627/gopherdojo-studyroom/kadai3-1/edm20627

go 1.15
26 changes: 26 additions & 0 deletions kadai3-1/edm20627/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
44 changes: 44 additions & 0 deletions kadai3-1/edm20627/typing/typing.go
Original file line number Diff line number Diff line change
@@ -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
}
92 changes: 92 additions & 0 deletions kadai3-1/edm20627/typing/typing_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}