Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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.
- */
- #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 "cpu.h"
- #include "credits.h"
- #include "history.h"
- #include "human.h"
- short checkOrder(char card[]) {
- int value;
- switch (card[0]) {
- case 'A':
- value = 10;
- break;
- case '7':
- value = 9;
- break;
- case 'K':
- value = 8;
- break;
- case 'J':
- value = 7;
- break;
- case 'Q':
- value = 6;
- break;
- case '6':
- value = 5;
- break;
- case '5':
- value = 4;
- break;
- case '4':
- value = 3;
- break;
- case '3':
- value = 2;
- break;
- case '2':
- value = 1;
- break;
- }
- return value;
- }
- double checkValue(char card[]) {
- double value;
- switch (card[0]) {
- case 'A':
- value = 11;
- break;
- case '7':
- value = 10;
- break;
- case 'K':
- value = 4;
- break;
- case 'J':
- value = 3;
- break;
- case 'Q':
- value = 2;
- break;
- case '6':
- value = 0.6; //For the propose of sorting cards with the value of 0
- break;
- case '5':
- value = 0.5;
- break;
- case '4':
- value = 0.4;
- break;
- case '3':
- value = 0.3;
- break;
- case '2':
- value = 0.2;
- break;
- }
- return value;
- }
- void switchCard(char player[][20], int cardPoints[10], int i, int j, int k) {
- int m;
- char n[2];
- n[0] = player[i][j];
- n[1] = player[i][j + 1];
- player[i][j] = player[i][k];
- player[i][j + 1] = player[i][k + 1];
- player[i][k] = n[0];
- player[i][k + 1] = n[1];
- m = cardPoints[j / 2];
- cardPoints[j / 2] = cardPoints[k / 2];
- cardPoints[k / 2] = m;
- }
- void arrangeHand(char player[][20]) {
- int i, k, p, q, count, j;
- int cardPoints[10];
- char card[1];
- //For each player
- for (i = 0; i < 4; i++) {
- //Saves cards' points to @cardPoints array;
- for (q = 0; q < 20; q++) {
- card[0] = player[i][q];
- cardPoints[q / 2] = checkOrder(card);
- q++;
- }
- j = 0; //Reset @j tu run the while cycle for the next player
- while (j < 20) {
- count = 0;
- for (k = 0; k < 20; k++) {
- if (cardPoints[j / 2] < cardPoints[k / 2] && player[i][j + 1] == player[i][k + 1]) {
- //If it finds a card of the same suit with a highest value
- if (j <= k) {
- switchCard(player, cardPoints, i, j, k);
- count++;
- } else {
- p = k + 2;
- q = 0;
- while (q == 0) {
- if (player[i][j + 1] == player[i][p + 1]) {//If suits are the same
- if (cardPoints[j / 2] < cardPoints[p / 2]) { //If card p is higher than card j
- p = p + 2;
- } else if (cardPoints[j / 2] == cardPoints[p / 2]) {
- //
- q = 1;
- } else {
- switchCard(player, cardPoints, i, j, p);
- k++;
- count++;
- q = 1;
- }
- } else {
- switchCard(player, cardPoints, i, j, p);
- k++;
- count++;
- q = 1;
- }
- }
- }
- }
- k++;
- }
- if (count > 0) {
- j = 0;
- } else {
- j = j + 2;
- }
- }
- }
- }
- void dealCards(char deck[][2], char player[][20], char matchSettings[][2]) {
- int option = 0, i, j = 0, k = 0, playerDealingCards, m;
- playerDealingCards = (atoi(matchSettings[0]) - 1) % 4;
- if (playerDealingCards == 0) {
- readInt(&option, 1, 2, "Deseja tirar o trunfo por \n1- cima, ou 2- baixo?\n");
- } else {
- option = rand() % 2 + 1; //Generate a random option between 1 and 2
- }
- if (option == 1) { //In case he chose to deal the trump from the top...
- // Deal cards to each player
- // k = position of the card, by the end of the external for's iteration, k will be 40
- printf("O trunfo foi tirado por cima\n");
- for (i = playerDealingCards; i >= playerDealingCards - 3; i--) {//Deal cards for the 4 players
- 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)
- m = (i % 4 + 4) % 4; //Get the positive module of players
- player[m][j] = deck[k][0];
- j++;
- player[m][j] = deck[k][1];
- k++;
- }
- }
- //Declaring game's trump
- matchSettings[2][0] = player[playerDealingCards][0]; //Because he dealt the trump from the top, the trump is the first card he dealt for him.
- matchSettings[2][1] = player[playerDealingCards][1];
- } else {
- printf("O trunfo foi tirado por baixo\n");
- // Deal cards to each player
- // k = position of the card, by the end of the external for's iteration, k will be 40
- for (i = playerDealingCards - 3; i <= playerDealingCards; i++) {//for each player, starts on player 0, then goes 3, 2 and finally 1
- for (j = 0; j < 20; j++) { //each 20 chars = 10 cards composed by a face then a suit; i.e: 6D (6 of Diamonds)
- m = (i % 4 + 4) % 4; //Get the positive module of players
- player[m][j] = deck[k][0];
- j++;
- player[m][j] = deck[k][1];
- k++;
- }
- }
- //Declaring game's trump
- 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
- matchSettings[2][1] = player[playerDealingCards][19];
- }
- // Mostra a mão de cada um
- // printf("\nO trunfo é: %c%c\n", matchSettings[2][0], matchSettings[2][1]);
- //
- // Printa as cartas de cada jogador
- // for (j = 0; j < 4; j++) {
- // printf("Player %d\n", j);
- // for (i = 0; i < 20; i++) {
- // printf("%c", player[j][i]);
- // if (i % 2 != 0) {
- // printf("\n");
- // }
- // }
- // printf("\n");
- // }
- }
- // Shuffle an array, passing 2 arguments, the array and its size
- void shuffle(int *array, size_t n) {
- srand(time(NULL)); //New seed for rand based on current time
- if (n > 1) {
- size_t i;
- for (i = 0; i < n; i++) {
- size_t j = rand() % (n);
- int t = array[j];
- array[j] = array[i];
- array[i] = t;
- }
- }
- }
- //Builds deck
- void buildDeck(char deck[][2]) {
- int i, j, randomList[40];
- int k = 0;
- char suits[4] = {'H', 'S', 'D', 'C'}; //Hearts, Spaces, Diamonds, Clubs;
- char faces[10] = {'A', '2', '3', '4', '5', '6', '7', 'J', 'Q', 'K'};
- for (i = 0; i < 40; i++) { //Generate a list of 40 numbers ascending from 0 to 39
- randomList[i] = i;
- }
- shuffle(randomList, 40); //Shuffle the list
- /*
- * Deck Builder
- * Every number randomized earlier will now assume the value of a card, this way the deck is shuffled
- * Model: Deck[position][face + suit]
- * Position will be taken from the randomized list, and face+suit from the previously declared variables
- * Instead of having [0] = AH; [1] = 2H; [2] = 3H, it will not get random numbers for its position
- * Example: [27] = AC; [15] = 2H; [32] = 3H etc. The whole deck is shuffled.
- */
- for (i = 0; i < 4; i++) { //Suits
- for (j = 0; j < 10; j++) { //Faces
- deck[randomList[k]][0] = faces[j]; //Stores faces in shuffleDeck
- deck[randomList[k]][1] = suits[i]; //A shuffledDeck stored value will look like 6D (Six of Diamonds, 6♦)
- k++;
- }
- }
- }
- short roundWinner(char playedCards[], char matchSettings[][2], int round, int firstTurnOfRound[]) {
- char roundSuit[1];
- int position, i, winner, highestCard = 0;
- char card[2], prevHighestCard[2];
- double value;
- //In case trumps were played in this round
- for (i = 1; i < 8; i = i + 2) { //Gets the highest trump played
- card[0] = playedCards[round * 8 + i - 1];
- card[1] = playedCards[round * 8 + i];
- if (playedCards[round * 8 + i] == matchSettings[2][1]) {
- if (highestCard == 0) { //Store the first trump he founds to start the comparation
- highestCard = i;
- } else {
- value = checkValue(card); //Gets the value of the card
- prevHighestCard[0] = playedCards[round * 8 + highestCard - 1]; //Previous highest trump's face
- prevHighestCard[1] = playedCards[round * 8 + highestCard]; //Previous highest trump's suit
- if (value > checkValue(prevHighestCard)) {
- highestCard = i;
- }
- }
- }
- }
- //No trumps played, get the highest card
- if (highestCard == 0) { //If there was no trumps, checks for the highest card of the round's suit
- position = round * 8 + firstTurnOfRound[round] *2 + 1;
- roundSuit[0] = playedCards[position];
- for (i = 1; i < 8; i = i + 2) { //Gets the highest card played
- card[0] = playedCards[round * 8 + i - 1];
- card[1] = playedCards[round * 8 + i];
- if (playedCards[round * 8 + i] == roundSuit[0]) {
- if (highestCard == 0) { //Store the first card he founds to start the comparation
- highestCard = i;
- } else {
- value = checkValue(card); //Gets the value of the card
- prevHighestCard[0] = playedCards[round * 8 + highestCard - 1]; //Previous highest card's face
- prevHighestCard[1] = playedCards[round * 8 + highestCard]; //Previous highest card's suit
- if (value > checkValue(prevHighestCard)) {
- highestCard = i;
- }
- }
- }
- }
- }
- winner = (highestCard - 1) / 2;
- printf("Quem ganhou a vazada foi o jogador %d\n\n", (highestCard - 1) / 2);
- return (highestCard - 1) / 2;
- }
- short countPoints(char playedCards[], int teamPoints[], int firstTurnOfRound[]) {
- int i, j, points;
- char card[1];
- for (i = 0; i < 10; i++) {
- points = 0;
- for (j = 0; j < 8; j += 2) {
- card[0] = playedCards[i * 8 + j];
- points += floor(checkValue(card));
- }
- if (firstTurnOfRound[i + 1] % 2 == 0) {
- teamPoints[0] += points;
- } else {
- teamPoints[1] += points;
- }
- }
- }
Add Comment
Please, Sign In to add comment