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 by xlune #43

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
69 changes: 69 additions & 0 deletions kadai3/xlune/001/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"time"

"github.com/xlune/dojo1/kadai3/xlune/001/word"
)

var (
limitSec int
)

func init() {
flag.IntVar(&limitSec, "t", 30, "ゲームの制限時間")
}

func main() {
flag.Parse()

// 入力チャネル
ch := input(os.Stdin)
// タイムアウト設定
timeout := time.After(time.Duration(limitSec) * time.Second)
// 最初の問題生成
makeQuestion()

fmt.Printf("英語タイピングスタート!!\n(制限時間: %d 秒)\n\n", limitSec)
for {
fmt.Printf("出題 > %s\n", word.GetLatest())
select {
case result := <-ch:
if word.CheckLatest(result) {
fmt.Println("=> OK!!")
// 次の問題生成
makeQuestion()
} else {
fmt.Println("=> NG...")
}
case <-timeout:
fmt.Printf("終了それまで!!\n--\n正解数は %d 問でした。\n\n", word.CountHistory()-1)
os.Exit(0)
}
}
}

func makeQuestion() {
_, err := word.Issue()
if err != nil {
fmt.Println("Error: 出題できませんでした")
os.Exit(1)
}
}

func 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
}
Loading