4-gewinnt  1.0.0
Klassiker "4-gewinnt" als Konsolenanwendung
 Alle Datenstrukturen Dateien Funktionen Variablen Makrodefinitionen Seiten
hallofshame.c-Dateireferenz
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
Include-Abhängigkeitsdiagramm für hallofshame.c:
Dieser Graph zeigt, welche Datei direkt oder indirekt diese Datei enthält:

gehe zum Quellcode dieser Datei

Funktionen

void showHallOfShame (int highlight, int startFrom)
 
int updateSaveHoS (char *victor, char *victim, int moves)
 
int extractMoves (char *line)
 
int getOldFileLength ()
 

Dokumentation der Funktionen

int extractMoves ( char *  line)

This function extracts the number of moves of the parameter line.

Parameter
linePointer to char array (string).
Rückgabe
moves Just the moves as an int.

Definiert in Zeile 160 der Datei hallofshame.c.

Benutzt moves.

Wird benutzt von updateSaveHoS().

160  {
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 }
int moves
Definition: gamefunction.c:19

Hier ist ein Graph der zeigt, wo diese Funktion aufgerufen wird:

int getOldFileLength ( )

Determine the length of the current HallOfShame.dat file.

Rückgabe
len Just the length of the HoS as an int.

Definiert in Zeile 185 der Datei hallofshame.c.

Wird benutzt von updateSaveHoS().

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 }

Hier ist ein Graph der zeigt, wo diese Funktion aufgerufen wird:

void showHallOfShame ( int  highlight,
int  startFrom 
)

This function prints a certain number (c.f. HOS_LINES) of lines of the HallOfShame.dat to the console.

Parameter
highlightHighlight a special line with "-> "
startFromThe output is reduced to a constant number of lines (c.f. variables.h HOS_LINES). The parameter controls which lines actually are printed.

Definiert in Zeile 13 der Datei hallofshame.c.

Benutzt flushBuffer(), HOS_LINES, moves, output(), setLineAlign(), startBuffer(), userInput und victor.

Wird benutzt von gameFunction() und mainMenuReactToEnter().

13  {
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 }
void setLineAlign(int align)
Definition: system.c:344
void flushBuffer()
Definition: system.c:188
void startBuffer(int maxTextLength)
Definition: system.c:319
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

Hier ist ein Graph, der zeigt, was diese Funktion aufruft:

Hier ist ein Graph der zeigt, wo diese Funktion aufgerufen wird:

int updateSaveHoS ( char *  victor,
char *  victim,
int  moves 
)

This functions uses the 3 parameters to update and save the Hall of Shame. The 3 parameters will be interpreted as a line which has to be inserted to the HallOfShame.dat document. A kind of Insertion Sort is used to put the line on the right place.

Parameter
victorName of player who won the game.
vicitimName of player who lost.
movesNumber of steps after the game has finished.
Rückgabe
The line number of the inserted one.

Definiert in Zeile 101 der Datei hallofshame.c.

Benutzt EXITCODE_OUTOFMEMORY, extractMoves() und getOldFileLength().

Wird benutzt von gameFunction().

101  {
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 }
const int EXITCODE_OUTOFMEMORY
Definition: variables.h:3
int getOldFileLength()
Definition: hallofshame.c:185
int extractMoves(char *line)
Definition: hallofshame.c:160
char victor
Definition: gamefunction.c:18
int moves
Definition: gamefunction.c:19

Hier ist ein Graph, der zeigt, was diese Funktion aufruft:

Hier ist ein Graph der zeigt, wo diese Funktion aufgerufen wird: