-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
127 lines (99 loc) · 4 KB
/
index.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
var ws = require('ws')
var server = new ws.Server({port: 3000})
let lobby=[]
let board = {'1':"no", '2':"no", '3':"no", '4':"no", '5':"no", '6':"no", '7':"no", '8':"no", '9':"no"}
let turn = 1;
let count = 0;
let state = 'Null';
let latest_pos='Null';
let pos_entry='Null';
server.on('connection', server => {
lobby.push(server);
server.on('message', message => {
let data = JSON.parse(message);
console.log(data);
if(data!="het connected"){
count=data['count'];
board[data['latest_pos']]=data['latest_entry'];
latest_pos=data['latest_pos'];
pos_entry=data['latest_entry'];
console.log(board)
if(turn == 1) {
turn = 0
}
else {
turn = 1
}
}
else{
console.log("hahahhaahhah")
}
});
server.on('close', (code, reason) => {
console.log(code, reason);
const index = lobby.indexOf(ws);
lobby.splice(index, 1);
});
if (lobby.length >= 2) {
// Match up the first two players in the lobby
const player1 = lobby.shift();
const player2 = lobby.shift();
console.log('works')
// Send message to start the game
// player1.send(JSON.stringify({ type: 'start', mark: 'X' }));
// player2.send(JSON.stringify({ type: 'start', mark: 'O' }));
// // Keep track of the players' game state
// player1.gameState = { mark: 'X', opponent: player2 };
// player2.gameState = { mark: 'O', opponent: player1 };
player1.send(JSON.stringify({ 'isX' : 'True', isTurn : 'True', 'pos':latest_pos,'pos_entry' : pos_entry, 'count' : count, 'state': state }));
player2.send(JSON.stringify({ 'isX' : 'False', isTurn : 'False','pos':latest_pos, 'pos_entry' : pos_entry, 'count' : count, 'state': state }));
setInterval(() => {
// turn =int(count)%2;
if (checkWin() == 'X') {
state = 'X'
}
else if (checkWin() == 'O') {
state = 'O'
}
else if(checkWin() == 'Draw' && count >= 9) {
state = 'D'
}
else {
state = 'Null'
}
if (turn == 1) {
player1.send(JSON.stringify({ 'isX' : 'True', isTurn : 'True', 'pos':latest_pos,'pos_entry' : pos_entry, 'count' : count, 'state': state }));
player2.send(JSON.stringify({ 'isX' : 'False', isTurn : 'False','pos':latest_pos, 'pos_entry' : pos_entry, 'count' : count, 'state': state }));
}
else {
player2.send(JSON.stringify({ 'isX' : 'False', isTurn : 'True','pos':latest_pos, 'pos_entry' : pos_entry, 'count' : count, 'state': state }));
player1.send(JSON.stringify({ 'isX' : 'True', isTurn : 'False','pos':latest_pos,'pos_entry' : pos_entry, 'count' : count, 'state': state }));
}
if(state!='Null'){
board = {'1':"no", '2':"no", '3':"no", '4':"no", '5':"no", '6':"no", '7':"no", '8':"no", '9':"no"}
turn = 1;
count = 0;
state = 'Null';
latest_pos='Null';
pos_entry='Null';
}
}, 100);
}
});
function checkWin() {
var winningCombinations = [
[1, 2, 3], [4, 5, 6], [7, 8, 9], // rows
[1, 4, 7], [2, 5, 8], [3, 6, 9], // columns
[1, 5, 9], [3, 5, 7] // diagonals
];
for (var i = 0; i < winningCombinations.length; i++) {
var combination = winningCombinations[i];
if (board[combination[0]] === "X" && board[combination[1]] === "X" && board[combination[2]] === "X") {
return "X";
}
if (board[combination[0]] === "O" && board[combination[1]] === "O" && board[combination[2]] === "O") {
return "O";
}
}
return "Draw";
}