开发者

getting array coordinates (8 queens problem)

开发者 https://www.devze.com 2023-02-04 02:17 出处:网络
I am trying to make a graphic program that solves the 8 queens problem and so far what i have is the chess board

I am trying to make a graphic program that solves the 8 queens problem and so far what i have is the chess board

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();

(thanks André for this code)

this creates a black and white checkered board for the problem but now i need to be able to place the queens. How am i able to see where the user clicks in order to put the queen in the box that is clicked on?

(sorry if my question isn't fully clear)


I added a very simple example to your question. See below:

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();

Good luck, Rob

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号