1
我在C++中創建Yahtzee。骰子滾動一個數字,並根據滾動的數字,通過從randNum中減去1來創建位置(因爲數組基於零)。然後使用該位置,我想增加我的數組中的適當元素,以表示我已經滾動了該特定數字。例如:骰子1,1,1,5,6,4。我的數組應顯示:3,0,0,1,1,1。使用我當前的代碼,元素不會增加。任何幫助表示讚賞。C++設置私有數組的值
頭:
class dice {
public:
dice();
void rollDice();
int getRoll(int diceNumber);
//This is the array that tells us how many of one number we have
//ex) element 4 belongs to the #5 so if there is a 3 stored in element 4 that means we rolled 3 5's
int arrayOfEachNumberRolled[5]{0,0,0,0,0};
~dice();
}
源:
void dice::rollDice(){
//roll a random number and store it in the appropriate location
int randNum;
for(int i = 0; i < 6; i++){
//Get the new random number between 1 & 6
randNum = rand() % 6 + 1;
//set the appropriate dice
if(i == 0){//dice1
dice1->set(randNum);
arrayOfEachNumberRolled[randNum - 1]+=1;
}
else if(i == 1){//dice 2
dice2->set(randNum);
arrayOfEachNumberRolled[randNum - 1]++;
}
else if(i == 2){//dice 3
dice3->set(randNum);
arrayOfEachNumberRolled[randNum - 1]++;
}
else if(i == 3){//dice 4
dice4->set(randNum);
arrayOfEachNumberRolled[randNum - 1]++;
}
else if(i == 4){//dice 5
dice5->set(randNum);
arrayOfEachNumberRolled[randNum - 1]++;
}
else if (i == 5){//dice 6
dice6->set(randNum);
arrayOfEachNumberRolled[randNum - 1]++;
}
}
}
Facepalm表情符號。不能相信我錯過了這一點。工作完美。 TY – MattCucco
@MattCucco,別難過。我們大多數人都在那裏:) –