js实现五子棋小游戏

代码语言:html

所属分类:游戏

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>五子棋</title>
    <link rel="stylesheet" href="">
    <style type="text/css" media="screen">
        * {
            margin: 0;
            padding: 0;
        }
        img {
            border: 0;
        }
        input {
            outline: none;
        }
        html,body {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
        ol, ul ,li {
            list-style: none;
        }
        .wrap {
            max-width: 750px;
            max-height: 750px;
            margin: 0px auto;
            background: #fc9e46;
            position: relative;
        }
        #layout {
            width: 100%;
            height: 100%;
            position: relative;
            background: url('http://repo.bfw.wiki/bfwrepo/image/wzqbj2.png') center center no-repeat / cover;
        }

        span.W {
            background: #fff;
        }

        span.B {
            background: #000;
        }
        span {
            display: block;
            border-radius: 50%;
            background: #000;
            position: absolute;

        }
        #start,#win {
            width: 100%;
            height: 32px;
            padding-bottom: 1000px;
            position: absolute;
            text-align: center;
            top: 0;
            left: 0;
        }
        #win {
            display: none;
        }
        #start input,#win input {
            margin: 0 5px;
            padding: 5px 20px;
            border: none;
            border-radius: 5px;
            font-size: 32px;
            color: #fff;
            background: #333;
            box-shadow: 1px 1px 1px #111,inset 0px 0px 2px #333;
            cursor: pointer;
        }
        #start input:active,#win input:active {
            background: #222;
        }
        #showwin {
            width: 100%;
            height: 100px;
            line-height: 100px;
            background: #333;
            text-align: center;
            position: absolute;
            color: red;
            font-size: 30px;
            font-weight: bold;
            opacity: 0.9;
        }
        #steps {
            position: absolute;
            width: 100px;
            text-align: center;
            color: red;
            font-size: 18px;
            font-weight: bold;
            top: 5px;
            z-index: 1;
        }
    </style>
    <script src="http://repo.bfw.wiki/bfwrepo/js/jquery.17.js"></script>
    <script>

        $(function() {
            myPlay.init();
        })

        var myPlay = {

            $steps: 0,

            len: 15, //棋盘长度
            count: 0,

            chessboard: [], //棋盘布局数组

            wins: [], //赢法数组
            myWin: [],
            computerWin: [],

            myScore: [], //评分数组
            computerScore: [],

            starting: function() {

                That = this;
                var $start = $('#start');

                $start.css({
                    'padding-top': this.wrapH/2 - 25
                });

                $start.find('input:last-of-type').on('click', function() {
                    //电脑先
                    That.myFalling('B', 7, 7, 2, That.$outobj, That.computerWin, That.myWin);
                    $start.fadeOut(That.tablesize);
                    $(this).off('click');
                });

                $start.find('input:first-of-type').on('click', function() {
                    //我先
                    $start.fadeOut(That.tablesize);
                    $(this).off('click');
                    $start.find('input:last-of-type').off('click');
                });
            },

            init: function() {

                var winH = $('body').height(),
                $outobj = $('#layout');

                $('.wrap').css({
                    'width': winH*0.92, 'height': winH*0.92, 'margin-top': winH*0.02, 'padding': winH*0.02
                });

                this.wrapH = $('.wrap').outerHeight();
                this.$outobj = $outobj;
                this.tablesize = $outobj.height()/this.len;

                if ($outobj.children().length > 0) {
                    $outobj.children().remove();
                };

                for (var i = 0; i < this.len; i++) {
                    //棋盘和赢法数组初始化
                    this.chessboard[i] = [];
                    this.wins[i] = [];
                    for (var j = 0; j < this.len; j++) {
                        this.chessboard[i][j] = 0;
                        this.wins[i][j] = [];
                    };
                };

                var len_4 = this.len - 4;

                for (var i = 0; i < this.len; i++) {
                    //横向赢法
                    for (var j = 0; j < len_4; j++) {
                        for (var k = 0; k < 5; k++) {
                            this.wins[i][j+k][this.count] = true;
                        }
                        this.count++;
                    }
                }

                for (var i = 0; i < this.len; i++) {
                    //纵向赢法
                    for (var j = 0; j < len_4; j++) {
                        for (var k = 0; k < 5; k++) {
                            this.wins[j+k][i][this.count] = true;
                        }
                        this.count++;
                    }
                }

                for (var i = 0; i < len_4; i++) {
                    //正斜赢法
                    for (var j = 0; j < len_4; j++) {
                        for (var k = 0; k < 5; k++) {
                            this.wins[i+k][j+k][this.count] = true;
                        }
                        this.count++;
                    }
                }

                for (var i = 0; i < len_4; i++) {
                    //反斜赢法
                    for (var j = 14; j > 3; j--) {
                        for (var k = 0; k < 5; k++) {
                            this.wins[i+k][j-k][this.count] = true;
                        }
                        this.count++;
                    }
                }

                for (var i = 0; i < this.count; i++) {
                    this.myWin[i] = 0;
                    this.computerWin[i] = 0;
                }

                $('#steps strong').html(0);
                this.$steps = 0;

                this.starting(); //
                this.falling(); //落子
            },

            falling: function() {

                var That = this;

                this.$outobj.on('click', function() {

                    var ex = event.clientX,
                    ey .........完整代码请登录后点击上方下载按钮下载查看

网友评论0