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 chokkoyamada #55

Open
wants to merge 2 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.idea
61 changes: 61 additions & 0 deletions kadai3-1/chokkoyamada/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

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

func input(r io.Reader) <-chan string {
c := make(chan string)
go func() {
s := bufio.NewScanner(r)
for s.Scan() {
c <- s.Text()
}
close(c)
}()
return c

}

func main() {
//制限時間
limit := 15
//問題の単語はこの中から毎回ランダムで抽出される
question := []string{"apple", "orange", "pine", "strawberry", "banana"}
rand.Seed(time.Now().Unix())

score := 0

fmt.Printf("タイピングゲーム開始! 表示された単語を入力してください。制限時間は%d秒です。\n", limit)
t := time.After(time.Duration(limit) * time.Second)

ch := input(os.Stdin)

M:
for {
s := question[rand.Intn(len(question))]
fmt.Printf(">%s\n", s)
select {
case v := <-ch:
fmt.Println(v)
if s == v {
fmt.Println("正解!")
score += 1
} else {
fmt.Println("間違い")
}

case <-t:
fmt.Println("タイムアップ!")
break M
}
}

fmt.Printf("タイピングゲーム終了です。スコアは%d点です。\n", score)

}