-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
300 lines (283 loc) · 8.14 KB
/
index.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
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
293
294
295
296
297
298
299
300
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<style type="text/css">
html,body,div{
padding: 0;margin: 0;
}
html{
height:100%;
}
body{
position:relative;
height:100%;
}
.login{
/*position:absolute;*/
/*left:0;
right:0;
top:0;
bottom:0;*/
/*background:#3d3d3d;*/
}
.table{
display:table;
width:100%;
height:100%;
}
.table_login{
display:table-cell;
text-align:center;
vertical-align:middle;
}
#wuzi
{
max-height:100%;
width: 100%;
padding: 20px 0;
}
.myCanvas{
display:block;
margin:20px auto;
box-shadow: -2px -2px 2px #EFEFEF,5px 5px 5px #b9b9b9;
cursor: pointer;
}
</style>
</head>
<body>
<div class="login">
<div class="table">
<div class="table_login">
<p>自已的ID:</p>
<input type="text" name="username" id="username" value="">
<button id="btn_name">输入对方的ID</button>
<button id="regame">重新开始游戏</button>
</div>
</div>
</div>
<div id="wuzi">
</div>
<script src="//cdn.bootcss.com/socket.io/1.7.1/socket.io.js"></script>
<script type="text/javascript">
const socket = io.connect('127.0.0.1:3000');
socket.on('connect', () => {
a = socket.id;
document.querySelector('.table_login P').innerHTML='自已的ID:'+a;
});
var idclick=document.getElementById('btn_name');
var regame = document.getElementById('regame');
idclick.onclick=function(){
username = document.getElementById('username').value;
const b={
target: username,
sid: a
}
socket.emit('link',JSON.stringify(b));
};
regame.addEventListener("click",function(){
parent.removeChild(parent.childNodes[0]);
draw.createcvs(570,570,parent,15);
});
(function(){
let wins=[];
let mywins=[];
let computerwins=[];
let count = 0;
let chessboard = []
function Cursor(width,height,parent,y){
this.canPlay=false;
this.gameOver=false;
this.player=true;
let self = this;
// 私有属性
// 公有属性
this.lineColoer='#BFBFBF';
this.x = (width-30)/(y-1);
this.canvas = document.createElement('canvas');
// 公有方法
this.createcvs=function(width,height,parent,y){
this.canvas = document.createElement('canvas');
this.canvas.className="myCanvas";
this.canvas.width = width;
this.canvas.height = height;
parent.appendChild(this.canvas);
this._ctx = this.canvas.getContext("2d");
// this.init();
///画棋盘 没有弄成方法 下面为画棋盘
this._ctx.strokeStyle=this.lineColoer;
for(let i=0;i<y;i++){
this._ctx.moveTo(15+this.x*i,15);
this._ctx.lineTo(15+this.x*i,this.canvas.width-15)
this._ctx.stroke();
this._ctx.moveTo(15,15+this.x*i);
this._ctx.lineTo(this.canvas.width-15,15+this.x*i)
this._ctx.stroke();
}
win32(y);
}
this.createcvs(width,height,parent,y);
this.init();
// 公有方法
this.oneStep=function(i,j){
self._ctx.beginPath();
self._ctx.arc(15+i*this.x,15+j*this.x,13,0,2*Math.PI)
self._ctx.closePath();
let radgrad = self._ctx.createRadialGradient(15+i*this.x+2,15+j*this.x-2,13,15+i*this.x+2,15+j*this.x+2,0);
if (this.player==0){
radgrad.addColorStop(0, '#E4C700');
radgrad.addColorStop(1, '#F4F201');
self._ctx.fillStyle=radgrad;
self._ctx.fill();
for (var k = 0 ; k<count ; k++){
if(wins[i][j][k]){
computerwins[k]++;
mywins[k]=6; //代表计算永远都达到5了
if(computerwins[k]==5){
window.alert("你赢了")
this.gameOver=true;
}
}
}
}else{
radgrad.addColorStop(0, "#0A0A0A");
radgrad.addColorStop(1, "#636766");
self._ctx.fillStyle=radgrad;
this._ctx.fill();
for (var k = 0 ; k<count ; k++){
if(wins[i][j][k]){
mywins[k]++;
computerwins[k]=6; //代表计算永远都达到5了
if(mywins[k]==5){
window.alert("你赢了")
this.gameOver=true;
}
}
}
}
this.player=!this.player;
chessboard.push(i,j);
this.canPlay = !this.canPlay;
};
// 继承
this.oneStepTwo=function(i,j){
this.oneStep.call(this,i,j);
let result={
i:i,
j:j,
gameOver:this.gameOver,
}
socket.emit('clickmsg', JSON.stringify(result));
}
//私有方法
function win32(y){
for(var i=0;i<y;i++){
wins[i]=[];
for(var j=0;j<y;j++){
wins[i][j] = [];
}
}
for(var i=0;i<y;i++){
for(var j=0;j<(y-4);j++){
for (var k=0;k<5;k++){
wins[i][j+k][count] = true;
}
count++;
}
}
for(var i=0;i<y;i++){
for(var j=0;j<(y-4);j++){
for (var k=0;k<5;k++){
wins[j+k][i][count] = true;
}
count++;
}
}
for(var i=0;i<(y-4);i++){
for(var j=0;j<(y-4);j++){
for (var k=0;k<5;k++){
wins[i+k][j+k][count]=true;
}
count++;
}
}
for(var i=0;i<(y-4);i++){
for(var j=(y-1);j>3;j--){
for (var k=0;k<5;k++){
wins[i+k][j-k][count]=true;
}
count++;
}
}
for (var i=0;i<count;i++){
mywins[i]=0;
computerwins[i]=0;
}
};
}
Cursor.prototype={
constructor:Cursor,
//人下载
canClick:function(){
console.log(this.gameOver);
if(!this.gameOver)
{
let _this=this;
this.canvas.onclick=function(e){
if(_this.canPlay){
let x=e.offsetX;
let y=e.offsetY;
let i = Math.floor(x/_this.x);
let j = Math.floor(y/_this.x);
for (let value of chessboard)
{
if (value.toString()==[i,j].toString()){ //数组不能直接用判断
console.log("不能重复点击")
return;
}
}
_this.oneStepTwo(i,j);
}else {
return
}
}
}
},
//init 初始化
init:function(){
_this=this;
socket.on('linkOK', function(){
_this.canPlay =true;
alert('可以下棋了');
// setTimeout(function(){
//// document.getElementsByClassName('login')[0].style="display:none";
// },500)
// console.log(document.getElementsByClassName('login')[0]);
});
socket.on('linked',function(){
window.alert('等待对手下棋');
// setTimeout(function(){
// document.getElementsByClassName('login')[0].style="display:none";
// },500)
});
socket.on('linkerror',function(){
window.alert('不确在这个ID');
});
socket.on('tick-back',function(d){
const c = JSON.parse(d);
_this.oneStep(c.i,c.j);
_this.gameOver = c.gameOver;
console.log(_this.gameOver);
});
},
}
window.Cursor = Cursor;
})()
var parent = document.getElementById('wuzi');
var draw = new Cursor(570,570,parent,15);//
draw.canClick();
</script>
</body>
</html>