2011-01-12 9 views
3

私は8クイーンの問題を解決しようとしています(スペースを選んで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)iが数字に対応してチェスボード上の正方形を描画しない方法

+1

2次元配列を使用すると、アルゴリズムを簡単にコーディングできます。 (http://www.actionscript.org/forums/showpost.php3?s=492d976bbadb22650e813fb71e99da91&p=143220&postcount=5) –

答えて

2
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(); 
+0

また、私はそこに女王を置くことができるように配列の座標にアクセスする方法を教えてくれますか? – master565

1

その座標の合計が奇数と偶数の場合は白であるときは、セルが黒であると仮定することができます:あなたは広場クラスの作成を開始することができ

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

、これは各広場に固有の特性を与えるためにあなたを可能にします。例えば、1つの正方形に2つの部分があるのを避けたい場合は、色を設定したい、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; 
      } 
    } 

を加えます。