gpt4 book ai didi

actionscript-3 - 8皇后问题

转载 作者:行者123 更新时间:2023-12-04 04:31:09 25 4
gpt4 key购买 nike

我正在尝试解决 8 皇后问题(您选择一个空间,它会放下 8 个皇后,这样它们就不会互相攻击)但是我在制作棋盘时遇到了问题。

现在我有这个

var chessBoard:Array = new Array(); 

chessBoard["row1"] = [1,0,1,0,1,0,1,0];
chessBoard["row2"] = [0,1,0,1,0,1,0,1];
chessBoard["row3"] = [1,0,1,0,1,0,1,0];
chessBoard["row4"] = [0,1,0,1,0,1,0,1];
chessBoard["row5"] = [1,0,1,0,1,0,1,0];
chessBoard["row6"] = [0,1,0,1,0,1,0,1];
chessBoard["row7"] = [1,0,1,0,1,0,1,0];
chessBoard["row8"] = [0,1,0,1,0,1,0,1];

我需要知道两件事

a) 我可以用它来解决问题吗(我可以让它检查是否有任何皇后会与其数组坐标发生碰撞)

b) 如何在棋盘上绘制与数字对应的方 block

最佳答案

var chessBoard:Array = new Array(); 
// Setup the 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));
}

// Size of the tile
var tileSize:int = 20;

function createChessBoard():void
{
// Itterate through the "chessBoard"-array
for(var i:int = 0; i < chessBoard.length; i++)
{
// Itterate through the arrays in the "chessBoard"-array
for(var j:int = 0; j < chessBoard[i].length; j++)
{
// Create new tile
var tile:Sprite = new Sprite();
// Create the color variable and check to see what value to put
// in it dependingin the value of the current itteration - 1 or 0
var tileColor:int = chessBoard[i][j] * 0xffffff;

// Tile-coloring-setup-thingie-routine
tile.graphics.beginFill(tileColor);
tile.graphics.drawRect(0, 0, tileSize, tileSize);
tile.graphics.endFill();

// Tile positioning
tile.x = j * tileSize;
tile.y = i * tileSize;

// Adding tile to the displaylist
addChild(tile);
}
}
}

// Run function
createChessBoard();

关于actionscript-3 - 8皇后问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4669514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com