Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 <malloc.h>
- #include <time.h>
- #include <math.h>
- #include "LP_Leitura.h"
- #include "LP_Utils.h"
- #include "common.h"
- #include "cpu.h"
- #include "credits.h"
- #include "history.h"
- #include "human.h"
- /*
- * 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.
- */
- 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, 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 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}, gamesNumber, maxSize;
- char playedCards[80], hand[20], gameType[STR_SIZE] = "cpu";
- gamesHistory *game;
- 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]) {
- //Print blue for the first card of that round
- 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]);
- } else if (playedCards[round * 8 + g * 2 + 1] == matchSettings[2][1]) {
- //Print red if they play a trump card
- 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]);
- } else if (playedCards[round * 8 + g * 2 + 1] == playedCards[firstTurnOfRound[round]*2 + round * 8 + 1]) {
- //Print green if they assist
- 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]);
- readData(&game, &gamesNumber, &maxSize); //Read the games data
- addHistory(&game, &gamesNumber, &maxSize, matchSettings, teamPoints, gameType); //Processes data
- // listGamesHistory(game, gamesNumber);
- saveOnFile(game, gamesNumber); //Writes it on file
- if (game != NULL) {
- free(game);
- }
- }
- short assistsAccordingly(char player[][20], int playerNumber, int playableCards[], int num, char playedCards[], int firstTurnOfRound, int round, char trump) {
- int i, cardToPlay, trumpped = 0;
- double partnersValue;
- char playerCard[1], comparingCard[1], partnersCard[1];
- i = firstTurnOfRound;
- partnersValue = 0;
- while (i % 4 != playerNumber) { //For each card played this round
- if (i % 4 == (playerNumber - 2) % 4) { //If player's partner has played
- partnersCard[0] = playedCards[round * 8 + (i % 4)*2];
- partnersValue = checkValue(partnersCard); //Stores the value of the card played by partner
- }
- playerCard[0] = player[playerNumber][(playableCards[0] - 1)*2]; //Stores the value of the highest playable card (of the suit played)
- comparingCard[0] = playedCards[round * 8 + (i % 4)*2]; //Compares to the other cards played in this round
- //If player's card or partner's card is higher than all the others played by opponents
- if (checkValue(playerCard) > checkValue(comparingCard) || partnersValue > checkValue(comparingCard)) {
- cardToPlay = playableCards[0];
- } else {
- cardToPlay = playableCards[num - 1];
- }
- //If the opponent played a trump card
- if (playedCards[round * 8 + (i % 4)*2] == trump && ((i % 4 - 2) % 4) != playerNumber) {
- trumpped++;
- }
- i++;
- }
- if (trumpped > 0) {
- cardToPlay = playableCards[num - 1];
- }
- return cardToPlay;
- }
- 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) {
- //Assists accordingly, plays a high card if player's team is winning the round, and the lowest if not
- playableCards[0] = assistsAccordingly(player, playerNumber, playableCards, assistCount, playedCards, firstTurnOfRound[round], round, matchSettings[2][1]);
- }
- 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], highest = 1, lowest = 1;
- double partnersValue;
- char firstCard[2], partnersCard[1], comparingCard[1], playerCard[1], compareHighest[1], compareLowest[1];
- 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 ((playerNumber - firstTurnOfRound[round]) % 4 == 3) {//Check if player is the last player in that round
- playableCards[0] = playableCards[trumpCount - 1]; //Plays the lowest trump because there is no need to play a higher
- }
- //@playableCards[0] will store the highest trump as default
- }
- }
- if (assistCount == 0 && trumpCount == 0) { //If playerNumber doesn't have cards to assit or trumps, he plays a random card
- compareHighest[0] = player[playerNumber][highest * 2];
- compareLowest[0] = player[playerNumber][lowest * 2];
- i = firstTurnOfRound[round];
- for (i = 0; i < round; i++) {
- playerCard[0] = player[playerNumber][i * 2];
- if (checkValue(playerCard) > checkValue(compareHighest)) {
- highest = i;
- }
- if (checkValue(playerCard) < checkValue(compareLowest)) {
- lowest = i;
- }
- }
- partnersValue = 0;
- while (i % 4 != playerNumber) { //For each card played this round
- if (i % 4 == (playerNumber - 2) % 4) { //If player's partner has played
- partnersCard[0] = playedCards[round * 8 + (i % 4)*2];
- partnersValue = checkValue(partnersCard); //Stores the value of the card played by partner
- }
- comparingCard[0] = playedCards[round * 8 + (i % 4)*2]; //Compares to the other cards played in this round
- if (partnersValue > checkValue(comparingCard) || playedCards[round * 8 + (i % 4)*2 + 1] != firstCard[1]) {
- playableCards[0] = highest + 1;
- } else {
- playableCards[0] = lowest + 1;
- }
- i++;
- }
- }
- 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 that round's suit matches the ace he wants to play
- if (firstSuitOfThatRound[0] == player[playerNumber][2 * playableAces[i] + 1]) {
- //If players of the other team have played a trump card
- 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;
- }
Add Comment
Please, Sign In to add comment