-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.go
141 lines (115 loc) · 2.89 KB
/
core.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
package core
import (
"math/rand"
"time"
)
type NumberGenerator interface {
Next() int // 获取一个随机数
SetMaxNumber(number int) // 设置最大的数随机数
GetMaxNumber() int // 获取最大的数 最大的数
}
type Game interface {
SetNumber(number int) // 设置谜底数字
GetNumber() int // 获取谜底数字
GetGuess() int // 获取本地猜测的数字
SetGuess(guess int) // 设置本次猜测的数字
GetSmallest() int // 获取最小数
SetSmallest(number int) // 设置最小数
GetBiggest() int // 获取最大数
SetBiggest(number int) // 设置最大值
SetGuessTime(number int) // 设置猜测次数
GetGuessTime() int // 获取本轮猜测总次数
GetCurrentGuessTime() int // 当前猜测次数
OnGuess() // 消耗一次答题
Reset() // 重置
Check() // 检查猜测
IsValidNumberRange() bool // 输入是否在有效范围
IsGameWon() bool // 游戏是否成功
IsGameLost() bool // 游戏是否失败
}
type Generator struct {
maxNumber int
}
func (gen *Generator) Next() int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(gen.GetMaxNumber()) + 1
}
func (gen *Generator) GetMaxNumber() int {
return gen.maxNumber
}
func (gen *Generator) SetMaxNumber(number int) {
gen.maxNumber = number
}
type GuessGame struct {
number int
count int
currentGuessNumber int
currentCount int
answer []int
smallest int
biggest int
}
func (g *GuessGame) SetSmallest(number int) {
g.smallest = number
}
func (g *GuessGame) SetBiggest(number int) {
g.biggest = number
}
func (g *GuessGame) SetNumber(number int) {
g.number = number
}
func (g *GuessGame) GetNumber() int {
return g.number
}
func (g *GuessGame) GetGuess() int {
return g.currentGuessNumber
}
func (g *GuessGame) SetGuess(guess int) {
g.currentGuessNumber = guess
}
func (g *GuessGame) GetSmallest() int {
return g.smallest
}
func (g *GuessGame) GetBiggest() int {
return g.biggest
}
func (g *GuessGame) SetGuessTime(number int) {
g.count = number
}
func (g *GuessGame) GetCurrentGuessTime() int {
return g.currentCount
}
func (g *GuessGame) GetGuessTime() int {
return g.count
}
func (g *GuessGame) Reset() {
}
func (g *GuessGame) Check() {
if g.currentGuessNumber >= g.number {
g.biggest = g.currentGuessNumber
} else {
g.smallest = g.currentGuessNumber
}
}
func (g *GuessGame) IsValidNumberRange() bool {
if g.currentGuessNumber <= g.biggest && g.currentGuessNumber >= g.smallest {
return true
} else {
return false
}
}
func (g *GuessGame) IsGameWon() bool {
if g.currentGuessNumber == g.number {
return true
}
return false
}
func (g *GuessGame) IsGameLost() bool {
if g.currentCount == g.count {
return true
}
return false
}
func (g *GuessGame) OnGuess() {
g.currentCount++
}