2016-12-02 63 views
0

中未處理枚舉值'MAX_RANKS'在sublimetext中出現錯誤,即即使定義了Max_ranks也未處理? sublimetext也不會讓用戶在初始構建之後繼續玩Blackjack。例如,當我構建遊戲時,它顯示「經銷商正在展示6.你有:15.(h)打或站立」。但是,當我按H或S沒有任何反應..警告:在交換機

#include <iostream> 
#include <array> 
#include <ctime> // for time() 
#include <cstdlib> // for rand() and srand() 

enum CardSuit 
{ 
    SUIT_CLUB, 
    SUIT_DIAMOND, 
    SUIT_HEART, 
    SUIT_SPADE, 
    MAX_SUITS 
}; 

enum CardRank 
{ 
    RANK_2, 
    RANK_3, 
    RANK_4, 
    RANK_5, 
    RANK_6, 
    RANK_7, 
    RANK_8, 
    RANK_9, 
    RANK_10, 
    RANK_JACK, 
    RANK_QUEEN, 
    RANK_KING, 
    RANK_ACE, 
    MAX_RANKS 
}; 

struct Card 
{ 
    CardRank rank; 
    CardSuit suit; 
}; 

void printCard(const Card &card) 
{ 
    switch (card.rank) 
    { 
     case RANK_2:  std::cout << '2'; break; 
     case RANK_3:  std::cout << '3'; break; 
     case RANK_4:  std::cout << '4'; break; 
     case RANK_5:  std::cout << '5'; break; 
     case RANK_6:  std::cout << '6'; break; 
     case RANK_7:  std::cout << '7'; break; 
     case RANK_8:  std::cout << '8'; break; 
     case RANK_9:  std::cout << '9'; break; 
     case RANK_10:  std::cout << 'T'; break; 
     case RANK_JACK:  std::cout << 'J'; break; 
     case RANK_QUEEN: std::cout << 'Q'; break; 
     case RANK_KING:  std::cout << 'K'; break; 
     case RANK_ACE:  std::cout << 'A'; break; 
    } 

    switch (card.suit) 
    { 
     case SUIT_CLUB:  std::cout << 'C'; break; 
     case SUIT_DIAMOND: std::cout << 'D'; break; 
     case SUIT_HEART: std::cout << 'H'; break; 
     case SUIT_SPADE: std::cout << 'S'; break; 
    } 
} 

void printDeck(const std::array<Card, 52> &deck) 
{ 
    for (const auto &card : deck) 
    { 
     printCard(card); 
     std::cout << ' '; 
    } 

    std::cout << '\n'; 
} 

void swapCard(Card &a, Card &b) 
{ 
    Card temp = a; 
    a = b; 
    b = temp; 
} 

// Generate a random number between min and max (inclusive) 
// Assumes srand() has already been called 
int getRandomNumber(int min, int max) 
{ 
    static const double fraction = 1.0/(static_cast<double>(RAND_MAX) + 1.0); // static used for efficiency, so we only calculate this value once 
    // evenly distribute the random number across our range 
    return static_cast<int>(rand() * fraction * (max - min + 1) + min); 
} 

void shuffleDeck(std::array<Card, 52> &deck) 
{ 
    // Step through each card in the deck 
    for (int index = 0; index < 52; ++index) 
    { 
     // Pick a random card, any card 
     int swapIndex = getRandomNumber(0, 51); 
     // Swap it with the current card 
     swapCard(deck[index], deck[swapIndex]); 
    } 
} 

int getCardValue(const Card &card) 
{ 
    switch (card.rank) 
    { 
    case RANK_2:  return 2; 
    case RANK_3:  return 3; 
    case RANK_4:  return 4; 
    case RANK_5:  return 5; 
    case RANK_6:  return 6; 
    case RANK_7:  return 7; 
    case RANK_8:  return 8; 
    case RANK_9:  return 9; 
    case RANK_10:  return 10; 
    case RANK_JACK:  return 10; 
    case RANK_QUEEN: return 10; 
    case RANK_KING:  return 10; 
    case RANK_ACE:  return 11; 
    } 

    return 0; 
} 

char getPlayerChoice() 
{ 
    std::cout << "(h) to hit, or (s) to stand: "; 
    char choice; 
    do 
    { 
     std::cin >> choice; 
    } while (choice != 'h' && choice != 's'); 

    return choice; 
} 

bool playBlackjack(const std::array<Card, 52> &deck) 
{ 
    // Set up the initial game state 
    const Card *cardPtr = &deck[0]; 

    int playerTotal = 0; 
    int dealerTotal = 0; 

    // Deal the dealer one card 
    dealerTotal += getCardValue(*cardPtr++); 
    std::cout << "The dealer is showing: " << dealerTotal << '\n'; 

    // Deal the player two cards 
    playerTotal += getCardValue(*cardPtr++); 
    playerTotal += getCardValue(*cardPtr++); 

    // Player goes first 
    while (1) 
    { 
     std::cout << "You have: " << playerTotal << '\n'; 

     // See if the player has busted 
     if (playerTotal > 21) 
      return false; 

     char choice = getPlayerChoice(); 
     if (choice == 's') 
      break; 

     playerTotal += getCardValue(*cardPtr++); 
    } 

    // If player hasn't busted, dealer goes until he has at least 17 points 
    while (dealerTotal < 17) 
    { 
     dealerTotal += getCardValue(*cardPtr++); 
     std::cout << "The dealer now has: " << dealerTotal << '\n'; 
    } 

    // If dealer busted, player wins 
    if (dealerTotal > 21) 
     return true; 

    return (playerTotal > dealerTotal); 
} 

int main() 
{ 
    srand(static_cast<unsigned int>(time(0))); // set initial seed value to system clock 
    rand(); // If using Visual Studio, discard first random value 

    std::array<Card, 52> deck; 

    // We could initialize each card individually, but that would be a pain. Let's use a loop. 
    int card = 0; 
    for (int suit = 0; suit < MAX_SUITS; ++suit) 
    for (int rank = 0; rank < MAX_RANKS; ++rank) 
    { 
     deck[card].suit = static_cast<CardSuit>(suit); 
     deck[card].rank = static_cast<CardRank>(rank); 
     ++card; 
    } 

    shuffleDeck(deck); 

    if (playBlackjack(deck)) 
     std::cout << "You win!\n"; 
    else 
     std::cout << "You lose!\n"; 

    return 0; 
} 
+1

編譯器不知道你永遠不會向交換機傳遞'MAX_RANKS',如果你想處理它,你應該添加一個'default'的情況。 –

+0

回答你的問題的第二部分:如果你需要它來輸入,不要從崇高中運行; sublime不會從輸出窗口收集輸入並將其傳遞迴正在運行的程序。 – OdatNurd

+0

所以我需要在Code :: blocks或Xcode或一些IDE中運行它?而不是因爲這些C++ IDE太笨重而無法使用..sublime非常乾淨容易 – Bryn

回答

0

由於是非常正常的,你應該添加一個case default:switch聲明。即使你將它留空,它也會使編譯器感到高興。

我建議你和你的代碼的下一個讀者友好(也許你!)並留下評論爲什麼這是一個空的情況。

從命令行運行它進行測試。 Sublime是一個文本編輯器,不是IDE。

+0

你如何在命令行中運行.cpp?我進入保存文件的目錄,但是當我運行cl CTextbook.cpp命令行狀態cl沒有被定義? – Bryn

+0

我不知道你正在使用哪個編譯器。 – erip

+0

我想在終端命令行中運行我的.cpp文件... – Bryn