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/gkoki #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
kadai3提出
yuan-h-1994 committed Jun 18, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 55bef48c3923f6180881669b6b9ab18a3f084e8e
3 changes: 3 additions & 0 deletions kadai3-1/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module main.go

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

import (
"bufio"
"flag"
"fmt"
"io"
"math/rand"
"os"
"time"
)

var t int

//制限時間をオプションにする
func init() {
flag.IntVar(&t, "t", 20, "制限時間s")
flag.Parse()
}

func main() {
fmt.Println("タイピングゲームが始まった!制限時間", t, "s")
num, score := 0, 0
timeout := time.After(time.Second * time.Duration(t))
for sign := true; sign == true; {
word := RandomWord()
num++
fmt.Println("単語NO:", num, "この英単語をタイピングしてください:", word)
c := imp(os.Stdin)
select {
case right := <-c:
if right == word {
fmt.Println("正解です!")
score++
} else {
fmt.Println("残念、不正解です。")
}
case <-timeout:
fmt.Println("時間です!")
sign = false
}
}
fmt.Println("ゲーム終了! やった単語数", num, " 時間内に正確単語数:", score)
}

//英単語をランダムに取り出す
func RandomWord() (word string) {
words := []string{"banana", "apple", "milk", "fruits", "cat", "car", "elephant", "unbralla", "interface", "tissues", "format"}
rand.Seed(time.Now().Unix())
num := rand.Intn(len(words))
return words[num]
}

//入力単語を取得する
func imp(r io.Reader) <-chan string {
wordCh := make(chan string, 1)
scanner := bufio.NewScanner(r)
scanner.Scan()
wordCh <- scanner.Text()
//close(wordCh)
return wordCh
}
12 changes: 12 additions & 0 deletions kadai3-1/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"testing"
)

func TestRandomWord(t *testing.T) {
word := RandomWord()
if word == nil {
t.Error("文字取り出さない")
}
}