title: C, card game WAR
body: <p>I am trying to create a War card game using C. Instead of making 2 decks for the "discarded" cards to be added to a players hand at a later time, I am instead just trying to add the dealers card[i] - which was previously compared with players card[i] to ensure that the player won the hand - to the players hand directly. index is used to make sure there are no empty elements playerhand ever points to by appending another card on to the end of the “hand,” which initially has 26 cards (half the deck). the deck is initially split in 2, the first 26 cards deck[0-25] going to the player's hand, and the last 26 deck[26-51] going to the dealers hand.</p>
  
  <p>here is my code in question:</p>
  
  <pre><code>    //adds new card to deck at given index
  void addToPlayerHand(int i) {
      int index=0;
      playerHand[index+26] = deck[i+26];
      index++;
      }
  </code></pre>
  
  <p>here is my full game code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  //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&lt;26;j++){
              hitPlayer(deck[j]); // adding cards to playerHand
              PlayerCardsRemaining+=1;  //adding to player deck count.
          }
          for (j=26;j&lt;52;j++){
              hitDealer(deck[j]);
              DealerCardsRemaining+=1;}  // adding to dealer deck count.
  
  
  
          i=0;
          while(PlayerCardsRemaining&gt;0 &amp;&amp; DealerCardsRemaining&gt;0 &amp;&amp; i&lt;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 &amp;&amp; DealerCardsRemaining&gt;=4 &amp;&amp; PlayerCardsRemaining&gt;=4){
                      temp=getComparison(playerHand, dealerHand, i+4);
  
                      while(war==1){
  
                          if(temp==0){
                              for(i=i;i&lt;i+4;i++){
                                  addToDealerHand(i);
                              }
                              war=0;
                              PlayerCardsRemaining-=4;
                              DealerCardsRemaining+=4;
                          }
                          else if(temp==1){
                              for(i=i;i&lt;i+4;i++){
                                  addToPlayerHand(i);
                              }
                              war=0;
                              DealerCardsRemaining-=4;
                              PlayerCardsRemaining+=4;
                          }
                          else
                              war=1;
                      }
                  return 1;
                  }
                  if (DealerCardsRemaining&lt;&lt;4){
                      for(i=i;i&lt;i+4;i++){
                          addToPlayerHand(i);
                      }
                  }
                  else if (PlayerCardsRemaining&lt;&lt;4){
                      for(i=i;i&lt;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&lt;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&lt;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&lt;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&lt;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&lt;11 &amp;&amp; playerHand[i].face&lt;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&gt;a)
          return 0;
      else if (a&gt;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 &lt; 5; i++) {
          for (p = 2; p &lt;= 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 &lt; 100; i++) {
          for (p = 0; p &lt; 52; p++) {
              int r = rand()%52;
  
              temp = deck[p];  // swaps to randomly pick another card
              deck[p] = deck[r];
              deck[r] = temp;
          }
      }
  
      return 0;
  }
  </code></pre>
  
  <p>it keeps telling me i have a bad access code, code=2.</p>
  
  <p>any help would be awesome.</p>