-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxo.html
185 lines (177 loc) · 6.8 KB
/
xo.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="xo.css">
<title>Multiplayer game</title>
</head>
<body>
<h1> Multiplayer X-O </h1>
<!-- <img src="xo.png" style="position: absolute;left:35%;height:400px"> -->
<button id="b1" onclick="myfunc1()"></button>
<button id="b2" onclick="myfunc2()"></button>
<button id="b3" onclick="myfunc3()"></button>
<button id="b4" onclick="myfunc4()"></button>
<button id="b5" onclick="myfunc5()"></button>
<button id="b6" onclick="myfunc6()"></button>
<button id="b7" onclick="myfunc7()"></button>
<button id="b8" onclick="myfunc8()"></button>
<button id="b9" onclick="myfunc9()"></button>
<button id="reset">Play Again</button>
<script>
/*
BOARD = {
"1": "",
"2": "",
"3": "",
"4": "",
"5": "",
"6": "",
"7": "",
"8": "",
"9": "",
}
X_POS = set()
O_POS = set()
GAMEOVER = False
*/
function myfunc1() {
websocket.send(JSON.stringify({action: "set cell", player: player, pos: 1}))
var elem = document.getElementById("b1");
elem.innerText = player;
}
function myfunc2() {
websocket.send(JSON.stringify({action: "set cell", player: player, pos: 2}))
var elem = document.getElementById("b2");
elem.innerText = player;
}
function myfunc3() {
websocket.send(JSON.stringify({action: "set cell", player: player, pos: 3}))
var elem = document.getElementById("b3");
elem.innerText = player;
}
function myfunc4() {
websocket.send(JSON.stringify({action: "set cell", player: player, pos: 4}))
var elem = document.getElementById("b4");
elem.innerText = player;
}
function myfunc5() {
websocket.send(JSON.stringify({action: "set cell", player: player, pos: 5}))
var elem = document.getElementById("b5");
elem.innerText = player;
}
function myfunc6() {
websocket.send(JSON.stringify({action: "set cell", player: player, pos: 6}))
var elem = document.getElementById("b6");
elem.innerText = player;
}
function myfunc7() {
websocket.send(JSON.stringify({action: "set cell", player: player, pos: 7}))
var elem = document.getElementById("b7");
elem.innerText = player;
}
function myfunc8() {
websocket.send(JSON.stringify({action: "set cell", player: player, pos: 8}))
var elem = document.getElementById("b8");
elem.innerText = player;
}
function myfunc9() {
websocket.send(JSON.stringify({action: "set cell", player: player, pos: 9}))
var elem = document.getElementById("b9");
elem.innerText = player;
}
var websocket = new WebSocket("ws://127.0.0.1/")
var playercount = 0
var gameover = false
var board = null
websocket.onopen = function (e) {
console.log("Connected to server...")
}
websocket.onmessage = function (event) {
data = JSON.parse(event.data);
console.log(data)
switch (data.type) {
case 'state':
// {"type": "state", "gameover": GAMEOVER, "board": BOARD}
gameover = data.gameover
if (gameover) {
if (data.winner==1){
alert("X won the game")
}
if (data.winner==2){
alert("O won the game")
}
if (data.winner==3){
alert("ISSA TIE")
}
}
board = data.board
handle_board(board)
break;
case 'playercount':
playercount = data.count.toString()
break;
case 'player':
player = data.player
console.log(player)
break;
default:
console.error(
"unsupported event", data);
}
};
function handle_board(_board) {
for (const [key, value] of Object.entries(_board)) {
if (key == 1 && value != '') {
var b1 = document.getElementById("b1")
b1.innerText = value
b1.disabled = true
}
if (key == 2 && value != '') {
var b2 = document.getElementById("b2")
b2.innerText = value
b2.disabled = true
}
if (key == 3 && value != '') {
var b3 = document.getElementById("b3")
b3.innerText = value
b3.disabled = true
}
if (key == 4 && value != '') {
var b4 = document.getElementById("b4")
b4.innerText = value
b4.disabled = true
}
if (key == 5 && value != '') {
var b5 = document.getElementById("b5")
b5.innerText = value
b5.disabled = true
}
if (key == 6 && value != '') {
var b6 = document.getElementById("b6")
b6.innerText = value
b6.disabled = true
}
if (key == 7 && value != '') {
var b7 = document.getElementById("b7")
b7.innerText = value
b7.disabled = true
}
if (key == 8 && value != '') {
var b8 = document.getElementById("b8")
b8.innerText = value
b8.disabled = true
}
if (key == 9 && value != '') {
var b9 = document.getElementById("b9")
b9.innerText = value
b9.disabled = true
}
}
}
// websocket.send(JSON.stringify({action: "set cell", player: player, pos: "num"}))
</script>
</body>
</html>