-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquestion_utils.go
67 lines (59 loc) · 1.51 KB
/
question_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package utils
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"time"
)
// 質問格納用struct
type Question struct {
Question string `json:"question"`
Answer string `json:"answer"`
}
// 回答結果の格納用struct
type Result struct {
Count int
Correct int
}
// JSONファイルから問題をロード
func LoadQuestions(path string) []Question {
bytes, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
var questions []Question
if err := json.Unmarshal(bytes, &questions); err != nil {
log.Fatal(err)
}
return questions
}
// 問題の配列からランダムに一問抽出
func GetQuestion(questions []Question) Question {
rand.Seed(time.Now().UnixNano())
rand_index := rand.Intn(len(questions))
return questions[rand_index]
}
// 問題の表示
func PopQuestion(questions []Question, q_count int) Question {
question := GetQuestion(questions)
fmt.Printf("Q%d \"%s\" は日本語で? : ", q_count, question.Question)
return question
}
// 回答があっているか確認して結果を表示
func CheckAnswer(question Question, answer string) bool {
correct := false
if answer == question.Answer {
fmt.Printf("正解です!\n")
correct = true
} else {
fmt.Printf("不正解です。 正解は、\"%s\" です。\n", question.Answer)
}
return correct
}
// 正解の合計を表示
func ShowResult(result Result) {
fmt.Printf("\n!!! タイムオーバーです。!!!\n")
fmt.Printf("問題%d問中、正解は%d問でした。\n", result.Count, result.Correct)
}