8 problems with the queen

I am trying to solve the problem of 8 queens (where you select the space, and it will postpone 8 queens so that none of them hit each other), but I am having problems creating a checkerboard.

right now i have it

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) can I use this for a problem (can I be able to check if any queens will collide on its array coordinates)

b) how to draw squares on a chessboard to match the numbers

+3
source share
3 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();
+2
source

We can assume that the cell is black if the sum of its coordinates is odd and white if it is even:

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

Square, . , , , , , a1, c4 ..

, .

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

     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 the Queen to a specific instance of Square, use an array of squares.

+1
source

Source: https://habr.com/ru/post/1784962/


All Articles