-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoriginal
More file actions
263 lines (246 loc) · 6.98 KB
/
Copy pathoriginal
File metadata and controls
263 lines (246 loc) · 6.98 KB
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
package main
import (
"flag"
"fmt"
"math/rand"
"net"
"net/rpc"
"os"
"sync"
"time"
"uk.ac.bris.cs/gameoflife/gol"
"uk.ac.bris.cs/gameoflife/util"
)
/** Super-Secret `reversing a string' method we can't allow clients to see. **/
func calculateAliveCells(world [][]uint8) []util.Cell {
var alive []util.Cell
for i := 0; i < len(world); i++ {
for k := 0; k < len(world[i]); k++ {
if world[i][k] == 255 {
alive = append(alive, util.Cell{X: k, Y: i})
}
}
}
return alive
}
type Pair struct {
y int
x int
}
func calculateNearAlive(world [][]uint8, row, col int) int {
adjacent := []Pair{
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1},
}
//add to get all adj nodes within 0-15
for i := 0; i < 8; i++ {
adjacent[i].y += row
if adjacent[i].y == len(world) {
adjacent[i].y = 0
}
if adjacent[i].y == -1 {
adjacent[i].y = len(world) - 1
}
adjacent[i].x += col
if adjacent[i].x == len(world[i]) {
adjacent[i].x = 0
}
if adjacent[i].x == -1 {
adjacent[i].x = len(world[i]) - 1
}
}
//count alive using for
count := 0
for _, node := range adjacent {
if world[node.y][node.x] == 255 {
count++
}
}
return count
}
func calculateNextState(startY, endY int, world [][]uint8) [][]uint8 {
newWorld := make([][]uint8, len(world))
for i := range newWorld {
newWorld[i] = make([]uint8, len(world[i]))
}
for i := startY; i < endY; i++ { //each row
for k := 0; k < len(world[i]); k++ { //each item in row
numOfAlive := calculateNearAlive(world, i, k)
currentNode := world[i][k]
//rules for updating the cell state
if world[i][k] == 255 {
if numOfAlive < 2 {
newWorld[i][k] = 0
} else if numOfAlive == 2 || numOfAlive == 3 {
newWorld[i][k] = currentNode
} else if numOfAlive > 3 {
newWorld[i][k] = 0
}
} else if currentNode == 0 && numOfAlive == 3 {
newWorld[i][k] = 255
}
}
}
return newWorld
}
func copyWhole(dst, src [][]uint8) {
for i := range src {
copy(dst[i], src[i])
}
}
type GolOp struct {
completedTurn int
cellCount int
world [][]uint8
pause chan bool
needResume bool
needSave chan bool
//worldBeforeQuit [][]uint8
//turnBeforeQuit int
}
var mutex sync.Mutex
func (g *GolOp) ExecuteTurns(req gol.Request, res *gol.Response) (err error) {
mutex.Lock()
g.needSave = make(chan bool, 1)
g.pause = make(chan bool, 1)
//turn := 0 need to un comment when change to extention mode(game can be saved and continue)
g.world = make([][]uint8, req.P.ImageHeight)
for i := range g.world {
g.world[i] = make([]uint8, req.P.ImageWidth)
}
copyWhole(g.world, req.World)
g.completedTurn = 0
//resume logic
//content below is used for q extension.............................................................................................
//if g.needResume && req.P.ImageHeight == len(g.worldBeforeQuit) {
// g.world = make([][]uint8, req.P.ImageHeight)
// //g.worldBeforeQuit = make([][]uint8, req.P.ImageHeight)
// for i := range g.world {
// g.world[i] = make([]uint8, req.P.ImageWidth)
// //g.worldBeforeQuit[i] = make([]uint8, req.P.ImageWidth)
// }
// copyWhole(g.world, g.worldBeforeQuit)
// g.completedTurn = g.turnBeforeQuit
// turn = g.turnBeforeQuit
// g.needResume = false
//} else {
// g.world = make([][]uint8, req.P.ImageHeight)
// g.worldBeforeQuit = make([][]uint8, req.P.ImageHeight)
// for i := range g.world {
// g.world[i] = make([]uint8, req.P.ImageWidth)
// g.worldBeforeQuit[i] = make([]uint8, req.P.ImageWidth)
// }
// copyWhole(g.world, req.World)
// g.completedTurn = 0
//}
//content above is used for q extention..............................................................
mutex.Unlock()
if req.P.Turns == 0 {
res.NewWorld = req.World
res.Final = gol.FinalTurnComplete{CompletedTurns: req.P.Turns, Alive: calculateAliveCells(g.world)}
return
} else {
//fmt.Println("executing Turns:", t)
for t := 0; t < req.P.Turns; t++ { //t need to be turn when extention mode
select {
case <-g.pause:
pause := <-g.pause
if !pause {
break
}
case <-g.needSave:
//resume logic for extention mode---------------------------------------------------
//g.worldBeforeQuit = make([][]uint8, req.P.ImageHeight)
//for i := range g.world {
// g.worldBeforeQuit[i] = make([]uint8, req.P.ImageWidth)
//}
//copyWhole(g.worldBeforeQuit, g.world)
//g.turnBeforeQuit = g.completedTurn
//--------------------------------------------------------------------------------------
fmt.Println("q is pressed, the process pop off")
return
default:
break
}
res.NewWorld = calculateNextState(0, req.P.ImageHeight, g.world)
mutex.Lock()
copyWhole(g.world, res.NewWorld)
g.completedTurn = t + 1
fmt.Println("in loop, turn completed:", g.completedTurn)
mutex.Unlock()
}
res.Final = gol.FinalTurnComplete{CompletedTurns: req.P.Turns, Alive: calculateAliveCells(res.NewWorld)}
fmt.Println("game/test finished")
return
}
}
func (g *GolOp) Timer(req gol.Request, res *gol.ReportAlive) (err error) {
mutex.Lock()
fmt.Println("reported in turn", g.completedTurn)
res.Alive = gol.AliveCellsCount{CellsCount: len(calculateAliveCells(g.world)), CompletedTurns: g.completedTurn}
mutex.Unlock()
return
}
func (g *GolOp) KeyOp(op gol.KeyPress, res *gol.Response) (err error) {
res.NewWorld = make([][]uint8, op.P.ImageHeight)
for i := range res.NewWorld {
res.NewWorld[i] = make([]uint8, op.P.ImageWidth)
}
switch op.Key {
case 's':
// Save the game state
fmt.Println("s is pressed, the instant is saved")
mutex.Lock()
copyWhole(res.NewWorld, g.world)
res.CurrentTurn = g.completedTurn
fmt.Println("saved the current state at turn:", g.completedTurn)
mutex.Unlock()
case 'k':
fmt.Println("k is pressed, the game is saved")
mutex.Lock()
copyWhole(res.NewWorld, g.world)
res.CurrentTurn = g.completedTurn
res.Final = gol.FinalTurnComplete{CompletedTurns: g.completedTurn, Alive: calculateAliveCells(res.NewWorld)}
fmt.Println("saved the current state at turn:", g.completedTurn)
mutex.Unlock()
case 'p':
fmt.Println("p is preesed, the instant is saved")
mutex.Lock()
copyWhole(res.NewWorld, g.world)
res.CurrentTurn = g.completedTurn
fmt.Println("saved the current state at turn:", g.completedTurn)
g.pause <- true
mutex.Unlock()
case 'q':
fmt.Println("q is pressed, the instant should be saved and continue next time")
mutex.Lock()
g.needSave <- true
g.needResume = true
mutex.Unlock()
}
return
}
func (g *GolOp) Kill(op gol.KeyPress, res *gol.Response) (err error) {
os.Exit(0)
return
}
func (g *GolOp) Resume(op gol.KeyPress, res *gol.Response) (err error) {
fmt.Println("Resume")
g.pause <- false
return
}
func main() {
pAddr := flag.String("port", "8030", "Port to listen on")
flag.Parse()
rand.Seed(time.Now().UnixNano())
rpc.Register(&GolOp{})
listener, _ := net.Listen("tcp", ":"+*pAddr)
defer func(listener net.Listener) {
err := listener.Close()
if err != nil {
}
}(listener)
rpc.Accept(listener)
fmt.Println("connected")
}