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 "human.h"
- #define STR_SIZE 6
- typedef struct History {
- int gameNumber;
- int teamPoints[2];
- char gameType[STR_SIZE];
- } gamesHistory;
- /*
- * 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 readData(gamesHistory **game, int *gamesNumber, int *maxSize) {
- FILE *f;
- if ((f = fopen("history.bin", "rb")) == NULL) {
- *gamesNumber = 0; //if it failed to open
- perror(NULL);
- return;
- }
- fread(gamesNumber, sizeof (int), 1, f); //reads number of games saved in the file
- //Allocates necessary memory dynamically, more than it really needs.
- *game = malloc((*gamesNumber) * sizeof (gamesHistory));
- if (game == NULL) //If the allocation failed
- {
- printf("Impossível alocar memória");
- *gamesNumber = 0;
- *maxSize = 0;
- } else {
- // printf("Alocou %d bytes de memória.\n", (*gamesNumber) * sizeof (gamesHistory));
- fread(*game, sizeof (gamesHistory), *gamesNumber, f); //It succeeded to allocate the memory and reads the games' history
- *maxSize = *gamesNumber;
- }
- fclose(f);
- }
- void listGamesHistory(gamesHistory *game, int gamesNumber) {
- clrscr(); //Clear the screen
- int i;
- for (i = 1; i < gamesNumber; i++) {
- printf("Nº do jogo: %d | Tipo de jogo: %s\nPontos da Equipa 1: %d \nPontos da Equipa 2: %d\n\n", game[i].gameNumber, game[i].gameType, game[i].teamPoints[0], game[i].teamPoints[1]);
- }
- printf("Clique Enter para voltar ao menu principal");
- getchar();
- }
- /* @matchSettings should look like:
- * matchSettings[0] = number of games played
- * matchSettings[1] = who's dealing cards
- * matchSettings[2] = game's trump
- */
- void addHistory(gamesHistory **game, int *gamesNumber, int *maxSize, char matchSettings[][2], int teamPoints[], char gameType[]) {
- int matchNumber = atoi(matchSettings[0]);
- //Verifica se é necessário realocar antes de adicionar
- if (*gamesNumber == *maxSize) {
- // printf("A lista está cheia\n");
- /*
- * Tenta alocar um novo bloco de memória com espaço para dois novos alunos.
- * A política "normal" é em cada realloc duplicar o número de elementos
- * da coleção.
- * */
- gamesHistory *newGame = realloc(*game, ((*gamesNumber) + 2) * sizeof (gamesHistory));
- if (newGame == NULL) {
- // printf("Realocação de memória falhou, utilizador não adicionado.\n");
- return;
- } else {
- // printf("Realocou memória. Novo tamanho: %d bytes.", ((*gamesNumber) + 2) * sizeof (gamesHistory));
- *game = newGame;
- (*maxSize) += 2;
- }
- }
- (*game)[*gamesNumber].gameNumber = matchNumber;
- (*game)[*gamesNumber].teamPoints[0] = teamPoints[0];
- (*game)[*gamesNumber].teamPoints[1] = teamPoints[1];
- strcpy((*game)[*gamesNumber].gameType, gameType);
- (*gamesNumber)++;
- }
- void saveOnFile(gamesHistory *game, int gamesNumber) {
- FILE *f;
- if ((f = fopen("history.bin", "wb")) == NULL) {
- perror(NULL);
- return;
- }
- fwrite(&gamesNumber, sizeof (int), 1, f); //escreve nº de registos a ser escritos
- fwrite(game, sizeof (gamesHistory), gamesNumber, f); //escreve registos
- }
Add Comment
Please, Sign In to add comment