2013-04-24 132 views
0

我想用C創建一個戰爭紙牌遊戲,而不是讓2張牌在稍後時間被添加到玩家手中的「丟棄」牌中,而是試圖添加經銷商卡[i] - 之前與玩家卡[i]比較,以確保玩家贏得手牌 - 直接向玩家提供。索引用於確保玩家手中沒有空的元素,通過將另一張牌附加到「手牌」的末尾,最初有26張牌(一半是牌組)。首先將甲板拆分爲2個,前26個牌組[0-25]前往玩家手中,最後26個牌組[26-51]前往經銷商手中。C,紙牌遊戲WAR

這裏是我的代碼有問題:

//adds new card to deck at given index 
void addToPlayerHand(int i) { 
    int index=0; 
    playerHand[index+26] = deck[i+26]; 
    index++; 
    } 

這裏是我完整的遊戲代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

//Type Stucture for Card 
typedef struct cards { 
    char suit; 
    int face; 
    struct cards *listp; // didn't use in this program 
} card; 

//Functions 
// 
int welcome(); 
//int checkWinWar(); 
int playGame(); 

int getComparison(card *firstCard,card *secndCard,int index); 


void printingGame(int a,int b, int i); 
void addToPlayerHand(int i); 
void addToDealerHand(int i); 
int addToDeck(card myCards, int index); 
void hitPlayer(card theCard); 
void hitDealer(card theCard); 
void initilizeDeck(); 
int shuffleDeck(); 
int printFun(); // not used for game play 

void freePlayers(); 


//Card Dynamics 
// 
card *playerHand; // contains contents of players cards 
card *dealerHand; // contains contents of dealers cards 

//Deck of 52 Cards with discard decks 
card deck[52]; 
card playerDeck[52]; 
card dealerDeck[52]; 

int numPlayer = 0; 
int numAllPlayer = 0; 

int numDealer = 0; // needed for temp 
int numAllDealer = 0; 




//Main 
int main(void) { 


    printf("Hello..Welcome to WAR\n"); 



    //Game loop 
    while (welcome() == 1) { // welcome returns whether the player wants to play (1) or not (0) 

     // put first card face up 
     // display how many cards 
     // indicate (if/if no) war has occurred 
     // complete the war 
     // add cards to player discard pile 
     // check player for card count 
     // if end game conition met, end hame 


     if(playGame() == 1) { // playGame returns 1 if the function went through correctly 

      if(checkWinWar()==1){ 

      } 
      else{ 

      } 

     } else { 
      printf("Bust! Try Again\n\n"); 
      freePlayers(); // refreshes the dynamics of the hands 
     } 

    } 



    //initilizeDeck(); 
    //shuffleDeck(); 
    //printFun(); 

    return 0; 
} 

//To allow the player to begin the game; 1 = True 0 = False 
int welcome() { 

    printf("Play a new round of WAR? (enter/return for yes):\t"); 

    if(getchar()=='\n'){ 
     return 1; 
    } 
    else 
     return 0; 


} 

// refreshes the game and players 
void freePlayers() { 

    numDealer = 0; 
    numPlayer = 0; 

    numAllPlayer = 0; 
    numAllDealer = 0; 

    free(playerHand); 
    free(dealerHand); 

    playerHand = NULL; // needed so playerHand doesn't point to anything 
    dealerHand = NULL; 

    return; 
} 


// Playing of the game 
int playGame() { 

    initilizeDeck(); // initialize Deck returns the new deck of cards 
    shuffleDeck(); // shuffleDeck returns the shuffled deck of cards 

    int gameTrue = 1; // gameTrue checks the state of the game 

    int war = 0; 
    int PlayerCardsRemaining=0; 
    int DealerCardsRemaining=0; 

    int temp,i,j=0; 

    while (gameTrue == 1) { 
     // splitting the deck 
     for (j=0;j<26;j++){ 
      hitPlayer(deck[j]); // adding cards to playerHand 
      PlayerCardsRemaining+=1; //adding to player deck count. 
     } 
     for (j=26;j<52;j++){ 
      hitDealer(deck[j]); 
      DealerCardsRemaining+=1;} // adding to dealer deck count. 



     i=0; 
     while(PlayerCardsRemaining>0 && DealerCardsRemaining>0 && i<26) { 

      printingGame(DealerCardsRemaining,PlayerCardsRemaining, i); 

      if (getComparison(playerHand, dealerHand, i)==0){ 
       addToDealerHand(i); 
       DealerCardsRemaining++; 
       return 1; 
      } 
      else if(getComparison(playerHand,dealerHand,i)==1){ 
       addToPlayerHand(i); 
       PlayerCardsRemaining++; 
       return 1; 
      } 
      else if(getComparison(playerHand, dealerHand, i)==2){ 
       printf("There is an impending WAR\n"); 
       printf("press enter to engage in the WAR"); 
       war=1; 



       if(war==1 && DealerCardsRemaining>=4 && PlayerCardsRemaining>=4){ 
        temp=getComparison(playerHand, dealerHand, i+4); 

        while(war==1){ 

         if(temp==0){ 
          for(i=i;i<i+4;i++){ 
           addToDealerHand(i); 
          } 
          war=0; 
          PlayerCardsRemaining-=4; 
          DealerCardsRemaining+=4; 
         } 
         else if(temp==1){ 
          for(i=i;i<i+4;i++){ 
           addToPlayerHand(i); 
          } 
          war=0; 
          DealerCardsRemaining-=4; 
          PlayerCardsRemaining+=4; 
         } 
         else 
          war=1; 
        } 
       return 1; 
       } 
       if (DealerCardsRemaining<<4){ 
        for(i=i;i<i+4;i++){ 
         addToPlayerHand(i); 
        } 
       } 
       else if (PlayerCardsRemaining<<4){ 
        for(i=i;i<i+4;i++){ 
         addToDealerHand(i); 
        } 
       } 
      } 
      i++; // increment i 

     } 


    } 
    return 0; 
} 

void printingGame(int DealerCardsRemaining, int PlayerCardsRemaining, int i) 
{ 
    // Gameplay Prompts 

    //Prints interface that comes up after player(s) choose to play 
    printf("*********\t\t*********\n"); 
    printf("*\t \t*\t\t*\t \t*\n"); 

    // Cases for cards that are face cards 

    if(dealerHand[i].face==11){ // if dealer card is a jack 
     printf("*\tJ\t*\t\t"); 
     if (playerHand[i].face<11){ // if player card isnt a face card 
      printf("*\t%d\t*\n",playerHand[i].face); 
     } 
     else if(playerHand[i].face==11){ // if player card is a face card 
      printf("*\tJ\t*\n"); 
     } 
     else if(playerHand[i].face==12){ 
      printf("*\tQ\t*\n"); 
     } 
     else if(playerHand[i].face==13){ 
      printf("*\tK\t*\n"); 
     } 
     else if(playerHand[i].face==14){ 
      printf("*\tA\t*\n"); 
     } 
    } 
    else if(dealerHand[i].face==12){ // if dealer card is a queen 
     printf("*\tQ\t*\t\t"); 
     if (playerHand[i].face<11){ // if player card isnt a face card 
      printf("*\t%d\t*\n",playerHand[0].face); 
     } 
     else if(playerHand[i].face==11){ // if player card is a face card 
      printf("*\tJ\t*\n"); 
     } 
     else if(playerHand[i].face==12){ 
      printf("*\tQ\t*\n"); 
     } 
     else if(playerHand[i].face==13){ 
      printf("*\tK\t*\n"); 
     } 
     else if(playerHand[i].face==14){ 
      printf("*\tA\t*\n"); 
     } 
    } 
    else if(dealerHand[i].face==13){ // if dealer card is a king 
     printf("*\tK\t*\t\t"); 
     if (playerHand[i].face<11){ // if player card isnt a face card 
      printf("*\t%d\t*\n",playerHand[i].face); 
     } 
     else if(playerHand[i].face==11){ // if player card is a face card 
      printf("*\tJ\t*\n"); 
     } 
     else if(playerHand[i].face==12){ 
      printf("*\tQ\t*\n"); 
     } 
     else if(playerHand[i].face==13){ 
      printf("*\tK\t*\n"); 
     } 
     else if(playerHand[i].face==14){ 
      printf("*\tA\t*\n"); 
     } 
    } 
    else if(dealerHand[i].face==14){ // if dealer card is an ace 
     printf("*\tA\t*\t\t"); 
     if (playerHand[i].face<11){ // if player card isnt a face card 
      printf("*\t%d\t*\n",playerHand[i].face); 
     } 
     else if(playerHand[i].face==11){ // if player card is a face card 
      printf("*\tJ\t*\n"); 
     } 
     else if(playerHand[i].face==12){ 
      printf("*\tQ\t*\n"); 
     } 
     else if(playerHand[i].face==13){ 
      printf("*\tK\t*\n"); 
     } 
     else if(playerHand[i].face==14){ 
      printf("*\tA\t*\n"); 
     } 
    } 
    if (dealerHand[i].face<11 && playerHand[i].face<11){ // if both cards arent face cards 
     printf("*\t%d\t*\t\t*\t%d\t*\n",dealerHand[i].face,playerHand[i].face); 
    } 
    printf("*\t \t*\t\t*\t \t*\n"); 
    printf("*********\t\t*********\n"); 
    printf("Dealer: %d cards. ",DealerCardsRemaining); // print how many cards the 'dealer' has 
    printf("Player: %d cards. ",PlayerCardsRemaining); // print how many cards the player has 

} 


int getComparison (card *firstCard, card *secndCard, int i) 
{ 
    int a,b; 
    a=firstCard[i].face; 
    b=secndCard[i].face; 
    if (b>a) 
     return 0; 
    else if (a>b) 
     return 1; 
    else 
     return 2; 
} 

// gives player another card 
void hitPlayer (card theCard) { 
    if(numPlayer == 0) { // if hitPlayer is giving playerHand its first card (for new deck or initial deck) 

     if (numAllPlayer == 0)  // if its the first hand 
      numAllPlayer = 1; // there is only one card in need of space 
     else      // if its not the first hand 
      numAllPlayer++; // you need space for all existing cards plus the one just added 

     void *_tmp = realloc(playerHand, (numAllPlayer * sizeof(card))); // realloc is used since playerHand never has a constant size 

     if (!_tmp) 
     { 
      fprintf(stderr, "ERROR: Couldn't realloc memory!\n"); // stderr prints 
     } 
     playerHand = (card*)_tmp; // playerHand is card type pointer which points to void pointer tmp which is reallocating memory 
    } 
    playerHand[numPlayer] = theCard; 
    numPlayer++; 
} 

// gives dealer another card 
void hitDealer (card theCard) { 
    if(numDealer == 0) { 

     if (numAllDealer == 0) 
      numAllDealer = 1; 
     else 
      numAllDealer++; 

     void *_tmp = realloc(dealerHand, (numAllDealer * sizeof(card))); 

     if (!_tmp) 
     { 
      fprintf(stderr, "ERROR: Couldn't realloc memory!\n"); 
     } 
     dealerHand = (card*)_tmp; 
    } 
    dealerHand[numDealer] = theCard; 
    numDealer++; 
} 

// creates a new deck of cards 
void initilizeDeck() { 

    //Loop Indexes like: count, i.... etc 
    int i, p; 

    int count = 0; 
    card temp; 

    for (i = 1; i < 5; i++) { 
     for (p = 2; p <= 14; p++) { 
      //Clubs 
      if (i == 1) { 
       temp.suit = 'C'; 
       temp.face = p; 
      } 
      //Diamonds 
      if (i == 2) { 
       temp.suit = 'D'; 
       temp.face = p; 
      } 
      //Hearts 
      if (i == 3) { 
       temp.suit = 'H'; 
       temp.face = p; 
      } 
      //Spades 
      if (i == 4) { 
       temp.suit = 'S'; 
       temp.face = p; 
      } 
      addToDeck(temp, count); 
      count++; 
     } 
    } 
} 

//adds new card to deck at given index 
int addToDeck(card myCards, int index) { 
    deck[index] = myCards; 
    return 1; 
} 
//adds new card to deck at given index 
void addToPlayerHand(int i) { 
    int index=0; 
    playerHand[index+26] = deck[i+26]; 
    index++; 
    } 
//adds new card to deck at given index 
void addToDealerHand(int i) { 
    int index=0; 
    dealerHand[index+26] = deck[i]; 
    index++; 
} 


//finds deck and shuffles cards 100 times from swaping a random value from 1-52 
int shuffleDeck() { 

    card temp; // for an individual card 

    int i,p; 

    srand(time(NULL)); 

    for (i = 0; i < 100; i++) { 
     for (p = 0; p < 52; p++) { 
      int r = rand()%52; 

      temp = deck[p]; // swaps to randomly pick another card 
      deck[p] = deck[r]; 
      deck[r] = temp; 
     } 
    } 

    return 0; 
} 

它總是告訴我,我有一個壞的接入代碼,代碼= 2。

任何幫助將是可怕的。

+1

我相當確定這不是*所有*您的代碼。發佈'playerHand []'和'deck []''的聲明將說明您的問題。看到傳遞給這個函數時如何控制'i'也同樣有幫助。 – WhozCraig 2013-04-24 01:18:54

+0

虐待只是發佈我的整個代碼 – WheezyTrees 2013-04-24 01:22:19

+2

優秀。如果你能把問題壓縮到不到一百行,並*更新問題*(不要試圖在評論中發佈),那將是非常棒的。 – WhozCraig 2013-04-24 01:24:13

回答

1

我會免去了「我從來沒有這樣的代碼這樣」對話足夠長的時間表明,這種循環的意識形態:

for(i=i;i<i+4;i++) 

會很好看過去你的卡陣列,進入的限制未定義行爲。想一想。然後問自己這個問題:在什麼時候i不會再小於i+4?答案是:當你達到i+4溢出變爲負數的時候(假設優化器沒有完全拋出這個條件,並假設它總是如此,謝謝Jeremy)。

I.e. INT_MAX-3。在32位系統上,只有大約2147483562的插槽過去了陣列的末尾。

該錯誤在playGame()函數中重複了3次以上。

+0

上方的「有問題的代碼」塊中。您提到未定義的行爲。爲了擴大這一點:編譯器得出結論認爲「i 2013-04-24 03:57:53

+0

@JeremyRoman總之,哎。 – WhozCraig 2013-04-24 04:53:42