获得数组坐标(8个皇后问题)

时间:2011-01-12 19:07:01

标签: actionscript-3 flash-cs4

我正在尝试制作一个解决8皇后问题的图形程序,到目前为止我所拥有的是棋盘

var chessBoard:Array = new Array(); 
for(var i:int = 0; i < 4; i++)
{
    chessBoard.push(new Array(1,0,1,0,1,0,1,0));
    chessBoard.push(new Array(0,1,0,1,0,1,0,1));
}

var tileSize:int = 20;

function createChessBoard():void
{
    for(var i:int = 0; i < chessBoard.length; i++)
    {
        for(var j:int = 0; j < chessBoard[i].length; j++)
        {
            var tile:Sprite = new Sprite();
            var tileColor:int = chessBoard[i][j] * 0xffffff;

            tile.graphics.beginFill(tileColor);
            tile.graphics.drawRect(0, 0, tileSize, tileSize);
            tile.graphics.endFill();

            tile.x = j * tileSize;
            tile.y = i * tileSize;

            addChild(tile);
        }
    }
}

createChessBoard();

(感谢André的代码)

这会为问题创建一个黑白格仔板,但现在我需要能够放置女王。我如何才能看到用户点击的位置,以便将女王放入点击的框中?

(对不起,如果我的问题不完全清楚)

1 个答案:

答案 0 :(得分:1)

我在你的问题中添加了一个非常简单的例子。见下文:

var chessBoard:Array = new Array(); 
for(var i:int = 0; i < 4; i++)
{
    chessBoard.push(new Array(1,0,1,0,1,0,1,0));
    chessBoard.push(new Array(0,1,0,1,0,1,0,1));
}

var tileSize:int = 20;

function createChessBoard():void
{
    for(var i:int = 0; i < chessBoard.length; i++)
    {
        for(var j:int = 0; j < chessBoard[i].length; j++)
        {
            var tile:Sprite = new Sprite();
            var tileColor:int = chessBoard[i][j] * 0xffffff;

            tile.graphics.beginFill(tileColor);
            tile.graphics.drawRect(0, 0, tileSize, tileSize);
            tile.graphics.endFill();

            //I added the name property and a MouseEvent.CLICK event listener
            tile.name = "tile__" + i + "_" j + "_sp";
        tile.addEventListener(MouseEvent.CLICK, onTileClick);

            tile.x = j * tileSize;
            tile.y = i * tileSize;

            addChild(tile);
        }
    }
}

function onTileClick(event:MouseEvent):void
{
     //This tells you which tile the user clicked on
     trace(event.target.name);
};

createChessBoard();
祝你好运, 罗布