-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 課題3-1 タイピングゲームを作ろう | ||
|
||
## タイピングゲームの説明 | ||
|
||
``` | ||
go run main.go | ||
``` | ||
ランダムに果物の名前が表示され30秒のタイピングゲームを行う。 | ||
制限時間になると入力した問題数と正解した問題数が出力される。 | ||
|
||
以下結果出力例 | ||
|
||
``` | ||
total_input : 16 correct_answer : 15 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"math/rand" | ||
"os" | ||
"time" | ||
) | ||
|
||
const ( | ||
ExitCodeOK = iota | ||
) | ||
|
||
var words = []string{"apple", "banana", "strawberry", "kiwi", "cherry", "melon", "plum", "pear", "pineapple", "grape", "peach"} | ||
|
||
type inputer interface { | ||
input(io.Reader) <-chan string | ||
} | ||
|
||
type question struct { | ||
} | ||
|
||
type Vocabulary []string | ||
|
||
type Score struct { | ||
correctInput int | ||
totalInput int | ||
} | ||
|
||
func (w Vocabulary) chooseWord() string { | ||
index := rand.Intn(len(w)) | ||
return w[index] | ||
} | ||
|
||
func (q *question) 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 | ||
} | ||
|
||
func Run(candidates []string, i inputer) int { | ||
var score Score | ||
ch := i.input(os.Stdin) | ||
timeout := time.After(35 * time.Second) | ||
fmt.Println("---------Typing Start!! ---------") | ||
for { | ||
answer := Vocabulary.chooseWord(candidates) | ||
fmt.Println(answer) | ||
fmt.Print("> ") | ||
select { | ||
case input := <-ch: | ||
if answer == input { | ||
score.correctInput++ | ||
fmt.Printf("correct!!\n\n") | ||
} else { | ||
fmt.Printf("failed!\n\n") | ||
} | ||
score.totalInput++ | ||
case <-timeout: | ||
fmt.Printf("\n\n---------Timed Out---------\n\n") | ||
fmt.Printf("total_input : %d correct_answer : %d\n", score.totalInput, score.correctInput) | ||
return ExitCodeOK | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
q := &question{} | ||
os.Exit(Run(words, q)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"testing" | ||
) | ||
|
||
var q = question{} | ||
|
||
func TestInput(t *testing.T) { | ||
t.Helper() | ||
file, err := os.Open("testdata/sample.txt") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
defer file.Close() | ||
result := q.input(file) | ||
expects := []string{"banana", "apple"} | ||
for _, expected := range expects { | ||
r := <-result | ||
if r != expected { | ||
t.Error(r) | ||
t.Error(expected) | ||
} | ||
} | ||
} | ||
|
||
type testQuestion struct{} | ||
|
||
func (m *testQuestion) input(r io.Reader) <-chan string { | ||
ch := make(chan string) | ||
go func() { | ||
ch <- "banana" | ||
}() | ||
return ch | ||
} | ||
|
||
func TestRun(t *testing.T) { | ||
t.Helper() | ||
m := &testQuestion{} | ||
result := Run(words, m) | ||
if result != ExitCodeOK { | ||
t.Error(result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
banana | ||
apple |