Exemple #1
0
void test_deck_deleteDeck()
{
    cut_assert_equal_int(POINTER_NULL, deck_deleteDeck(NULL));
    struct Deck *deck = deck_createDeck(MAX_GAME_PLAYERS);
    cut_assert_equal_int(FUNCTIOON_NO_ERROR, deck_deleteDeck(&deck));
    cut_assert_equal_pointer(NULL, deck);
    cut_assert_equal_int(DECK_NULL, deck_deleteDeck(&deck));
}
Exemple #2
0
void test_round_distributeDeck()
{
    struct Round *round = round_createRound(MAX_CARDS);
    struct Deck  *deck  = deck_createDeck(MAX_GAME_PLAYERS);
    struct Player *players[MAX_GAME_PLAYERS];

    cut_assert_equal_int(ROUND_NULL, round_distributeDeck(NULL, deck));
    cut_assert_equal_int(DECK_NULL, round_distributeDeck(round, NULL));

    for (int i = 0; i < MAX_GAME_PLAYERS; i++) {
        players[i] = player_createPlayer("A", i);
        round_addPlayer(round, players[i]);
    }

    cut_assert_equal_int(FUNCTIOON_NO_ERROR, round_distributeDeck(round, deck));
    cut_assert_equal_pointer(NULL, round->trump);
    for (int i = 0; i < MAX_GAME_PLAYERS; i++) {
        int nullCards = 0;
        int duplicate = 0;
        for (int j = 0; j < MAX_CARDS; j++)
            if (round->players[i]->hand[j] == NULL)
                nullCards++;
        for (int j = 0; j < MAX_CARDS - 1; j++)
            for (int k = j + 1; k < MAX_CARDS; k++)
                if (round->players[i]->hand[j] == round->players[i]->hand[k])
                    duplicate++;
        cut_assert_equal_int(0, nullCards);
        cut_assert_equal_int(0, duplicate);
    }

    round_deleteRound(&round);
    deck_deleteDeck(&deck);
    for (int i = 0; i < MAX_GAME_PLAYERS; i++)
        player_deletePlayer(&players[i]);
}
Exemple #3
0
void test_round_disitrbuteCard()
{
    struct Round *round = round_createRound(MAX_CARDS);
    struct Deck  *deck  = deck_createDeck(MAX_GAME_PLAYERS);
    struct Player *players[MAX_GAME_PLAYERS];

    cut_assert_equal_int(ROUND_NULL, round_distributeCard(NULL, deck));
    cut_assert_equal_int(DECK_NULL, round_distributeCard(round, NULL));
    cut_assert_equal_int(INSUFFICIENT_PLAYERS,
                         round_distributeCard(round, deck));

    for (int i = 0; i < MAX_GAME_PLAYERS; i++) {
        players[i] = player_createPlayer("A", i);
        round_addPlayer(round, players[i]);
    }

    for (int i = 0; i < MAX_CARDS; i++) {
        cut_assert_equal_int(FUNCTIOON_NO_ERROR,
                             round_distributeCard(round, deck));
        cut_assert_equal_int(DECK_SIZE - (i + 1) * MAX_GAME_PLAYERS,
                             deck_getDeckSize(deck));
        for (int j = 0; j < MAX_GAME_PLAYERS; j++) {
            int cardsNumber = 0;
            for (int k = 0; k < MAX_CARDS; k++)
                if (round->players[j]->hand[k] != NULL)
                    cardsNumber++;
            cut_assert_equal_int(i + 1, cardsNumber);
        }
    }

    for (int i = 0; i < MAX_GAME_PLAYERS; i++)
        player_deletePlayer(&players[i]);
    round_deleteRound(&round);
    deck_deleteDeck(&deck);
}
Exemple #4
0
void test_hand_addCard()
{
    struct Hand *hand = hand_createHand();
    struct Deck *deck = deck_createDeck(MAX_GAME_PLAYERS);
    struct Player *players[MAX_GAME_PLAYERS + 1];   

    for (int i = 0; i < MAX_GAME_PLAYERS; i++) {
        players[i] = player_createPlayer("A", i);
        hand_addPlayer(hand, players[i]);
        struct Card *card  = deck->cards[i];
        struct Card *card2 = deck->cards[i];
        cut_assert_equal_int(FUNCTION_NO_ERROR, 
                            hand_addCard(hand, players[i], &(deck->cards[i])));
        cut_assert_equal_pointer(NULL, deck->cards[i]);
        int check = 0;
        for (int j = 0; j < MAX_GAME_PLAYERS; j++)
            if (players[i] == hand->players[j] && card == hand->cards[j])
                check++;
        cut_assert_equal_int(1, check);
    }
    players[MAX_GAME_PLAYERS] = player_createPlayer("A", 1);
    cut_assert_equal_int(NOT_FOUND,
                         hand_addCard(hand, players[MAX_GAME_PLAYERS],
                                      &deck->cards[MAX_GAME_PLAYERS]));

    for (int i = 0; i <= MAX_GAME_PLAYERS; i++)
        player_deletePlayer(&players[i]);
    deck_deleteDeck(&deck);
    hand_deleteHand(&hand);
}
Exemple #5
0
void test_deck_createDeck()
{
    struct Deck *deck;
    cut_assert_equal_pointer(NULL, deck_createDeck(MAX_GAME_PLAYERS + 1));
    cut_assert_equal_pointer(NULL, deck_createDeck(MIN_GAME_PLAYERS - 1));

    for (int i = MIN_GAME_PLAYERS; i <= MAX_GAME_PLAYERS; i++) {
        deck = deck_createDeck(i);
        int check = 0;
        if (deck == NULL)
            check++;
        cut_assert_equal_int(0, check);
        cut_assert_equal_int(i * MAX_CARDS, deck_getDeckSize(deck));
        
        int duplicates = 0;
        for (int j = 0; j < DECK_SIZE; j++)
            for (int k = 0; k < DECK_SIZE; k++)
                if (k != j && deck->cards[k] != NULL && deck->cards[j] != NULL
                    && deck->cards[k]->suit == deck->cards[j]->suit &&
                    deck->cards[k]->value == deck->cards[j]->value)
                    duplicates++;
        cut_assert_equal_int(0, duplicates);
        deck_deleteDeck(&deck);
    }
}
Exemple #6
0
void test_deck_getDeckSize()
{
    cut_assert_equal_int(DECK_NULL, deck_getDeckSize(NULL));
    struct Deck *deck;

    for (int i = MIN_GAME_PLAYERS; i <= MAX_GAME_PLAYERS; i++) {
        deck = deck_createDeck(i);
        cut_assert_equal_int(i * MAX_CARDS, deck_getDeckSize(deck));
        deck_deleteDeck(&deck);
    }
}
Exemple #7
0
void test_player_addCard()
{
    struct Deck *deck = deck_createDeck(MIN_GAME_PLAYERS);
    struct Player *player = player_createPlayer("A", 1);

    cut_assert_equal_int(PLAYER_NULL, player_addCard(NULL, &deck->cards[0]));
    cut_assert_equal_int(POINTER_NULL, player_addCard(player, NULL));
    cut_assert_operator_int(0, >, player_addCard(NULL, NULL));

    int i;
    for (i = 0; i < MAX_CARDS; i++) {
        cut_assert_equal_int(FUNCTION_NO_ERROR,
                             player_addCard(player, &deck->cards[i]));
        cut_assert_equal_int(CARD_NULL, 
                             player_addCard(player, &deck->cards[i]));
    }
    cut_assert_equal_int(FULL, player_addCard(player, &deck->cards[++i]));

    player_deletePlayer(&player);
    deck_deleteDeck(&deck);
}
Exemple #8
0
void test_deck_shuffleDeck()
{
    cut_assert_equal_int(DECK_NULL, deck_shuffleDeck(NULL));
    struct Deck *deck = deck_createDeck(MAX_GAME_PLAYERS);
    struct Deck *shuffled_deck = malloc(sizeof(struct Deck));
    memcpy(shuffled_deck, deck, sizeof(struct Deck));
    cut_assert_equal_int(FUNCTIOON_NO_ERROR, deck_shuffleDeck(shuffled_deck));

    int deckSize = deck_getDeckSize(deck);
    int differences = 0;
    for (int j = 0; j < 100; j++) {
        differences = 0;
        for (int i = 0; i < deckSize; i++)
            if (deck->cards[i]->suit  != shuffled_deck->cards[i]->suit ||
                deck->cards[i]->value != shuffled_deck->cards[i]->value)
                differences++;
        cut_assert_not_equal_int(0, differences);
    }

    deck_deleteDeck(&deck);
}
Exemple #9
0
/**
 * @brief Starts the game, connecting libraries and UI
 */
int cruceGameLogic()
{
    setlocale(LC_ALL, "");
    initscr();
    cbreak();

    if (has_colors() == FALSE) {
        endwin();
        printf("Your terminal does not support colors!");
        return 0;
    }

    start_color();
    init_pair(1, COLOR_RED, COLOR_BLACK);
    init_pair(2, COLOR_GREEN, COLOR_BLACK);
    init_pair(3, COLOR_YELLOW, COLOR_BLACK);
    init_pair(4, COLOR_BLUE, COLOR_BLACK);
    init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
    init_pair(7, COLOR_WHITE, COLOR_BLACK);
    refresh();

    welcomeMessage();
    int limitScore  = getScoreLimit();
    int noOfPlayers = getNoOfPlayers();

    struct Game *game = game_createGame(limitScore);
    for (int i = 0; i < noOfPlayers; i++) {
        int err;
        while ((err = game_addPlayer(newPlayer(i + 1), game)) == DUPLICATE_NAME)
            printw("The player's name have to be unique\n");
        if (err != 0)
            printw("ERROR: game_addPlayer() %d\n", err);
    }
    formTeams(game);

    for (int i = 0; !game_winningTeam(game); i++) {
        game_arrangePlayersRound(game, i % MAX_GAME_PLAYERS);

        struct Deck *deck = deck_createDeck();
        deck_deckShuffle(deck);

        round_distributeDeck(deck, game->round);
        clear();
        refresh();

        for (int i = 0; i < game->numberPlayers; i++) {
            getBid(game, i);
            clear();
            refresh();
        }
        displayBids(game, game->numberPlayers);
        refresh();
        sleep(2);
        clear();
        refresh();

        struct Player *bidWinner = round_getBidWinner(game->round);
        int first = round_findPlayerIndexRound(bidWinner, game->round);
        for (int i = 0; team_hasCards(game->players[0]); i++) {
            round_arrangePlayersHand(game->round, first);

            for (int j = 0; j < game->numberPlayers; j++) {
                WINDOW *scoreTableWindow = newwin(11, 49, 0, 30);
#ifdef BORDERS
                box(scoreTableWindow, 0, 0);
#endif
                printScore(game, game->round, scoreTableWindow);
                wrefresh(scoreTableWindow);
                delwin(scoreTableWindow);

                displayCardsAndPickCard(game, j);
                clear();
                refresh();
            }

            struct Player *handWinner = round_handWinner(game->round->hands[i],
                                        game->round);
            first = round_findPlayerIndexRound(handWinner, game->round);

            if (deck_cardsNumber(deck) > 0)
                round_distributeCard(deck, game->round);

        }

        int oldScore[MAX_GAME_PLAYERS];
        for(int i = 0; i < MAX_GAME_TEAMS; i++) {
            if(game->teams[i] != NULL) {
                oldScore[i] = game->teams[i]->score;
            }
        }

        game_updateScore(game, bidWinner);

        printRoundTerminationMessage(game, oldScore);
        getch();

        deck_deleteDeck(&deck);
        round_deleteRound(&game->round);
    }

    clear();
    refresh();
    gameEndingMessage(game_winningTeam(game));

    for (int i = 0; i < MAX_GAME_PLAYERS; i++)
        if (game->players[i])
            team_deletePlayer(&game->players[i]);
    for (int i = 0; i < MAX_GAME_TEAMS; i++)
        if (game->teams[i])
            team_deleteTeam(&game->teams[i]);
    game_deleteGame(&game);

    getch();
    endwin();
    return EXIT_SUCCESS;
}