2011-01-12 3 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]; 

하고 난 두 가지

을 알 필요가

가) 내가이있는 경우 확인하도록 내가 할 수 문제 (이를 사용할 수 있습니다 그 배열 좌표 즈)가 충돌 할

b) 나 개수에 대응하기 위해 체스 보드 사각형을 그리 어떻게

+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

이 각 광장에 특정 속성을 부여 할 수 있도록한다. 예를 들어 하나의 사각형에 두 개의 피스가있는 것을 피하기 위해 색을 설정하려면 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; 
      } 
    } 

를 추가합니다.