js实现简单的围棋

代码语言:html

所属分类:游戏

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

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>BFW NEW PAGE</title>

    <script src="http://repo.bfw.wiki/bfwrepo/js/easeljs-0.7.1.min.js"></script>
    <script>

        function Circle() {
            createjs.Shape.call(this);
            this.setCircleType = function(type) {
                this._circleType = type;
                switch (type) {
                    case Circle.TYPE_UNSELECTED:
                        this.setColor("#cccccc");
                        break;
                    case Circle.TYPE_SELECTED:
                        this.setColor("#ff6600");
                        break;
                    case Circle.TYPE_CAT:
                        this.setColor("#0000ff");
                        break;
                }
            }

            this.setColor = function (colorString) {
                this.graphics.beginFill(colorString);
                this.graphics.drawCircle(0, 0, 25);
                this.graphics.endFill();
            }
            this.getCircleType = function() {
                return this._circleType;
            }
            this.setCircleType(1);
        }
        Circle.prototype = new createjs.Shape();
        Circle.TYPE_UNSELECTED = 1;
        Circle.TYPE_SELECTED = 2;
        Circle.TYPE_CAT = 3;

    </script>

</head>
<body>

    <div style="width:530px;margin:0 auto;">
        <canvas width="530px" height="500px" id="gameView"></canvas>
    </div>

    <script>
        var stage = new createjs.Stage("gameView");
        createjs.Ticker.setFPS(30);
        createjs.Ticker.addEventListener("tick", stage);

        var gameView = new createjs.Container();
        gameView.x = 30;
        gameView.y = 30;
        stage.addChild(gameView);

        var circleArr = [[], [], [], [], [], [], [], [], []];
        var currentCat;
        var MOVE_NONE=-1, MOVE_LEFT = 0, MOVE_UP_LEFT = 1, MOVE_UP_RIGHT = 2, MOVE_RIGHT = 3, MOVE_DOWN_RIGHT = 4, MOVE_DOWN_LEFT = 5;

        function getMoveDir(cat) {

            var distanceMap = [];
            //left
            var can = true;
            for (var x = cat.indexX; x >= 0; x--) {
                if (circleArr[x][cat.indexY].getCircleType() == Circle.TYPE_SELECTED) {
                    can = false;
                    distanceMap[MOVE_LEFT] = cat.indexX-x;
                    break;
                }
            }
            if (can) {
                return MOVE_LEFT;
            }
            //left up
            can = true;
            var x = cat.indexX, y = cat.indexY;
            while (true) {
                if (circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED) {
                    can = false;
                    distanceMap[MOVE_UP_LEFT] = cat.indexY-y;
                    break;
                }
                if (y%2 == 0) {
                    x--;
                }
                y--
                if (y < 0 || x < 0) {
                    break;
                }
            }
            if (can) {
                return MOVE_UP_LEFT;
            }
            //right up
            can = true;
            x = cat.indexX, y = cat.indexY
            while (true) {
                if (circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED) {
                    can = false;
                    distanceMap[MOVE_UP_RIGHT] = cat.indexY - y;
                    break;
                }

                if (y%2 == 1) {
                    x++
                }
                y--;
                if (y < 0 || x > 8) {
                    break
                }
            }

            if (can) {
                return MOVE_UP_RIGHT;
            }

            //right
            can = true;
            for (var x = cat.indexX; x < 9; x++) {
                if (circleArr[x][cat.indexY].getCircleType() == Circle.TYPE_SELECTED) {
                    can = false;
                    distanceMap[MOVE_RIGHT] = x - cat.indexX;
                    break;
                }
            }
            if (can) {
                return MOVE_RIGHT;
            }
            //right down
            can = true;
            x = cat.indexX, y = cat.indexY;
            while (true) {
                if (circleArr[x][y].getCircleType() == Circle.TYPE_SELECTED) {
                    can = false;
                    distanceMap[MOVE_DOWN_RIGHT] = y-cat.indexY;
                    break;
                }
                if (y%2 == 1) {
                    x++;
                }
                y.........完整代码请登录后点击上方下载按钮下载查看

网友评论0