Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * File: main.c
- * Author: francisco, juliana, pedro
- *
- * Created on 21 de Novembro de 2017, 9:17
- */
- #ifdef _WIN32
- #include <conio.h>
- #else
- #include <stdio.h>
- #define clrscr() printf("\e[1;1H\e[2J")
- #endif
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <math.h>
- #include "LP_Leitura.h"
- #include "LP_Utils.h"
- void showMenu();
- void setupPlayCPU();
- void setupPlayHuman();
- short checkOrder(char card[]);
- void switchCard(char player[][20], int cardPoints[10], int i, int j, int k);
- void arrangeHand(char player[][20]);
- short mustAssistConditions(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2], int playableCards[], int assistCount);
- short checkIfHasSuit(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]);
- short CPUFirstTurn(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]);
- short playCPUCard(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]);
- short playHumanCard(char player[][20], int playerNumber, char matchSettings[][2], int round, int firstTurnOfRound, char playedCards[]);
- void playTimeCPU(char deck[][2], char player[][20], char matchSettings[][2]);
- void playTimeHuman(char deck[][2], char player[][20], char matchSettings[][2]);
- void dealCards(char deck[][2], char player[][20], char matchSettings[][2]);
- void shuffle(int *array, size_t n);
- void buildDeck(char deck[][2]);
- void showMenu() {
- int option = 0;
- readInt(&option, 1, 5, "Introduza um número de 1 a 5\n"
- "1- Jogar contra o CPU\n"
- "2- Jogar localmente\n"
- "3- Ver histórico de jogos\n"
- "4- Créditos\n"
- "5- Sair\n");
- if (option > 0 && option < 6) {
- switch (option) {
- case 1:
- setupPlayCPU();
- break;
- case 2:
- setupPlayHuman();
- break;
- case 5:
- exit(0);
- break;
- }
- }
- }
- /* @matchSettings should look like:
- * matchSettings[0] = number of games played
- * matchSettings[1] = who's dealing cards
- * matchSettings[2] = game's trump
- */
- void setupPlayCPU() {
- static char deck[40][2]; //Deck consisting of 20 cards, 2 chars each
- char player[4][20]; // will receive values player[number of player][cards]
- char matchSettings[3][2]; //Will store the number of games played, who's the one dealing cards and game's trump
- int matchNumber, repeat = 1;
- while (repeat == 1) {
- buildDeck(deck);
- matchNumber = atoi(matchSettings[0]) + 1; //Increments number of the match
- sprintf(matchSettings[0], "%d", matchNumber); //Stores it on matchSettings[0][0]
- dealCards(deck, player, matchSettings);
- arrangeHand(player); //Cards will be arranged descending, for each suit in order of
- playTimeCPU(deck, player, matchSettings);
- readInt(&repeat, 1, 2, "Deseja jogar novamente?\n1- Sim, 2- Não\n");
- }
- }
- void setupPlayHuman() {
- static char deck[40][2]; //Deck consisting of 20 cards, 2 chars each
- char player[4][20]; // will receive values player[number of player][cards]
- char matchSettings[3][2]; //Will store the number of games played, who's the one dealing cards and game's trump
- int matchNumber, repeat = 1;
- while (repeat == 1) {
- buildDeck(deck);
- matchNumber = atoi(matchSettings[0]) + 1; //Increments number of the match
- sprintf(matchSettings[0], "%d", matchNumber); //Stores it on matchSettings[0][0]
- dealCards(deck, player, matchSettings);
- arrangeHand(player); //Cards will be arranged descending, for each suit in order of
- playTimeHuman(deck, player, matchSettings);
- readInt(&repeat, 1, 2, "Deseja jogar novamente?\n1- Sim, 2- Não\n");
- }
- }
- short checkOrder(char card[]) {
- int value;
- switch (card[0]) {
- case 'A':
- value = 10;
- break;
- case '7':
- value = 9;
- break;
- case 'K':
- value = 8;
- break;
- case 'J':
- value = 7;
- break;
- case 'Q':
- value = 6;
- break;
- case '6':
- value = 5;
- break;
- case '5':
- value = 4;
- break;
- case '4':
- value = 3;
- break;
- case '3':
- value = 2;
- break;
- case '2':
- value = 1;
- break;
- }
- return value;
- }
- double checkValue(char card[]) {
- double value;
- switch (card[0]) {
- case 'A':
- value = 11;
- break;
- case '7':
- value = 10;
- break;
- case 'K':
- value = 4;
- break;
- case 'J':
- value = 3;
- break;
- case 'Q':
- value = 2;
- break;
- case '6':
- value = 0.6; //For the propose of sorting cards with the value of 0
- break;
- case '5':
- value = 0.5;
- break;
- case '4':
- value = 0.4;
- break;
- case '3':
- value = 0.3;
- break;
- case '2':
- value = 0.2;
- break;
- }
- return value;
- }
- void switchCard(char player[][20], int cardPoints[10], int i, int j, int k) {
- int m;
- char n[2];
- n[0] = player[i][j];
- n[1] = player[i][j + 1];
- player[i][j] = player[i][k];
- player[i][j + 1] = player[i][k + 1];
- player[i][k] = n[0];
- player[i][k + 1] = n[1];
- m = cardPoints[j / 2];
- cardPoints[j / 2] = cardPoints[k / 2];
- cardPoints[k / 2] = m;
- }
- void arrangeHand(char player[][20]) {
- int i, k, p, q, count, j;
- int cardPoints[10];
- char card[1];
- //For each player
- for (i = 0; i < 4; i++) {
- //Saves cards' points to @cardPoints array;
- for (q = 0; q < 20; q++) {
- card[0] = player[i][q];
- cardPoints[q / 2] = checkOrder(card);
- q++;
- }
- j = 0; //Reset @j tu run the while cycle for the next player
- while (j < 20) {
- count = 0;
- for (k = 0; k < 20; k++) {
- if (cardPoints[j / 2] < cardPoints[k / 2] && player[i][j + 1] == player[i][k + 1]) {
- //If it finds a card of the same suit with a highest value
- if (j <= k) {
- switchCard(player, cardPoints, i, j, k);
- count++;
- } else {
- p = k + 2;
- q = 0;
- while (q == 0) {
- if (player[i][j + 1] == player[i][p + 1]) {//If suits are the same
- if (cardPoints[j / 2] < cardPoints[p / 2]) { //If card p is higher than card j
- p = p + 2;
- } else if (cardPoints[j / 2] == cardPoints[p / 2]) {
- //
- q = 1;
- } else {
- switchCard(player, cardPoints, i, j, p);
- k++;
- count++;
- q = 1;
- }
- } else {
- switchCard(player, cardPoints, i, j, p);
- k++;
- count++;
- q = 1;
- }
- }
- }
- }
- k++;
- }
- if (count > 0) {
- j = 0;
- } else {
- j = j + 2;
- }
- }
- }
- }
- short mustAssistConditions(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2], int playableCards[], int assistCount) {
- int i, j, trumpped = 0, cardFound = 0, suitableCard = 0, cardsOfSuitPlayed = 0, r;
- char firstSuitOfThatRound[1];
- //Count how many cards of that suit have been played already
- for (i = 1; i < round * 8; i += 2) {
- if (playedCards[i] == playedCards[round * 8 + firstTurnOfRound[round] * 2 + 1]) {
- cardsOfSuitPlayed++;
- }
- }
- //Check if they have Aces
- for (i = 0; i < assistCount; i++) { //Searches for Aces in their hand
- if (player[playerNumber][2 * (playableCards[i] - 1)] == 'A' && player[playerNumber][(playableCards[i] - 1) * 2 + 1] != matchSettings[2][1]) { //If it's an ace and it isn't the ace of trump
- cardFound = playableCards[i];
- }
- }
- if (cardFound > 0) {
- for (j = 0; j <= round; j++) {
- firstSuitOfThatRound[0] = playedCards[firstTurnOfRound[j] * j * 8 + 1];
- //If suit matches the ace he wants to play
- if (firstSuitOfThatRound[0] == player[playerNumber][(cardFound - 1) * 2 + 1]) {
- //if any of the two opponents played a trump
- if (playedCards[((playerNumber - 1) % 4) * j * 2 + 1] == matchSettings[2][1] || playedCards[((playerNumber + 1) % 4) * j * 2 + 1] == matchSettings[2][1]) {
- trumpped++;
- }
- }
- }
- //Check if in previous plays the opposite team trumped
- if (trumpped == 0) {
- if (cardsOfSuitPlayed + assistCount < 7) {
- playableCards[0] = cardFound;
- suitableCard++;
- }
- }
- }
- if (suitableCard < 1) {
- r = (rand() % assistCount);
- playableCards[0] = playableCards[r];
- }
- return playableCards[0];
- }
- //CPU CONDITIONS CPU_Conditions
- short checkIfHasSuit(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]) {
- int i, assistCount = 0, trumpCount = 0, playableCards[10 - round], r;
- char firstCard[2];
- firstCard[0] = playedCards[round * 8 + firstTurnOfRound[round] * 2];
- firstCard[1] = playedCards[round * 8 + firstTurnOfRound[round] * 2 + 1];
- for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
- if (player[playerNumber][2 * i + 1] == firstCard[1]) { //If suits matches
- playableCards[assistCount] = i + 1;
- assistCount++;
- }
- }
- if (assistCount > 0) { //Has cards to assit
- if (assistCount > 1) {
- playableCards[0] = mustAssistConditions(player, playerNumber, round, firstTurnOfRound, playedCards, matchSettings, playableCards, assistCount);
- }
- } else if (firstCard[1] != matchSettings[2][1]) {
- for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
- if (player[playerNumber][2 * i + 1] == matchSettings[2][1]) { //If suits matches
- playableCards[trumpCount] = i + 1;
- trumpCount++;
- }
- }
- if (trumpCount > 0) { //Didn't have cards to assist, so plays a trump card if he has it
- if (trumpCount > 1) {
- r = (rand() % trumpCount);
- playableCards[0] = playableCards[r];
- }
- }
- }
- if (assistCount == 0 && trumpCount == 0) { //If playerNumber doesn't have cards to assit or trumps, he plays a random card
- playableCards[0] = r = (rand() % (10 - round)) + 1;
- }
- return playableCards[0];
- }
- short CPUFirstTurn(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]) {
- int i, j, aces = 0, k = 0, playableCards[10 - round], playableAces[4], trumpped, r;
- char firstSuitOfThatRound[1];
- //Check if they have Aces
- for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
- if (player[playerNumber][2 * i] == 'A' && player[playerNumber][2 * i + 1] != matchSettings[2][1]) { //If it's an ace and it isn't the Ace of Trump
- playableAces[aces] = i + 1;
- aces++;
- }
- }
- if (aces > 0) {
- //Check if the suit of the ace has been played before and someone of the other team trumped over it
- for (i = 0; i < aces; i++) { //Check for each Ace's suit
- trumpped = 0;
- for (j = 0; j < round; j++) {
- firstSuitOfThatRound[0] = playedCards[firstTurnOfRound[j] * j * 8 + 1];
- //If suit matches the ace he wants to play
- if (firstSuitOfThatRound[0] == player[playerNumber][2 * playableAces[i] + 1]) {
- if (playedCards[((playerNumber - 1) % 4) * j * 2 + 1] == matchSettings[2][1] || playedCards[((playerNumber + 1) % 4) * j * 2 + 1] == matchSettings[2][1]) {
- trumpped++;
- }
- }
- }
- //Check if in previous plays the opposite team trumped
- if (trumpped == 0) {
- playableCards[k] = playableAces[i];
- k++;
- }
- }
- if (k > 0) {
- //If there's at least one playableAce
- if (k > 1) { //Randomize the list of aces
- r = (rand() % k);
- playableCards[0] = playableCards[r];
- }
- } else {
- //If he deson't have a playable ace, he plays a random card
- playableCards[0] = 1 + rand() % (10 - round);
- }
- } else {
- playableCards[0] = 1 + rand() % (10 - round); //The player has no Aces
- }
- return playableCards[0];
- }
- short playCPUCard(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]) {
- int cardPlayed;
- if (firstTurnOfRound[round] == playerNumber) {
- cardPlayed = CPUFirstTurn(player, playerNumber, round, firstTurnOfRound, playedCards, matchSettings);
- } else {
- cardPlayed = checkIfHasSuit(player, playerNumber, round, firstTurnOfRound, playedCards, matchSettings);
- }
- return cardPlayed;
- }
- short playHumanCard(char player[][20], int playerNumber, char matchSettings[][2], int round, int firstTurnOfRound, char playedCards[]) {
- int i, j = 1, cardPlayed, assistCount, checkIfValidPlay = 0, playableCards[10 - round];
- char firstCard[2];
- firstCard[0] = playedCards[round * 8 + firstTurnOfRound * 2];
- firstCard[1] = playedCards[round * 8 + firstTurnOfRound * 2 + 1];
- printf("Trunfo: %c%c\n", matchSettings[2][0], matchSettings[2][1]);
- printf("As suas cartas:\n");
- for (i = 0; i < 20 - round * 2; i += 2) {
- if (player[playerNumber][i + 1] == firstCard[1]) {
- printf("%d- \033[22;32m%c%c\033[0m\n", j, player[playerNumber][i], player[playerNumber][i + 1]); //Paints of green the cards he must play
- assistCount++;
- playableCards[assistCount] = i / 2 + 1;
- } else if (player[playerNumber][i + 1] == matchSettings[2][1]) {
- printf("%d- \033[22;31m%c%c\033[0m\n", j, player[playerNumber][i], player[playerNumber][i + 1]); //Paints of red the trumps he has
- } else {
- printf("%d- %c%c\n", j, player[playerNumber][i], player[playerNumber][i + 1]); //Doesn't paint other cards
- }
- j++;
- }
- readInt(&cardPlayed, 1, 10 - round, "Escolha uma carta para jogar\n");
- if (firstTurnOfRound != 0) {
- while (checkIfValidPlay < 1) {
- assistCount = 0; //Resets assist count
- for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
- if (player[playerNumber][2 * i + 1] == firstCard[1]) { //If suits matches
- playableCards[assistCount] = i + 1;
- assistCount++;
- }
- }
- if (assistCount > 0) {
- for (i = 0; i < assistCount; i++)
- if (cardPlayed == playableCards[i]) {
- checkIfValidPlay++;
- }
- } else {
- checkIfValidPlay = 1;
- }
- if (checkIfValidPlay < 1) {
- readInt(&cardPlayed, 1, 10 - round, "Tem cartas para assistir, escolha outra carta para jogar\n");
- }
- }
- }
- return cardPlayed;
- }
- short roundWinner(char playedCards[], char matchSettings[][2], int round, int firstTurnOfRound[]) {
- char roundSuit[1];
- int position, i, winner, highestCard = 0;
- char card[2], prevHighestCard[2];
- double value;
- for (i = 1; i < 8; i = i + 2) { //Gets the highest trump played
- card[0] = playedCards[round * 8 + i - 1];
- card[1] = playedCards[round * 8 + i];
- if (playedCards[round * 8 + i] == matchSettings[2][1]) {
- if (highestCard == 0) { //Store the first trump he founds to start the comparation
- highestCard = i;
- } else {
- value = checkValue(card); //Gets the value of the card
- prevHighestCard[0] = playedCards[round * 8 + highestCard - 1]; //Previous highest trump's face
- prevHighestCard[1] = playedCards[round * 8 + highestCard]; //Previous highest trump's suit
- if (value > checkValue(prevHighestCard)) {
- highestCard = i;
- }
- }
- }
- }
- if (highestCard == 0) { //If there was no trumps, checks for the highest card of the round's suit
- position = round * 8 + firstTurnOfRound[round] *2 + 1;
- roundSuit[0] = playedCards[position];
- for (i = 1; i < 8; i = i + 2) { //Gets the highest card played
- card[0] = playedCards[round * 8 + i - 1];
- card[1] = playedCards[round * 8 + i];
- if (playedCards[round * 8 + i] == roundSuit[0]) {
- if (highestCard == 0) { //Store the first card he founds to start the comparation
- highestCard = i;
- } else {
- value = checkValue(card); //Gets the value of the card
- prevHighestCard[0] = playedCards[round * 8 + highestCard - 1]; //Previous highest card's face
- prevHighestCard[1] = playedCards[round * 8 + highestCard]; //Previous highest card's suit
- if (value > checkValue(prevHighestCard)) {
- highestCard = i;
- }
- }
- }
- }
- }
- winner = (highestCard - 1) / 2;
- printf("Quem ganhou a vazada foi o jogador %d\n\n", (highestCard - 1) / 2);
- return (highestCard - 1) / 2;
- }
- short countPoints(char playedCards[], int teamPoints[], int firstTurnOfRound[]) {
- int i, j, points;
- char card[1];
- for (i = 0; i < 10; i++) {
- points = 0;
- for (j = 0; j < 8; j += 2) {
- card[0] = playedCards[i * 8 + j];
- points += floor(checkValue(card));
- }
- if (firstTurnOfRound[i + 1] % 2 == 0) {
- teamPoints[0] += points;
- } else {
- teamPoints[1] += points;
- }
- }
- }
- void playTimeCPU(char deck[][2], char player[][20], char matchSettings[][2]) {
- int round, g, h, i, j, k, cardPlayed, idxToDel, firstTurnOfRound[11], playerTurn, position, teamPoints[2] = {0, 0};
- char playedCards[80], hand[20];
- firstTurnOfRound[0] = matchSettings[0][0] % 4; //On the first round, the first player playing is the one next to the one that dealt
- clrscr(); //Clear the screen
- //Player played cards
- for (round = 0; round < 10; round++) {
- for (i = firstTurnOfRound[round]; i <= firstTurnOfRound[round] + 3; i++) {
- playerTurn = (i % 4 + 4) % 4;
- if (playerTurn == 0) {//If it's the human's turn, call the function playHumanCard
- for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
- if (j == firstTurnOfRound[round]) {
- printf("A carta jogada pelo jogador %d foi \033[22;32m%c%c\033[0m\n", j, playedCards[round * 8 + j * 2], playedCards[round * 8 + j * 2 + 1]);
- } else {
- printf("A carta jogada pelo jogador %d foi %c%c\n", j, playedCards[round * 8 + j * 2], playedCards[round * 8 + j * 2 + 1]);
- }
- }
- cardPlayed = playHumanCard(player, playerTurn, matchSettings, round, firstTurnOfRound[round], playedCards);
- clrscr(); //Clear the screen
- } else { //Select the card to be played by CPU
- if (round < 9) {
- cardPlayed = playCPUCard(player, playerTurn, round, firstTurnOfRound, playedCards, matchSettings);
- } else {
- cardPlayed = 1;
- }
- }
- //Remove the card from the hand
- idxToDel = cardPlayed * 2 - 2; //Get the face of the card to remove
- position = round * 8 + playerTurn * 2; //
- playedCards[position] = player[playerTurn][idxToDel];
- playedCards[position + 1] = player[playerTurn][idxToDel + 1];
- k = 0; //Resets k
- for (h = 0; h < 10; h++) {
- if (h != cardPlayed - 1) {
- hand[k] = player[playerTurn][h * 2];
- k++;
- hand[k] = player[playerTurn][h * 2 + 1];
- k++;
- }
- }
- hand[18] = '0';
- hand[19] = '0';
- strncpy(player[playerTurn], hand, 20);
- }
- clrscr(); //Clear the screen
- //End of round info
- for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
- g = (j % 4 + 4) % 4;
- if (g == firstTurnOfRound[round]) {
- printf("A carta jogada pelo jogador %d foi \033[22;32m%c%c\033[0m\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
- } else {
- printf("A carta jogada pelo jogador %d foi %c%c\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
- }
- }
- firstTurnOfRound[round + 1] = roundWinner(playedCards, matchSettings, round, firstTurnOfRound);
- }
- countPoints(playedCards, teamPoints, firstTurnOfRound);
- printf("Pontos da primeira Equipa: %d\n", teamPoints[0]);
- printf("Pontos da segunda Equipa: %d\n", teamPoints[1]);
- }
- void playTimeHuman(char deck[][2], char player[][20], char matchSettings[][2]) {
- int round, g, h, i, j, k, cardPlayed, idxToDel, firstTurnOfRound[11], playerTurn, position, teamPoints[2] = {0, 0};
- char playedCards[80], hand[20];
- firstTurnOfRound[0] = matchSettings[0][0] % 4; //On the first round, the first player playing is the one next to the one that dealt
- clrscr(); //Clear the screen
- //Player played cards
- for (round = 0; round < 10; round++) {
- for (i = firstTurnOfRound[round]; i <= firstTurnOfRound[round] + 3; i++) {
- playerTurn = (i % 4 + 4) % 4;
- // clrscr(); //Clear the screen
- printf("Clique em qualquer tecla para o jogador %d jogar\n", playerTurn);
- getchar();
- clrscr(); //Clear the screen
- for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
- if (j == firstTurnOfRound[round]) {
- printf("A carta jogada pelo jogador %d foi \033[22;32m%c%c\033[0m\n", j, playedCards[round * 8 + j * 2], playedCards[round * 8 + j * 2 + 1]);
- } else {
- printf("A carta jogada pelo jogador %d foi %c%c\n", j, playedCards[round * 8 + j * 2], playedCards[round * 8 + j * 2 + 1]);
- }
- }
- cardPlayed = playHumanCard(player, playerTurn, matchSettings, round, firstTurnOfRound[round], playedCards);
- clrscr(); //Clear the screen
- //Remove the card from the hand
- idxToDel = cardPlayed * 2 - 2; //Get the face of the card to remove
- position = round * 8 + playerTurn * 2; //
- playedCards[position] = player[playerTurn][idxToDel];
- playedCards[position + 1] = player[playerTurn][idxToDel + 1];
- k = 0; //Resets k
- for (h = 0; h < 10; h++) {
- if (h != cardPlayed - 1) {
- hand[k] = player[playerTurn][h * 2];
- k++;
- hand[k] = player[playerTurn][h * 2 + 1];
- k++;
- }
- }
- hand[18] = '0';
- hand[19] = '0';
- strncpy(player[playerTurn], hand, 20);
- }
- clrscr(); //Clear the screen
- //End of round info
- for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
- g = (j % 4 + 4) % 4;
- if (g == firstTurnOfRound[round]) {
- printf("A carta jogada pelo jogador %d foi \033[22;32m%c%c\033[0m\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
- } else {
- printf("A carta jogada pelo jogador %d foi %c%c\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
- }
- }
- firstTurnOfRound[round + 1] = roundWinner(playedCards, matchSettings, round, firstTurnOfRound);
- }
- countPoints(playedCards, teamPoints, firstTurnOfRound);
- printf("Pontos da primeira Equipa: %d\n", teamPoints[0]);
- printf("Pontos da segunda Equipa: %d\n", teamPoints[1]);
- }
- void dealCards(char deck[][2], char player[][20], char matchSettings[][2]) {
- int option = 0, i, j = 0, k = 0, playerDealingCards, m;
- playerDealingCards = (atoi(matchSettings[0]) - 1) % 4;
- if (playerDealingCards == 0) {
- readInt(&option, 1, 2, "Deseja tirar o trunfo por \n1- cima, ou 2- baixo?\n");
- } else {
- option = rand() % 2 + 1; //Generate a random option between 1 and 2
- }
- if (option == 1) { //In case he chose to deal the trump from the top...
- // Deal cards to each player
- // k = position of the card, by the end of the external for's iteration, k will be 40
- printf("O trunfo foi tirado por cima\n");
- for (i = playerDealingCards; i >= playerDealingCards - 3; i--) {//Deal cards for the 4 players
- for (j = 0; j < 20; j++) { //each 20 chars = 10 cards composed by a face (j) then a suit(j++); i.e: 6D (6 of Diamonds)
- m = (i % 4 + 4) % 4; //Get the positive module of players
- player[m][j] = deck[k][0];
- j++;
- player[m][j] = deck[k][1];
- k++;
- }
- }
- //Declaring game's trump
- matchSettings[2][0] = player[playerDealingCards][0]; //Because he dealt the trump from the top, the trump is the first card he dealt for him.
- matchSettings[2][1] = player[playerDealingCards][1];
- } else {
- printf("O trunfo foi tirado por baixo\n");
- // Deal cards to each player
- // k = position of the card, by the end of the external for's iteration, k will be 40
- for (i = playerDealingCards - 3; i <= playerDealingCards; i++) {//for each player, starts on player 0, then goes 3, 2 and finally 1
- for (j = 0; j < 20; j++) { //each 20 chars = 10 cards composed by a face then a suit; i.e: 6D (6 of Diamonds)
- m = (i % 4 + 4) % 4; //Get the positive module of players
- player[m][j] = deck[k][0];
- j++;
- player[m][j] = deck[k][1];
- k++;
- }
- }
- //Declaring game's trump
- matchSettings[2][0] = player[playerDealingCards][18]; //10th card of the first player, in which its face occupies position 18 and suit occupies the 19th position
- matchSettings[2][1] = player[playerDealingCards][19];
- }
- // Mostra a mão de cada um
- // printf("\nO trunfo é: %c%c\n", matchSettings[2][0], matchSettings[2][1]);
- //
- // Printa as cartas de cada jogador
- // for (j = 0; j < 4; j++) {
- // printf("Player %d\n", j);
- // for (i = 0; i < 20; i++) {
- // printf("%c", player[j][i]);
- // if (i % 2 != 0) {
- // printf("\n");
- // }
- // }
- // printf("\n");
- // }
- }
- // Shuffle an array, passing 2 arguments, the array and its size
- void shuffle(int *array, size_t n) {
- srand(time(NULL)); //New seed for rand based on current time
- if (n > 1) {
- size_t i;
- for (i = 0; i < n; i++) {
- size_t j = rand() % (n);
- int t = array[j];
- array[j] = array[i];
- array[i] = t;
- }
- }
- }
- //Builds deck
- void buildDeck(char deck[][2]) {
- int i, j, randomList[40];
- int k = 0;
- char suits[4] = {'H', 'S', 'D', 'C'}; //Hearts, Spaces, Diamonds, Clubs;
- char faces[10] = {'A', '2', '3', '4', '5', '6', '7', 'J', 'Q', 'K'};
- for (i = 0; i < 40; i++) { //Generate a list of 40 numbers ascending from 0 to 39
- randomList[i] = i;
- }
- shuffle(randomList, 40); //Shuffle the list
- /*
- * Deck Builder
- * Every number randomized earlier will now assume the value of a card, this way the deck is shuffled
- * Model: Deck[position][face + suit]
- * Position will be taken from the randomized list, and face+suit from the previously declared variables
- * Instead of having [0] = AH; [1] = 2H; [2] = 3H, it will not get random numbers for its position
- * Example: [27] = AC; [15] = 2H; [32] = 3H etc. The whole deck is shuffled.
- */
- for (i = 0; i < 4; i++) { //Suits
- for (j = 0; j < 10; j++) { //Faces
- deck[randomList[k]][0] = faces[j]; //Stores faces in shuffleDeck
- deck[randomList[k]][1] = suits[i]; //A shuffledDeck stored value will look like 6D (Six of Diamonds, 6♦)
- k++;
- }
- }
- }
- int main(int argc, char** argv) {
- showMenu();
- return (EXIT_SUCCESS);
- }
Add Comment
Please, Sign In to add comment