<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>jquery五子棋游戏 -</title>
    <script type="text/javascript" src="js/CookieHandle.js"></script>
    <script type="text/javascript" src="js/jquery-1.7.2.js"></script>
</head>
<body style="ba-">
<div class="wrapper">
    <div class="chessboard">
        <!-- top line -->
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top"></div>
        <div class="chess-top chess-right"></div>
        <!-- line 1 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 2 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 3 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 4 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 5 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 6 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 7 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 8 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 9 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 10 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 11 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 12 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- line 13 -->
        <div class="chess-left"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-middle"></div>
        <div class="chess-right"></div>
        <!-- bottom line  -->
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom"></div>
        <div class="chess-bottom chess-right"></div>
    </div>

    <div class="operating-panel">
        <p>
            <a id="black_btn" class="btn selected" href="#">黑&nbsp;&nbsp;子</a>
            <a id="white_btn" class="btn" href="#">白&nbsp;&nbsp;子</a>
        </p>
        <p>
            <a id="first_move_btn" class="btn selected" href="#">先&nbsp;&nbsp;手</a>
            <a id="second_move_btn" class="btn" href="#">后&nbsp;&nbsp;手</a>
        </p>
        <a id="replay_btn" class="btn" href="#">开&nbsp;&nbsp;&nbsp;始</a>
        <p id="result_info">胜率:100%</p>
        <p id="result_tips"></p>
    </div>

    <div style="display: none">
        <!-- 图片需合并 减少http请求数 -->
        <img src="img/black.png" alt="preload" />
        <img src="img/white.png" alt="preload" />
        <img src="img/hover.png" alt="preload" />
        <img src="img/hover_up.png" alt="preload" />
        <img src="img/hover_down.png" alt="preload" />
        <img src="img/hover_up_left.png" alt="preload" />
        <img src="img/hover_up_right.png" alt="preload" />
        <img src="img/hover_left.png" alt="preload" />
        <img src="img/hover_right.png" alt="preload" />
        <img src="img/hover_down_left.png" alt="preload" />
        <img src="img/hover_down_right.png" alt="preload" />
        <img src="img/black_last.png" alt="preload" />
        <img src="img/white_last.png" alt="preload" />
    </div>
</div>
</body>
</html>

<style>
*{margin:0;padding:0;list-style-type:none;}
a,img{border:0;}
body{font:12px/180% Arial, Helvetica, sans-serif, "新宋体";}
.wrapper {
width: 600px;
position: relative;
}

/* 棋盘 */
div.chessboard {
    margin: 30px 0 0 50px;
    width: 542px;
    background: url(img/chessboard.png) no-repeat 14px 14px rgb(250, 250, 250);
    overflow: hidden;
    box-shadow: 2px 2px 8px #888;
    -webkit-box-shadow: 2px 2px 8px #888;
    -moz-box-shadow: 2px 2px 8px #888;
}

div.chessboard div {
    float: left;
    width: 36px;
    height: 36px;
    border-top: 0px solid #ccc;
    border-left: 0px solid #ccc;
    border-right: 0;
    border-bottom: 0;
    cursor: pointer;
}

/* 棋子 */
div.chessboard div.black {
    background: url(img/black.png) no-repeat 4px 4px;
}
div.chessboard div.white {
    background: url(img/white.png) no-repeat 4px 4px;
}
div.chessboard div.hover {
    background: url(img/hover.png) no-repeat 1px 1px;
}
div.chessboard div.hover-up {
    background: url(img/hover_up.png) no-repeat 1px 1px;
}
div.chessboard div.hover-down {
    background: url(img/hover_down.png) no-repeat 1px 1px;
}
div.chessboard div.hover-up-left {
    background: url(img/hover_up_left.png) no-repeat 1px 1px;
}
div.chessboard div.hover-up-right {
    background: url(img/hover_up_right.png) no-repeat 1px 1px;
}
div.chessboard div.hover-left {
    background: url(img/hover_left.png) no-repeat 1px 1px;
}
div.chessboard div.hover-right {
    background: url(img/hover_right.png) no-repeat 1px 1px;
}
div.chessboard div.hover-down-left {
    background: url(img/hover_down_left.png) no-repeat 1px 1px;
}
div.chessboard div.hover-down-right {
    background: url(img/hover_down_right.png) no-repeat 1px 1px;
}
div.chessboard div.white-last {
    background: url(img/white_last.png) no-repeat 4px 4px;
}
div.chessboard div.black-last {
    background: url(img/black_last.png) no-repeat 4px 4px;
}

/* 右侧 */

div.operating-panel {
    position: absolute;
    left: 610px;
    top: 150px;
    width: 200px;
    text-align: center;
}

.operating-panel a {
    display: inline-block;
    padding: 10px 15px;
    margin-bottom: 39px;
    margin-right: 8px;
    margin-left: 8px;
    background: rgb(100, 167, 233);
    text-decoration: none;
    color: #333;
    font-weight: bold;
    font-size: 16px;
    font-family: "微软雅黑", "宋体";
}

.operating-panel a:hover {
    background: rgb(48, 148, 247);
    text-decoration: none;
}

.operating-panel a.disable, .operating-panel a.disable:hover {
    cursor: default;
    background: rgb(197, 203, 209);
    color: rgb(130, 139, 148);
}

.operating-panel a.selected {
    border: 5px solid #F3C242;
    padding: 5px 10px;
}

#result_tips {
    color: #CE4242;
    font-size: 26px;
    font-family: "微软雅黑";
}

#result_info {
    margin-bottom: 26px;
}</style>
                <script>$(document).ready(function () {
    fiveChess.init();
});

                    var fiveChess = {
                        NO_CHESS: 0,
                        BLACK_CHESS: -1,
                        WHITE_CHESS: 1,
                        chessArr: [],    //记录棋子
                        chessBoardHtml: "",
                        humanPlayer: "black",    //玩家棋子颜色
                        AIPlayer: "white",    //AI棋子颜色
                        isPlayerTurn: true, //轮到player下棋
                        totalGames: cookieHandle.getCookie("totalGames") || 0,    //总局数
                        winGames: cookieHandle.getCookie("winGames") || 0,    //玩家赢局数
                        isGameStart: false,    //游戏已经开始
                        isGameOver: false, //游戏结束
                        playerLastChess: [], //玩家最后下子位置
                        AILastChess: [], //AI最后下子位置

                        init: function () {
                            this.chessBoardHtml = $("div.chessboard").html();
                            var _fiveChess = this;
                            //右侧操作按钮
                            $(".operating-panel a").click(function (event) {
                                event.preventDefault();
                                var id = $(this).attr("id");
                                if (_fiveChess.isGameStart && id !== "replay_btn") { return; }    //正在游戏 不操作
                                switch (id) {
                                    case "black_btn":
                                        _fiveChess.humanPlayer = "black";
                                        _fiveChess.AIPlayer = "white";
                                        break;
                                    case "white_btn":
                                        _fiveChess.humanPlayer = "white";
                                        _fiveChess.AIPlayer = "black";
                                        break;
                                    case "first_move_btn":
                                        _fiveChess.isPlayerTurn = true;
                                        break;
                                    case "second_move_btn":
                                        _fiveChess.isPlayerTurn = false;
                                        break;
                                    case "replay_btn":
                                        _fiveChess.resetChessBoard();
                                        if (_fiveChess.isGameStart) {    //点重玩
                                            _fiveChess.gameOver();
                                        }
                                        else {    //点开始
                                            _fiveChess.gameStart();
                                        }
                                        break;
                                }
                                if (id !== "replay_btn") {
                                    $(this).addClass("selected").siblings().removeClass("selected");
                                }
                            });
                            this.resetChessBoard();
                            $("#result_info").html("胜率:" + (this.winGames * 100 / this.totalGames | 0) + "%");
                        },
                        //重置棋盘
                        resetChessBoard: function () {
                            $("div.chessboard").html(this.chessBoardHtml);
                            $("#result_tips").html("");
                            this.isGameOver = false;
                            this.isPlayerTurn = $("#first_move_btn").hasClass("selected");
                            //初始化棋子状态
                            var i, j;
                            for (i = 0; i < 15; i++) {
                                this.chessArr[i] = [];
                                for (j = 0; j < 15; j++) {
                                    this.chessArr[i][j] = this.NO_CHESS;
                                }
                            }
                            //player下棋事件
                            var _fiveChess = this;
                            $("div.chessboard div").click(function () {
                                if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) {
                                    return;
                                }
                                if (!_fiveChess.isGameStart) {
                                    _fiveChess.gameStart();
                                }
                                var index = $(this).index(),
                                    i = index / 15 | 0,
                                    j = index % 15;
                                if (_fiveChess.chessArr[i][j] === _fiveChess.NO_CHESS) {
                                    _fiveChess.playChess(i, j, _fiveChess.humanPlayer);
                                    if (i === 0 && j === 0) {
                                        $(this).removeClass("hover-up-left");
                                    }
                                    else if (i === 0 && j === 14) {
                                        $(this).removeClass("hover-up-right");
                                    }
                                    else if (i === 14 && j === 0) {
                                        $(this).removeClass("hover-down-left");
                                    }
                                    else if (i === 14 && j === 14) {
                                        $(this).removeClass("hover-down-right");
                                    }
                                    else if (i === 0) {
                                        $(this).removeClass("hover-up");
                                    }
                                    else if (i === 14) {
                                        $(this).removeClass("hover-down");
                                    }
                                    else if (j === 0) {
                                        $(this).removeClass("hover-left");
                                    }
                                    else if (j === 14) {
                                        $(this).removeClass("hover-right");
                                    }
                                    else {
                                        $(this).removeClass("hover");
                                    }
                                    _fiveChess.playerLastChess = [i, j];
                                    _fiveChess.playerWinOrNot(i, j);
                                }
                            });
                            //鼠标在棋盘上移动效果
                            $("div.chessboard div").hover(
                                function () {
                                    if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) { return; }
                                    var index = $(this).index(),
                                        i = index / 15 | 0,
                                        j = index % 15;
                                    if (_fiveChess.chessArr[i][j] === _fiveChess.NO_CHESS) {
                                        if (i === 0 && j === 0) {
                                            $(this).addClass("hover-up-left");
                                        }
                                        else if (i === 0 && j === 14) {
                                            $(this).addClass("hover-up-right");
                                        }
                                        else if (i === 14 && j === 0) {
                                            $(this).addClass("hover-down-left");
                                        }
                                        else if (i === 14 && j === 14) {
                                            $(this).addClass("hover-down-right");
                                        }
                                        else if (i === 0) {
                                            $(this).addClass("hover-up");
                                        }
                                        else if (i === 14) {
                                            $(this).addClass("hover-down");
                                        }
                                        else if (j === 0) {
                                            $(this).addClass("hover-left");
                                        }
                                        else if (j === 14) {
                                            $(this).addClass("hover-right");
                                        }
                                        else {
                                            $(this).addClass("hover");
                                        }
                                    }
                                },
                                function () {
                                    if (!_fiveChess.isPlayerTurn || _fiveChess.isGameOver) { return; }
                                    var index = $(this).index(),
                                        i = index / 15 | 0,
                                        j = index % 15;
                                    if (i === 0 && j === 0) {
                                        $(this).removeClass("hover-up-left");
                                    }
                                    else if (i === 0 && j === 14) {
                                        $(this).removeClass("hover-up-right");
                                    }
                                    else if (i === 14 && j === 0) {
                                        $(this).removeClass("hover-down-left");
                                    }
                                    else if (i === 14 && j === 14) {
                                        $(this).removeClass("hover-down-right");
                                    }
                                    else if (i === 0) {
                                        $(this).removeClass("hover-up");
                                    }
                                    else if (i === 14) {
                                        $(this).removeClass("hover-down");
                                    }
                                    else if (j === 0) {
                                        $(this).removeClass("hover-left");
                                    }
                                    else if (j === 14) {
                                        $(this).removeClass("hover-right");
                                    }
                                    else {
                                        $(this).removeClass("hover");
                                    }
                                }
                            );
                        },
                        gameStart: function () {
                            this.totalGames++;
                            cookieHandle.setCookie({ name: "totalGames", value: this.totalGames, expiresHours: 365 * 24 });
                            //AI先手
                            if (!this.isPlayerTurn) {
                                this.AImoveChess();
                            }
                            this.isGameStart = true;
                            $(".operating-panel p a").addClass("disable");
                            $("#replay_btn").html("重&nbsp;&nbsp;&nbsp;玩");
                        },
                        gameOver: function () {
                            this.isGameStart = false;
                            $(".operating-panel a").removeClass("disable");
                            $("#replay_btn").html("开&nbsp;&nbsp;&nbsp;始");
                            $("#result_info").html("胜率:" + (this.winGames * 100 / this.totalGames | 0) + "%");
                        },

                        //下棋 i行,j列,color颜色
                        playChess: function (i, j, color) {
                            this.chessArr[i][j] = color === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
                            if (color === this.AIPlayer) {
                                $("div.chessboard div." + color + "-last").addClass(color).removeClass(color + "-last");
                                $("div.chessboard div:eq(" + (i * 15 + j) + ")").addClass(color + "-last");
                            }
                            else {
                                $("div.chessboard div:eq(" + (i * 15 + j) + ")").addClass(color);
                            }
                        },
                        //玩家是否取胜
                        playerWinOrNot: function (i, j) {
                            var nums = 1,    //连续棋子个数
                                chessColor = this.humanPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS,
                                m, n;
                            //x方向
                            for (m = j - 1; m >= 0; m--) {
                                if (this.chessArr[i][m] === chessColor) {
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            for (m = j + 1; m < 15; m++) {
                                if (this.chessArr[i][m] === chessColor) {
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            if (nums >= 5) {
                                this.playerWin();
                                return;
                            }
                            else {
                                nums = 1;
                            }
                            //y方向
                            for (m = i - 1; m >= 0; m--) {
                                if (this.chessArr[m][j] === chessColor) {
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            for (m = i + 1; m < 15; m++) {
                                if (this.chessArr[m][j] === chessColor) {
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            if (nums >= 5) {
                                this.playerWin();
                                return;
                            }
                            else {
                                nums = 1;
                            }
                            //左斜方向
                            for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
                                if (this.chessArr[m][n] === chessColor) {
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            for (m = i + 1, n = j + 1; m < 15 && n < 15; m++, n++) {
                                if (this.chessArr[m][n] === chessColor) {
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            if (nums >= 5) {
                                this.playerWin();
                                return;
                            }
                            else {
                                nums = 1;
                            }
                            //右斜方向
                            for (m = i - 1, n = j + 1; m >= 0 && n < 15; m--, n++) {
                                if (this.chessArr[m][n] === chessColor) {
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            for (m = i + 1, n = j - 1; m < 15 && n >= 0; m++, n--) {
                                if (this.chessArr[m][n] === chessColor) {
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            if (nums >= 5) {
                                this.playerWin();
                                return;
                            }
                            this.AImoveChess();
                        },
                        playerWin: function () {
                            this.winGames++;
                            cookieHandle.setCookie({ name: "winGames", value: this.winGames, expiresHours: 365 * 24 });
                            this.showResult(true);
                            this.gameOver();
                        },
                        //AI下棋
                        AImoveChess: function () {
                            this.isPlayerTurn = false;
                            var maxX = 0,
                                maxY = 0,
                                maxWeight = 0,
                                i, j, tem;
                            for (i = 14; i >= 0; i--) {
                                for (j = 14; j >= 0; j--) {
                                    if (this.chessArr[i][j] !== this.NO_CHESS) {
                                        continue;
                                    }
                                    tem = this.computeWeight(i, j);
                                    if (tem > maxWeight) {
                                        maxWeight = tem;
                                        maxX = i;
                                        maxY = j;
                                    }
                                }
                            }
                            this.playChess(maxX, maxY, this.AIPlayer);
                            this.AILastChess = [maxX, maxY];
                            if ((maxWeight >= 100000 && maxWeight < 250000) || (maxWeight >= 500000)) {
                                this.showResult(false);
                                this.gameOver();
                            }
                            else {
                                this.isPlayerTurn = true;
                            }
                        },
                        showResult: function (isPlayerWin) {
                            if (isPlayerWin) {
                                $("#result_tips").html("恭喜你获胜!");
                            }
                            else {
                                $("#result_tips").html("哈哈,你输咯!");
                            }
                            this.isGameOver = true;
                            this.showWinChesses(isPlayerWin);
                        },
                        //标记显示获胜棋子
                        showWinChesses: function (isPlayerWin) {
                            var nums = 1,    //连续棋子个数
                                lineChess = [],    //连续棋子位置
                                i,
                                j,
                                chessColor,
                                m, n;
                            if (isPlayerWin) {
                                chessColor = this.humanPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
                                i = this.playerLastChess[0];
                                j = this.playerLastChess[1];
                            }
                            else {
                                chessColor = this.AIPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
                                i = this.AILastChess[0];
                                j = this.AILastChess[1];
                            }
                            $("div.chessboard div." + this.AIPlayer + "-last").addClass(this.AIPlayer).removeClass(this.AIPlayer + "-last");
                            //x方向
                            lineChess[0] = [i];
                            lineChess[1] = [j];
                            for (m = j - 1; m >= 0; m--) {
                                if (this.chessArr[i][m] === chessColor) {
                                    lineChess[0][nums] = i;
                                    lineChess[1][nums] = m;
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            for (m = j + 1; m < 15; m++) {
                                if (this.chessArr[i][m] === chessColor) {
                                    lineChess[0][nums] = i;
                                    lineChess[1][nums] = m;
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            if (nums >= 5) {
                                for (k = nums - 1; k >= 0; k--) {
                                    this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.humanPlayer : this.AIPlayer);
                                }
                                return;
                            }
                            //y方向
                            nums = 1;
                            lineChess[0] = [i];
                            lineChess[1] = [j];
                            for (m = i - 1; m >= 0; m--) {
                                if (this.chessArr[m][j] === chessColor) {
                                    lineChess[0][nums] = m;
                                    lineChess[1][nums] = j;
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            for (m = i + 1; m < 15; m++) {
                                if (this.chessArr[m][j] === chessColor) {
                                    lineChess[0][nums] = m;
                                    lineChess[1][nums] = j;
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            if (nums >= 5) {
                                for (k = nums - 1; k >= 0; k--) {
                                    this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.humanPlayer : this.AIPlayer);
                                }
                                return;
                            }
                            //左斜方向
                            nums = 1;
                            lineChess[0] = [i];
                            lineChess[1] = [j];
                            for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
                                if (this.chessArr[m][n] === chessColor) {
                                    lineChess[0][nums] = m;
                                    lineChess[1][nums] = n;
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            for (m = i + 1, n = j + 1; m < 15 && n < 15; m++, n++) {
                                if (this.chessArr[m][n] === chessColor) {
                                    lineChess[0][nums] = m;
                                    lineChess[1][nums] = n;
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            if (nums >= 5) {
                                for (k = nums - 1; k >= 0; k--) {
                                    this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.humanPlayer : this.AIPlayer);
                                }
                                return;
                            }
                            //右斜方向
                            nums = 1;
                            lineChess[0] = [i];
                            lineChess[1] = [j];
                            for (m = i - 1, n = j + 1; m >= 0 && n < 15; m--, n++) {
                                if (this.chessArr[m][n] === chessColor) {
                                    lineChess[0][nums] = m;
                                    lineChess[1][nums] = n;
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            for (m = i + 1, n = j - 1; m < 15 && n >= 0; m++, n--) {
                                if (this.chessArr[m][n] === chessColor) {
                                    lineChess[0][nums] = m;
                                    lineChess[1][nums] = n;
                                    nums++;
                                }
                                else {
                                    break;
                                }
                            }
                            if (nums >= 5) {
                                for (k = nums - 1; k >= 0; k--) {
                                    this.markChess(lineChess[0][k] * 15 + lineChess[1][k], isPlayerWin ? this.humanPlayer : this.AIPlayer);
                                }
                            }
                        },
                        markChess: function (pos, color) {
                            $("div.chessboard div:eq(" + pos + ")").removeClass(color).addClass(color + "-last");
                        },
                        //下子到i,j X方向 结果: 多少连子 两边是否截断
                        putDirectX: function (i, j, chessColor) {
                            var m, n,
                                nums = 1,
                                side1 = false,
                                side2 = false;
                            for (m = j - 1; m >= 0; m--) {
                                if (this.chessArr[i][m] === chessColor) {
                                    nums++;
                                }
                                else {
                                    if (this.chessArr[i][m] === this.NO_CHESS) {
                                        side1 = true;
                                    }
                                    break;
                                }
                            }
                            for (m = j + 1; m < 15; m++) {
                                if (this.chessArr[i][m] === chessColor) {
                                    nums++;
                                }
                                else {
                                    if (this.chessArr[i][m] === this.NO_CHESS) {
                                        side2 = true;
                                    }
                                    break;
                                }
                            }
                            return { "nums": nums, "side1": side1, "side2": side2 };
                        },
                        //下子到i,j Y方向 结果
                        putDirectY: function (i, j, chessColor) {
                            var m, n,
                                nums = 1,
                                side1 = false,
                                side2 = false;
                            for (m = i - 1; m >= 0; m--) {
                                if (this.chessArr[m][j] === chessColor) {
                                    nums++;
                                }
                                else {
                                    if (this.chessArr[m][j] === this.NO_CHESS) {
                                        side1 = true;
                                    }
                                    break;
                                }
                            }
                            for (m = i + 1; m < 15; m++) {
                                if (this.chessArr[m][j] === chessColor) {
                                    nums++;
                                }
                                else {
                                    if (this.chessArr[m][j] === this.NO_CHESS) {
                                        side2 = true;
                                    }
                                    break;
                                }
                            }
                            return { "nums": nums, "side1": side1, "side2": side2 };
                        },
                        //下子到i,j XY方向 结果
                        putDirectXY: function (i, j, chessColor) {
                            var m, n,
                                nums = 1,
                                side1 = false,
                                side2 = false;
                            for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
                                if (this.chessArr[m][n] === chessColor) {
                                    nums++;
                                }
                                else {
                                    if (this.chessArr[m][n] === this.NO_CHESS) {
                                        side1 = true;
                                    }
                                    break;
                                }
                            }
                            for (m = i + 1, n = j + 1; m < 15 && n < 15; m++, n++) {
                                if (this.chessArr[m][n] === chessColor) {
                                    nums++;
                                }
                                else {
                                    if (this.chessArr[m][n] === this.NO_CHESS) {
                                        side2 = true;
                                    }
                                    break;
                                }
                            }
                            return { "nums": nums, "side1": side1, "side2": side2 };
                        },
                        putDirectYX: function (i, j, chessColor) {
                            var m, n,
                                nums = 1,
                                side1 = false,
                                side2 = false;
                            for (m = i - 1, n = j + 1; m >= 0 && n < 15; m--, n++) {
                                if (this.chessArr[m][n] === chessColor) {
                                    nums++;
                                }
                                else {
                                    if (this.chessArr[m][n] === this.NO_CHESS) {
                                        side1 = true;
                                    }
                                    break;
                                }
                            }
                            for (m = i + 1, n = j - 1; m < 15 && n >= 0; m++, n--) {
                                if (this.chessArr[m][n] === chessColor) {
                                    nums++;
                                }
                                else {
                                    if (this.chessArr[m][n] === this.NO_CHESS) {
                                        side2 = true;
                                    }
                                    break;
                                }
                            }
                            return { "nums": nums, "side1": side1, "side2": side2 };
                        },
                        //计算下子至i,j的权重
                        computeWeight: function (i, j) {
                            var weight = 14 - (Math.abs(i - 7) + Math.abs(j - 7)), //基于棋盘位置权重
                                pointInfo = {},    //某点下子后连子信息
                                chessColor = this.AIPlayer === "black" ? this.BLACK_CHESS : this.WHITE_CHESS;
                            //x方向
                            pointInfo = this.putDirectX(i, j, chessColor);
                            weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
                            pointInfo = this.putDirectX(i, j, -chessColor);
                            weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
                            //y方向
                            pointInfo = this.putDirectY(i, j, chessColor);
                            weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
                            pointInfo = this.putDirectY(i, j, -chessColor);
                            weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
                            //左斜方向
                            pointInfo = this.putDirectXY(i, j, chessColor);
                            weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
                            pointInfo = this.putDirectXY(i, j, -chessColor);
                            weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
                            //右斜方向
                            pointInfo = this.putDirectYX(i, j, chessColor);
                            weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, true);//AI下子权重
                            pointInfo = this.putDirectYX(i, j, -chessColor);
                            weight += this.weightStatus(pointInfo.nums, pointInfo.side1, pointInfo.side2, false);//player下子权重
                            return weight;
                        },
                        //权重方案   独:两边为空可下子,单:一边为空
                        weightStatus: function (nums, side1, side2, isAI) {
                            var weight = 0;
                            switch (nums) {
                                case 1:
                                    if (side1 && side2) {
                                        weight = isAI ? 15 : 10;    //独一
                                    }
                                    break;
                                case 2:
                                    if (side1 && side2) {
                                        weight = isAI ? 100 : 50;    //独二
                                    }
                                    else if (side1 || side2) {
                                        weight = isAI ? 10 : 5;    //单二
                                    }
                                    break;
                                case 3:
                                    if (side1 && side2) {
                                        weight = isAI ? 500 : 200;    //独三
                                    }
                                    else if (side1 || side2) {
                                        weight = isAI ? 30 : 20;    //单三
                                    }
                                    break;
                                case 4:
                                    if (side1 && side2) {
                                        weight = isAI ? 5000 : 2000;    //独四
                                    }
                                    else if (side1 || side2) {
                                        weight = isAI ? 400 : 100;    //单四
                                    }
                                    break;
                                case 5:
                                    weight = isAI ? 100000 : 10000;    //五
                                    break;
                                default:
                                    weight = isAI ? 500000 : 250000;
                                    break;
                            }
                            return weight;
                        }
                    };</script>

H5 五子棋源码的更多相关文章

  1. H5传奇源码,附带微信支付,商城系统,新增了元宝交易商城系统源码

    源码说明:传奇游戏是80年底的经典游戏,传奇源码,H5游戏源码下载,附带微信支付,商城系统,新增了元宝交易商城系统源码,内置很多任务,比如首冲任务,修复了很多BUG.[架设要求]游戏名称:H5传奇世界 ...

  2. 工具 - 怎么看微信h5的源码?

    这个问题在我看网易的h5案例的时候萌生的.因为想看他的源码,但是手机微信打开肯定看不了. 以下几种看代码的方法:(页面案例用网易大大刷屏的h5<二零一六娱乐圈画卷>,真的是一个值得我等众生 ...

  3. 台哥原创:java五子棋源码(人机对弈)

    最近整理以前的代码,发现一些项目还不错,发出来展示下,嘿嘿. 第一个,就是我的五子棋,这是当初学习java编程做的第一个项目. 算起来已经有12年了,后面对源码做了几次重构完善,清爽了很多. 有三种大 ...

  4. js+html5双人五子棋(源码下载)

    代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...

  5. 原声js 五子棋 源码

    Welcome to use MarkDown <style type="text/css"> .box{ width: 600px; height: 600px; b ...

  6. 434个H5游戏源码

    各种类型HTML5游戏,界面和JS均可供项目参考 下面是下载地址

  7. 分享微信h5支付源码

    类库代码 wechatH5Pay.php <?php //use Flight; /** * 微信支付服务器端下单 * 微信APP支付文档地址: https://pay.weixin.qq.co ...

  8. 微信h5支付源码DEMO参考

    类库代码 wechatH5Pay.php <?php //use Flight; /** * 微信支付服务器端下单 * 微信APP支付文档地址: https://pay.weixin.qq.co ...

  9. 五子棋C#源码,网络对战版---转载--待学习

    五子棋C#源码,网络对战版 支持网络对战的C#五子棋源码下载,VS2010环境编译后可运行,需要先输入ip地址才行,不是单机版的.部分代码摘录如下://将接收的消息转换成自定义集合MessClass ...

随机推荐

  1. PowerShell实现简单的搜索地区功能

    Add-Type -AssemblyName Microsoft.VisualBasic $VBI = [Microsoft.VisualBasic.Interaction] function Sea ...

  2. Educational Codeforces Round 25

    A 题意:给你一个01的字符串,0是个分界点,0把这个字符串分成(0的个数+1)个部分,分别求出这几部分1的个数.例如110011101 输出2031,100输出100,1001输出101 代码: # ...

  3. jenkins:一个jenkins项目远程触发另一个jenkins项目构建配置

    很多时候,我们会有这样的应用场景:一个jenkins上的项目构建后,需要远程触发另一台机子上的jenkins中某个项目的构建,可以通过Parameterized Remote Trigger Conf ...

  4. 打开Python IDLE时的错误:Subprocess Startup Error

    比较常见的是这个 方法1: 修改[Python目录]\Lib\idlelib\PyShell.py文件,在1300行附近,将def main():函数下面 use_subprocess = True ...

  5. 第一节:学会Java前提-手把手教你配置JDK环境变量

    前言 大家好,今天写一遍学会Java前提-手把手教你配置JDK环境变量的概述,希望你们喜欢 下载地址 下载jdk,和eclipse就比较简单了,提供JDK 9 地址: http://www.oracl ...

  6. OAuth2简易实战(一)-四种模式

    1. OAuth2简易实战(一)-四种模式 1.1. 授权码授权模式(Authorization code Grant) 1.1.1. 流程图 1.1.2. 授权服务器配置 配置授权服务器中 clie ...

  7. alibaba的FastJson找不到JSON对象问题

    在现在出现使用JSON.toJsonString()方法时,可能没有JSON这个对象. 这种问题可能是下载的jar版本比较高.在低版本的jar使用的是JSON对象. 我使用的是1.2.47版本的jar ...

  8. 在C语言中不使用任何中间变量如何将a、b的值进行交换(三种方法)——来自一小萌新工程师的复习

    今天面试嵌入式,突然遇到这么一道题目,虽然简单,但鉴于我答得不是很好,所以还是分析一下为好. 第一种方法: 通过加减法. #include"stdio.h" int main(vo ...

  9. git push 时发生 error: failed to push some refs to 错误 (解决办法)

    出现问题的原因:在github上更新了README.md,没有更新到本地仓库.而在本地git仓库又修改了文件,这时使用 git push origin master 推送到远程仓库后就出现了下面的问题 ...

  10. 11.Django2.0文档

    第四章 模板 1.标签 (1)if/else {% if %} 标签检查(evaluate)一个变量,如果这个变量为真(即,变量存在,非空,不是布尔值假),系统会显示在 {% if %} 和 {% e ...