xickoh

cpu

Jan 7th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.38 KB | None | 0 0
  1.  
  2. #ifdef _WIN32
  3. #include <conio.h>
  4. #else
  5. #include <stdio.h>
  6. #define clrscr() printf("\e[1;1H\e[2J")
  7. #endif
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <malloc.h>
  11. #include <time.h>
  12. #include <math.h>
  13. #include "LP_Leitura.h"
  14. #include "LP_Utils.h"
  15. #include "common.h"
  16. #include "cpu.h"
  17. #include "credits.h"
  18. #include "history.h"
  19. #include "human.h"
  20.  
  21. /*
  22. * To change this license header, choose License Headers in Project Properties.
  23. * To change this template file, choose Tools | Templates
  24. * and open the template in the editor.
  25. */
  26.  
  27. void setupPlayCPU() {
  28. static char deck[40][2]; //Deck consisting of 20 cards, 2 chars each
  29. char player[4][20]; // will receive values player[number of player][cards]
  30. char matchSettings[3][2]; //Will store the number of games played, and game's trump
  31. int matchNumber, repeat = 1;
  32.  
  33. while (repeat == 1) {
  34. buildDeck(deck);
  35. matchNumber = atoi(matchSettings[0]) + 1; //Increments number of the match
  36. sprintf(matchSettings[0], "%d", matchNumber); //Stores it on matchSettings[0][0]
  37.  
  38. dealCards(deck, player, matchSettings);
  39.  
  40. arrangeHand(player); //Cards will be arranged descending, for each suit in order of
  41.  
  42. playTimeCPU(deck, player, matchSettings);
  43.  
  44. readInt(&repeat, 1, 2, "Deseja jogar novamente?\n1- Sim, 2- Não\n");
  45. }
  46. }
  47.  
  48. void playTimeCPU(char deck[][2], char player[][20], char matchSettings[][2]) {
  49.  
  50. int round, g, h, i, j, k, cardPlayed, idxToDel, firstTurnOfRound[11], playerTurn, position, teamPoints[2] = {0, 0}, gamesNumber, maxSize;
  51. char playedCards[80], hand[20], gameType[STR_SIZE] = "cpu";
  52. gamesHistory *game;
  53.  
  54. firstTurnOfRound[0] = matchSettings[0][0] % 4; //On the first round, the first player playing is the one next to the one that dealt
  55.  
  56. clrscr(); //Clear the screen
  57. //Player played cards
  58. for (round = 0; round < 10; round++) {
  59. for (i = firstTurnOfRound[round]; i <= firstTurnOfRound[round] + 3; i++) {
  60. playerTurn = (i % 4 + 4) % 4;
  61.  
  62. if (playerTurn == 0) {//If it's the human's turn, call the function playHumanCard
  63.  
  64. for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
  65. if (j == firstTurnOfRound[round]) {
  66. 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]);
  67. } else {
  68. printf("A carta jogada pelo jogador %d foi %c%c\n", j, playedCards[round * 8 + j * 2], playedCards[round * 8 + j * 2 + 1]);
  69. }
  70. }
  71.  
  72. cardPlayed = playHumanCard(player, playerTurn, matchSettings, round, firstTurnOfRound[round], playedCards);
  73.  
  74. clrscr(); //Clear the screen
  75.  
  76. } else { //Select the card to be played by CPU
  77. if (round < 9) {
  78. cardPlayed = playCPUCard(player, playerTurn, round, firstTurnOfRound, playedCards, matchSettings);
  79. } else {
  80. cardPlayed = 1;
  81. }
  82. }
  83.  
  84. //Remove the card from the hand
  85. idxToDel = cardPlayed * 2 - 2; //Get the face of the card to remove
  86. position = round * 8 + playerTurn * 2; //
  87. playedCards[position] = player[playerTurn][idxToDel];
  88. playedCards[position + 1] = player[playerTurn][idxToDel + 1];
  89.  
  90. k = 0; //Resets k
  91. for (h = 0; h < 10; h++) {
  92. if (h != cardPlayed - 1) {
  93. hand[k] = player[playerTurn][h * 2];
  94. k++;
  95. hand[k] = player[playerTurn][h * 2 + 1];
  96. k++;
  97. }
  98. }
  99. hand[18] = '0';
  100. hand[19] = '0';
  101.  
  102. strncpy(player[playerTurn], hand, 20);
  103.  
  104. }
  105.  
  106. clrscr(); //Clear the screen
  107. //End of round info
  108. for (j = firstTurnOfRound[round]; j < i; j++) {//Tells the player which cards have been played this round
  109. g = (j % 4 + 4) % 4;
  110. if (g == firstTurnOfRound[round]) {
  111. //Print blue for the first card of that round
  112. 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]);
  113. } else if (playedCards[round * 8 + g * 2 + 1] == matchSettings[2][1]) {
  114. //Print red if they play a trump card
  115. 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]);
  116. } else if (playedCards[round * 8 + g * 2 + 1] == playedCards[firstTurnOfRound[round]*2 + round * 8 + 1]) {
  117. //Print green if they assist
  118. 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]);
  119. } else {
  120. printf("A carta jogada pelo jogador %d foi %c%c\n", g, playedCards[round * 8 + g * 2], playedCards[round * 8 + g * 2 + 1]);
  121. }
  122. }
  123.  
  124. firstTurnOfRound[round + 1] = roundWinner(playedCards, matchSettings, round, firstTurnOfRound);
  125. }
  126.  
  127. countPoints(playedCards, teamPoints, firstTurnOfRound);
  128.  
  129. printf("Pontos da primeira Equipa: %d\n", teamPoints[0]);
  130. printf("Pontos da segunda Equipa: %d\n", teamPoints[1]);
  131.  
  132. readData(&game, &gamesNumber, &maxSize); //Read the games data
  133. addHistory(&game, &gamesNumber, &maxSize, matchSettings, teamPoints, gameType); //Processes data
  134. // listGamesHistory(game, gamesNumber);
  135. saveOnFile(game, gamesNumber); //Writes it on file
  136. if (game != NULL) {
  137. free(game);
  138. }
  139.  
  140.  
  141.  
  142. }
  143.  
  144. short assistsAccordingly(char player[][20], int playerNumber, int playableCards[], int num, char playedCards[], int firstTurnOfRound, int round, char trump) {
  145. int i, cardToPlay, trumpped = 0;
  146. double partnersValue;
  147. char playerCard[1], comparingCard[1], partnersCard[1];
  148.  
  149. i = firstTurnOfRound;
  150. partnersValue = 0;
  151. while (i % 4 != playerNumber) { //For each card played this round
  152.  
  153. if (i % 4 == (playerNumber - 2) % 4) { //If player's partner has played
  154. partnersCard[0] = playedCards[round * 8 + (i % 4)*2];
  155. partnersValue = checkValue(partnersCard); //Stores the value of the card played by partner
  156. }
  157.  
  158. playerCard[0] = player[playerNumber][(playableCards[0] - 1)*2]; //Stores the value of the highest playable card (of the suit played)
  159. comparingCard[0] = playedCards[round * 8 + (i % 4)*2]; //Compares to the other cards played in this round
  160. //If player's card or partner's card is higher than all the others played by opponents
  161. if (checkValue(playerCard) > checkValue(comparingCard) || partnersValue > checkValue(comparingCard)) {
  162. cardToPlay = playableCards[0];
  163. } else {
  164. cardToPlay = playableCards[num - 1];
  165. }
  166.  
  167. //If the opponent played a trump card
  168. if (playedCards[round * 8 + (i % 4)*2] == trump && ((i % 4 - 2) % 4) != playerNumber) {
  169. trumpped++;
  170. }
  171. i++;
  172. }
  173.  
  174. if (trumpped > 0) {
  175. cardToPlay = playableCards[num - 1];
  176. }
  177. return cardToPlay;
  178. }
  179.  
  180. short mustAssistConditions(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2], int playableCards[], int assistCount) {
  181. int i, j, trumpped = 0, cardFound = 0, suitableCard = 0, cardsOfSuitPlayed = 0, r;
  182. char firstSuitOfThatRound[1];
  183.  
  184. //Count how many cards of that suit have been played already
  185. for (i = 1; i < round * 8; i += 2) {
  186. if (playedCards[i] == playedCards[round * 8 + firstTurnOfRound[round] * 2 + 1]) {
  187. cardsOfSuitPlayed++;
  188. }
  189. }
  190.  
  191. //Check if they have Aces
  192. for (i = 0; i < assistCount; i++) { //Searches for Aces in their hand
  193. 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
  194. cardFound = playableCards[i];
  195. }
  196. }
  197.  
  198. if (cardFound > 0) {
  199.  
  200. for (j = 0; j <= round; j++) {
  201. firstSuitOfThatRound[0] = playedCards[firstTurnOfRound[j] * j * 8 + 1];
  202.  
  203. //If suit matches the ace he wants to play
  204. if (firstSuitOfThatRound[0] == player[playerNumber][(cardFound - 1) * 2 + 1]) {
  205. //if any of the two opponents played a trump
  206. if (playedCards[((playerNumber - 1) % 4) * j * 2 + 1] == matchSettings[2][1] || playedCards[((playerNumber + 1) % 4) * j * 2 + 1] == matchSettings[2][1]) {
  207. trumpped++;
  208. }
  209. }
  210. }
  211.  
  212. //Check if in previous plays the opposite team trumped
  213. if (trumpped == 0) {
  214. if (cardsOfSuitPlayed + assistCount < 7) {
  215. playableCards[0] = cardFound;
  216. suitableCard++;
  217. }
  218. }
  219. }
  220.  
  221. if (suitableCard < 1) {
  222. //Assists accordingly, plays a high card if player's team is winning the round, and the lowest if not
  223. playableCards[0] = assistsAccordingly(player, playerNumber, playableCards, assistCount, playedCards, firstTurnOfRound[round], round, matchSettings[2][1]);
  224. }
  225.  
  226. return playableCards[0];
  227.  
  228. }
  229.  
  230. //CPU CONDITIONS CPU_Conditions
  231.  
  232. short checkIfHasSuit(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]) {
  233. int i, assistCount = 0, trumpCount = 0, playableCards[10 - round], highest = 1, lowest = 1;
  234. double partnersValue;
  235. char firstCard[2], partnersCard[1], comparingCard[1], playerCard[1], compareHighest[1], compareLowest[1];
  236.  
  237. firstCard[0] = playedCards[round * 8 + firstTurnOfRound[round] * 2];
  238. firstCard[1] = playedCards[round * 8 + firstTurnOfRound[round] * 2 + 1];
  239.  
  240. for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
  241. if (player[playerNumber][2 * i + 1] == firstCard[1]) { //If suits matches
  242. playableCards[assistCount] = i + 1;
  243. assistCount++;
  244. }
  245. }
  246.  
  247. if (assistCount > 0) { //Has cards to assit
  248. if (assistCount > 1) {
  249. playableCards[0] = mustAssistConditions(player, playerNumber, round, firstTurnOfRound, playedCards, matchSettings, playableCards, assistCount);
  250. }
  251. } else if (firstCard[1] != matchSettings[2][1]) {
  252. for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
  253. if (player[playerNumber][2 * i + 1] == matchSettings[2][1]) { //If suits matches
  254. playableCards[trumpCount] = i + 1;
  255. trumpCount++;
  256. }
  257. }
  258.  
  259. if (trumpCount > 0) { //Didn't have cards to assist, so plays a trump card if he has it
  260.  
  261. if ((playerNumber - firstTurnOfRound[round]) % 4 == 3) {//Check if player is the last player in that round
  262. playableCards[0] = playableCards[trumpCount - 1]; //Plays the lowest trump because there is no need to play a higher
  263. }
  264.  
  265. //@playableCards[0] will store the highest trump as default
  266.  
  267. }
  268.  
  269. }
  270.  
  271. if (assistCount == 0 && trumpCount == 0) { //If playerNumber doesn't have cards to assit or trumps, he plays a random card
  272.  
  273. compareHighest[0] = player[playerNumber][highest * 2];
  274. compareLowest[0] = player[playerNumber][lowest * 2];
  275. i = firstTurnOfRound[round];
  276.  
  277. for (i = 0; i < round; i++) {
  278. playerCard[0] = player[playerNumber][i * 2];
  279. if (checkValue(playerCard) > checkValue(compareHighest)) {
  280. highest = i;
  281. }
  282. if (checkValue(playerCard) < checkValue(compareLowest)) {
  283. lowest = i;
  284. }
  285. }
  286.  
  287. partnersValue = 0;
  288. while (i % 4 != playerNumber) { //For each card played this round
  289.  
  290. if (i % 4 == (playerNumber - 2) % 4) { //If player's partner has played
  291. partnersCard[0] = playedCards[round * 8 + (i % 4)*2];
  292. partnersValue = checkValue(partnersCard); //Stores the value of the card played by partner
  293. }
  294.  
  295. comparingCard[0] = playedCards[round * 8 + (i % 4)*2]; //Compares to the other cards played in this round
  296.  
  297. if (partnersValue > checkValue(comparingCard) || playedCards[round * 8 + (i % 4)*2 + 1] != firstCard[1]) {
  298. playableCards[0] = highest + 1;
  299. } else {
  300. playableCards[0] = lowest + 1;
  301. }
  302.  
  303. i++;
  304. }
  305.  
  306. }
  307.  
  308. return playableCards[0];
  309.  
  310.  
  311. }
  312.  
  313. short CPUFirstTurn(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]) {
  314. int i, j, aces = 0, k = 0, playableCards[10 - round], playableAces[4], trumpped, r;
  315. char firstSuitOfThatRound[1];
  316.  
  317. //Check if they have Aces
  318. for (i = 0; i < 10 - round; i++) { //Gets all the cards in player's hand
  319. 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
  320. playableAces[aces] = i + 1;
  321. aces++;
  322. }
  323. }
  324.  
  325. if (aces > 0) {
  326. //Check if the suit of the ace has been played before and someone of the other team trumped over it
  327. for (i = 0; i < aces; i++) { //Check for each Ace's suit
  328. trumpped = 0;
  329. for (j = 0; j < round; j++) {
  330. firstSuitOfThatRound[0] = playedCards[firstTurnOfRound[j] * j * 8 + 1];
  331.  
  332. //If that round's suit matches the ace he wants to play
  333. if (firstSuitOfThatRound[0] == player[playerNumber][2 * playableAces[i] + 1]) {
  334. //If players of the other team have played a trump card
  335. if (playedCards[((playerNumber - 1) % 4) * j * 2 + 1] == matchSettings[2][1] || playedCards[((playerNumber + 1) % 4) * j * 2 + 1] == matchSettings[2][1]) {
  336. trumpped++;
  337. }
  338. }
  339. }
  340. //Check if in previous plays the opposite team trumped
  341. if (trumpped == 0) {
  342. playableCards[k] = playableAces[i];
  343. k++;
  344. }
  345. }
  346.  
  347. if (k > 0) {
  348. //If there's at least one playableAce
  349. if (k > 1) { //Randomize the list of aces
  350. r = (rand() % k);
  351. playableCards[0] = playableCards[r];
  352. }
  353. } else {
  354. //If he deson't have a playable ace, he plays a random card
  355. playableCards[0] = 1 + rand() % (10 - round);
  356. }
  357.  
  358. } else {
  359. playableCards[0] = 1 + rand() % (10 - round); //The player has no Aces
  360. }
  361.  
  362. return playableCards[0];
  363. }
  364.  
  365. short playCPUCard(char player[][20], int playerNumber, int round, int firstTurnOfRound[], char playedCards[], char matchSettings[][2]) {
  366. int cardPlayed;
  367.  
  368. if (firstTurnOfRound[round] == playerNumber) {
  369. cardPlayed = CPUFirstTurn(player, playerNumber, round, firstTurnOfRound, playedCards, matchSettings);
  370. } else {
  371. cardPlayed = checkIfHasSuit(player, playerNumber, round, firstTurnOfRound, playedCards, matchSettings);
  372. }
  373. return cardPlayed;
  374. }
Add Comment
Please, Sign In to add comment