2016-12-02 64 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