-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.ec
195 lines (175 loc) · 4.4 KB
/
go.ec
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
#ifdef ECERE_STATIC
import static "ecere"
#else
import "ecere"
#endif
import remote "goConnection"
define margin = 40;
define goSize = 19;
define tileSize = 32;
enum DotState : byte
{
empty,
black,
white,
invalid
};
DotState board[goSize][goSize];
bool placing;
Point piecePos;
bool runAsServer;
GoConnection connection;
DotState myColor;
DotState playerTurn = white;
class GoApp : GuiApplication
{
bool Init()
{
runAsServer = MessageBox { contents = "Run Server?", type = yesNo }.Modal() == yes;
connection = GoConnection
{
void notifyMovePlayed(int color, int x, int y)
{
if(board[y][x] == empty)
{
board[y][x] = (DotState)color;
goWindow.checkSurrounded(x, y - 1);
goWindow.checkSurrounded(x, y + 1);
goWindow.checkSurrounded(x - 1, y);
goWindow.checkSurrounded(x + 1, y);
}
playerTurn = (DotState)color == black ? white : black;
goWindow.Update(null);
}
};
if(runAsServer)
goService.Start();
myColor = runAsServer ? black : white;
connection.Connect("192.168.38.219", goPort);
connection.join();
return true;
}
}
class GoWindow : Window
{
caption = $"Game of Go";
#ifdef __EMSCRIPTEN__
anchor = { 0, 0, 0, 0 };
#else
borderStyle = sizable;
hasMaximize = true;
hasMinimize = true;
hasClose = true;
clientSize = { 624, 624 };
#endif
void drawPiece(Surface surface, int x, int y, DotState state)
{
int sx = margin + tileSize * x;
int sy = margin + tileSize * y;
surface.background = state == white ? white : state == black ? black : red;
surface.foreground = gray;
surface.Area(sx - 8, sy - 8,
sx + 8,
sy + 8);
surface.Rectangle(sx - 8, sy - 8,
sx + 8,
sy + 8);
}
void OnRedraw(Surface surface)
{
int i;
int x, y;
/*surface.Rectangle(margin, margin,
margin + tileSize * (goSize-1),
margin + tileSize * (goSize-1));
*/
for(i = 0; i < goSize-1; i++)
{
surface.HLine(margin,
margin + tileSize * (goSize-2),
i * tileSize + margin);
surface.VLine(margin,
margin + tileSize * (goSize-2),
i * tileSize + margin);
}
for(y = 0; y < goSize; y++)
for(x = 0; x < goSize; x++)
{
DotState state = board[y][x];
if(state)
drawPiece(surface, x, y, state);
}
if(placing)
{
drawPiece(surface, piecePos.x, piecePos.y,
board[piecePos.y][piecePos.x] == empty ? playerColor : invalid);
}
}
bool OnLeftButtonDown(int x, int y, Modifiers mods)
{
if(myColor == playerTurn)
{
Point pos
{
(x - margin + tileSize/2) / tileSize,
(y - margin + tileSize/2) / tileSize
};
if(pos.x >= 0 && pos.x < goSize && pos.y >= 0 && pos.y < goSize)
{
piecePos = pos;
placing = true;
Capture();
Update(null);
}
}
return true;
}
bool OnMouseMove(int x, int y, Modifiers mods)
{
if(placing)
{
Point pos
{
(x - margin + tileSize/2) / tileSize,
(y - margin + tileSize/2) / tileSize
};
if(pos.x >= 0 && pos.x < goSize-1 && pos.y >= 0 && pos.y < goSize-1)
{
piecePos = pos;
Update(null);
}
}
return true;
}
bool OnLeftButtonUp(int x, int y, Modifiers mods)
{
if(placing)
{
Point pos
{
(x - margin + tileSize/2) / tileSize,
(y - margin + tileSize/2) / tileSize
};
if(pos.x >= 0 && pos.x < goSize-1 && pos.y >= 0 && pos.y < goSize-1)
connection.playMove(myColor, pos.x, pos.y);
placing = false;
ReleaseCapture();
Update(null);
}
return true;
}
void checkSurrounded(int x, int y)
{
if(x >= 1 && y >= 1 && x < goSize-1 && y < goSize-1)
{
if(board[y-1][x] == playerColor &&
board[y+1][x] == playerColor &&
board[y][x-1] == playerColor &&
board[y][x+1] == playerColor)
{
board[y][x] = empty;
}
}
}
}
GoWindow goWindow {};