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 kotaaaa #88

Open
wants to merge 6 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
Empty file removed kadai3-1/.gitkeep
Empty file.
60 changes: 60 additions & 0 deletions kadai3-1/kotaaaa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Typing Game
- Receive one line from standard input
- Output English words to standard output (you can choose what to output)
- Receive one line from standard input
- (Original Function) Display word's meaning of target English word.

# How to use
```
$ go build -o game
$ ./game
( or $ go run .)
```

# How to test
```
$ go test ./... --count=1 -cover
? github.com/kotaaaa/gopherdojo-studyroom/kadai3-1/kotaaaa [no test files]
ok github.com/kotaaaa/gopherdojo-studyroom/kadai3-1/kotaaaa/questions 0.010s coverage: 100.0% of statements
ok github.com/kotaaaa/gopherdojo-studyroom/kadai3-1/kotaaaa/starter 0.005s coverage: 69.2% of statements
```

# Notes
- Word source: ielts-4000-academic-word
- https://tuxdoc.com/download/ielts-4000-academic-word-listpdf-4_pdf
- Vocabulary file format
- "word:meaning"


# How to play
```
$ go run .
Target: generation
generation
Meaning: all offspring at same stage from common ancestor; interval of time between the birth of parents and their offspring
Correct! 1pt
Target: centigrade
centigrade
Meaning: measure of temperature, used widely in Europe
Correct! 2pt
Target: shield
shield
Meaning: protective covering or structure; protect; guard
Correct! 3pt
Target: ultimate
ultimate
Meaning: final; being the last or concluding; fundamental; elemental; extreme
Correct! 4pt
Target: numerous
aaaaa
Meaning: many; various; amounting to a large indefinite number
Miss! 4pt Target: network
rrrr
Meaning: any system of lines or channels crossing like the fabric of a net; complex, interconnected group or system
Miss! 4pt Target: admit

======================
Times up! point: 4 pt
======================

```
3 changes: 3 additions & 0 deletions kadai3-1/kotaaaa/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/kotaaaa/gopherdojo-studyroom/kadai3-1/kotaaaa

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

import (
"fmt"
"os"
"time"

"github.com/kotaaaa/gopherdojo-studyroom/kadai3-1/kotaaaa/questions"
"github.com/kotaaaa/gopherdojo-studyroom/kadai3-1/kotaaaa/starter"
)

const wordPath string = "./testdata/ielts_words.txt" // Vocabulary file (Format > "word:meaning")
const limit_time int = 30 // Limit time

var vc *questions.Vocab

func main() {
vc = questions.ReadWords(wordPath) // read vocab file
timeCh := time.NewTimer(time.Duration(limit_time) * time.Second) // start timer

var cnt int

for {
c := starter.Solve(vc, timeCh)
switch c {
case starter.Success:
cnt++
fmt.Print("Correct! ", cnt, "pt \n") // Correct answer
case starter.Fail:
fmt.Print("Miss! ", cnt, "pt ") // Missed answer
case starter.TimeUp:
fmt.Println("\n======================\nTimes up! point: ", cnt, "pt\n======================") // time up process
os.Exit(0)
}
}
}
37 changes: 37 additions & 0 deletions kadai3-1/kotaaaa/questions/questions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package questions

import (
"bufio"
"math/rand"
"os"
"strings"
"time"
)

type Vocab struct {
Words []string
Meanings []string
}

// Read words from word file
func ReadWords(filePath string) *Vocab {
var vc Vocab
f, _ := os.Open(filePath)
defer f.Close()

scanner := bufio.NewScanner(f)
for scanner.Scan() {
vc.Words = append(vc.Words, strings.Split(scanner.Text(), ":")[0])
vc.Meanings = append(vc.Meanings, strings.Split(scanner.Text(), ":")[1])
}
return &vc
}

// Get random words from word list
func CreateProblem(vc *Vocab) int {
// random seed
rand.Seed(time.Now().UnixNano())
// get idx of target word
idx := rand.Intn(len(vc.Words))
return idx
}
28 changes: 28 additions & 0 deletions kadai3-1/kotaaaa/questions/questions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package questions

import (
"testing"
)

func TestReadWords(t *testing.T) {
const wordPath string = "../testdata/ielts_words.txt"
vc := ReadWords(wordPath)
expected := 4223
if len(vc.Words) != expected {
t.Errorf("Word count is illegal. Result: %v, Expected: %v", expected, vc.Words)
}
if len(vc.Words) != expected {
t.Errorf("Word meaning count is illegal.Result: %v, Expected: %v", expected, vc.Meanings)
}
}

func TestCreateProblem(t *testing.T) {
vc := Vocab{
Words: []string{"apple", "orange"},
Meanings: []string{"red fruit", "orange fruit"},
}
result := CreateProblem(&vc)
if result != 0 && result != 1 {
t.Errorf("Target Seed is illegal. Result: %v, Expected: 0 or 1", result)
}
}
41 changes: 41 additions & 0 deletions kadai3-1/kotaaaa/starter/starter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package starter

import (
"fmt"
"time"

"github.com/kotaaaa/gopherdojo-studyroom/kadai3-1/kotaaaa/questions"
)

type Status int

const (
Success Status = iota
Fail
TimeUp
)

func Solve(vc *questions.Vocab, timeCh *time.Timer) Status {

var input string // for input from user
idx := questions.CreateProblem(vc)
fmt.Println("Target: ", vc.Words[idx])
ch := make(chan string)
go func() {
// Read input by user
fmt.Scan(&input)
ch <- input
}()
// each cases
select {
case <-timeCh.C:
return TimeUp
case val := <-ch: // if there is user's input
fmt.Println("Meaning: ", vc.Meanings[idx])
if val == vc.Words[idx] {
return Success
} else {
return Fail
}
}
}
20 changes: 20 additions & 0 deletions kadai3-1/kotaaaa/starter/starter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package starter

import (
"testing"
"time"

"github.com/kotaaaa/gopherdojo-studyroom/kadai3-1/kotaaaa/questions"
)

func TestSolve(t *testing.T) {
vc := questions.Vocab{
Words: []string{"apple", "red fruit"},
Meanings: []string{"orange", "orange fruit"},
}
timeCh := time.NewTimer(time.Duration(0) * time.Second)
ret := Solve(&vc, timeCh)
if ret != 2 {
t.Errorf("Return is illegal. Result: %v, Expected: 2(Fail) ", ret)
}
}
Loading