C,纸牌游戏WAR

时间:2013-04-24 01:14:32

标签: c

我正在尝试使用C创建一个战争卡片游戏。而不是为了稍后将“丢弃的”卡片添加到玩家手中而不是2个套牌,我只是试图添加经销商卡片[i ] - 之前与玩家卡[i]进行比较以确保玩家赢得了手 - 直接向玩家手牌。 index用于通过将另一张卡附加到“手”的末尾来确保没有玩家手指的空元素,“手”最初有26张牌(牌组的一半)。甲板最初分为2个,前26个牌组[0-25]进入玩家手中,最后26个牌组[26-51]进入经销商手中。

这是我的问题代码:

    //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 个答案:

答案 0 :(得分:2)

我将免除“我永远不会这样编码”对话的时间足够长,以表明这种循环意识形态:

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

将远远超出您的卡阵列的限制并进入未定义行为的范围。想一想。然后问自己这个问题:i在什么时候不再低于i+4?答案:当你到达i+4溢出变为负数的时候(假设优化器没有完全抛弃条件而只是假设它总是正确的;感谢Jeremy)。

即。 INT_MAX-3。在一个32位系统上,只有大约2147483562个插槽超过数组的末尾。

playGame()函数中重复此错误3次。