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'는 아무 작업도 수행하지 않습니다. 어떻게 될 것으로 예상됩니까? –

+0

"duplCheck"는 셔플의 메인 루프에서 두 번 부르는 것을 무엇이라고합니까? 30 초 동안 추가 출력을 추가하면 코드가 깨지는 부분이 표시됩니다. http://ideone.com/BkZKD9 – kfsone

+0

내가 말할 수있는 한 '셔플'을 결코 벗어날 수 없다. – 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 중복이있는 경우 "거짓"및 "true"를 반환하지만 그것의 사용이 반대를 예상 나타납니다 : 당신의 while 루프 duplCheck가 true를 돌려주는 경우, 그것은 기대 정지 중복이있는 경우 true를 반환하는 duplCheck입니다.

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