xickoh

Sueca, implementado o jogo local

Dec 28th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 30.52 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /*
  8.  * File:   main.c
  9.  * Author: francisco, juliana, pedro
  10.  *
  11.  * Created on 21 de Novembro de 2017, 9:17
  12.  */
  13.  
  14. #ifdef _WIN32
  15. #include <conio.h>
  16. #else
  17. #include <stdio.h>
  18. #define clrscr() printf("\e[1;1H\e[2J")
  19. #endif
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <time.h>
  23. #include <math.h>
  24. #include "LP_Leitura.h"
  25. #include "LP_Utils.h"
  26.  
  27. void showMenu();
  28. void setupPlayCPU();
  29. void setupPlayHuman();
  30. short checkOrder(char card[]);
  31. void switchCard(char player[][20], int cardPoints[10], int i, int j, int k);
  32. void arrangeHand(char player[][20]);
  33. short mustAssistConditions(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2], int playableCards[], int assistCount);
  34. short checkIfHasSuit(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]);
  35. short CPUFirstTurn(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]);
  36. short playCPUCard(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]);
  37. short playHumanCard(char player[][20], int playerNumber, char matchSettings[][2], int round, int firstTurnOfRound, char playedCards[]);
  38. void playTimeCPU(char deck[][2], char player[][20], char matchSettings[][2]);
  39. void playTimeHuman(char deck[][2], char player[][20], char matchSettings[][2]);
  40. void dealCards(char deck[][2], char player[][20], char matchSettings[][2]);
  41. void shuffle(int *array, size_t n);
  42. void buildDeck(char deck[][2]);
  43.  
  44. void showMenu() {
  45.     int option = 0;
  46.  
  47.     readInt(&option, 1, 5, "Introduza um número de 1 a 5\n"
  48.             "1- Jogar contra o CPU\n"
  49.             "2- Jogar localmente\n"
  50.             "3- Ver histórico de jogos\n"
  51.             "4- Créditos\n"
  52.             "5- Sair\n");
  53.  
  54.     if (option > 0 && option < 6) {
  55.         switch (option) {
  56.             case 1:
  57.                 setupPlayCPU();
  58.                 break;
  59.             case 2:
  60.                 setupPlayHuman();
  61.                 break;
  62.             case 5:
  63.                 exit(0);
  64.                 break;
  65.         }
  66.     }
  67. }
  68.  
  69. /* @matchSettings should look like:
  70.  * matchSettings[0] = number of games played
  71.  * matchSettings[1] = who's dealing cards
  72.  * matchSettings[2] = game's trump
  73.  */
  74. void setupPlayCPU() {
  75.     static char deck[40][2]; //Deck consisting of 20 cards, 2 chars each
  76.     char player[4][20]; // will receive values player[number of player][cards]
  77.     char matchSettings[3][2]; //Will store the number of games played, who's the one dealing cards and game's trump
  78.     int matchNumber, repeat = 1;
  79.  
  80.     while (repeat == 1) {
  81.         buildDeck(deck);
  82.         matchNumber = atoi(matchSettings[0]) + 1; //Increments number of the match
  83.         sprintf(matchSettings[0], "%d", matchNumber); //Stores it on matchSettings[0][0]
  84.  
  85.         dealCards(deck, player, matchSettings);
  86.  
  87.         arrangeHand(player); //Cards will be arranged descending, for each suit in order of
  88.  
  89.         playTimeCPU(deck, player, matchSettings);
  90.  
  91.         readInt(&repeat, 1, 2, "Deseja jogar novamente?\n1- Sim, 2- Não\n");
  92.     }
  93. }
  94.  
  95. void setupPlayHuman() {
  96.     static char deck[40][2]; //Deck consisting of 20 cards, 2 chars each
  97.     char player[4][20]; // will receive values player[number of player][cards]
  98.     char matchSettings[3][2]; //Will store the number of games played, who's the one dealing cards and game's trump
  99.     int matchNumber, repeat = 1;
  100.  
  101.     while (repeat == 1) {
  102.         buildDeck(deck);
  103.         matchNumber = atoi(matchSettings[0]) + 1; //Increments number of the match
  104.         sprintf(matchSettings[0], "%d", matchNumber); //Stores it on matchSettings[0][0]
  105.  
  106.         dealCards(deck, player, matchSettings);
  107.  
  108.         arrangeHand(player); //Cards will be arranged descending, for each suit in order of
  109.  
  110.         playTimeHuman(deck, player, matchSettings);
  111.  
  112.         readInt(&repeat, 1, 2, "Deseja jogar novamente?\n1- Sim, 2- Não\n");
  113.     }
  114. }
  115.  
  116. short checkOrder(char card[]) {
  117.     int value;
  118.  
  119.     switch (card[0]) {
  120.         case 'A':
  121.             value = 10;
  122.             break;
  123.         case '7':
  124.             value = 9;
  125.             break;
  126.         case 'K':
  127.             value = 8;
  128.             break;
  129.         case 'J':
  130.             value = 7;
  131.             break;
  132.         case 'Q':
  133.             value = 6;
  134.             break;
  135.         case '6':
  136.             value = 5;
  137.             break;
  138.         case '5':
  139.             value = 4;
  140.             break;
  141.         case '4':
  142.             value = 3;
  143.             break;
  144.         case '3':
  145.             value = 2;
  146.             break;
  147.         case '2':
  148.             value = 1;
  149.             break;
  150.     }
  151.     return value;
  152. }
  153.  
  154. double checkValue(char card[]) {
  155.     double value;
  156.  
  157.     switch (card[0]) {
  158.         case 'A':
  159.             value = 11;
  160.             break;
  161.         case '7':
  162.             value = 10;
  163.             break;
  164.         case 'K':
  165.             value = 4;
  166.             break;
  167.         case 'J':
  168.             value = 3;
  169.             break;
  170.         case 'Q':
  171.             value = 2;
  172.             break;
  173.         case '6':
  174.             value = 0.6; //For the propose of sorting cards with the value of 0
  175.             break;
  176.         case '5':
  177.             value = 0.5;
  178.             break;
  179.         case '4':
  180.             value = 0.4;
  181.             break;
  182.         case '3':
  183.             value = 0.3;
  184.             break;
  185.         case '2':
  186.             value = 0.2;
  187.             break;
  188.     }
  189.     return value;
  190. }
  191.  
  192. void switchCard(char player[][20], int cardPoints[10], int i, int j, int k) {
  193.     int m;
  194.     char n[2];
  195.  
  196.     n[0] = player[i][j];
  197.     n[1] = player[i][j + 1];
  198.     player[i][j] = player[i][k];
  199.     player[i][j + 1] = player[i][k + 1];
  200.     player[i][k] = n[0];
  201.     player[i][k + 1] = n[1];
  202.     m = cardPoints[j / 2];
  203.     cardPoints[j / 2] = cardPoints[k / 2];
  204.     cardPoints[k / 2] = m;
  205.  
  206. }
  207.  
  208. void arrangeHand(char player[][20]) {
  209.     int i, k, p, q, count, j;
  210.     int cardPoints[10];
  211.     char card[1];
  212.  
  213.     //For each player
  214.     for (i = 0; i < 4; i++) {
  215.         //Saves cards' points to @cardPoints array;
  216.         for (q = 0; q < 20; q++) {
  217.             card[0] = player[i][q];
  218.             cardPoints[q / 2] = checkOrder(card);
  219.             q++;
  220.         }
  221.  
  222.         j = 0; //Reset @j tu run the while cycle for the next player
  223.         while (j < 20) {
  224.             count = 0;
  225.             for (k = 0; k < 20; k++) {
  226.                 if (cardPoints[j / 2] < cardPoints[k / 2] && player[i][j + 1] == player[i][k + 1]) {
  227.                     //If it finds a card of the same suit with a highest value
  228.                     if (j <= k) {
  229.                         switchCard(player, cardPoints, i, j, k);
  230.                         count++;
  231.  
  232.                     } else {
  233.                         p = k + 2;
  234.                         q = 0;
  235.                         while (q == 0) {
  236.  
  237.                             if (player[i][j + 1] == player[i][p + 1]) {//If suits are the same
  238.  
  239.                                 if (cardPoints[j / 2] < cardPoints[p / 2]) { //If card p is higher than card j
  240.                                     p = p + 2;
  241.  
  242.                                 } else if (cardPoints[j / 2] == cardPoints[p / 2]) {
  243.                                     //
  244.                                     q = 1;
  245.                                 } else {
  246.                                     switchCard(player, cardPoints, i, j, p);
  247.                                     k++;
  248.                                     count++;
  249.                                     q = 1;
  250.                                 }
  251.                             } else {
  252.                                 switchCard(player, cardPoints, i, j, p);
  253.                                 k++;
  254.                                 count++;
  255.                                 q = 1;
  256.                             }
  257.                         }
  258.                     }
  259.                 }
  260.                 k++;
  261.             }
  262.             if (count > 0) {
  263.                 j = 0;
  264.  
  265.             } else {
  266.                 j = j + 2;
  267.  
  268.             }
  269.         }
  270.     }
  271. }
  272.  
  273. short mustAssistConditions(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2], int playableCards[], int assistCount) {
  274.     int i, j, trumpped = 0, cardFound = 0, suitableCard = 0, cardsOfSuitPlayed = 0, r;
  275.     char firstSuitOfThatRound[1];
  276.  
  277.     //Count how many cards of that suit have been played already
  278.     for (i = 1; i < round * 8; i += 2) {
  279.         if (playedCards[i] == playedCards[round * 8 + firstTurnOfRound[round] * 2 + 1]) {
  280.             cardsOfSuitPlayed++;
  281.         }
  282.     }
  283.  
  284.     //Check if they have Aces
  285.     for (i = 0; i < assistCount; i++) { //Searches for Aces in their hand
  286.         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
  287.             cardFound = playableCards[i];
  288.         }
  289.     }
  290.  
  291.     if (cardFound > 0) {
  292.  
  293.         for (j = 0; j <= round; j++) {
  294.             firstSuitOfThatRound[0] = playedCards[firstTurnOfRound[j] * j * 8 + 1];
  295.  
  296.             //If suit matches the ace he wants to play
  297.             if (firstSuitOfThatRound[0] == player[playerNumber][(cardFound - 1) * 2 + 1]) {
  298.                 //if any of the two opponents played a trump
  299.                 if (playedCards[((playerNumber - 1) % 4) * j * 2 + 1] == matchSettings[2][1] || playedCards[((playerNumber + 1) % 4) * j * 2 + 1] == matchSettings[2][1]) {
  300.                     trumpped++;
  301.                 }
  302.             }
  303.         }
  304.  
  305.         //Check if in previous plays the opposite team trumped
  306.         if (trumpped == 0) {
  307.             if (cardsOfSuitPlayed + assistCount < 7) {
  308.                 playableCards[0] = cardFound;
  309.                 suitableCard++;
  310.             }
  311.         }
  312.     }
  313.  
  314.     if (suitableCard < 1) {
  315.         r = (rand() % assistCount);
  316.         playableCards[0] = playableCards[r];
  317.     }
  318.  
  319.     return playableCards[0];
  320.  
  321. }
  322.  
  323. //CPU CONDITIONS CPU_Conditions
  324.  
  325. short checkIfHasSuit(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]) {
  326.     int i, assistCount = 0, trumpCount = 0, playableCards[10 - round], r;
  327.     char firstCard[2];
  328.  
  329.     firstCard[0] = playedCards[round * 8 + firstTurnOfRound[round] * 2];
  330.     firstCard[1] = playedCards[round * 8 + firstTurnOfRound[round] * 2 + 1];
  331.  
  332.     for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
  333.         if (player[playerNumber][2 * i + 1] == firstCard[1]) { //If suits matches
  334.             playableCards[assistCount] = i + 1;
  335.             assistCount++;
  336.         }
  337.     }
  338.  
  339.     if (assistCount > 0) { //Has cards to assit
  340.         if (assistCount > 1) {
  341.             playableCards[0] = mustAssistConditions(player, playerNumber, round, firstTurnOfRound, playedCards, matchSettings, playableCards, assistCount);
  342.         }
  343.     } else if (firstCard[1] != matchSettings[2][1]) {
  344.         for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
  345.             if (player[playerNumber][2 * i + 1] == matchSettings[2][1]) { //If suits matches
  346.                 playableCards[trumpCount] = i + 1;
  347.                 trumpCount++;
  348.             }
  349.         }
  350.  
  351.         if (trumpCount > 0) { //Didn't have cards to assist, so plays a trump card if he has it
  352.             if (trumpCount > 1) {
  353.                 r = (rand() % trumpCount);
  354.                 playableCards[0] = playableCards[r];
  355.             }
  356.         }
  357.  
  358.     }
  359.  
  360.     if (assistCount == 0 && trumpCount == 0) { //If playerNumber doesn't have cards to assit or trumps, he plays a random card
  361.         playableCards[0] = r = (rand() % (10 - round)) + 1;
  362.     }
  363.  
  364.     return playableCards[0];
  365.  
  366.  
  367. }
  368.  
  369. short CPUFirstTurn(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]) {
  370.     int i, j, aces = 0, k = 0, playableCards[10 - round], playableAces[4], trumpped, r;
  371.     char firstSuitOfThatRound[1];
  372.  
  373.     //Check if they have Aces
  374.     for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
  375.         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
  376.             playableAces[aces] = i + 1;
  377.             aces++;
  378.         }
  379.     }
  380.  
  381.     if (aces > 0) {
  382.         //Check if the suit of the ace has been played before and someone of the other team trumped over it
  383.         for (i = 0; i < aces; i++) { //Check for each Ace's suit
  384.             trumpped = 0;
  385.             for (j = 0; j < round; j++) {
  386.                 firstSuitOfThatRound[0] = playedCards[firstTurnOfRound[j] * j * 8 + 1];
  387.  
  388.                 //If suit matches the ace he wants to play
  389.                 if (firstSuitOfThatRound[0] == player[playerNumber][2 * playableAces[i] + 1]) {
  390.                     if (playedCards[((playerNumber - 1) % 4) * j * 2 + 1] == matchSettings[2][1] || playedCards[((playerNumber + 1) % 4) * j * 2 + 1] == matchSettings[2][1]) {
  391.                         trumpped++;
  392.                     }
  393.                 }
  394.             }
  395.             //Check if in previous plays the opposite team trumped
  396.             if (trumpped == 0) {
  397.                 playableCards[k] = playableAces[i];
  398.                 k++;
  399.             }
  400.         }
  401.  
  402.         if (k > 0) {
  403.             //If there's at least one playableAce
  404.             if (k > 1) { //Randomize the list of aces
  405.                 r = (rand() % k);
  406.                 playableCards[0] = playableCards[r];
  407.             }
  408.         } else {
  409.             //If he deson't have a playable ace, he plays a random card
  410.             playableCards[0] = 1 + rand() % (10 - round);
  411.         }
  412.  
  413.     } else {
  414.         playableCards[0] = 1 + rand() % (10 - round); //The player has no Aces
  415.     }
  416.  
  417.     return playableCards[0];
  418. }
  419.  
  420. short playCPUCard(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]) {
  421.     int cardPlayed;
  422.  
  423.     if (firstTurnOfRound[round] == playerNumber) {
  424.         cardPlayed = CPUFirstTurn(player, playerNumber, round, firstTurnOfRound, playedCards, matchSettings);
  425.     } else {
  426.         cardPlayed = checkIfHasSuit(player, playerNumber, round, firstTurnOfRound, playedCards, matchSettings);
  427.     }
  428.     return cardPlayed;
  429. }
  430.  
  431. short playHumanCard(char player[][20], int playerNumber, char matchSettings[][2], int round, int firstTurnOfRound, char playedCards[]) {
  432.     int i, j = 1, cardPlayed, assistCount, checkIfValidPlay = 0, playableCards[10 - round];
  433.     char firstCard[2];
  434.     firstCard[0] = playedCards[round * 8 + firstTurnOfRound * 2];
  435.     firstCard[1] = playedCards[round * 8 + firstTurnOfRound * 2 + 1];
  436.     printf("Trunfo: %c%c\n", matchSettings[2][0], matchSettings[2][1]);
  437.     printf("As suas cartas:\n");
  438.     for (i = 0; i < 20 - round * 2; i += 2) {
  439.         if (player[playerNumber][i + 1] == firstCard[1]) {
  440.             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
  441.             assistCount++;
  442.             playableCards[assistCount] = i / 2 + 1;
  443.         } else if (player[playerNumber][i + 1] == matchSettings[2][1]) {
  444.             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
  445.         } else {
  446.             printf("%d- %c%c\n", j, player[playerNumber][i], player[playerNumber][i + 1]); //Doesn't paint other cards
  447.         }
  448.         j++;
  449.     }
  450.     readInt(&cardPlayed, 1, 10 - round, "Escolha uma carta para jogar\n");
  451.     if (firstTurnOfRound != 0) {
  452.         while (checkIfValidPlay < 1) {
  453.             assistCount = 0; //Resets assist count
  454.             for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
  455.                 if (player[playerNumber][2 * i + 1] == firstCard[1]) { //If suits matches
  456.                     playableCards[assistCount] = i + 1;
  457.                     assistCount++;
  458.                 }
  459.             }
  460.  
  461.             if (assistCount > 0) {
  462.                 for (i = 0; i < assistCount; i++)
  463.                     if (cardPlayed == playableCards[i]) {
  464.                         checkIfValidPlay++;
  465.                     }
  466.             } else {
  467.                 checkIfValidPlay = 1;
  468.             }
  469.             if (checkIfValidPlay < 1) {
  470.                 readInt(&cardPlayed, 1, 10 - round, "Tem cartas para assistir, escolha outra carta para jogar\n");
  471.             }
  472.         }
  473.     }
  474.  
  475.     return cardPlayed;
  476. }
  477.  
  478. short roundWinner(char playedCards[], char matchSettings[][2], int round, int firstTurnOfRound[]) {
  479.  
  480.     char roundSuit[1];
  481.     int position, i, winner, highestCard = 0;
  482.     char card[2], prevHighestCard[2];
  483.     double value;
  484.  
  485.     for (i = 1; i < 8; i = i + 2) { //Gets the highest trump played
  486.         card[0] = playedCards[round * 8 + i - 1];
  487.         card[1] = playedCards[round * 8 + i];
  488.         if (playedCards[round * 8 + i] == matchSettings[2][1]) {
  489.  
  490.             if (highestCard == 0) { //Store the first trump he founds to start the comparation
  491.                 highestCard = i;
  492.             } else {
  493.                 value = checkValue(card); //Gets the value of the card
  494.                 prevHighestCard[0] = playedCards[round * 8 + highestCard - 1]; //Previous highest trump's face
  495.                 prevHighestCard[1] = playedCards[round * 8 + highestCard]; //Previous highest trump's suit
  496.                 if (value > checkValue(prevHighestCard)) {
  497.                     highestCard = i;
  498.                 }
  499.             }
  500.         }
  501.     }
  502.  
  503.     if (highestCard == 0) { //If there was no trumps, checks for the highest card of the round's suit
  504.  
  505.         position = round * 8 + firstTurnOfRound[round] *2 + 1;
  506.         roundSuit[0] = playedCards[position];
  507.  
  508.         for (i = 1; i < 8; i = i + 2) { //Gets the highest card played
  509.             card[0] = playedCards[round * 8 + i - 1];
  510.             card[1] = playedCards[round * 8 + i];
  511.             if (playedCards[round * 8 + i] == roundSuit[0]) {
  512.                 if (highestCard == 0) { //Store the first card he founds to start the comparation
  513.                     highestCard = i;
  514.                 } else {
  515.                     value = checkValue(card); //Gets the value of the card
  516.                     prevHighestCard[0] = playedCards[round * 8 + highestCard - 1]; //Previous highest card's face
  517.                     prevHighestCard[1] = playedCards[round * 8 + highestCard]; //Previous highest card's suit
  518.                     if (value > checkValue(prevHighestCard)) {
  519.                         highestCard = i;
  520.                     }
  521.                 }
  522.             }
  523.         }
  524.  
  525.     }
  526.     winner = (highestCard - 1) / 2;
  527.     printf("Quem ganhou a vazada foi o jogador %d\n\n", (highestCard - 1) / 2);
  528.     return (highestCard - 1) / 2;
  529.  
  530. }
  531.  
  532. short countPoints(char playedCards[], int teamPoints[], int firstTurnOfRound[]) {
  533.  
  534.     int i, j, points;
  535.     char card[1];
  536.  
  537.     for (i = 0; i < 10; i++) {
  538.         points = 0;
  539.         for (j = 0; j < 8; j += 2) {
  540.             card[0] = playedCards[i * 8 + j];
  541.             points += floor(checkValue(card));
  542.         }
  543.         if (firstTurnOfRound[i + 1] % 2 == 0) {
  544.  
  545.             teamPoints[0] += points;
  546.         } else {
  547.             teamPoints[1] += points;
  548.         }
  549.     }
  550.  
  551. }
  552.  
  553. void playTimeCPU(char deck[][2], char player[][20], char matchSettings[][2]) {
  554.  
  555.     int round, g, h, i, j, k, cardPlayed, idxToDel, firstTurnOfRound[11], playerTurn, position, teamPoints[2] = {0, 0};
  556.     char playedCards[80], hand[20];
  557.  
  558.     firstTurnOfRound[0] = matchSettings[0][0] % 4; //On the first round, the first player playing is the one next to the one that dealt
  559.  
  560.     clrscr(); //Clear the screen
  561.     //Player played cards
  562.     for (round = 0; round < 10; round++) {
  563.         for (i = firstTurnOfRound[round]; i <= firstTurnOfRound[round] + 3; i++) {
  564.             playerTurn = (i % 4 + 4) % 4;
  565.  
  566.             if (playerTurn == 0) {//If it's the human's turn, call the function playHumanCard
  567.  
  568.                 for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
  569.                     if (j == firstTurnOfRound[round]) {
  570.                         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]);
  571.                     } else {
  572.                         printf("A carta jogada pelo jogador %d foi %c%c\n", j, playedCards[round * 8 + j * 2], playedCards[round * 8 + j * 2 + 1]);
  573.                     }
  574.                 }
  575.  
  576.                 cardPlayed = playHumanCard(player, playerTurn, matchSettings, round, firstTurnOfRound[round], playedCards);
  577.  
  578.                 clrscr(); //Clear the screen
  579.  
  580.             } else { //Select the card to be played by CPU
  581.                 if (round < 9) {
  582.                     cardPlayed = playCPUCard(player, playerTurn, round, firstTurnOfRound, playedCards, matchSettings);
  583.                 } else {
  584.                     cardPlayed = 1;
  585.                 }
  586.             }
  587.  
  588.             //Remove the card from the hand
  589.             idxToDel = cardPlayed * 2 - 2; //Get the face of the card to remove
  590.             position = round * 8 + playerTurn * 2; //
  591.             playedCards[position] = player[playerTurn][idxToDel];
  592.             playedCards[position + 1] = player[playerTurn][idxToDel + 1];
  593.  
  594.             k = 0; //Resets k
  595.             for (h = 0; h < 10; h++) {
  596.                 if (h != cardPlayed - 1) {
  597.                     hand[k] = player[playerTurn][h * 2];
  598.                     k++;
  599.                     hand[k] = player[playerTurn][h * 2 + 1];
  600.                     k++;
  601.                 }
  602.             }
  603.             hand[18] = '0';
  604.             hand[19] = '0';
  605.  
  606.             strncpy(player[playerTurn], hand, 20);
  607.  
  608.         }
  609.  
  610.         clrscr(); //Clear the screen
  611.         //End of round info
  612.         for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
  613.             g = (j % 4 + 4) % 4;
  614.             if (g == firstTurnOfRound[round]) {
  615.                 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]);
  616.             } else {
  617.                 printf("A carta jogada pelo jogador %d foi %c%c\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
  618.             }
  619.         }
  620.  
  621.         firstTurnOfRound[round + 1] = roundWinner(playedCards, matchSettings, round, firstTurnOfRound);
  622.     }
  623.  
  624.     countPoints(playedCards, teamPoints, firstTurnOfRound);
  625.  
  626.     printf("Pontos da primeira Equipa: %d\n", teamPoints[0]);
  627.     printf("Pontos da segunda Equipa: %d\n", teamPoints[1]);
  628.  
  629.  
  630.  
  631.  
  632. }
  633.  
  634. void playTimeHuman(char deck[][2], char player[][20], char matchSettings[][2]) {
  635.  
  636.     int round, g, h, i, j, k, cardPlayed, idxToDel, firstTurnOfRound[11], playerTurn, position, teamPoints[2] = {0, 0};
  637.     char playedCards[80], hand[20];
  638.  
  639.     firstTurnOfRound[0] = matchSettings[0][0] % 4; //On the first round, the first player playing is the one next to the one that dealt
  640.  
  641.     clrscr(); //Clear the screen
  642.     //Player played cards
  643.     for (round = 0; round < 10; round++) {
  644.         for (i = firstTurnOfRound[round]; i <= firstTurnOfRound[round] + 3; i++) {
  645.             playerTurn = (i % 4 + 4) % 4;
  646.  
  647.             //            clrscr(); //Clear the screen
  648.             printf("Clique em qualquer tecla para o jogador %d jogar\n", playerTurn);
  649.             getchar();
  650.             clrscr(); //Clear the screen
  651.  
  652.             for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
  653.                 if (j == firstTurnOfRound[round]) {
  654.                     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]);
  655.                 } else {
  656.                     printf("A carta jogada pelo jogador %d foi %c%c\n", j, playedCards[round * 8 + j * 2], playedCards[round * 8 + j * 2 + 1]);
  657.                 }
  658.             }
  659.             cardPlayed = playHumanCard(player, playerTurn, matchSettings, round, firstTurnOfRound[round], playedCards);
  660.  
  661.             clrscr(); //Clear the screen
  662.  
  663.             //Remove the card from the hand
  664.             idxToDel = cardPlayed * 2 - 2; //Get the face of the card to remove
  665.             position = round * 8 + playerTurn * 2; //
  666.             playedCards[position] = player[playerTurn][idxToDel];
  667.             playedCards[position + 1] = player[playerTurn][idxToDel + 1];
  668.  
  669.             k = 0; //Resets k
  670.             for (h = 0; h < 10; h++) {
  671.                 if (h != cardPlayed - 1) {
  672.                     hand[k] = player[playerTurn][h * 2];
  673.                     k++;
  674.                     hand[k] = player[playerTurn][h * 2 + 1];
  675.                     k++;
  676.                 }
  677.             }
  678.             hand[18] = '0';
  679.             hand[19] = '0';
  680.  
  681.             strncpy(player[playerTurn], hand, 20);
  682.         }
  683.  
  684.         clrscr(); //Clear the screen
  685.         //End of round info
  686.         for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
  687.             g = (j % 4 + 4) % 4;
  688.             if (g == firstTurnOfRound[round]) {
  689.                 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]);
  690.             } else {
  691.                 printf("A carta jogada pelo jogador %d foi %c%c\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
  692.             }
  693.         }
  694.  
  695.         firstTurnOfRound[round + 1] = roundWinner(playedCards, matchSettings, round, firstTurnOfRound);
  696.  
  697.     }
  698.  
  699.     countPoints(playedCards, teamPoints, firstTurnOfRound);
  700.  
  701.     printf("Pontos da primeira Equipa: %d\n", teamPoints[0]);
  702.     printf("Pontos da segunda Equipa: %d\n", teamPoints[1]);
  703.  
  704.  
  705.  
  706.  
  707. }
  708.  
  709. void dealCards(char deck[][2], char player[][20], char matchSettings[][2]) {
  710.     int option = 0, i, j = 0, k = 0, playerDealingCards, m;
  711.     playerDealingCards = (atoi(matchSettings[0]) - 1) % 4;
  712.     if (playerDealingCards == 0) {
  713.         readInt(&option, 1, 2, "Deseja tirar o trunfo por \n1- cima, ou 2- baixo?\n");
  714.     } else {
  715.         option = rand() % 2 + 1; //Generate a random option between 1 and 2
  716.     }
  717.  
  718.     if (option == 1) { //In case he chose to deal the trump from the top...
  719.         //    Deal cards to each player
  720.         //    k = position of the card, by the end of the external for's iteration, k will be 40
  721.         printf("O trunfo foi tirado por cima\n");
  722.         for (i = playerDealingCards; i >= playerDealingCards - 3; i--) {//Deal cards for the 4 players
  723.             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)
  724.                 m = (i % 4 + 4) % 4; //Get the positive module of players
  725.                 player[m][j] = deck[k][0];
  726.                 j++;
  727.                 player[m][j] = deck[k][1];
  728.                 k++;
  729.             }
  730.         }
  731.  
  732.         //Declaring game's trump
  733.         matchSettings[2][0] = player[playerDealingCards][0]; //Because he dealt the trump from the top, the trump is the first card he dealt for him.
  734.         matchSettings[2][1] = player[playerDealingCards][1];
  735.  
  736.     } else {
  737.         printf("O trunfo foi tirado por baixo\n");
  738.         //    Deal cards to each player
  739.         //    k = position of the card, by the end of the external for's iteration, k will be 40
  740.  
  741.         for (i = playerDealingCards - 3; i <= playerDealingCards; i++) {//for each player, starts on player 0, then goes 3, 2 and finally 1
  742.             for (j = 0; j < 20; j++) { //each 20 chars = 10 cards composed by a face then a suit; i.e: 6D (6 of Diamonds)
  743.                 m = (i % 4 + 4) % 4; //Get the positive module of players
  744.                 player[m][j] = deck[k][0];
  745.                 j++;
  746.                 player[m][j] = deck[k][1];
  747.                 k++;
  748.             }
  749.         }
  750.  
  751.         //Declaring game's trump
  752.         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
  753.         matchSettings[2][1] = player[playerDealingCards][19];
  754.  
  755.     }
  756.  
  757.  
  758.     // Mostra a mão de cada um
  759.     //    printf("\nO trunfo é: %c%c\n", matchSettings[2][0], matchSettings[2][1]);
  760.     //
  761.     //        Printa as cartas de cada jogador
  762.     //    for (j = 0; j < 4; j++) {
  763.     //        printf("Player %d\n", j);
  764.     //        for (i = 0; i < 20; i++) {
  765.     //            printf("%c", player[j][i]);
  766.     //            if (i % 2 != 0) {
  767.     //                printf("\n");
  768.     //            }
  769.     //        }
  770.     //        printf("\n");
  771.     //    }
  772.  
  773.  
  774. }
  775.  
  776. // Shuffle an array, passing 2 arguments, the array and its size
  777.  
  778. void shuffle(int *array, size_t n) {
  779.     srand(time(NULL)); //New seed for rand based on current time
  780.     if (n > 1) {
  781.         size_t i;
  782.         for (i = 0; i < n; i++) {
  783.  
  784.             size_t j = rand() % (n);
  785.             int t = array[j];
  786.             array[j] = array[i];
  787.             array[i] = t;
  788.         }
  789.     }
  790. }
  791.  
  792. //Builds deck
  793.  
  794. void buildDeck(char deck[][2]) {
  795.  
  796.  
  797.     int i, j, randomList[40];
  798.     int k = 0;
  799.  
  800.     char suits[4] = {'H', 'S', 'D', 'C'}; //Hearts, Spaces, Diamonds, Clubs;
  801.     char faces[10] = {'A', '2', '3', '4', '5', '6', '7', 'J', 'Q', 'K'};
  802.  
  803.  
  804.     for (i = 0; i < 40; i++) { //Generate a list of 40 numbers ascending from 0 to 39
  805.         randomList[i] = i;
  806.     }
  807.  
  808.     shuffle(randomList, 40); //Shuffle the list
  809.  
  810.     /*
  811.      * Deck Builder
  812.      * Every number randomized earlier will now assume the value of a card, this way the deck is shuffled
  813.      * Model: Deck[position][face + suit]
  814.      * Position will be taken from the randomized list, and face+suit from the previously declared variables
  815.      * Instead of having [0] = AH; [1] = 2H; [2] = 3H, it will not get random numbers for its position
  816.      * Example: [27] = AC; [15] = 2H; [32] = 3H etc. The whole deck is shuffled.
  817.      */
  818.     for (i = 0; i < 4; i++) { //Suits
  819.         for (j = 0; j < 10; j++) { //Faces
  820.  
  821.             deck[randomList[k]][0] = faces[j]; //Stores faces in shuffleDeck
  822.             deck[randomList[k]][1] = suits[i]; //A shuffledDeck stored value will look like 6D (Six of Diamonds, 6♦)
  823.             k++;
  824.         }
  825.     }
  826.  
  827. }
  828.  
  829. int main(int argc, char** argv) {
  830.  
  831.  
  832.     showMenu();
  833.  
  834.  
  835.     return (EXIT_SUCCESS);
  836. }
Add Comment
Please, Sign In to add comment