Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

8 queens problem

I am trying to solve the 8 queens problem (where you select a space and it will put down 8 queens so that none of them will hit each other) but i am having trouble making the chess board.

right now i have this

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];

and i need to know two things

a) will i be able to use this for the problem (will i be able to have it check if any queens will collide by its array coordinates)

b) how do i draw the squares on the chess board to correspond with the numbers

like image 286
master565 Avatar asked May 08 '26 02:05

master565


1 Answers

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();
like image 178
André Avatar answered May 09 '26 17:05

André



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!