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 yuuyamad #44

Open
wants to merge 5 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
9 changes: 9 additions & 0 deletions kadai3/yuuyamad/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# typing game

## Command Line Options

### -t
typing game time

## example
./yuuyamad typing_game_word_examplefile.txt -t 60s
13 changes: 13 additions & 0 deletions kadai3/yuuyamad/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"log"
"os"
)

func logError(err error){
if err != nil{
log.Fatal(err)
os.Exit(1)
}
}
90 changes: 90 additions & 0 deletions kadai3/yuuyamad/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"fmt"
"os"
"time"
"bufio"
"strconv"
"flag"
)

func main() {

var (
seconds = flag.Duration("t", 10*time.Second, "timer set")
)
flag.Parse()
args := flag.Args()

//問題用のファイルを読み込み
word, err := readfile(args[0])
if err != nil {
logError(err)
}

fmt.Println("Typing GAME Start!!")

ch := quiz(word)
ch2 := time.After(*seconds)

j := 0

END:
for {
select {
case <-ch2:
break END
case _, ok := <-ch:
if ok {
j++
}else{
break END
}
}
}
s := strconv.Itoa(j)
fmt.Println("Correct Answer is " + s)

}
func readfile(file string) ([]string, error) {

var word []string
var fp *os.File
var err error

fp, err = os.Open(file)
if err != nil {
return nil, err
}
defer fp.Close()

var sc = bufio.NewScanner(fp);
sc.Split(bufio.ScanWords)
for sc.Scan(){
word = append(word, sc.Text())
}
return word, nil
}

func quiz(quizes []string) <-chan struct{}{
ch := make(chan struct{})

go func(){

for _, quiz := range quizes {
fmt.Print(quiz + ":")
s:= bufio.NewScanner(os.Stdin)
s.Scan()
scan := s.Text()
if quiz == scan{
ch <- struct{}{}
}

}
close(ch)

}()
return ch
}

36 changes: 36 additions & 0 deletions kadai3/yuuyamad/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"testing"
"reflect"
)

func TestReadfile(t *testing.T){

var tests = []struct {
filename string
word []string
}{
{"testdata/hoge.txt", []string{"hoge", "moga", "hoge", "moga", "moga", "fuga"}},
{"testdata/fuga.txt", []string{"hogehoge", "mogamoga", "fugafuga", "hogehoge"}},
{"testdata/moga.txt", []string{"hoga", "hoge", "fuga"}},
{"testdata/unknown", []string{}},

}
for _, test := range tests {
ret, err := readfile(test.filename)
if test.filename == "testdata/unknown" && err == nil {
t.Error(`readfile(testdata/unknown)`)
}

if test.filename != "testdata/unknown" && !reflect.DeepEqual(test.word, ret){
t.Error(`readfile(%q)`, test.filename)
}

}

}

func Testquiz(t *testing.T){

}
1 change: 1 addition & 0 deletions kadai3/yuuyamad/testdata/fuga.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hogehoge mogamoga fugafuga hogehoge
3 changes: 3 additions & 0 deletions kadai3/yuuyamad/testdata/hoge.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hoge moga
hoge moga
moga fuga
3 changes: 3 additions & 0 deletions kadai3/yuuyamad/testdata/moga.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hoga
hoge
fuga
3 changes: 3 additions & 0 deletions kadai3/yuuyamad/typing_game_word_examplefile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hoge moga
hoge moga
moga fuga