xickoh

human

Jan 7th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.88 KB | None | 0 0
  1. #ifdef _WIN32
  2. #include <conio.h>
  3. #else
  4. #include <stdio.h>
  5. #define clrscr() printf("\e[1;1H\e[2J")
  6. #endif
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <malloc.h>
  10. #include <time.h>
  11. #include <math.h>
  12. #include "LP_Leitura.h"
  13. #include "LP_Utils.h"
  14. #include "common.h"
  15. #include "cpu.h"
  16. #include "credits.h"
  17. #include "history.h"
  18. #include "human.h"
  19.  
  20. /*
  21. * To change this license header, choose License Headers in Project Properties.
  22. * To change this template file, choose Tools | Templates
  23. * and open the template in the editor.
  24. */
  25.  
  26. void playTimeHuman(char deck[][2], char player[][20], char matchSettings[][2]) {
  27.  
  28. int round, g, h, i, j, k, cardPlayed, idxToDel, firstTurnOfRound[11], playerTurn, position, teamPoints[2] = {0, 0}, gamesNumber, maxSize;
  29. char playedCards[80], hand[20], gameType[STR_SIZE] = "local";
  30. gamesHistory *game;
  31.  
  32. firstTurnOfRound[0] = matchSettings[0][0] % 4; //On the first round, the first player playing is the one next to the one that dealt
  33.  
  34. clrscr(); //Clear the screen
  35. //Player played cards
  36. for (round = 0; round < 10; round++) {
  37. for (i = firstTurnOfRound[round]; i <= firstTurnOfRound[round] + 3; i++) {
  38. playerTurn = (i % 4 + 4) % 4;
  39.  
  40. // clrscr(); //Clear the screen
  41. printf("Clique em qualquer tecla para o jogador %d jogar\n", playerTurn);
  42. getchar();
  43. clrscr(); //Clear the screen
  44.  
  45. for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
  46. g = (j % 4 + 4) % 4;
  47. if (g == firstTurnOfRound[round]) {
  48. 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]);
  49. } else {
  50. printf("A carta jogada pelo jogador %d foi %c%c\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
  51. }
  52. }
  53. cardPlayed = playHumanCard(player, playerTurn, matchSettings, round, firstTurnOfRound[round], playedCards);
  54.  
  55. clrscr(); //Clear the screen
  56.  
  57. //Remove the card from the hand
  58. idxToDel = cardPlayed * 2 - 2; //Get the face of the card to remove
  59. position = round * 8 + playerTurn * 2; //
  60. playedCards[position] = player[playerTurn][idxToDel];
  61. playedCards[position + 1] = player[playerTurn][idxToDel + 1];
  62.  
  63. k = 0; //Resets k
  64. for (h = 0; h < 10; h++) {
  65. if (h != cardPlayed - 1) {
  66. hand[k] = player[playerTurn][h * 2];
  67. k++;
  68. hand[k] = player[playerTurn][h * 2 + 1];
  69. k++;
  70. }
  71. }
  72. hand[18] = '0';
  73. hand[19] = '0';
  74.  
  75. strncpy(player[playerTurn], hand, 20);
  76. }
  77.  
  78. clrscr(); //Clear the screen
  79. //End of round info
  80. for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
  81. g = (j % 4 + 4) % 4;
  82. if (g == firstTurnOfRound[round]) {
  83. //Print blue for the first card of that round
  84. printf("A carta jogada pelo jogador %d foi \033[22;36m%c%c\033[0m\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
  85. } else if (playedCards[round * 8 + g * 2 + 1] == matchSettings[2][1]) {
  86. //Print red if they play a trump card
  87. printf("A carta jogada pelo jogador %d foi \033[22;31m%c%c\033[0m\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
  88. } else if (playedCards[round * 8 + g * 2 + 1] == playedCards[firstTurnOfRound[round]*2 + round * 8 + 1]) {
  89. //Print green if they assist
  90. 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]);
  91. } else {
  92. printf("A carta jogada pelo jogador %d foi %c%c\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
  93. }
  94. }
  95.  
  96. firstTurnOfRound[round + 1] = roundWinner(playedCards, matchSettings, round, firstTurnOfRound);
  97.  
  98. }
  99.  
  100. countPoints(playedCards, teamPoints, firstTurnOfRound);
  101.  
  102. printf("Pontos da primeira Equipa: %d\n", teamPoints[0]);
  103. printf("Pontos da segunda Equipa: %d\n", teamPoints[1]);
  104.  
  105. readData(&game, &gamesNumber, &maxSize); //Read the games data
  106. addHistory(&game, &gamesNumber, &maxSize, matchSettings, teamPoints, gameType); //Processes data
  107. // listGamesHistory(game, gamesNumber);
  108. saveOnFile(game, gamesNumber); //Writes it on file
  109. if (game != NULL) {
  110. free(game);
  111. }
  112.  
  113.  
  114. }
  115.  
  116. void setupPlayHuman() {
  117. static char deck[40][2]; //Deck consisting of 20 cards, 2 chars each
  118. char player[4][20]; // will receive values player[number of player][cards]
  119. char matchSettings[3][2]; //Will store the number of games played, who's the one dealing cards and game's trump
  120. int matchNumber, repeat = 1;
  121.  
  122. while (repeat == 1) {
  123. buildDeck(deck);
  124. matchNumber = atoi(matchSettings[0]) + 1; //Increments number of the match
  125. sprintf(matchSettings[0], "%d", matchNumber); //Stores it on matchSettings[0][0]
  126.  
  127. dealCards(deck, player, matchSettings);
  128.  
  129. arrangeHand(player); //Cards will be arranged descending, for each suit in order of
  130.  
  131. playTimeHuman(deck, player, matchSettings);
  132.  
  133. readInt(&repeat, 1, 2, "Deseja jogar novamente?\n1- Sim, 2- Não\n");
  134. }
  135. }
  136.  
  137. short playHumanCard(char player[][20], int playerNumber, char matchSettings[][2], int round, int firstTurnOfRound, char playedCards[]) {
  138. int i, j = 1, cardPlayed, assistCount, checkIfValidPlay = 0, playableCards[10 - round];
  139. char firstCard[2];
  140. firstCard[0] = playedCards[round * 8 + firstTurnOfRound * 2];
  141. firstCard[1] = playedCards[round * 8 + firstTurnOfRound * 2 + 1];
  142. printf("Trunfo: %c%c\n", matchSettings[2][0], matchSettings[2][1]);
  143. printf("As suas cartas:\n");
  144. for (i = 0; i < 20 - round * 2; i += 2) {
  145. if (player[playerNumber][i + 1] == firstCard[1]) {
  146. 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
  147. assistCount++;
  148. playableCards[assistCount] = i / 2 + 1;
  149. } else if (player[playerNumber][i + 1] == matchSettings[2][1]) {
  150. 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
  151. } else {
  152. printf("%d- %c%c\n", j, player[playerNumber][i], player[playerNumber][i + 1]); //Doesn't paint other cards
  153. }
  154. j++;
  155. }
  156. readInt(&cardPlayed, 1, 10 - round, "Escolha uma carta para jogar\n");
  157.  
  158.  
  159. if (firstTurnOfRound != 0) {
  160. while (checkIfValidPlay < 1) {
  161. assistCount = 0; //Resets assist count
  162. for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
  163. if (player[playerNumber][2 * i + 1] == firstCard[1]) { //If suits matches
  164. playableCards[assistCount] = i + 1;
  165. assistCount++;
  166. }
  167. }
  168.  
  169. if (assistCount > 0) { //If player assisted
  170. for (i = 0; i < assistCount; i++)
  171. if (cardPlayed == playableCards[i]) {
  172. checkIfValidPlay++; //It is a valid play
  173. }
  174. } else {
  175. checkIfValidPlay = 1;
  176. }
  177. if (checkIfValidPlay < 1) {
  178. readInt(&cardPlayed, 1, 10 - round, "Tem cartas para assistir, escolha outra carta para jogar\n");
  179. }
  180. }
  181. }
  182.  
  183. return cardPlayed;
  184. }
Add Comment
Please, Sign In to add comment