2012-03-16 78 views
0

所以我的教授給出了一個任務,我們必須創建一個運行的老虎機程序。該機有3個卷軸,其本身包括存儲在一個枚舉6個六個符號:非常基本的東西:在模擬老虎機的程序時遇到問題 - 枚舉和數組

public enum Symbol 
{ 
    SEVEN(12,"images/seven.jpg"), 
    WATERMELON(10,"images/watermelon.jpg"), 
    ORANGE(8,"images/orange.jpg"), 
    PLUM(6,"images/plum.jpg"), 
    LEMON(4,"images/lemon.jpg"), 
    CHERRY(2,"images/cherry.jpg"); 
} 

我們應該使用這些符號來創建一個「卷軸」的對象。我的問題是圍繞着這個卷軸(這實際上只是一個符號[]數組),並按照教授請求的順序填充這些符號。

他要求我們使用Random類來填充使用10作爲種子編號的卷軸。這是我到目前爲止有:

//Creating an unrandomized reel array, which contains the symbols in the exact order as they appear in the enum 
private Symbol[] unrandomizedReel = new Symbol[]{Symbol.SEVEN, Symbol.WATERMELON, Symbol.ORANGE, Symbol.PLUM, Symbol.LEMON, Symbol.CHERRY}; 

//Declares local Symbol array 
Symbol[] randomizedReel = new Symbol[6]; 

//Keeps track of the position in the unradomized reel array 
int unrandomizedReelIndex = 0; 

//Creates a Random object which will be used to generate values based 
//on the seed. **seed == 10** 
Random randNum = new Random(seed); 

//Loop will populate the randomized reel 
while(unrandomizedReelIndex < 6) 
    { 
    //This int will hold values ranging from 0-5. Will be used as an index 
    //to populate randomized reel 
    int rangedRandomNumIndex = randNum.nextInt(6); 

    //if the element at the given index in the randomized reel is empty 
    if(randomizedReel[rangedRandomNumIndex] == null) 
     { 
     //A symbol from the unrandomized reel is added to it 
     randomizedReel[rangedRandomNumIndex] = unrandomizedReel[unrandomizedReelIndex]; 

     //The index used to iterate through the unrandomized reel is incremented by 1 
     unrandomizedReelIndex++; 
     } 
    } 

運行這個「洗牌」的代碼,我得到以下的輸出:

WATERMELON 
PLUM 
CHERRY 
SEVEN 
ORANGE 
LEMON 

但是,根據我的教授,輸出應該是:

ORANGE 
PLUM 
SEVEN 
LEMON 
WATERMELON 
CHERRY 

我在做什麼錯了?爲什麼我的輸出與他不一樣,儘管我們都使用10作爲種子?任何幫助,將不勝感激。謝謝。

+0

您是否需要使用特定的算法來進行洗牌?我沒有看到你的解決方案有什麼問題,可能與你的教授稍有不同(例如,隨機數可能代表舊數組中的索引而不是新數組,或者你可以使用隨機數來交換數值次數) – kylewm 2012-03-16 06:14:27

+0

「可能與你的教授稍微有點不同......」那就是訣竅。謝謝,凱爾! – Haque1 2012-03-16 07:15:34

回答

1
new Symbol[]{Symbol.SEVEN, Symbol.WATERMELON, Symbol.ORANGE, Symbol.PLUM, Symbol.LEMON, Symbol.CHERRY}; 

可以簡化爲

Symbol.values() 

我的猜測是,你的教授是把

unrandomizedReel[Random.nextInt(6)] 

位置0,然後歸零說出來的unrandomizedReel該元素,得到一個隨機數字x在1和5之間,並取第x個非空值並將其置於位置1,將其置零,然後用1和4之間的數字重複,等等。

如果您有理由相信您的教授正在使用不同的算法,請使用該信息編輯您的問題。

+0

謝謝你的迴應,Mike。不幸的是,我的教授告訴我們,我們不能使用數組以外的任何其他東西。 – Haque1 2012-03-16 06:07:52