Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "Game.h"
  5.  
  6. #define SUITS 5
  7. #define COLORS 5
  8. #define VALUES 16
  9. #define NO_OF_PLAYERS 4
  10. #define INITIAL_HAND_SIZE 7
  11.  
  12. typedef struct _cardNode *CardNode;
  13.  
  14. typedef struct _cardNode {
  15.     Card card;
  16.     CardNode next;
  17. } cardNode;
  18.  
  19. typedef struct _game {
  20.     int initialDeckSize;
  21.     int remainingDeckSize;
  22.     int initialSuitCount[SUITS];            // [HEARTS, DIAMONDS, CLUBS, SPADES, QUESTIONS]
  23.     int initialColorCount[COLORS];          // [RED, BLUE, GREEN, YELLOW, PURPLE]
  24.     int initialValueCount[VALUES];          // [ZERO, ONE, DRAW_TWO, THREE, FOUR, FIVE, SIX, SEVEN. EIGHT, NINE, A, B, C, D, E, F]
  25.     int currentPlayer;                      // Number ID of current player
  26.     int currentTurn;                        // Current turn number
  27.     int turnsPlayed;                        // Total turns played since start of game
  28.     int currentMovesMade;                   // Number of moves made this turn
  29.     int stackedTwos;                        // Number of stacked twos at the top of the discard pile
  30.     int twosActive;                         // Number of stacked twos that havent been drawn for yet
  31.     int previousPlayer;                     // Number ID of previousPlayer
  32.     int lastPlayedTurn;                     // Turn number of the last turn in which a card was discarded
  33.     int cardsDiscarded;                     // Total number of cards in discard pile
  34.     color forcedColor;                      // Color that is currently enforced by D card played last turn if none enforced it is NULL
  35.     CardNode deck;                          // Linked list of CardNodes for deck
  36.     Card topDiscardPile;                    // Just points to top card of discarded pile
  37.     CardNode discardPile;                   // Linked list of CardNodes for discarded pile
  38.     CardNode playerHands[NO_OF_PLAYERS];    // [0, 1, 2, 4] each contains a pointer to a linked list of each players hand
  39. } game;
  40.  
  41. CardNode addCardToHead(CardNode head, CardNode new);
  42. CardNode addCardToTail(CardNode head, CardNode new);
  43. CardNode removeCardHead(CardNode head);
  44.  
  45. Game newGame(int deckSize, value values[], color colors[], suit suits[]) {
  46.     Game createdGame = calloc(1, sizeof(game));
  47.     createdGame->initialDeckSize = deckSize;
  48.     createdGame->remainingDeckSize = deckSize;
  49.  
  50.     Card tmpCard;
  51.     CardNode tmpCardNode;
  52.     CardNode bottomOfDeck = NULL;
  53.  
  54.     int i = 0;
  55.     while (i < deckSize) {
  56.         tmpCard = newCard(values[i], colors[i], suits[i]);
  57.         tmpCardNode->card = tmpCard;
  58.         tmpCardNode->next = NULL;
  59.         if (i == 0) {
  60.             createdGame->deck = tmpCardNode;
  61.             bottomOfDeck = createdGame->deck;
  62.         } else {
  63.             bottomOfDeck->next = tmpCardNode;
  64.             bottomOfDeck = tmpCardNode;
  65.         }
  66.  
  67.         createdGame->initialSuitCount[suits[i]]++;
  68.         createdGame->initialColorCount[colors[i]]++;
  69.         createdGame->initialValueCount[values[i]]++;
  70.  
  71.         i++;
  72.     }
  73.  
  74.     int player;
  75.     CardNode playerhand;
  76.  
  77.     i = 0;
  78.     while (i < NO_OF_PLAYERS * INITIAL_HAND_SIZE) {
  79.         player = i % 4;
  80.         playerHand = createdGame->playerHands[player];
  81.         playerHand = addCardToTail(playerHand, createdGame->deck);
  82.         createdGame->deck = removeCardHead(createdGame->deck);
  83.     }
  84.  
  85.     return createdGame;
  86. }
  87.  
  88. CardNode addCardToHead(CardNode head, CardNode new) {
  89.     if (head == NULL) {
  90.         new->next = NULL;
  91.         return new;
  92.     } else {
  93.         new->next = head;
  94.         return new;
  95.     }
  96. }
  97.  
  98. CardNode addCardToTail(CardNode head, CardNode new) {
  99.     if (head == NULL) {
  100.         new->next = NULL;
  101.         return new;
  102.     } else {
  103.         CardNode cur = head;
  104.  
  105.         while (cur->next != NULL) {
  106.             cur = cur->next;
  107.         }
  108.  
  109.         new->next = NULL;
  110.         cur->next = new;
  111.         return head;
  112.     }
  113. }
  114.  
  115. CardNode removeCardHead(CardNode head) {
  116.     return head->next;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement