开发者

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 co开发者_如何学Gorrespond with the numbers


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


You can assume that cell is black when sum of its coordinates is odd and white if even:

function getColor(x, y) {
  return (x + y) % 2 == 0 ? 0 : 1;
}
// or even
function getColor(x, y) {
  return (x + y) % 2;
}


You could start with creating a Square class, this would enable you to give specific properties to each Square. You want to avoid having two pieces on one square for instance, you want to set the color, also you would need to know the coordinates such as a1, c4 etc...

To draw your Chessboard, you could create rows of Squares.

    private function squares:Array = [];

    private function addRow( black:Boolean , _y:int , rowName:String ):void
    {
        for( var i:int ; i < 8 ; ++i )
        {
             var sq:Square = new Square();

            //alternate colors
            if( black )
                sq.color = 0;
            else
                sq.color = 0xffffff;
            black = !black;

            sq.x = i* sq.width;
            sq.y = _y;

            //save square Chess coordinates
            sq.coord = {letter: rowName , number: i + 1}

            addChild(sq);
            //add the Square instance to an Array for further reference
            squares.push( sq );
        }
    }

Then simply add the rows

     private function createBoard():void
     {
          var black:Boolean;
          var letters:Array = [ a , b , c , d , e , f , g , h ]

          for( var i:int ; i < 8 ; ++i )
          {
               addRow( black , i * squareSize , letters[i] );
               black = !black;
          }
     }

To add a Queen to a specific Square instance , use the squares Array.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜