-
Notifications
You must be signed in to change notification settings - Fork 1
/
halma.go
279 lines (228 loc) · 5.97 KB
/
halma.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2013 by Jörg Müller <[email protected]> All rights reserved.
//
// This file is part of stern-halma.
//
// stern-halma is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// stern-halma is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with stern-halma. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"math/rand"
)
type HalmaColor int
const (
Empty HalmaColor = iota
Red
Green
Blue
)
type HalmaGameState int
const (
New HalmaGameState = iota
Running
Done
)
type HalmaField struct {
Type HalmaColor
Pin HalmaColor
}
type Player struct {
Name string
Password string
}
type HalmaGame struct {
ID int
State HalmaGameState
Fields [17][17]*HalmaField
CurrentPlayer HalmaColor
Players []*HalmaGamePlayer
Clients []HalmaClient
}
type HalmaGamePlayer struct {
Player *Player
Game *HalmaGame
Color HalmaColor
}
type HalmaClient interface {
NotifyMove(color HalmaColor, from Position, to Position)
NotifyTurn(current HalmaColor)
}
type Position struct {
X int
Y int
}
func abs(x int) int {
if(x < 0) {
return -x
}
return x
}
func (g *HalmaGame) createField(pos Position, Type HalmaColor, Pin HalmaColor) {
g.Fields[pos.X + 8][pos.Y + 8] = &HalmaField{Type, Pin}
}
func (g *HalmaGame) fieldValid(pos Position) bool {
return abs(pos.X) <= 8 && abs(pos.Y) <= 8 && g.getField(pos) != nil
}
func (g *HalmaGame) getField(pos Position) *HalmaField {
return g.Fields[pos.X + 8][pos.Y + 8]
}
func (g *HalmaGame) calculatePossible(position Position) []Position {
var visited [17][17]bool
neighbors := []Position{
{1, 0}, {1, -1}, {0, -1},
{-1, 0}, {-1, 1}, {0, 1},
}
todo := []Position{position}
possible := make([]Position, 0)
first := true
var pos Position
for len(todo) > 0 {
pos, todo = todo[len(todo) - 1], todo[:len(todo) - 1]
if visited[pos.X + 8][pos.Y + 8] {
continue
}
if !first {
possible = append(possible, pos)
}
visited[pos.X + 8][pos.Y + 8] = true
for i := 0; i < len(neighbors); i++ {
nb1 := Position{pos.X + neighbors[i].X, pos.Y + neighbors[i].Y}
nb2 := Position{pos.X + 2 * neighbors[i].X, pos.Y + 2 * neighbors[i].Y}
if g.fieldValid(nb1) && g.fieldValid(nb2) && g.getField(nb1).Pin != Empty && g.getField(nb2).Pin == Empty {
todo = append(todo, nb2)
}
if first && g.fieldValid(nb1) && g.getField(nb1).Pin == Empty {
possible = append(possible, nb1)
}
}
first = false
}
return possible
}
func (g *HalmaGame) checkWinner(color HalmaColor) bool {
for x := 0; x < len(g.Fields); x++ {
for y := 0; y < len(g.Fields[x]); y++ {
if g.Fields[x][y] != nil {
if g.Fields[x][y].Type == color && g.Fields[x][y].Type != g.Fields[x][y].Pin {
return false
}
}
}
}
return true
}
func (g *HalmaGame) GetFreeColor() HalmaColor {
color := HalmaColor(len(g.Players) + 1)
if color > 3 {
color = Empty
}
return color
}
func (g *HalmaGame) GetPlayerColor(player *Player) HalmaColor {
hgp := g.GetPlayer(player)
if hgp != nil {
return hgp.Color
}
return Empty
}
func (g *HalmaGame) Start() {
green := Empty
blue := Empty
if len(g.Players) > 1 {
green = Green
if len(g.Players) > 2 {
blue = Blue
}
}
for u := -8; u <= 8; u++ {
for v := -8; v <= 8; v++ {
tu := abs(u)
tv := abs(v)
tw := abs(-u - v)
if (tu <= 8 && tv <= 4 && tw <= 4) || (tu <= 4 && tv <= 8 && tw <=4) || (tu <= 4 && tv <= 4 && tw <=8) {
if (tu + tv + tw) / 2 < 4 {
g.createField(Position{u, v}, Empty, Empty)
} else if tv >= 4 && tv > tu && tv > tw {
if v > 0 {
g.createField(Position{u, v}, Empty, Red)
} else {
g.createField(Position{u, v}, Red, Empty)
}
} else if tw >= 4 && tw > tv && tw > tu {
if -u - v > 0 {
g.createField(Position{u, v}, Empty, green)
} else {
g.createField(Position{u, v}, Green, Empty)
}
} else if tu >= 4 && tu > tv && tu > tw {
if u > 0 {
g.createField(Position{u, v}, Empty, blue)
} else {
g.createField(Position{u, v}, Blue, Empty)
}
}
}
}
}
g.createField(Position{0, -4}, Red, green)
g.createField(Position{-4, 0}, Blue, green)
g.createField(Position{-4, 4}, Blue, Red)
g.createField(Position{0, 4}, Green, Red)
g.createField(Position{4, 0}, Green, blue)
g.createField(Position{4, -4}, Red, blue)
g.State = Running
g.CurrentPlayer = HalmaColor(rand.Intn(len(g.Players)) + 1)
}
func (g *HalmaGame) Move(color HalmaColor, from Position, to Position) {
if color != g.CurrentPlayer || !g.fieldValid(from) || !g.fieldValid(to) || g.getField(from).Pin != color {
return
}
possible := g.calculatePossible(from)
for _, pos := range(possible) {
if pos.X == to.X && pos.Y == to.Y {
g.Fields[to.X + 8][to.Y + 8].Pin = g.Fields[from.X + 8][from.Y + 8].Pin
g.Fields[from.X + 8][from.Y + 8].Pin = Empty
if g.checkWinner(g.CurrentPlayer) {
g.CurrentPlayer = Empty
} else {
g.CurrentPlayer++
if int(g.CurrentPlayer) > len(g.Players) {
g.CurrentPlayer = Red
}
}
for _, client := range(g.Clients) {
client.NotifyMove(color, from, to)
client.NotifyTurn(g.CurrentPlayer)
}
}
}
}
func (g *HalmaGame) Join(player *Player) *HalmaGamePlayer {
color := g.GetFreeColor()
if color == Empty {
return nil
}
hgp := &HalmaGamePlayer{player, g, color}
g.Players = append(g.Players, hgp)
return hgp
}
func (g *HalmaGame) GetPlayer(player *Player) *HalmaGamePlayer {
for _, hgp := range(g.Players) {
if hgp.Player == player {
return hgp
}
}
return nil
}
func NewPlayer(name string, password string) *Player {
return &Player{name, password}
}