4-gewinnt  1.0.0
Klassiker "4-gewinnt" als Konsolenanwendung
 Alle Datenstrukturen Dateien Funktionen Variablen Makrodefinitionen Seiten
hallofshame.c
gehe zur Dokumentation dieser Datei
1 #pragma once
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
13 void showHallOfShame(int highlight,int startFrom){
14  int viewHoS = 1;
15  // correct the argument if necessary
16  startFrom = startFrom > 0 ? startFrom : 0;
17  while(viewHoS){
18  //decalartions
19  startBuffer(64);
20  FILE *fh;
21  char victor[20];
22  char victim[20];
23  char moves[4];
24  //counting the lines
25  int lineCounter = 0;
26  //open file in read mode
27  //or leave loop
28  fh = fopen("HallOfShame.dat", "r");
29  if (fh == NULL || ferror(fh)){
30  if (fh){
31  fclose(fh);
32  break;
33  }
34  }
35 
36  setLineAlign(0);
37  output("[ HALL OF SHAME ]\n\nWelcome to our Hall of Shame...\n");
38  setLineAlign(-1);
39  // notice if HoS is empty
40  int somethingPrinted =0;
41  while((fscanf(fh,"%19[^,],%19[^,],%2[^,^\n]",victor,victim,moves)) != EOF ) {
42 
43  while (victor[0] == '\n'){
44  memmove(victor, victor+1, strlen(victor));
45  }
46  if(lineCounter>=startFrom && lineCounter<=startFrom+HOS_LINES && strlen(victor)>0 && strlen(victim)>0){
47  if(highlight==lineCounter){
48  output("-->%d. %s busted %s with %s moves\n",lineCounter+1,victor,victim,moves);
49  somethingPrinted =1;
50  }
51  else{
52  output(" %d. %s busted %s with %s moves\n",lineCounter+1,victor,victim,moves);
53  somethingPrinted =1;
54  }
55  }
56 
57  lineCounter++;
58  }
59  fclose(fh);
60  if(somethingPrinted == 0){
61  output(" No entries.");
62  }
63  output("\n");
64  setLineAlign(0);
65  output("[...Press up/down for scrolling...]\n");
66  output("[...Press Enter/Escape to exit...]\n");
67  flushBuffer();
68  fflush(stdin);
69  char userInput = getch();
70  switch(userInput) {
71  case 72: // key up
72  startFrom = startFrom-1 > 0 ? startFrom-1 : 0;
73  break;
74  case 80: // key down
75  startFrom = startFrom+1 < lineCounter-HOS_LINES ? startFrom+1 : startFrom;
76  break;
77 
78  case 13: // key enter
79  viewHoS=0;
80  break;
81  case 27: //escape
82  viewHoS =0;
83  //default:
84  // viewHoS=0;
85  }
86  fflush(stdin);
87  }
88 }
89 
90 
101 int updateSaveHoS(char* victor,char* victim,int moves){
102  char line[60];
103  FILE *fh;
104  char* buffer = malloc((getOldFileLength()+strlen(victor)+strlen(victim))*sizeof(char)+3);
105  int insertedLineNumber = 0;
106  if(buffer == NULL){
107  exit(EXITCODE_OUTOFMEMORY);
108  }
109  buffer[0]='\0';
110 
111 
112  int inserted =0;
113  fh = fopen("HallOfShame.dat", "r");
114  // while End-Of-File not reached line per line
115  while((fscanf(fh,"%59[^\n]\n",line)) != EOF ) {
116 
117  int currentMoves = extractMoves(line);
118  // compare both
119  if(currentMoves <= moves){
120  strcat(line,"\n");
121  strcat (buffer,line);
122  insertedLineNumber++;
123  }else{
124  if(inserted==0){
125  char insertLine[120];
126 
127  sprintf(insertLine,"%s,%s,%d\n%s\n",victor,victim,moves,line);
128  strcat (buffer,insertLine);
129  inserted = 1;
130  }
131  else{
132  strcat(line,"\n");
133  strcat (buffer,line);
134  }
135  }
136  }
137  char lastLine[60];
138  // this is the case when the new lines goes at the end
139  if(inserted ==0){
140  sprintf(lastLine,"%s,%s,%d\n",victor,victim,moves);
141  strcat(buffer,lastLine);
142  }
143  //close the current file mode just for reading
144  fclose(fh);
145  //open file again in new mode, dat will be blank at the opening moment
146  //so everything will be overwritten
147  fh = fopen("HallOfShame.dat","w");
148  fprintf(fh,buffer);
149  fclose(fh);
150  //do not forget to free your allocated memory
151  free(buffer);
152  return insertedLineNumber;
153 }
154 
160 int extractMoves(char* line){
161  char work[60];
162  strcpy(work,line);
163  int moves =0;
164  char *partOfLine;
165  partOfLine = strtok(work, ",");
166  int i =0 ;
167  while(partOfLine != NULL) {
168  switch (i%3) {
169  case 2:
170  moves = atoi(partOfLine);
171  break;
172  default:
173  break;
174  }
175  partOfLine = strtok(NULL, ",");
176  i++;
177  }
178  return moves;
179 }
180 
186 {
187  FILE *f = fopen("HallOfShame.dat", "rb");
188 
189  if (f == NULL || ferror(f))
190  {
191  if (f)
192  fclose(f);
193  return -1;
194  }
195 
196  fseek(f, 0, SEEK_END);
197  int len = ftell(f);
198  fclose(f);
199  return len;
200 }
void showHallOfShame(int highlight, int startFrom)
Definition: hallofshame.c:13
const int EXITCODE_OUTOFMEMORY
Definition: variables.h:3
void setLineAlign(int align)
Definition: system.c:344
void flushBuffer()
Definition: system.c:188
void startBuffer(int maxTextLength)
Definition: system.c:319
int updateSaveHoS(char *victor, char *victim, int moves)
Definition: hallofshame.c:101
int getOldFileLength()
Definition: hallofshame.c:185
int extractMoves(char *line)
Definition: hallofshame.c:160
const int HOS_LINES
Definition: variables.h:66
char victor
Definition: gamefunction.c:18
int moves
Definition: gamefunction.c:19
unsigned char userInput
Definition: variables.h:36
int output(const char *input,...)
Definition: system.c:144