2014-02-12 71 views
0

我必須爲我的第一個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 

回答

1

這是一個功能是非常有用的 - 避免重複的代碼。這是你的代碼,在功能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}; 


void printFrameType(int a) 
{ 
    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; 
    } 
} 


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]; 

    printFrameType(a); 
    printFrameType(b); 
    printFrameType(c); 
} // end main 

還有其他的方法來改善這個代碼,但你說這是你的第一個C++類,所以我不會有新的信息淹沒你。由於frameReel1,frameReel2frameReel3是相同的,爲什麼不只有一個,並將其稱爲frameReel

而不是使用功能,縮短了代碼,你也可以使用一個循環是這樣的:

for(int i = 0; i < 3; i++) 
{ 
    any code you put in here will run three times, so 
    if you put the random choice and the switch statement 
    in here, it will print 3 random frame types 
} 

您將瞭解什麼是for(int i = 0; i < 3; i++)後來手段,但你可以把它當作魔術現在。如果您希望它運行不同的次數,請將3更改爲其他號碼。

Tony D's和LeonardBlunderbuss的回答比較好,但涉及更高級的概念。

0

您可以編寫可重用的支持功能:

std::ostream& operator<<(std::ostream& os, frameType x) 
{ 
    switch (a) 
    { 
     case PETROLEUM:  return os << "[petroleum]n"; 
     case COAL:    return os << "[coal]"; 
     case NATURAL_GAS:  return os << "[natural gas]"; 
     case URANIUM:   return os << "[uranium]"; 
     case GLOBAL_WARMING: return os << "[global warming]"; 
     case NUCLEAR_DISASTER: return os << "[nuclear disaster]"; 
     case DIRT:    return os << "[dirt]"; 
     default:    return os << "<invalid>"; // or throw? 
    } 
} 

然後:

frameType a = static_cast<frameType>(frameReel1[RandIndex1]); 
... 

std::cout << a << b << c; 

(你真的應該在frameReel1/2/3存儲frameType小號

1

試試這個代碼:

int outcomes[3] = {frameReel1[RandIndex1], frameReel2[RandIndex2], frameReel3[RandIndex3]}; 
for(int i=0; i<3; i++) 
    switch(outcomes[i]) 
    // Cases...