2012-01-17 68 views
1

我目前正在研究一個隨機數生成器腳本,它將生成三個數字。前兩個數字將用於通過引用行和列來選擇「表」中的「單元格」。每個單元格內有兩個數字可供選擇。第三個號碼將決定選擇哪兩個號碼。我已經找出了大部分,但我在最後一部分遇到問題。從嵌套數組中打印Javascript

我的「解決方案」是創建二十個數組,每個數組中有二十多個數組。這是第一個陣列的一個例子:

在標題我有以下:

腳本來生成所述第一兩個數字:

var randOne20 = Math.floor(Math.random()*20); 
var randTwo20 = Math.floor(Math.random()*20); 

這裏是20個陣列的第一行:

var lvl1Roll1 = [[ 1,0 ],[ 1,1 ],[ 1,2 ],[ 1,3 ],[ 1,4 ],[ 1,5 ],[ 1,6 ],[ 1,7 ],[ 1,8 ],[ 1,9 ],[ 1,10 ],[ 1,11 ],[ 1,12 ],[ 1,13 ],[ 1,14 ],[ 1,15 ],[ 1,16 ],[ 1,17 ],[ 1,18 ],[ 1,19 ]]; 

第二行是「lvl1Roll2」,第三是「lvl1Roll3」等一路攀升到「lvl1Roll20。」

在身體,我有以下腳本:

var randOne = randOne20 + 1; 

document.write(window['lvl1Roll'+randOne][randTwo20]); 

的randOne變量來選擇相應的行。

我可以弄清楚如何選擇特定的單元格(使用randTwo20變量),但我不知道如何選擇每個單元格中的第一個或第二個數字。

現在,只是爲了澄清,我沒有列出第三個隨機數發生器代碼,因爲此刻我只是想弄清楚如何選擇每個單元格中的第一個或第二個數字。一旦我明白了,我可以使用一個if/else語句。

此外,如果我不想打印出數字,但將其選作變量,我該怎麼做?

謝謝你的幫助!

保重,有一個偉大的日子....

的Ciao, 約翰。

+0

如何在表達式中添加另一個方括號索引? – Pointy 2012-01-17 00:18:28

回答

2

我真的不確定你想要做什麼,但...獲取所述第一陣列的第二元件的是作爲arr[0]arr[1]一樣簡單,像這樣:

// First: 
document.write(window['lvl1Roll'+randOne][randTwo20][0]); 

// Second: 
document.write(window['lvl1Roll'+randOne][randTwo20][1]); 

爲了儘可能之前隨機化,只要按照相同的模式,但使用Math.round代替Math.floor並且不通過乘20:

var randOne20 = Math.floor(Math.random()*20); 
var randTwo20 = Math.floor(Math.random()*20); 
var randOne = Math.round(Math.random()); 

document.write(window['lvl1Roll'+randOne20][randTwo20][randOne]); 

但是還有一件事:您不需要將不同的行放在單獨的變量中。你可以只讓一個大陣列:

var lvl1Roll = [ 
    [ 
     [ 1,0 ], [ 1,1 ], [ 1,2 ], [ 1,3 ], [ 1,4 ], [ 1,5 ], [ 1,6 ], [ 1,7 ], [ 1,8 ], [ 1,9 ], [ 1,10 ], [ 1,11 ], [ 1,12 ], [ 1,13 ], [ 1,14 ], [ 1,15 ], [ 1,16 ], [ 1,17 ], [ 1,18 ], [ 1,19 ] 
    ], 
    // [ lvl1Roll2 ], 
    // [ lvl1Roll3 ], etc. 
]; 

並使用,然後用它來從表中選擇一個值,並把它放入變量result

var result = lvl1Roll[randOne20][randTwo20][randOne]; 

最後:我懷疑,我要告訴我們「滾動」背後的邏輯,你根本不需要這個「表」。我可能是錯的,但是......可能值得發佈關於該問題的另一個問題嗎?只是一個想法。

+0

謝謝大家的回覆。 PPvG,arr [0]/arr [1]正是我所需要的。我必須有一個巨大的大腦放屁,因爲我認爲我嘗試過,並沒有得到我期待的結果。我只是試了一下,它運行良好。謝謝! 關於隨機函數,我應該提到我正在生成1到20之間的隨機數。 – Mock26 2012-01-17 01:38:22

+1

(也許你試過'[1]'和'[2]'?)給我打電話,但是.. 。不會'1 + Math.floor(Math.random()* 20)'完全符合你的需求?它會生成一個介於1和20(包括)之間的隨機數。 – PPvG 2012-01-17 01:59:42

0

如果你想兩個數字(假設0和1)之間的隨機選擇,然後:

var i = Math.random() < 0.5? 0 : 1; 

var i = Math.random()*2 | 0; 

應該做的工作。所以:

document.write(window['lvl1Roll'+randOne][randTwo20][i]); 

document.write(window['lvl1Roll'+randOne][randTwo20][Math.random() < 0.5? 0 : 1]); 

document.write(window['lvl1Roll'+randOne][randTwo20][Math.random()*2 | 0); 

多少你想要什麼? :-)

+0

或者只是'Math.round(Math.random())' – PPvG 2012-01-17 00:21:33

0

如果我正確理解你,我認爲最簡單的方法是使用一個包含多個數組的數組。第一個將是table陣列,table陣列中的每個條目將是row,並且row中的每個條目將是cell。這將使得引用表中的任何單元格變得非常簡單。這裏是一個3x3例如:

var table = [ 
    [0,1,2], 
    [3,4,5], 
    [6,7,8], 
]; 

table[0][0]; // First Row, First Column, Equals 0; 
table[1][1]; // Second Row, Second Column, Equals 4; 
table[2][2]; // Third Row, Third Column, Equals 8; 

其實你可以把你的20個陣列到陣列中創建表,如果你想:

var table = []; 
table.push(randOne20); 
table.push(randTwo20); 
etc... 

如果要自動加載隨機陣列數字你可以很容易地做到這一點循環。

var table = []; 
for (var i = 0; i < 20; i++){ // Create 20 rows 
    var row = []; 
    for (var x=0; i<20; i++){ // Create 20 cells; 
     var cell = [Math.round(Math.random), Math.round(Math.random)]; // Adds a two item array of random 0's or 1's 
     row.push(cell); // Adds the cell to the row 
    } 
    table.push(row); // Adds the row to table 
}