-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgame.go
174 lines (144 loc) · 3.28 KB
/
game.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
Typing game main logic
*/
package game
import (
"bufio"
"context"
"fmt"
"io"
"math/rand"
"time"
"github.com/gopherdojo/dojo5/kadai3-1/nagaa052/pkg/questions"
)
const (
ExitOK = iota
ExitError
)
// Game is manages game information
type Game struct {
qs *questions.Questions
*Result
Options
inStream io.Reader
outStream, errStream io.Writer
}
// Options is a specifiable option.
type Options struct {
TimeUpSecond int
IsColor bool
}
// DefaultOptions is the default value of Options.
var DefaultOptions = Options{
TimeUpSecond: 30,
IsColor: false,
}
// Result is manages result information.
type Result struct {
Questions []*questions.Question
CorrectCount int
}
// Print is Output the result.
func (r *Result) Print(out io.Writer) {
cr := float64(r.CorrectCount) / float64(len(r.Questions)) * 100
fmt.Fprintln(out, "========================")
fmt.Fprintf(out, "Correct Count: %d\n", r.CorrectCount)
fmt.Fprintf(out, "Correct Rate: %.1f%\n", cr)
}
// New is Generate a new game.
func New(opt Options, inStream io.Reader, outStream, errStream io.Writer) (*Game, error) {
if opt.TimeUpSecond <= 0 {
opt.TimeUpSecond = DefaultOptions.TimeUpSecond
}
qs, err := questions.New()
if err != nil {
return nil, err
}
return &Game{
qs: qs,
Result: &Result{},
Options: opt,
inStream: inStream,
outStream: outStream,
errStream: errStream,
}, nil
}
// Start is Start Game
func (g *Game) Start() int {
g.printStart()
bc := context.Background()
ctx, cannel := context.WithTimeout(bc, time.Duration(g.TimeUpSecond)*time.Second)
defer cannel()
dst := g.startScanner()
return func() int {
for {
q, err := g.getQuestion()
if err != nil {
fmt.Fprintf(g.errStream, "%v\n", err.Error())
return ExitError
}
g.printQuestion(q.Word)
select {
case <-ctx.Done():
g.printTimeOut()
return ExitOK
case input := <-dst:
if q.IsCorrect(input) {
g.Result.CorrectCount++
}
}
}
}()
}
func (g *Game) startScanner() <-chan string {
dst := make(chan string)
go func() {
scanner := bufio.NewScanner(g.inStream)
defer close(dst)
for scanner.Scan() {
dst <- scanner.Text()
}
}()
return dst
}
func (g *Game) getQuestion() (*questions.Question, error) {
rand.Seed(time.Now().UnixNano())
index := rand.Intn(g.qs.GetSize())
q, err := g.qs.GetOne(index)
if err != nil {
return nil, err
}
g.Result.Questions = append(g.Result.Questions, q)
return q, nil
}
func (g *Game) printStart() {
for i := 3; i > 0; i-- {
fmt.Fprintf(g.outStream, "%d", i)
time.Sleep(250 * time.Millisecond)
fmt.Fprint(g.outStream, ".")
time.Sleep(250 * time.Millisecond)
fmt.Fprint(g.outStream, ".")
time.Sleep(250 * time.Millisecond)
fmt.Fprint(g.outStream, ".")
time.Sleep(250 * time.Millisecond)
}
fmt.Fprintln(g.outStream, "Start!!")
fmt.Fprintln(g.outStream, "========================")
time.Sleep(500 * time.Millisecond)
}
func (g *Game) printQuestion(word string) {
if g.IsColor {
cFPrint(green, g.outStream, fmt.Sprintf("%s", word))
} else {
fmt.Fprintf(g.outStream, "%s", word)
}
}
func (g *Game) printTimeOut() {
if g.IsColor {
cFPrint(red, g.outStream, "\nTimeUp!!!")
} else {
fmt.Fprintf(g.outStream, "\nTimeUp!!!")
}
time.Sleep(1 * time.Second)
g.Result.Print(g.outStream)
}