Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "Game.h"
- #define SUITS 5
- #define COLORS 5
- #define VALUES 16
- #define NO_OF_PLAYERS 4
- #define INITIAL_HAND_SIZE 7
- typedef struct _cardNode *CardNode;
- typedef struct _cardNode {
- Card card;
- CardNode next;
- } cardNode;
- typedef struct _game {
- int initialDeckSize;
- int remainingDeckSize;
- int initialSuitCount[SUITS]; // [HEARTS, DIAMONDS, CLUBS, SPADES, QUESTIONS]
- int initialColorCount[COLORS]; // [RED, BLUE, GREEN, YELLOW, PURPLE]
- int initialValueCount[VALUES]; // [ZERO, ONE, DRAW_TWO, THREE, FOUR, FIVE, SIX, SEVEN. EIGHT, NINE, A, B, C, D, E, F]
- int currentPlayer; // Number ID of current player
- int currentTurn; // Current turn number
- int turnsPlayed; // Total turns played since start of game
- int currentMovesMade; // Number of moves made this turn
- int stackedTwos; // Number of stacked twos at the top of the discard pile
- int twosActive; // Number of stacked twos that havent been drawn for yet
- int previousPlayer; // Number ID of previousPlayer
- int lastPlayedTurn; // Turn number of the last turn in which a card was discarded
- int cardsDiscarded; // Total number of cards in discard pile
- color forcedColor; // Color that is currently enforced by D card played last turn if none enforced it is NULL
- CardNode deck; // Linked list of CardNodes for deck
- Card topDiscardPile; // Just points to top card of discarded pile
- CardNode discardPile; // Linked list of CardNodes for discarded pile
- CardNode playerHands[NO_OF_PLAYERS]; // [0, 1, 2, 4] each contains a pointer to a linked list of each players hand
- } game;
- CardNode addCardToHead(CardNode head, CardNode new);
- CardNode addCardToTail(CardNode head, CardNode new);
- CardNode removeCardHead(CardNode head);
- Game newGame(int deckSize, value values[], color colors[], suit suits[]) {
- Game createdGame = calloc(1, sizeof(game));
- createdGame->initialDeckSize = deckSize;
- createdGame->remainingDeckSize = deckSize;
- Card tmpCard;
- CardNode tmpCardNode;
- CardNode bottomOfDeck = NULL;
- int i = 0;
- while (i < deckSize) {
- tmpCard = newCard(values[i], colors[i], suits[i]);
- tmpCardNode->card = tmpCard;
- tmpCardNode->next = NULL;
- if (i == 0) {
- createdGame->deck = tmpCardNode;
- bottomOfDeck = createdGame->deck;
- } else {
- bottomOfDeck->next = tmpCardNode;
- bottomOfDeck = tmpCardNode;
- }
- createdGame->initialSuitCount[suits[i]]++;
- createdGame->initialColorCount[colors[i]]++;
- createdGame->initialValueCount[values[i]]++;
- i++;
- }
- int player;
- CardNode playerhand;
- i = 0;
- while (i < NO_OF_PLAYERS * INITIAL_HAND_SIZE) {
- player = i % 4;
- playerHand = createdGame->playerHands[player];
- playerHand = addCardToTail(playerHand, createdGame->deck);
- createdGame->deck = removeCardHead(createdGame->deck);
- }
- return createdGame;
- }
- CardNode addCardToHead(CardNode head, CardNode new) {
- if (head == NULL) {
- new->next = NULL;
- return new;
- } else {
- new->next = head;
- return new;
- }
- }
- CardNode addCardToTail(CardNode head, CardNode new) {
- if (head == NULL) {
- new->next = NULL;
- return new;
- } else {
- CardNode cur = head;
- while (cur->next != NULL) {
- cur = cur->next;
- }
- new->next = NULL;
- cur->next = new;
- return head;
- }
- }
- CardNode removeCardHead(CardNode head) {
- return head->next;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement