我必須爲我的第一個C++類創建一臺老虎機,並且我必須爲卷軸使用枚舉數據類型和數組。我的程序目前選擇了所有三個卷軸隨機幀,但對於我的生活,我想不出一種更緊湊的方法。我必須保留所有的卷軸值,以便我可以用if語句創建輸贏結果。有沒有辦法通過同一個開關循環每個變量?通過一個switch語句發送不同的變量
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
enum frameType {PETROLEUM=1, COAL, NATURAL_GAS, URANIUM, GLOBAL_WARMING,NUCLEAR_DISASTER, DIRT};
enum outcomeType {PETROLEUM_WIN, COAL_WIN, NATURAL_GAS_WIN, URANIUM_WIN, LOSS, DRAW};
int main()
{
srand(time(NULL));
frameType frm1;
int frameReel1[] = {1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,5,5,6,7,7,7,7,7};
int frameReel2[] = {1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,5,5,6,7,7,7,7,7};
int frameReel3[] = {1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,5,5,6,7,7,7,7,7};
int RandIndex1 = rand() % 25;
int RandIndex2 = rand() % 25;
int RandIndex3 = rand() % 25;
int a = frameReel1[RandIndex1];
int b = frameReel2[RandIndex2];
int c = frameReel3[RandIndex3];
switch (a)
{
case PETROLEUM:
cout << "[petroleum]n";
break;
case COAL:
cout << "[coal]";
break;
case NATURAL_GAS:
cout << "[natural gas]";
break;
case URANIUM:
cout << "[uranium]";
break;
case GLOBAL_WARMING:
cout << "[global warming]";
break;
case NUCLEAR_DISASTER:
cout << "[nuclear disaster]";
break;
case DIRT:
cout << "[dirt]";
break;
}
switch (b)
{
case PETROLEUM:
cout << "[petroleum]";
break;
case COAL:
cout << "[coal]";
break;
case NATURAL_GAS:
cout << "[natural gas]";
break;
case URANIUM:
cout << "[uranium]";
break;
case GLOBAL_WARMING:
cout << "[global warming]";
break;
case NUCLEAR_DISASTER:
cout << "[nuclear disaster]";
break;
case DIRT:
cout << "[dirt]";
break;
}
switch (c)
{
case PETROLEUM:
cout << "[petroleum]";
break;
case COAL:
cout << "[coal]\n";
break;
case NATURAL_GAS:
cout << "[natural gas]";
break;
case URANIUM:
cout << "[uranium]";
break;
case GLOBAL_WARMING:
cout << "[global warming]";
break;
case NUCLEAR_DISASTER:
cout << "[nuclear disaster]";
break;
case DIRT:
cout << "[dirt]";
break;
}
} // end main