所以我的教授給出了一個任務,我們必須創建一個運行的老虎機程序。該機有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作爲種子?任何幫助,將不勝感激。謝謝。
您是否需要使用特定的算法來進行洗牌?我沒有看到你的解決方案有什麼問題,可能與你的教授稍有不同(例如,隨機數可能代表舊數組中的索引而不是新數組,或者你可以使用隨機數來交換數值次數) – kylewm 2012-03-16 06:14:27
「可能與你的教授稍微有點不同......」那就是訣竅。謝謝,凱爾! – Haque1 2012-03-16 07:15:34