0

我正在制作一个程序来洗牌一套牌,并发出两只手,但现在,没有输出!我梳理了无限循环的代码,但我找不到任何东西!有人可以帮帮我吗?程序中没有输出?

// (Description in Word File) 
#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <string> 
using namespace std; 

unsigned seed = time(0); // for use of rand() 

const int SUIT = 4; 
const int FACE = 13; 
const int HAND = 5; 

// A couple of reference arrays. 
const string SUITS[SUIT] = {" Hearts", " Diamonds", " Clubs", " Spades"}; 
const string FACES[FACE] = {"Ace of", "Two of", "Three of", 
          "Four of", "Five of", "Six of", "Seven of", 
          "Eight of", "Nine of", "Ten of", 
          "Jack of", "Queen of", "King of"}; 

// The reason that these functions are in Main is because 
// it gives me error 2065: undeclared identifier otherwise. 
void shuffle(string[][FACE]); 
void deal(string[][FACE], string[HAND]); 
void displayDeck(string[][FACE]); 
void displayHand(string[HAND]); 

int main() 
{ 
    string deck[SUIT][FACE]; 
    string hand1[HAND]; 
    string hand2[HAND]; 

    srand(seed); // for use of rand() 

    // Shuffle the deck. 
    shuffle(deck); 

     // Now display the deck 
    displayDeck(deck); 

    // Deal for each hand. 
    deal(deck, hand1); 
    deal(deck, hand2); 

    // Now display each hand. 
    displayHand(hand1); 
    displayHand(hand2); 

    return 0; 
} 

// This function will keep track of face values 
// for the shuffle function 
bool duplCheck(string cards[][FACE], int suit, int face) 
{ 
    bool status = true; 
    for (int count1 = 0; count1 != SUIT; count1++) 
     for (int count2 = 0; count2 != FACE; count2++) 
      if (cards[count1][count2] == cards[suit][face] 
       && suit != count1 && face != count2) 
       return false; 
    return status; 
} 

// This function will shuffle the deck. 
void shuffle(string cards[][FACE]) 
{ 
    int randFace, randSuit; 
    // These loops will assign random face values 
    // and suits to each place in cards[][]. 
    for (int count1 = 0; count1 != SUIT; count1++) 
     for (int count2 = 0; count2 != FACE; count2++) 
     { 
      do 
      { 
       randFace = rand() % FACE; 
       randSuit = rand() % SUIT; 
       if (duplCheck(cards, randSuit, randFace) == true) 
        cards[count1][count2] = 
         FACES[randFace] + SUITS[randSuit]; 
      } while(duplCheck(cards, randSuit, randFace) == false); 
     } 
} 

// This function deals out a hand of five random cards. 
void deal(string cards[][FACE], string hand[HAND]) 
{ 
    for (int count = 0; count != HAND; count++) 
    { 
     // make random suit and face numbers 
     int randSuit = rand() % SUIT; 
     int randFace = rand() % FACE; 

     // If random card is not obsolete... 
     if (cards[randSuit][randFace] != "null") 
      // ...assign that card to hand. 
      hand[count] = cards[randSuit][randFace]; 

     // obsolete that card 
     cards[randSuit][randFace] = "null"; 
    } 
} 

void displayDeck(string cards[][FACE]) 
{ 
    std::cout << "\t\tThe Deck:\n\n"; 
    for (int count1 = 0; count1 != SUIT; count1++) 
     for (int count2 = 0; count2 != FACE; count2++) 
     { 
      std::cout << ((count1 * FACE) + count2 + 1) << " " 
       << cards[count1][count2] << endl; 
     } 
} 

void displayHand(string hand[HAND]) 
{ 

} 
+2

'displayHand' does not nothing。 你预计会发生什么? –

+0

“duplCheck”是否在shuffle的主循环中调用了两次? 30秒添加一些额外的输出显示这是您的代码中断的地方。 http://ideone.com/BkZKD9 – kfsone

+0

据我可以告诉你,从来没有摆脱'shuffle' – Duck

回答

2

你的问题是在洗牌,特别是在使用duplCheck。

void shuffle(string cards[][FACE]) 
{ 
    int randFace, randSuit; 
    // These loops will assign random face values 
    // and suits to each place in cards[][]. 
    for (int count1 = 0; count1 != SUIT; count1++) 
     for (int count2 = 0; count2 != FACE; count2++) 
     { 
      do 
      { 
       randFace = rand() % FACE; 
       randSuit = rand() % SUIT; 
       std::cout << count1 << "," << count2 << " trying " << randFace << "/" << randSuit << std::endl; 
       if (duplCheck(cards, randSuit, randFace) == true) { 
        std::cout << "duplCheck returned true" << std::endl; 
        cards[count1][count2] = 
         FACES[randFace] + SUITS[randSuit]; 
       } 
      } while(duplCheck(cards, randSuit, randFace) == false); 
     } 
} 

我添加了一些额外的输出。这演示了(http://ideone.com/BkZKD9)第一次运行duplCheck时不会返回false。

do 
{ 
    randFace = rand() % FACE; 
    randSuit = rand() % SUIT; 
    if (duplCheck(cards, randSuit, randFace) == true) { 
       ... this doesn't happen 
    } 
} while(duplCheck(cards, randSuit, randFace) == false); 

由于duplCheck返回false,您将永久保留在此循环中。

bool duplCheck(string cards[][FACE], int suit, int face) 
{ 
    bool status = true; 
    for (int count1 = 0; count1 != SUIT; count1++) 
     for (int count2 = 0; count2 != FACE; count2++) 
      if (cards[count1][count2] == cards[suit][face] 
       && suit != count1 && face != count2) 
       return false; 
    return status; 
} 

看来,duplCheck如果没有返回“假”,如果有重复和“真”,但你对它的使用预期相反:while循环停止时duplCheck返回true,预计如果有重复,duplCheck返回true。

+0

无论我改变了什么,它都不起作用。我想我会采取不同的方法。 – SoloMael

+0

是的,我想通了另一种方式。男孩,这个项目很痛苦! – SoloMael

0

您的显示手部功能为空。

void displayHand(string hand[HAND]) 
{ 
    cout << "Put some output here"; 
} 
+0

有效但不是答案。 – kfsone

+0

我想添加它作为评论,但我没有足够的声誉... @kfsone – charlieparker

0

问题是你的牌组最初是以空(“”)值开始的。您可以在开始调用shuffle()之前填写此项,也可以将此检查添加到duplCheck()的开头:

if(cards[suit][face].length() == 0) 
    return true;