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 playTimeHuman(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] = "local";
- 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;
- // clrscr(); //Clear the screen
- printf("Clique em qualquer tecla para o jogador %d jogar\n", playerTurn);
- getchar();
- clrscr(); //Clear the screen
- 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]) {
- 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]);
- }
- }
- cardPlayed = playHumanCard(player, playerTurn, matchSettings, round, firstTurnOfRound[round], playedCards);
- clrscr(); //Clear the screen
- //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);
- }
- }
- void setupPlayHuman() {
- 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, who's the one dealing cards 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
- playTimeHuman(deck, player, matchSettings);
- readInt(&repeat, 1, 2, "Deseja jogar novamente?\n1- Sim, 2- Não\n");
- }
- }
- short playHumanCard(char player[][20], int playerNumber, char matchSettings[][2], int round, int firstTurnOfRound, char playedCards[]) {
- int i, j = 1, cardPlayed, assistCount, checkIfValidPlay = 0, playableCards[10 - round];
- char firstCard[2];
- firstCard[0] = playedCards[round * 8 + firstTurnOfRound * 2];
- firstCard[1] = playedCards[round * 8 + firstTurnOfRound * 2 + 1];
- printf("Trunfo: %c%c\n", matchSettings[2][0], matchSettings[2][1]);
- printf("As suas cartas:\n");
- for (i = 0; i < 20 - round * 2; i += 2) {
- if (player[playerNumber][i + 1] == firstCard[1]) {
- 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
- assistCount++;
- playableCards[assistCount] = i / 2 + 1;
- } else if (player[playerNumber][i + 1] == matchSettings[2][1]) {
- 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
- } else {
- printf("%d- %c%c\n", j, player[playerNumber][i], player[playerNumber][i + 1]); //Doesn't paint other cards
- }
- j++;
- }
- readInt(&cardPlayed, 1, 10 - round, "Escolha uma carta para jogar\n");
- if (firstTurnOfRound != 0) {
- while (checkIfValidPlay < 1) {
- assistCount = 0; //Resets assist count
- 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) { //If player assisted
- for (i = 0; i < assistCount; i++)
- if (cardPlayed == playableCards[i]) {
- checkIfValidPlay++; //It is a valid play
- }
- } else {
- checkIfValidPlay = 1;
- }
- if (checkIfValidPlay < 1) {
- readInt(&cardPlayed, 1, 10 - round, "Tem cartas para assistir, escolha outra carta para jogar\n");
- }
- }
- }
- return cardPlayed;
- }
Add Comment
Please, Sign In to add comment