五子棋 html 5

<html>  
<head>  
<script src="http://res.yiwugou.com/res/ywgo/js/ywgo.js"></script>  
<script src="js/socket.io.js"></script>  
</head>  
  
  
<style>  
</style>  
  
<body style="background: #D5D5D5;">  
    <div style="text-align: center; padding: 20px; height: 100%;">  
        <div style="background: #FF8C00; margin: 10px 0; padding: 10px;">  
            <h1 id="msg">msg</h1>  
        </div>  
        <canvas id="fiveChess"></canvas>  
  
    </div>  
</body>  
  
  
  
<script type="text/javascript">  
    var CommonUtils = {  
        /**  
         * 画圆  
         */  
        drawCircle : function(ctx, x, y, r, color) {  
            ctx.beginPath();  
            ctx.fillStyle = color;  
            ctx.lineWidth = 2;  
            ctx.strokeStyle = '#000';  
            //ctx.shadowOffsetX = 3; // 阴影Y轴偏移  
            //ctx.shadowOffsetY = 3; // 阴影X轴偏移  
            //ctx.shadowBlur = 14; // 模糊尺寸  
            //ctx.shadowColor = 'rgba(0, 0, 0, 0.5)'; // 颜色  
            ctx.arc(x, y, r, 0, 2 * Math.PI);  
            ctx.stroke();  
            ctx.fill();  
        },  
        /**  
         * 画圆角矩形边框  
         */  
        drawRoundLine : function(ctx, x, y, w, h, r, lineWidth, strokeStyle) {  
            ctx.beginPath();  
            ctx.lineWidth = lineWidth || 5;  
            ctx.strokeStyle = strokeStyle || '#000';//线条颜色  
            ctx.moveTo(x + r, y);  
            ctx.lineTo(x + w - r, y);  
            ctx.arc(x + w - r, y + r, r, 3 * Math.PI / 2, 2 * Math.PI, false);  
            ctx.lineTo(x + w, y + h - r);  
            ctx.arc(x + w - r, y + h - r, r, 0, Math.PI / 2, false);  
            ctx.lineTo(x + r, y + h);  
            ctx.arc(x + r, y + h - r, r, Math.PI / 2, Math.PI, false);  
            ctx.lineTo(x, y + r);  
            ctx.arc(x + r, y + r, r, Math.PI, 3 * Math.PI / 2, false);  
            ctx.stroke();//画线框  
        },  
        /**  
         * 画圆角矩形  
         */  
        drawRoundRect : function(ctx, x, y, w, h, r, lineWidth, strokeStyle, fillStyle) {  
            CommonUtils.drawRoundLine(ctx, x, y, w, h, r, lineWidth, strokeStyle);  
            ctx.fillStyle = fillStyle || '#eee';//填充颜色  
            ctx.fill();//填充颜色  
        }  
    };  
  
    function ChessInfo(chess, ctx, x, y, width, height) {  
        this.chess = chess;  
        this.ctx = ctx;  
        this.x = x;  
        this.y = y;  
        this.width = width || 300;  
        this.height = height;  
        this.padding = 20;  
        this.lineWidth = 10;  
        this.itemHeight = 100;  
        this.itemRadius = 20;  
        this.oneX = this.x + this.padding;  
        this.oneY = this.y + this.padding;  
        this.twoX = this.x + this.padding;  
        this.twoY = this.y + this.padding * 2 + this.itemHeight;  
        this.msgX = this.x + this.padding;  
        this.msgY = this.y + this.padding * 3 + this.itemHeight * 2;  
    }  
    ChessInfo.prototype = {  
        drawOne : function(strokeStyle) {  
            CommonUtils.drawRoundRect(this.ctx, this.oneX, this.oneY, this.width - this.padding * 2, this.itemHeight, this.itemRadius, this.lineWidth, strokeStyle, "#B5B5B5");  
            CommonUtils.drawCircle(this.ctx, this.oneX + 40, this.oneY + 50, this.itemRadius, FiveChess.USER_ONE);  
        },  
        drawOneName : function(name) {  
            this.ctx.beginPath();  
            this.ctx.font = "bold 20px Courier New";  
            this.ctx.fillStyle = "#000";  
            this.ctx.fillText(name, this.oneX + 70, this.oneY + 55);  
        },  
        drawTwo : function(strokeStyle) {  
            CommonUtils.drawRoundRect(this.ctx, this.twoX, this.twoY, this.width - this.padding * 2, this.itemHeight, this.itemRadius, this.lineWidth, strokeStyle, "#B5B5B5");  
            CommonUtils.drawCircle(this.ctx, this.twoX + 40, this.twoY + 50, this.itemRadius, FiveChess.USER_TWO);  
        },  
        drawTwoName : function(name) {  
            this.ctx.beginPath();  
            this.ctx.font = "bold 20px Courier New";  
            this.ctx.fillStyle = "#000";  
            this.ctx.fillText(name, this.twoX + 70, this.twoY + 55);  
        },  
        drawInfo : function() {  
            this.ctx.beginPath();  
            this.ctx.fillStyle = '#ff8c00';  
            this.ctx.fillRect(this.x, this.y, this.width, this.height);  
  
            this.drawOne("red");  
            this.drawTwo("#fff");  
  
            CommonUtils.drawRoundRect(this.ctx, this.msgX, this.msgY, this.width - this.padding * 2, this.itemHeight * 5, this.itemRadius, this.lineWidth, "#FFF", "#B5B5B5");  
        },  
        drawMsg : function(msg, line) {  
            this.ctx.beginPath();  
            this.ctx.font = "bold 20px Courier New";  
            this.ctx.fillStyle = "#000";  
            this.ctx.fillText(msg, this.msgX + 20, this.msgY + 55 * line);  
        },  
        changeUser : function() {  
            var color = this.chess.lastPoint().color;  
            if (color == FiveChess.USER_ONE) {  
                CommonUtils.drawRoundLine(this.ctx, this.oneX, this.oneY, this.width - this.padding * 2, this.itemHeight, this.itemRadius, this.lineWidth, "#fff");  
                CommonUtils.drawRoundLine(this.ctx, this.twoX, this.twoY, this.width - this.padding * 2, this.itemHeight, this.itemRadius, this.lineWidth, "red");  
            } else {  
                CommonUtils.drawRoundLine(this.ctx, this.oneX, this.oneY, this.width - this.padding * 2, this.itemHeight, this.itemRadius, this.lineWidth, "red");  
                CommonUtils.drawRoundLine(this.ctx, this.twoX, this.twoY, this.width - this.padding * 2, this.itemHeight, this.itemRadius, this.lineWidth, "#fff");  
            }  
        }  
    };  
  
    function FiveChess(num, id) {  
        var infoWidth = 300;  
        this.num = num;  
        this.id = id;  
        this.cvs = document.getElementById(this.id);  
        this.ctx = this.cvs.getContext('2d');  
        this.cellWidth = 50;//棋盘格单元大小  
        this.paddingTop = this.cellWidth * 1.5;  
        this.paddingLeft = this.cellWidth * 1.5;  
        this.boardArray = [];  
        this.historyPoints = [];  
        this.boardWidth = this.paddingLeft * 2 + this.cellWidth * (this.num - 1);  
        this.boardHeight = this.paddingTop * 2 + this.cellWidth * (this.num - 1);  
        this.cvs.width = this.boardWidth + 20 + infoWidth;  
        this.cvs.height = this.boardHeight;  
        this.chessInfo = new ChessInfo(this, this.ctx, this.boardWidth + 20, 0, infoWidth, this.boardHeight);  
        this.status = 0;  
        this.fromUser = '';  
        this.toUser = '';  
        this.room = '';  
        this.socket = '';  
    }  
    FiveChess.prototype = {  
        init : function() {  
            this.initBoardArray();  
            this.drawBoard();  
            this.drawInfo();  
            this.addEvent();  
        },  
        initBoardArray : function() {  
            for (var i = 1; i <= this.num; i++) {  
                this.boardArray[i] = new Array();  
                for (var j = 1; j <= this.num; j++) {  
                    this.boardArray[i][j] = 0;  
                }  
            }  
        },  
  
        lastPoint : function() {  
            var len = this.historyPoints.length;  
            if (len == 0) {  
                return new Point(0, 0, FiveChess.USER_TWO);  
            } else {  
                return this.historyPoints[len - 1];  
            }  
        },  
        changeColor : function(color) {  
            if (color == FiveChess.USER_ONE) {  
                return FiveChess.USER_TWO;  
            } else {  
                return FiveChess.USER_ONE;  
            }  
        },  
        drawInfo : function() {  
            this.chessInfo.drawInfo();  
        },  
        drawBoard : function() {  
            this.ctx.beginPath();  
            this.ctx.fillStyle = "#ff8c00";  
            this.ctx.fillRect(0, 0, this.boardWidth, this.boardWidth);  
  
            this.ctx.beginPath();  
            this.ctx.fillStyle = '#eee';//填充颜色  
            this.ctx.strokeStyle = '#000';//线条颜色  
            this.ctx.lineWidth = 2;//设置线宽  
            for (var i = 1; i <= this.num; i++) {//划横  
                this.ctx.moveTo(this.paddingLeft, this.paddingTop + this.cellWidth * (i - 1));  
                this.ctx.lineTo(this.paddingLeft + this.cellWidth * (this.num - 1), this.paddingTop + this.cellWidth * (i - 1));  
            }  
            for (var i = 1; i <= this.num; i++) {//划竖  
                this.ctx.moveTo(this.paddingLeft + this.cellWidth * (i - 1), this.paddingTop);  
                this.ctx.lineTo(this.paddingLeft + this.cellWidth * (i - 1), this.paddingTop + this.cellWidth * (this.num - 1));  
            }  
            this.ctx.stroke();//画线框  
            this.ctx.fill();//填充颜色  
            //this.ctx.closePath();  
  
            this.ctx.beginPath();  
            this.ctx.font = "bold 20px Courier New";  
            this.ctx.fillStyle = "#000";  
            for (var i = 1; i <= this.num; i++) {  
                var code = 'A'.charCodeAt() - 1 + i;  
                this.ctx.fillText(String.fromCharCode(code), this.paddingLeft + (i - 1) * this.cellWidth - 5, this.paddingTop - 25);  
            }  
            for (var i = 1; i <= this.num; i++) {  
                this.ctx.fillText(i + '', this.paddingLeft + this.cellWidth * (this.num - 1) + 25, this.paddingTop + (i - 1) * this.cellWidth + 5);  
            }  
        },  
        addEvent : function() {  
            var self = this;  
            this.cvs.addEventListener('click', function(e) {  
                var wx = e.layerX;  
                var wy = e.layerY;  
                var x = parseInt((wx - self.paddingLeft + self.cellWidth / 2) / self.cellWidth) + 1;  
                var y = parseInt((wy - self.paddingTop + self.cellWidth / 2) / self.cellWidth) + 1;  
                var currColor = self.changeColor(self.lastPoint().color);  
                var point = new Point(x, y, currColor);  
                if (self.status == 1 && self.drawChess(point)) {  
                    self.emitPrivate(point);  
                    self.status = 2;  
                    $("#msg").html('Waiting for the opponent to play chess......');  
                }  
            }, false);  
        },  
        drawChess : function(point) {  
            var x = point.x;  
            var y = point.y;  
            var color = point.color;  
            if (x > 0 && y > 0 && x <= this.num && y <= this.num && this.boardArray[x][y] == 0) {  
                this.boardArray[x][y] = color;  
                this.historyPoints.push(point);  
                this.chessInfo.changeUser();  
                CommonUtils.drawCircle(this.ctx, point.getLayerX(this.cellWidth, this.paddingLeft), point.getLayerY(this.cellWidth, this.paddingTop), (this.cellWidth - 5) / 2,  
                        point.color);  
                return true;  
            }  
            return false;  
        },  
        initSocket : function() {  
            var self = this;  
            this.socket = io.connect('http://10.6.104.111:10000');  
            this.socket.on("connect", function() {  
  
            });  
  
            this.socket.emit('login', {  
                name : self.fromUser,  
                room : self.room  
            }, function(data) {  
                console.info(data);  
                if (data.code == -1) {  
                    $("#msg").html("Room Full! Please choose another room.");  
                } else if (data.code == -2) {  
                    $("#msg").html("Waiting for another user.");  
                }  
            });  
  
            this.socket.on('private', function(data) {  
                console.info(data);  
                var point = new Point(data.x, data.y, data.color);  
                if (self.status == 2 && self.drawChess(point)) {  
                    self.status = 1;  
                    $("#msg").html("Now it's your turn!!!");  
                }  
            });  
  
            this.socket.on('status', function(data) {  
                console.info(data);  
                self.toUser = data.toUser;  
                self.status = data.status;  
                if (data.status == 1) {  
                    $("#msg").html("It's your turn");  
                    self.chessInfo.drawOneName('Self:' + self.fromUser);  
                    self.chessInfo.drawTwoName('Opp:' + self.toUser);  
                } else if (data.status == 2) {  
                    $("#msg").html("Wait Opponent chess");  
                    self.chessInfo.drawOneName('Opp:' + self.toUser);  
                    self.chessInfo.drawTwoName('Self:' + self.fromUser);  
                }  
            });  
        },  
        emitPrivate : function(point) {  
            var sendData = {  
                room : this.room,  
                fromUser : this.fromUser,  
                toUser : this.toUser,  
                x : point.x,  
                y : point.y,  
                color : point.color  
            };  
            this.socket.emit('private', sendData, function(data) {  
                console.info(data);  
            });  
        }  
    };  
    FiveChess.USER_ONE = '#000';  
    FiveChess.USER_TWO = '#FFF';  
  
    function Point(x, y, color) {  
        this.x = x;  
        this.y = y;  
        this.color = color;  
        this.getLayerX = function(cellWidth, paddingLeft) {  
            return cellWidth * (this.x - 1) + paddingLeft;  
        };  
        this.getLayerY = function(cellWidth, paddingTop) {  
            return cellWidth * (this.y - 1) + paddingTop  
        };  
    }  
  
    $(function() {  
        var fiveChess = new FiveChess(15, 'fiveChess');  
        fiveChess.init();  
  
        var room = $.trim(prompt("Room", ""));  
        if (room == '') {  
            return;  
        }  
        fiveChess.chessInfo.drawMsg('Room:' + room, 1);  
        var fromUser = $.trim(prompt("Your name", ""));  
        if (fromUser == '') {  
            return;  
        }  
        fiveChess.fromUser = fromUser;  
        fiveChess.room = room;  
        fiveChess.initSocket();  
    });  
</script>  
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容