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

发布于 2024-10-11 20:34:09 字数 945 浏览 6 评论 0原文

我正在尝试制作一个图形程序来解决 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();

(感谢安德烈提供此代码),

这为该问题创建了一个黑白方格棋盘,但现在我需要能够放置皇后。我如何才能看到用户单击的位置以便将皇后放入单击的框中?

(抱歉,如果我的问题不完全清楚)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2024-10-18 20:34:09

我为你的问题添加了一个非常简单的例子。请参阅下文:

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

祝你好运,

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文