xickoh

common

Jan 7th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.90 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. #ifdef _WIN32
  8. #include <conio.h>
  9. #else
  10. #include <stdio.h>
  11. #define clrscr() printf("\e[1;1H\e[2J")
  12. #endif
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <malloc.h>
  16. #include <time.h>
  17. #include <math.h>
  18. #include "LP_Leitura.h"
  19. #include "LP_Utils.h"
  20. #include "cpu.h"
  21. #include "credits.h"
  22. #include "history.h"
  23. #include "human.h"
  24.  
  25. short checkOrder(char card[]) {
  26. int value;
  27.  
  28. switch (card[0]) {
  29. case 'A':
  30. value = 10;
  31. break;
  32. case '7':
  33. value = 9;
  34. break;
  35. case 'K':
  36. value = 8;
  37. break;
  38. case 'J':
  39. value = 7;
  40. break;
  41. case 'Q':
  42. value = 6;
  43. break;
  44. case '6':
  45. value = 5;
  46. break;
  47. case '5':
  48. value = 4;
  49. break;
  50. case '4':
  51. value = 3;
  52. break;
  53. case '3':
  54. value = 2;
  55. break;
  56. case '2':
  57. value = 1;
  58. break;
  59. }
  60. return value;
  61. }
  62.  
  63. double checkValue(char card[]) {
  64. double value;
  65.  
  66. switch (card[0]) {
  67. case 'A':
  68. value = 11;
  69. break;
  70. case '7':
  71. value = 10;
  72. break;
  73. case 'K':
  74. value = 4;
  75. break;
  76. case 'J':
  77. value = 3;
  78. break;
  79. case 'Q':
  80. value = 2;
  81. break;
  82. case '6':
  83. value = 0.6; //For the propose of sorting cards with the value of 0
  84. break;
  85. case '5':
  86. value = 0.5;
  87. break;
  88. case '4':
  89. value = 0.4;
  90. break;
  91. case '3':
  92. value = 0.3;
  93. break;
  94. case '2':
  95. value = 0.2;
  96. break;
  97. }
  98. return value;
  99. }
  100.  
  101. void switchCard(char player[][20], int cardPoints[10], int i, int j, int k) {
  102. int m;
  103. char n[2];
  104.  
  105. n[0] = player[i][j];
  106. n[1] = player[i][j + 1];
  107. player[i][j] = player[i][k];
  108. player[i][j + 1] = player[i][k + 1];
  109. player[i][k] = n[0];
  110. player[i][k + 1] = n[1];
  111. m = cardPoints[j / 2];
  112. cardPoints[j / 2] = cardPoints[k / 2];
  113. cardPoints[k / 2] = m;
  114.  
  115. }
  116.  
  117. void arrangeHand(char player[][20]) {
  118. int i, k, p, q, count, j;
  119. int cardPoints[10];
  120. char card[1];
  121.  
  122. //For each player
  123. for (i = 0; i < 4; i++) {
  124. //Saves cards' points to @cardPoints array;
  125. for (q = 0; q < 20; q++) {
  126. card[0] = player[i][q];
  127. cardPoints[q / 2] = checkOrder(card);
  128. q++;
  129. }
  130.  
  131. j = 0; //Reset @j tu run the while cycle for the next player
  132. while (j < 20) {
  133. count = 0;
  134. for (k = 0; k < 20; k++) {
  135. if (cardPoints[j / 2] < cardPoints[k / 2] && player[i][j + 1] == player[i][k + 1]) {
  136. //If it finds a card of the same suit with a highest value
  137. if (j <= k) {
  138. switchCard(player, cardPoints, i, j, k);
  139. count++;
  140.  
  141. } else {
  142. p = k + 2;
  143. q = 0;
  144. while (q == 0) {
  145.  
  146. if (player[i][j + 1] == player[i][p + 1]) {//If suits are the same
  147.  
  148. if (cardPoints[j / 2] < cardPoints[p / 2]) { //If card p is higher than card j
  149. p = p + 2;
  150.  
  151. } else if (cardPoints[j / 2] == cardPoints[p / 2]) {
  152. //
  153. q = 1;
  154. } else {
  155. switchCard(player, cardPoints, i, j, p);
  156. k++;
  157. count++;
  158. q = 1;
  159. }
  160. } else {
  161. switchCard(player, cardPoints, i, j, p);
  162. k++;
  163. count++;
  164. q = 1;
  165. }
  166. }
  167. }
  168. }
  169. k++;
  170. }
  171. if (count > 0) {
  172. j = 0;
  173.  
  174. } else {
  175. j = j + 2;
  176.  
  177. }
  178. }
  179. }
  180. }
  181.  
  182.  
  183. void dealCards(char deck[][2], char player[][20], char matchSettings[][2]) {
  184. int option = 0, i, j = 0, k = 0, playerDealingCards, m;
  185. playerDealingCards = (atoi(matchSettings[0]) - 1) % 4;
  186. if (playerDealingCards == 0) {
  187. readInt(&option, 1, 2, "Deseja tirar o trunfo por \n1- cima, ou 2- baixo?\n");
  188. } else {
  189. option = rand() % 2 + 1; //Generate a random option between 1 and 2
  190. }
  191.  
  192. if (option == 1) { //In case he chose to deal the trump from the top...
  193. // Deal cards to each player
  194. // k = position of the card, by the end of the external for's iteration, k will be 40
  195. printf("O trunfo foi tirado por cima\n");
  196. for (i = playerDealingCards; i >= playerDealingCards - 3; i--) {//Deal cards for the 4 players
  197. 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)
  198. m = (i % 4 + 4) % 4; //Get the positive module of players
  199. player[m][j] = deck[k][0];
  200. j++;
  201. player[m][j] = deck[k][1];
  202. k++;
  203. }
  204. }
  205.  
  206. //Declaring game's trump
  207. matchSettings[2][0] = player[playerDealingCards][0]; //Because he dealt the trump from the top, the trump is the first card he dealt for him.
  208. matchSettings[2][1] = player[playerDealingCards][1];
  209.  
  210. } else {
  211. printf("O trunfo foi tirado por baixo\n");
  212. // Deal cards to each player
  213. // k = position of the card, by the end of the external for's iteration, k will be 40
  214.  
  215. for (i = playerDealingCards - 3; i <= playerDealingCards; i++) {//for each player, starts on player 0, then goes 3, 2 and finally 1
  216. for (j = 0; j < 20; j++) { //each 20 chars = 10 cards composed by a face then a suit; i.e: 6D (6 of Diamonds)
  217. m = (i % 4 + 4) % 4; //Get the positive module of players
  218. player[m][j] = deck[k][0];
  219. j++;
  220. player[m][j] = deck[k][1];
  221. k++;
  222. }
  223. }
  224.  
  225. //Declaring game's trump
  226. 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
  227. matchSettings[2][1] = player[playerDealingCards][19];
  228.  
  229. }
  230.  
  231.  
  232. // Mostra a mão de cada um
  233. // printf("\nO trunfo é: %c%c\n", matchSettings[2][0], matchSettings[2][1]);
  234. //
  235. // Printa as cartas de cada jogador
  236. // for (j = 0; j < 4; j++) {
  237. // printf("Player %d\n", j);
  238. // for (i = 0; i < 20; i++) {
  239. // printf("%c", player[j][i]);
  240. // if (i % 2 != 0) {
  241. // printf("\n");
  242. // }
  243. // }
  244. // printf("\n");
  245. // }
  246.  
  247.  
  248. }
  249.  
  250. // Shuffle an array, passing 2 arguments, the array and its size
  251.  
  252. void shuffle(int *array, size_t n) {
  253. srand(time(NULL)); //New seed for rand based on current time
  254. if (n > 1) {
  255. size_t i;
  256. for (i = 0; i < n; i++) {
  257.  
  258. size_t j = rand() % (n);
  259. int t = array[j];
  260. array[j] = array[i];
  261. array[i] = t;
  262. }
  263. }
  264. }
  265.  
  266. //Builds deck
  267.  
  268. void buildDeck(char deck[][2]) {
  269.  
  270.  
  271. int i, j, randomList[40];
  272. int k = 0;
  273.  
  274. char suits[4] = {'H', 'S', 'D', 'C'}; //Hearts, Spaces, Diamonds, Clubs;
  275. char faces[10] = {'A', '2', '3', '4', '5', '6', '7', 'J', 'Q', 'K'};
  276.  
  277.  
  278. for (i = 0; i < 40; i++) { //Generate a list of 40 numbers ascending from 0 to 39
  279. randomList[i] = i;
  280. }
  281.  
  282. shuffle(randomList, 40); //Shuffle the list
  283.  
  284. /*
  285. * Deck Builder
  286. * Every number randomized earlier will now assume the value of a card, this way the deck is shuffled
  287. * Model: Deck[position][face + suit]
  288. * Position will be taken from the randomized list, and face+suit from the previously declared variables
  289. * Instead of having [0] = AH; [1] = 2H; [2] = 3H, it will not get random numbers for its position
  290. * Example: [27] = AC; [15] = 2H; [32] = 3H etc. The whole deck is shuffled.
  291. */
  292. for (i = 0; i < 4; i++) { //Suits
  293. for (j = 0; j < 10; j++) { //Faces
  294.  
  295. deck[randomList[k]][0] = faces[j]; //Stores faces in shuffleDeck
  296. deck[randomList[k]][1] = suits[i]; //A shuffledDeck stored value will look like 6D (Six of Diamonds, 6♦)
  297. k++;
  298. }
  299. }
  300.  
  301. }
  302.  
  303.  
  304. short roundWinner(char playedCards[], char matchSettings[][2], int round, int firstTurnOfRound[]) {
  305.  
  306. char roundSuit[1];
  307. int position, i, winner, highestCard = 0;
  308. char card[2], prevHighestCard[2];
  309. double value;
  310.  
  311. //In case trumps were played in this round
  312. for (i = 1; i < 8; i = i + 2) { //Gets the highest trump played
  313. card[0] = playedCards[round * 8 + i - 1];
  314. card[1] = playedCards[round * 8 + i];
  315. if (playedCards[round * 8 + i] == matchSettings[2][1]) {
  316.  
  317. if (highestCard == 0) { //Store the first trump he founds to start the comparation
  318. highestCard = i;
  319. } else {
  320. value = checkValue(card); //Gets the value of the card
  321. prevHighestCard[0] = playedCards[round * 8 + highestCard - 1]; //Previous highest trump's face
  322. prevHighestCard[1] = playedCards[round * 8 + highestCard]; //Previous highest trump's suit
  323. if (value > checkValue(prevHighestCard)) {
  324. highestCard = i;
  325. }
  326. }
  327. }
  328. }
  329.  
  330. //No trumps played, get the highest card
  331. if (highestCard == 0) { //If there was no trumps, checks for the highest card of the round's suit
  332.  
  333. position = round * 8 + firstTurnOfRound[round] *2 + 1;
  334. roundSuit[0] = playedCards[position];
  335.  
  336. for (i = 1; i < 8; i = i + 2) { //Gets the highest card played
  337. card[0] = playedCards[round * 8 + i - 1];
  338. card[1] = playedCards[round * 8 + i];
  339. if (playedCards[round * 8 + i] == roundSuit[0]) {
  340. if (highestCard == 0) { //Store the first card he founds to start the comparation
  341. highestCard = i;
  342. } else {
  343. value = checkValue(card); //Gets the value of the card
  344. prevHighestCard[0] = playedCards[round * 8 + highestCard - 1]; //Previous highest card's face
  345. prevHighestCard[1] = playedCards[round * 8 + highestCard]; //Previous highest card's suit
  346. if (value > checkValue(prevHighestCard)) {
  347. highestCard = i;
  348. }
  349. }
  350. }
  351. }
  352.  
  353. }
  354. winner = (highestCard - 1) / 2;
  355. printf("Quem ganhou a vazada foi o jogador %d\n\n", (highestCard - 1) / 2);
  356. return (highestCard - 1) / 2;
  357.  
  358. }
  359.  
  360. short countPoints(char playedCards[], int teamPoints[], int firstTurnOfRound[]) {
  361.  
  362. int i, j, points;
  363. char card[1];
  364.  
  365. for (i = 0; i < 10; i++) {
  366. points = 0;
  367. for (j = 0; j < 8; j += 2) {
  368. card[0] = playedCards[i * 8 + j];
  369. points += floor(checkValue(card));
  370. }
  371. if (firstTurnOfRound[i + 1] % 2 == 0) {
  372.  
  373. teamPoints[0] += points;
  374. } else {
  375. teamPoints[1] += points;
  376. }
  377. }
  378.  
  379. }
Add Comment
Please, Sign In to add comment