2013-04-18 171 views
1

您好,這是我第一次在本網站發佈信息,所以我會盡可能地澄清。比較多個陣列的個別值

我正在使用javaScript創建一個戰艦遊戲以生成隨機船位。我創建了遊戲板的兩個陣列(X & Y),創建了一個類生成隨機放置,以及一個對象陣列用於不同的船隻如下....

// game board 
var xAxis = ["a","b","c","d","e","f","g","h","i","j"]; 
var yAxis = [1,2,3,4,5,6,7,8,9,10]; 

// Location Class decleration // 

function Location (size,name){ 
//// Parameters /////////////////////////////////////////// 
    this.size = size; 
    this.name = name;  
//// Class functions ////////////////////////////////////// 

// sets the ship to a random position 
this.shipPosition = function(){ 
    var orientation = Math.random();   
    var area = []; 
    if (orientation < 0.5){ 
     x = Math.floor(Math.random()*(xAxis.length-size+1)); 
     y = Math.floor(Math.random()*(yAxis.length)); 
     for (i=x; i < (size + x); i++){ 
      area.push([xAxis[i],yAxis[y]]); 
     } 
    } else { 
     x = Math.floor(Math.random()*(xAxis.length)); 
     y = Math.floor(Math.random()*(yAxis.length-size+1)); 
     for (i=y; i < (size + y); i++){ 
      area.push([xAxis[x],yAxis[i]]); 
     } 
    } 
    return area; 
}; 
///// Stored variables ///////////////////////////////////////// 

//stores position 
    var positionArray = this.shipPosition(); 
//assigns value to each element in array for checking 
//and makes the private value public. ex. a = ['h',1]  
    var a = positionArray[0]; 
    var b = positionArray[1]; 
    var c = positionArray[2]; 
    var d = positionArray[3]; 
    var e = positionArray[4]; 
    this.getA = a;  
    this.getB = b;   
    this.getC = c; 
    this.getD = d; 
    this.getE = e; 

// locator console logging function 

    this.whereIs = function(){ 
     console.log(this.name); 
     console.log("ship size is "+ size);  
     console.log("-- X - Y --"); 
     console.log(a); 
     console.log(b); 
     if (size > 2){console.log(c);} 
     if (size > 3){console.log(d);} 
     if (size > 4){console.log(e);} 
     console.log(" ***********************************  "); 
    }; 
} 
//ship array 
var computerShip = new Array(); 
computerShip[0] = new Location(2, "SHIP 1"); 
computerShip[1] = new Location(3, "SHIP 2"); 
computerShip[2] = new Location(3, "SHIP 3"); 
computerShip[3] = new Location(4, "SHIP 4"); 
computerShip[4] = new Location(5, "SHIP 5"); 

//used to call whereIs method for each ship 

var genetateComputerShip = function(){ 
    for(i=0; i<computerShip.length; i++){   
     computerShip[i].whereIs();   
    } 
};  
genetateComputerShip(); 

如何檢查的每個值船舶陣列檢查重疊? 我已經嘗試了多種方式,但遇到麻煩要麼挑出元素,要麼找到方法來輕鬆交叉引用它們。有任何想法嗎?

回答

0

我對代碼做了一些修改,以保持一個全局的「板子」概念,它被表示爲一個多維數組。看到這裏的代碼:

http://jsfiddle.net/mchail/ytwyS/1/

擊中「運行」後,請檢查您的瀏覽器的JavaScript控制檯,你應該在全板的放置每艘船最終看到的打印輸出。我修改了shipPosition(現在稱爲setShipPosition),以在放置船舶之前檢查船上的衝突。

不錯的工作,順便說一句!

UPDATE

修改了的jsfiddle打印板到屏幕爲好。繼續點擊「運行」以查看更多隨機生成的電路板。

+0

所以我是新手,在codeacademy.com上學到了一切,並且有幾個問題以確保我能理解。您創建了一個空白網格,當生成一個隨機點時,該函數檢查它是否已經採用「!== 0」,並且因爲它的while語句將運行至其真實。太棒了!我還不熟悉JS的css和html方面,所以打印板功能沒有意義,但生病了。 –

+0

完全正確。我正在使用你現有的代碼隨機選擇一個起點和方向,然後檢查是否有任何這些「方塊」在網格中被佔用。如果檢查失敗,我們需要重新開始一個新的隨機放置。 'printBoard()'只是爲了調試目的,所以不要太擔心。祝你好運! – mchail