-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamestate.go
204 lines (174 loc) · 4.46 KB
/
gamestate.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package gamestate
import (
"errors"
"github.com/Amari-Mecheri/GoSnakeLogic/pkg/common"
"github.com/Amari-Mecheri/GoSnakeLogic/pkg/gameboard"
)
// GameStater is the gameState interface
type GameStater interface {
InitBoard(size common.Size) (err error)
FreeSpace() rune
SnakePart() rune
CandyBody() rune
CreateObjects() (listSprite []common.Sprite, err error)
Start()
Play() (listSprite []common.Sprite, err error)
GameInProgress() bool
SetGameInProgress(bool)
Dirty() bool
HighScore() int
Score() int
Round() int
MoveLeft()
MoveRight()
MoveDown()
MoveUp()
BoardSize() common.Size
SnakePosition() (position common.Position, err error)
SnakeTail() (position common.Position, err error)
SnakeDirection() (direction common.Direction, err error)
SnakeSize() (size int, err error)
}
type gameState struct {
gameInProgress bool
round int
score int
highScore int
dirty bool
gameboard.GameBoarder
}
// ErrInvalidBoardReference is a custom error thrown when the board object is nil
var ErrInvalidBoardReference = errors.New("the board object is nil")
var (
goLeft common.Direction = common.Direction{
DX: -1,
DY: 0,
}
goRight common.Direction = common.Direction{
DX: 1,
DY: 0,
}
goUp common.Direction = common.Direction{
DX: 0,
DY: -1,
}
goDown common.Direction = common.Direction{
DX: 0,
DY: 1,
}
)
// New returns an instance of gameState
func New() GameStater {
var aGameState gameState
aGameState.GameBoarder = gameboard.New()
return &aGameState
}
func (aGameState *gameState) InitBoard(size common.Size) (err error) {
defer common.ErrorWrapper(common.GetCurrentFuncName(), &err)
aGameState.GameBoarder = gameboard.New()
if err = aGameState.InitGameBoard(size); err != nil {
return err
}
aGameState.dirty = false
return nil
}
func (aGameState *gameState) FreeSpace() rune {
return gameboard.FreeSpace
}
func (aGameState *gameState) SnakePart() rune {
return gameboard.SnakePart
}
func (aGameState *gameState) CandyBody() rune {
return gameboard.CandyBody
}
func (aGameState *gameState) CreateObjects() (listSprite []common.Sprite, err error) {
defer common.ErrorWrapper(common.GetCurrentFuncName(), &err)
if aGameState.GameBoarder == nil {
return listSprite, ErrInvalidBoardReference
}
position := common.Position{
X: aGameState.BoardSize().Width / 2,
Y: aGameState.BoardSize().Height / 2,
}
snake, err := aGameState.CreateSnake(position, goRight)
if err != nil {
return nil, err
}
candy, err := aGameState.CreateCandy()
return []common.Sprite{snake, candy}, err
}
func (aGameState *gameState) Start() {
aGameState.gameInProgress = true
aGameState.score = 0
aGameState.round = 0
aGameState.dirty = true
}
func (aGameState *gameState) Play() (listSprite []common.Sprite, err error) {
defer common.ErrorWrapper(common.GetCurrentFuncName(), &err)
if aGameState.GameBoarder == nil {
return listSprite, ErrInvalidBoardReference
}
//Plays a round
aGameState.round++
//Move the snake
oldValue, spriteList, err := aGameState.MoveSnake()
if err != nil {
aGameState.gameInProgress = false
return spriteList, err
}
//Game over?
if aGameState.IsSnakePart(oldValue) {
aGameState.gameInProgress = false
return spriteList, nil
}
//Ate a candy?
if aGameState.IsCandy(oldValue) {
//Remove the candy since it's been eaten
aGameState.RemoveCandy()
//updates the score
aGameState.score++
//updates the highscore
if aGameState.score > aGameState.highScore {
aGameState.highScore = aGameState.score
}
}
//No more candies?
if !aGameState.CandyAlive() {
sprite, err := aGameState.CreateCandy()
if err != nil {
return nil, err
}
spriteList = append(spriteList, sprite)
}
return spriteList, nil
}
func (aGameState *gameState) GameInProgress() bool {
return aGameState.gameInProgress
}
func (aGameState *gameState) SetGameInProgress(val bool) {
aGameState.gameInProgress = val
}
func (aGameState *gameState) Dirty() bool {
return aGameState.dirty
}
func (aGameState *gameState) HighScore() int {
return aGameState.highScore
}
func (aGameState *gameState) Score() int {
return aGameState.score
}
func (aGameState *gameState) Round() int {
return aGameState.round
}
func (aGameState *gameState) MoveLeft() {
aGameState.SetSnakeDirection(goLeft)
}
func (aGameState *gameState) MoveRight() {
aGameState.SetSnakeDirection(goRight)
}
func (aGameState *gameState) MoveDown() {
aGameState.SetSnakeDirection(goDown)
}
func (aGameState *gameState) MoveUp() {
aGameState.SetSnakeDirection(goUp)
}