-
Notifications
You must be signed in to change notification settings - Fork 0
/
a2.js
292 lines (257 loc) · 6.88 KB
/
a2.js
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
const ws = require('ws')
const http = require('http')
const fs = require('fs')
const EMPTY = '-'
var active_pairs = []
var unmatched_client = null
var turn = []
var states = {}
var blocked = []
const symbols = ['X', 'O']
const readFile = filename => new Promise(resolve => fs.readFile(filename, 'utf-8', (err, data) => resolve(data)))
const create_msg = (type, data) =>
{
let msg = {}
msg['type'] = type
msg['data'] = data
return JSON.stringify(msg)
}
const update_client_pair = (pair, msg) =>
{
pair[0].send(msg)
pair[1].send(msg)
}
const start_match = game_id =>
{
let state = {}
for(var i = 0; i < 6; ++i)
{
for(var j = 0; j < 7; ++j)
state[`${i}x${j}`] = EMPTY
}
states[game_id] = state
update_client_pair(active_pairs[game_id], create_msg('game_id', game_id))
update_client_pair(active_pairs[game_id], create_msg('info', 'Match started'))
update_client_pair(active_pairs[game_id], create_msg('state_update', state))
}
const daig = (game_id, pos, sign, symbol) =>
{
const row = parseInt(pos[0], 10)
const col = parseInt(pos[2], 10)
let consec = 0
for(var i = -3; i < 4; ++i)
{
let r = row + sign * i
let c = col + i
if(r < 0 || r >= 6 || c < 0 || c > 6)
continue
if(states[game_id][`${r}x${c}`] == symbol)
{
if(++consec == 4)
return true
}
else consec = 0
}
return false
}
const vert = (game_id, pos, symbol) =>
{
const row = parseInt(pos[0], 10)
const col = parseInt(pos[2], 10)
let consec = 0
let c = col
for(var i = -3; i < 4; ++i)
{
let r = row - i
if(r < 0 || r >= 6 || c < 0 || c > 6)
continue
if(states[game_id][`${r}x${c}`] == symbol)
{
if(++consec == 4)
return true
}
else consec = 0
}
return false
}
const hor = (game_id, pos, symbol) =>
{
const row = parseInt(pos[0], 10)
const col = parseInt(pos[2], 10)
let consec = 0
let r = row
for(var i = -3; i < 4; ++i)
{
let c = col + i
if(r < 0 || r >= 6 || c < 0 || c > 6)
continue
if(states[game_id][`${r}x${c}`] == symbol)
{
if(++consec == 4)
return true
}
else consec = 0
}
return false
}
const check_win = (game_id, pos, s) =>
{
return daig(game_id, pos, -1, s) || daig(game_id, pos, 1, s) || vert(game_id, pos, s) || hor(game_id, pos, s)
}
const valid_move = (game_id, col) =>
{
if(states[game_id][`0x${col}`] != EMPTY)
{
active_pairs[game_id][turn[game_id]].send(create_msg('info', 'column full!'))
return false
}
return true
}
const apply_move = (game_id, col) =>
{
if(!valid_move(game_id, col))
return
let ins_point = ''
for(var i = 0; i < 6; ++i)
{
if(states[game_id][`${i}x${col}`] != EMPTY)
{
ins_point = `${i-1}x${col}`
states[game_id][ins_point] = symbols[turn[game_id]]
break;
}
if(i == 5)
{
ins_point = `${i}x${col}`
states[game_id][ins_point] = symbols[turn[game_id]]
}
}
update_client_pair(active_pairs[game_id], create_msg('state_update', states[game_id]))
if(check_win(game_id, ins_point, symbols[turn[game_id]]))
{
active_pairs[game_id][turn[game_id]].send(create_msg('info', 'You WIN!'))
active_pairs[game_id][(turn[game_id]+1)%2].send(create_msg('info', 'You luzr!'))
blocked[game_id] = true
//start_match(game_id)
}
turn[game_id] = (turn[game_id] + 1) % 2
}
const remove_dummy_move = (game_id, col) =>
{
for(var i = 0; i < 6; ++i)
{
if(states[game_id][`${i}x${col}`] != EMPTY)
{
states[game_id][`${i}x${col}`] = EMPTY
return
}
}
}
const apply_dummy_move = (game_id, col, symbol) =>
{
let ins_point = ''
for(var i = 0; i < 6; ++i)
{
if(states[game_id][`${i}x${col}`] != EMPTY)
{
ins_point = `${i-1}x${col}`
states[game_id][ins_point] = symbol
break;
}
if(i == 5)
{
ins_point = `${i}x${col}`
states[game_id][ins_point] = symbol
}
}
return ins_point
}
const predict_opponent_win = (game_id, col, t) =>
{
let dummy_check = apply_dummy_move(game_id, col, symbols[t])
if(check_win(game_id, dummy_check, symbols[t]))
{
remove_dummy_move(game_id, col)
return false
}
for(var c = 0; c < 7; ++c)
{
if(!valid_move(game_id, c))
continue
let ins_point = apply_dummy_move(game_id, c, symbols[(t+1)%2])
if(check_win(game_id, ins_point, symbols[(t+1)%2]))
{
remove_dummy_move(game_id, c)
remove_dummy_move(game_id, col)
return true
}
remove_dummy_move(game_id, c)
}
remove_dummy_move(game_id, col)
return false
}
const decide_color = (game_id, col, t) =>
{
if(predict_opponent_win(game_id, col, t))
return 'red-grid-item'
return 'green-grid-item'
}
const message_handler = (client, msg) =>
{
let type = msg['type']
let data = msg['data']
let game_id = msg['game_id']
let player_id = msg['player_id']
if(blocked[game_id])
return
if(type === 'on_click')
{
if(player_id === turn[game_id])
apply_move(game_id, data)
else
client.send(create_msg('info', 'not your turn'))
}
if(type === 'mouse_hover')
{
let color = decide_color(game_id, data, player_id)
client.send(create_msg('highlight', {0: data, 1:color}))
}
}
const onClientConnected = client =>
{
console.log(`client connected`)
if(unmatched_client)
{
active_pairs.push([unmatched_client, client])
turn.push(0)
blocked.push(false)
unmatched_client.send(create_msg('player_id', 0))
client.send(create_msg('player_id', 1))
unmatched_client = null
let game_id = active_pairs.length -1
start_match(game_id)
}
else
{
unmatched_client = client
client.send(create_msg('info', 'wait for player'))
}
client.on('message', msg =>
{
message_handler(client, JSON.parse(msg));
})
}
const server = http.createServer(async (req, resp) =>
{
if(req.url === '/')
resp.end(await readFile('connect4_clientside.html'))
else if(req.url === '/vue.js')
resp.end(await readFile('vue.js'))
else if(req.url === '/connect4_clientside.js')
resp.end(await readFile('connect4_clientside.js'))
else
resp.end('sowwy :(((');
})
new ws.Server({server}).on('connection', onClientConnected)
server.listen(6969)
console.log(`listening on port`)