Skip to content

Kadai3 1 kzkick2nd #60

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

Open
wants to merge 5 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
37 changes: 37 additions & 0 deletions kadai3-1/kzkick2nd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 課題3-1
- 標準出力に英単語を出す(出すものは自由)
- 標準入力から1行受け取る
- 制限時間内に何問解けたか表示する

## 基本設計
- main
- 入出力パッケージ if
- 出題と採点パッケージ dealer
- out if
- in if
- from 単語パッケージ words
- with 制限時間パッケージ clock
- with 残機システム lives
- to 採点集計パッケージ scorer
- to ランキングパッケージ ranking

## チャネルで繋げては?(チャネル練習)
game chan
word 単語を選ぶ
question 出題する
listener 回答を聞く
checker 答えあわせをする
scorer 採点する
timer 時間を計測する

## NOTE
- オープニング画面が出る
- 単語パッケージはgithub APIで単語取得(コミットメッセージ?ライブラリー名?)
- 難易度 = 単語長・制限時間(easy|normal|hard)
- 残機式。正解で残秒数が増える.
- ハイスコアを保存してランク表示

- チャネルはゴールーチン同士で通信するが、関数同士で共有できるのか?
- できる。呼び出し元でチャネルに格納する
- チャネルでベルトコンベアは作れるか?
- チャネル内でチャネルを動かせるか?(時間切れ測定ができる)
75 changes: 75 additions & 0 deletions kadai3-1/kzkick2nd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

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

func main() {
fmt.Println("Start Typing Game.")
fmt.Println("Type the word appearing.")

var correct int
var words = []string{
"archive",
"tar",
"zip",
"bufio",
"builtin",
"bytes",
"compress",
"bzip2",
"flate",
"gzip",
"lzw",
"zlib",
"container",
"heap",
"list",
"ring",
"context",
"crypto",
"aes",
"cipher",
}

ch := input(os.Stdin)
timeout := time.After(10 * time.Second)

for {
w := word(words)
fmt.Println(w)

select {
case <-timeout:
fmt.Println("Finish.")
fmt.Printf("Your score: %d\n", correct)
return
case answer := <-ch:
if w == answer {
correct++
}
}
}
}

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
}

func word(s []string) string {
rand.Seed(time.Now().UnixNano())
return s[rand.Intn(len(s))]
}