-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.spx
87 lines (78 loc) · 1.49 KB
/
main.spx
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
var (
Chess Chess
Button4 Button4
CurrentChess CurrentChess
Calf1 Calf1
gameState List
ret int
col int
score int
currentCol int
currentPlayer int
currentRow int
row int
)
const (
_Black = 1
_White = 2
_BlackAndWhite = _Black + _White
)
func calcHitPosition(x float64, y float64) {
col = iround((x + 159) / 17.7)
row = iround((y - 159) / -17.7)
if row < 0 || col < 0 || row > 18 || col > 18 {
ret = 0
} else {
ret = row*19 + col + 1
}
}
func initGameState() {
gameState.delete All
for i := 0; i < 361; i++ {
gameState.append 0
}
}
func calcScore(dx int, dy int) {
row = currentRow + dy
col = currentCol + dx
for row > 0 && col > 0 && row < 18 && col < 18 && currentPlayer == gameState.at(row*19+col).int {
score++
row += dy
col += dx
}
}
func checkHasWon() {
checkHasWonByRow 1, 1
checkHasWonByRow 1, 0
checkHasWonByRow 0, 1
checkHasWonByRow 1, -1
}
func checkHasWonByRow(dx int, dy int) {
score = 1
calcScore dx, dy
calcScore -dx, -dy
if score > 4 {
broadcast "game over", true
}
}
onStart => {
initGameState
score = 0
currentPlayer = _Black
}
onClick => {
calcHitPosition mouseX, mouseY
if ret > 0 && gameState.at(ret-1).int == 0 {
currentCol = col
currentRow = row
broadcast "try put chess"
}
}
onMsg "confirm to put chess", => {
if score < 5 {
gameState.set currentRow*19+currentCol, currentPlayer
checkHasWon
Chess.clone
broadcast "put chess done", true
}
}