xickoh

history

Jan 7th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #ifdef _WIN32
  2. #include <conio.h>
  3. #else
  4. #include <stdio.h>
  5. #define clrscr() printf("\e[1;1H\e[2J")
  6. #endif
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <malloc.h>
  10. #include <time.h>
  11. #include <math.h>
  12. #include "LP_Leitura.h"
  13. #include "LP_Utils.h"
  14. #include "common.h"
  15. #include "cpu.h"
  16. #include "credits.h"
  17. #include "human.h"
  18.  
  19. #define STR_SIZE 6
  20.  
  21. typedef struct History {
  22. int gameNumber;
  23. int teamPoints[2];
  24. char gameType[STR_SIZE];
  25. } gamesHistory;
  26.  
  27. /*
  28. * To change this license header, choose License Headers in Project Properties.
  29. * To change this template file, choose Tools | Templates
  30. * and open the template in the editor.
  31. */
  32.  
  33.  
  34. void readData(gamesHistory **game, int *gamesNumber, int *maxSize) {
  35.  
  36. FILE *f;
  37.  
  38. if ((f = fopen("history.bin", "rb")) == NULL) {
  39. *gamesNumber = 0; //if it failed to open
  40. perror(NULL);
  41. return;
  42. }
  43.  
  44. fread(gamesNumber, sizeof (int), 1, f); //reads number of games saved in the file
  45.  
  46. //Allocates necessary memory dynamically, more than it really needs.
  47. *game = malloc((*gamesNumber) * sizeof (gamesHistory));
  48.  
  49. if (game == NULL) //If the allocation failed
  50. {
  51. printf("Impossível alocar memória");
  52. *gamesNumber = 0;
  53. *maxSize = 0;
  54. } else {
  55. // printf("Alocou %d bytes de memória.\n", (*gamesNumber) * sizeof (gamesHistory));
  56. fread(*game, sizeof (gamesHistory), *gamesNumber, f); //It succeeded to allocate the memory and reads the games' history
  57. *maxSize = *gamesNumber;
  58. }
  59.  
  60. fclose(f);
  61.  
  62. }
  63.  
  64. void listGamesHistory(gamesHistory *game, int gamesNumber) {
  65. clrscr(); //Clear the screen
  66. int i;
  67. for (i = 1; i < gamesNumber; i++) {
  68. 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]);
  69. }
  70.  
  71. printf("Clique Enter para voltar ao menu principal");
  72. getchar();
  73.  
  74. }
  75.  
  76. /* @matchSettings should look like:
  77. * matchSettings[0] = number of games played
  78. * matchSettings[1] = who's dealing cards
  79. * matchSettings[2] = game's trump
  80. */
  81.  
  82.  
  83. void addHistory(gamesHistory **game, int *gamesNumber, int *maxSize, char matchSettings[][2], int teamPoints[], char gameType[]) {
  84. int matchNumber = atoi(matchSettings[0]);
  85. //Verifica se é necessário realocar antes de adicionar
  86. if (*gamesNumber == *maxSize) {
  87. // printf("A lista está cheia\n");
  88. /*
  89. * Tenta alocar um novo bloco de memória com espaço para dois novos alunos.
  90. * A política "normal" é em cada realloc duplicar o número de elementos
  91. * da coleção.
  92. * */
  93. gamesHistory *newGame = realloc(*game, ((*gamesNumber) + 2) * sizeof (gamesHistory));
  94. if (newGame == NULL) {
  95. // printf("Realocação de memória falhou, utilizador não adicionado.\n");
  96. return;
  97. } else {
  98. // printf("Realocou memória. Novo tamanho: %d bytes.", ((*gamesNumber) + 2) * sizeof (gamesHistory));
  99. *game = newGame;
  100. (*maxSize) += 2;
  101. }
  102. }
  103.  
  104. (*game)[*gamesNumber].gameNumber = matchNumber;
  105. (*game)[*gamesNumber].teamPoints[0] = teamPoints[0];
  106. (*game)[*gamesNumber].teamPoints[1] = teamPoints[1];
  107. strcpy((*game)[*gamesNumber].gameType, gameType);
  108.  
  109. (*gamesNumber)++;
  110.  
  111. }
  112.  
  113. void saveOnFile(gamesHistory *game, int gamesNumber) {
  114. FILE *f;
  115.  
  116. if ((f = fopen("history.bin", "wb")) == NULL) {
  117. perror(NULL);
  118. return;
  119. }
  120.  
  121. fwrite(&gamesNumber, sizeof (int), 1, f); //escreve nº de registos a ser escritos
  122. fwrite(game, sizeof (gamesHistory), gamesNumber, f); //escreve registos
  123.  
  124. }
Add Comment
Please, Sign In to add comment