0 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 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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525  nodeJs+express+soket.io五子棋实战 - 日记屋
nodeJs+express+soket.io五子棋实战
发布时间:

window.onload=function(){

    var sence = document.getElementById('sence'),

        //棋盘大小

        ROW = 20,NUM = ROW*ROW,

 

        //场景宽度

        senceWidth = sence.offsetWidth,

 

        //每颗棋子的宽度

        blockWidth = Math.floor( (senceWidth-ROW)/ROW ) + 'px',

         //用户开始默认可以落子

        canDrop = true,

 

        //用户默认落子为白棋

        color = 'white',

        //两个字典,用来存放白棋和黑棋的已落子的位置;以坐标为建,值为true

        whiteBlocks = {},blackBlocks = {};

        console.log(sence);

 

//创建场景

(function (){

    var el,

    //在棋盘上画线

        rowline,

        colline;

    for ( var i = 0;i < ROW;i++){

        //按照计算好的间隔放置横线

        rowline = document.createElement('div');

        rowline.setAttribute('class','row');

        rowline.style.top= (senceWidth/ROW)/2 + (senceWidth/ROW)*i + 'px';

        sence.appendChild(rowline);

 

        //按照计算好的间隔放置竖线

        colline = document.createElement('div');

        colline.setAttribute('class','col');

        colline.style.left= (senceWidth/ROW)/2 + (senceWidth/ROW)*i + 'px';

        sence.appendChild(colline);

        

        for ( var j = 0;j < ROW;j++){

            el = document.createElement('div');

            el.style.width = blockWidth;

            el.style.height = blockWidth;

            el.setAttribute('class','block');

            el.setAttribute('id',i + '_' + j);

            sence.appendChild(el);

        }

    }

})();

console.log('1')

var id2Position = function(id){

    console.log(id)

    return {x:Number(id.split('_')[0]),y:Number(id.split('_')[1])};

};

var position2Id = function(x,y){

    return x + '_' + y;

};

 

console.log('abc');

//判断落子皇后该色其是否连5

var isHasWinner = function(id,dic){

    var x = id2Position(id).x;

    var y = id2Position(id).y;

 

    //用来记录横,竖,左斜,右斜方向的连续棋子数量

    var rowCount = 1,colCout = 1,leftSkewLineCount = 1,righeSkewlineCount = 1;

    //tx ty作为游标,左移,右移,上移,下移,左上,右下,左下,右上移动

    //每次数万某个方向的连续棋子后,游标会回到原点

 

    var tx,ty;

    //注意:一下判断5连以上不算成功,如果规则有变动,条件改为大于五就可以

    tx = x;ty = y;

    while(dic[ position2Id(tx,ty+1) ]){

        rowCount++;

        ty++;

    }

    tx = x;ty = y;

    while(dic[ position2Id(tx,ty-1) ]){

        rowCount++;

        ty--;

    };

    if( rowCount ==5 ) return true;

 

    tx = x;ty = y;

    while(dic[ position2Id(tx+1,ty) ]){

        colCout++;

        tx++;

    }

    tx = x;ty = y;

    while(dic[ position2Id(tx-1,ty) ]){

        colCout++;

        tx--;

    };

    if( colCout ==5 ) return true;

 

 

    tx = x;ty = y;

    while(dic[ position2Id(tx+1,ty+1) ]){

        leftSkewLineCount++;

        tx++;

        ty++;

    }

    tx = x;ty = y;

    while(dic[ position2Id(tx-1,ty-1) ]){

        leftSkewLineCount++;

        tx--;

        ty--;

    }

    if( leftSkewLineCount == 5){

        return true;

    }

 

 

    tx = x;ty = y;

    while(dic[ position2Id(tx-1,ty+1) ]){

        righeSkewlineCount++;

        tx--;

        ty++;

    }

    tx = x;ty = y;

    while(dic[ position2Id(tx+1,ty-1) ]){

        leftSkewLineCount++;

        tx++;

        ty--;

    }

    if( righeSkewlineCount == 5) return true;

    return false;

};

 //处理对手发送过来的信息;

//  var socket = io.connect('http://****/span>

if (/Firefox\/\s/.test(navigator.userAgent)){

    var socket = io.connect('http://****/span>

}

else if (/MSIE (\d+.\d+);/.test(navigator.userAgent)){

    var socket = io.connect('http://****/span>

}

else {

    var socket = io.connect('http://****/span>

}

    socket.on('message',function(data){

        // console.log('data');

        canDrop = true;

        var el = document.getElementById(data.id);

        // console.log(el)

        el.setAttribute('has-one','true');

        if(data.color == 'white'){

            color = 'black';

            el.setAttribute('class','block white');

            whiteBlocks[data.id] = true;

             if(isHasWinner(data.id,whiteBlocks)){

                 alert('白棋赢了');

                 location.reload();//Location.reload() 方法用来刷新当前页面。该方法只有一个参数,当值为 true 时,将强制浏览器从服务器加载页面资源,当值为 false 或者未传参时,浏览器则可能从缓存中读取页面。

             }

            }else{

                 el.setAttribute('class','block black');

                 blackBlocks[data.id] = true;

                 if( isHasWinner(data.id,blackBlocks)){

                     alert('黑棋赢了');

                     location.reload();

                 }

             }

        });

 

        sence.onclick = function(e){

            // console.log('gyu')

             var el = e.target;//触发事件的对象 (某个DOM元素) 的引用。当事件处理程序在事件的冒泡或捕获阶段被调用时;

             if( !canDrop || el.hasAttribute('has-one') || el == this){//hasAttributes属性返回一个布尔值truefalse,来表明当前元素节点是否有至少一个的属性

                return;

             }

             el.setAttribute('has-one','true');

             canDrop = false;

             var id = el.getAttribute('id');

             if(color == 'white'){

                 el.setAttribute('class','block white');

                 whiteBlocks[id] = true;

                 socket.emit('message',{id:id,color:'white'});//socket.emit('action',data);表示发送了一个action命令,还有data数据,在另一端接收时,可以这么写: socket.on('action',function(data){...});

                 if(isHasWinner(id,whiteBlocks)){

                     alert('白棋赢');

                     location.reload();

                 }

             }

             if( color == 'black'){

                 el.setAttribute('class','block black');

                 blackBlocks[id]=true;

                 socket.emit('message' , {id:id,color:'black'});

                 if(isHasWinner(id,blackBlocks)){

                     alert('黑棋赢了');

                     location.reload();

                 }

             }

       

     };

};

 

 

样式index.css

body{

    background: #4b4832;

    font-family: 微软雅黑;

    color: #666;

}

.sence{

    width: 600px;

    height: 600px;

    margin: 50px auto;

    border-right: none;

    border-bottom: none;

    position: relative;

    box-shadow: -10px 10px 15px black;

    background: #8d6d45;

    border: 2px solid black;

}

.sence .block{

    float: left;

    margin-right: 1px;

    margin-bottom: 1px;

    border-radius: 50%;

    position: relative;

    z-index: 8884;

}

.sence .row,.sence .col{

    background: #4d392b;

    position: absolute;

}

.sence .row{

    width:100%;

    height: 1px;

    top: 0;

}

.sence .col{

    width:1px;

    height: 100%;

    top: 0;

}

.white{

    background: #ffffff;

}

.black{

    background: #2c1d1b;

}

index.html:

<!DOCTYPE html>

<html>

<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">

    <title>五子棋</title>

    <link rel="stylesheet" href="./public/index.css">

</head>

<body>

    <div id="sence"></div>

    <script src="/img/L3B1YmxpYy9zb2NrZXQuaW8uanM="></script>

    <script src="/img/L3B1YmxpYy9pbmRleC5qcw==" type="text/javascript" ></script>

</body>

</html>

服务端index.js

var express = require('express');

function(){ //ThinkMarkets智汇入金 http://****/question/kaifx/1819.html

var path = require('path');

var app = express()

var http = require('http').Server(app);

var io = require('socket.io')(http);

 

io.on('connection',function(socket){

    socket.on('message',function(data){

        socket.broadcast.emit('message',data);

    });

});

app.use('/public/',express.static(path.join(__dirname,'./public/')))

app.use('/node_modules/',express.static(path.join(__dirname,'./node_modules/')))

app.get('/',function(req,res){

    res.sendFile(__dirname +  '/index.html');

});

 

 http.listen(3100,function(){

     console.log('runing...')

 })

socket.io 兼容性代码:

if (/Firefox\/\s/.test(navigator.userAgent)){

    var socket = io.connect('http://****/span>

}

else if (/MSIE (\d+.\d+);/.test(navigator.userAgent)){

    var socket = io.connect('http://****/span>

}

else {

    var socket = io.connect('http://****/span>

}

 


本周热门