Information

Author(s) Hippolyte Hilgers Anthony Doeraene
Deadline Keine Frist
Abgabenlimit No limitation

Tags

Einloggen

Bataille

In this exercice, you have to write 2 functions which will simulate the card game War.

The hands of the players will be represented by queues and the definition of the hands is given.

You can use the following structure in your code:

typedef struct card{
    int value;
    struct card* next;
} card_t;

typedef struct deck{
    int id_player;
    int size;
    struct card* tail;   // la carte en dessous de son paquet
    struct card* head;   // la carte tout au dessus de son paquet
} deck_t;

You have access to the following functions too :

  • int addCard(deck_t* d, int val) : enqueue the deck : add a card of value val at the bottom of the deck (-1 if an error occurred) (0 otherwise).
  • int drawCard(deck_t* d) : dequeue the deck : draw the card at the top of the deck, removing it from the deck and returning it's value. Return -1 if the deck is empty.
  • deck_t* createDeck(int id, int* cardsData, int nCards) : create a deck (structure deck_t) and fill it with values of the array cardsData (in order). This array is of size nCards;

You can download the header file containing helper functions and their implémentations using these links.

Notice :

  • Be careful about the order when enqueuing the cards into the winner deck.
  • The order is defined by the player's order. So if player 1 loses, his card must be the first to be enqueued
  • The first player (in memory and not in id) plays before the second, etc.
  • In case of draw between players, each player must play 2 cards and the winner is the one with the card of highest value accross all cards that have been drawn. If there is still a draw between players, repeat the process until a player win.

Question 1: Simulation de tours

The simulateOneRound function simulate 1 round and modify the player's deck. It doesn't return anything.

/*
 * @pre playersDeck != NULL, manyPlayers > 0
 * @post simulate 1 round and modify the player's deck (if there is a draw, see the upper note about it)
 */
void simulateOneRound(deck_t** playersDeck, int manyPlayers);
Question 2: Le dernier debout

The guessWhoWins function simulate at most n rounds, and if a player possess all the cards in his deck, he won, the game stops, and the function returns the id of this player.

/*
 * @pre playersDeck != NULL, manyPlayers > 0, maxRounds > 0
 * @post return the id of the winner, or -1 if game isn't ended yet
 */
int guessWhoWins(deck_t **playersDeck, int manyPlayers, int maxRounds);