4-gewinnt  1.0.0
Klassiker "4-gewinnt" als Konsolenanwendung
 Alle Datenstrukturen Dateien Funktionen Variablen Makrodefinitionen Seiten
signatures.h-Dateireferenz
Dieser Graph zeigt, welche Datei direkt oder indirekt diese Datei enthält:

gehe zum Quellcode dieser Datei

Funktionen

void HelloWorld ()
 
void setFancy ()
 
void initBuffer ()
 
void startBuffer (int maxTextLength)
 
void flushBuffer ()
 
int copyChar (char *src, char *dst)
 
int output (const char *input,...)
 
struct LineIteminsertNewLineItem (struct LineItem *prev, struct LineItem *next, int maxTextLength)
 
void deleteLineItem (struct LineItem *line, int deleteAllBelow)
 
void setLineAlign (int align)
 
void consoleClear ()
 
void printfBanner (int width, int startAt)
 
void animateBanner (int slideIn)
 
void printEmptyBox (int w, int h)
 
void animateBox (int wFrom, int hFrom, int wTo, int hTo)
 
void strcatRepeat (char *target, const char *source, unsigned int howOften)
 
char lower_to_upper (char ch1)
 
void drawMainMenu ()
 
int mainMenu ()
 
void mainMenuReactToEnter (int pPos)
 
void drawLeftArrow (char *menu, int pPos)
 
void drawRightArrow (char *menu, int pPos)
 
void showHallOfShame (int highlight, int startFrom)
 
int updateSaveHoS (char *victor, char *victim, int moves)
 
int extractMoves (char *line)
 
int getOldFileLength ()
 
int newBoard (struct board *target, unsigned int width, unsigned int height)
 
void clearBoard (struct board *target)
 
char * calcFieldAddress (struct board *target, int x, int y)
 
char getField (struct board *target, int x, int y)
 
void setField (struct board *target, int x, int y, char value)
 
void freeBoard (struct board *target)
 
void drawBoard (struct board *target)
 
void gameFunction ()
 
void playerAction ()
 
void drawCoin (int pos, char CoinType)
 
void throwCoin (int pos, char player)
 
void startGame ()
 
int neighbourRow (int x, int y, int xMovement, int yMovement, char player)
 
char checkForWinner (int x, int y, char player)
 
void clearAll ()
 
int checkDraw ()
 
int irand (int a, int e)
 
void animateFalling (struct board *currBoard, unsigned int yPos, char CoinType)
 
void showRules ()
 
void showCredits ()
 

Dokumentation der Funktionen

void animateBanner ( int  slideIn)

Animate ASCII art logo.

Parameter
slideInSet to 1 for slide in from top, 0 for slide out.

Definiert in Zeile 378 der Datei system.c.

Benutzt consoleBufferWidth, consoleClear() und printfBanner().

Wird benutzt von mainMenu().

378  {
379  int i = (slideIn ? 5 : 0);
380  while(i >= 0 && i <=5){
381  consoleClear();
383  Sleep(50);
384  i += (slideIn ? -1 : +1);
385  }
386 }
void printfBanner(int width, int startAt)
Definition: system.c:356
int consoleBufferWidth
Definition: system.c:8
void consoleClear()
Definition: system.c:428

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

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

void animateBox ( int  wFrom,
int  hFrom,
int  wTo,
int  hTo 
)

Change the horizontal align of the current line within the buffer box

Parameter
alignLeft align (-1), centered (0) or right align (+1).

Definiert in Zeile 392 der Datei system.c.

Benutzt printEmptyBox().

Wird benutzt von mainMenu() und showCredits().

392  {
393  int w = wFrom;
394  int h = hFrom;
395  while(w < wTo){
396  printEmptyBox(w,h);
397  w++;
398  }
399  while(h < hTo){
400  printEmptyBox(w,h);
401  h++;
402  }
403  while(h > hTo){
404  printEmptyBox(w,h);
405  h--;
406  }
407  while(w > wTo){
408  printEmptyBox(w,h);
409  w--;
410  }
411 }
void printEmptyBox(int w, int h)
Definition: system.c:418

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

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

void animateFalling ( struct board currBoard,
unsigned int  xPos,
char  CoinType 
)

this Method animates a falling Coin by moving the coin one field lower than wait and repeat this until there's another coin or it reaches the end of the board This Method fully cleans the board up after finishing–>means: after execution the board will look like nothing ever happened. The animated coin is not saved permanetly saved on the board

Parameter
xPosthe row in which the coin is going to fall
currBoardthe board with the current game state
CoinTypevariable to determine if the current Player has 'X' or '0' as Coin

final clean up deleting the last coin if this last part of the code will be deleted the last position of the coin will be permantely saved on the supplied board

Definiert in Zeile 446 der Datei gamefunction.c.

Benutzt calcFieldAddress(), drawBoard(), FIELD_EMPTY, FIELD_PLAYER1 und board::height.

446  {
447  //determing which type of coin is going to fall
448  char coin;
449  if(CoinType == FIELD_PLAYER1){
450  coin='X';
451  }else{
452  coin='O';
453  }
454 
455 
456 
457 
458 
459  unsigned int currY=currBoard->height-1;
460  char* old;
461  //(getField(currBoard,xPos,currY)!=FIELD_EMPTY)&&
462  while((currY!=0)){
463  //no deletions possible at first run of loop
464  if(currY!=currBoard->height-1){
465  //delete coin one field above
466  old=calcFieldAddress(currBoard,xPos,currY+1);
467  old=&FIELD_EMPTY;
468  }
469  //setting coin one field lower
470  char* curr=calcFieldAddress(currBoard,xPos,currY);
471  curr=&coin;
472  currY--;
473 
474  drawBoard(currBoard);
475  //improvised wait Method
476  int x=0;
477  for(x=0;x<1000000;x++){
478  //funny nothing will ever happen here :D
479  x=x;
480  };
481 
482 
483  }
488  old=calcFieldAddress(currBoard,xPos,currY);
489 // &old=FIELD_EMPTY;
490  old=FIELD_EMPTY;
491  //return;
492 
493 }
const char FIELD_PLAYER1
Definition: variables.h:39
const char FIELD_EMPTY
Definition: variables.h:38
unsigned int height
Definition: variables.h:45
char * calcFieldAddress(struct board *target, int x, int y)
Definition: board.c:60
void drawBoard(struct board *target)
Definition: board.c:103

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

char* calcFieldAddress ( struct board target,
int  x,
int  y 
)

Internal helper method to calculate the data offset of a given field on the board

Parameter
targetBoard to base calculations on.
xX-coordinate.
yY-coordinate.
Rückgabe
Pointer to requested field.

Definiert in Zeile 60 der Datei board.c.

Benutzt board::content und board::width.

Wird benutzt von animateFalling(), getField() und setField().

60  {
61  int offset = y*(target->width) + x;
62  return (target->content + offset);
63 }
unsigned int width
Definition: variables.h:44
char * content
Definition: variables.h:47

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

int checkDraw ( )

Checks if the board is full. Only possible if nobody has won earlier.

Rückgabe
1 for game can go on - 0 for game ended with a draw

Definiert in Zeile 419 der Datei gamefunction.c.

Benutzt FIELD_EMPTY, gameField, gameFieldHeigth, gameFieldWidth und getField().

Wird benutzt von gameFunction().

419  {
420  int i =0;
421  for(i=0;i<gameFieldWidth;i++){
423  return 1;
424  }
425  }
426  return 0;
427 }
char getField(struct board *target, int x, int y)
Definition: board.c:72
struct board gameField
Definition: gamefunction.c:11
const char FIELD_EMPTY
Definition: variables.h:38
int gameFieldHeigth
Definition: gamefunction.c:13
int gameFieldWidth
Definition: gamefunction.c:12

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

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

char checkForWinner ( int  x,
int  y,
char  player 
)

Look for a winner of the current game. First vertically, then horizontally and then the 2 diagonal rows. lineCount is the number of Coins which are neighbours.

Parameter
xX-Position of last dropped coin.
yY-Position of last dropped coin.
playerThe last coin which was dropped to the game, as the starting point of this algorithm.
Rückgabe
The winnig coin type is returned X or O.

Definiert in Zeile 356 der Datei gamefunction.c.

Benutzt FIELD_EMPTY und neighbourRow().

Wird benutzt von throwCoin().

356  {
357  // horizontal check first right then left summarized together
358  int lineCount = 0;
359  lineCount = 1 + neighbourRow(x+1,y,+1,0,player) + neighbourRow(x-1,y,-1,0,player);
360  if(lineCount >= 4){
361  return player;
362  }
363  else{
364  lineCount=0;
365  }
366 
367  // vertical check first up and then down summarized together
368  lineCount = 1 + neighbourRow(x,y+1,0,1,player) + neighbourRow(x,y-1,0,-1,player);
369  if(lineCount >= 4){
370  return player;
371  }
372  else{
373  lineCount=0;
374  }
375 
376  // diagonal checks
377 
378  // first variant /
379  lineCount = 1 + neighbourRow(x-1,y+1,-1,1,player) + neighbourRow(x+1,y-1,1,-1,player);
380  if(lineCount >= 4){
381  return player;
382  }
383  else{
384  lineCount=0;
385  }
386  //second variant
387  lineCount = 1 + neighbourRow(x+1,y+1,1,1,player) + neighbourRow(x-1,y-1,-1,-1,player);
388  if(lineCount >= 4){
389  return player;
390  }
391  else{
392  lineCount=0;
393  }
394  return FIELD_EMPTY;
395 
396 }
int neighbourRow(int x, int y, int xMovement, int yMovement, char player)
Definition: gamefunction.c:406
const char FIELD_EMPTY
Definition: variables.h:38

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

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

void clearAll ( )

Clears all elements which are used within the gameflow Board, loop condition, positions

Rückgabe
no return because of void

Definiert in Zeile 171 der Datei gamefunction.c.

Benutzt clearBoard(), coinPosition, end, freeBoard(), gameField, gameFieldCreated, playersTurn und victor.

Wird benutzt von gameFunction().

171  {
172  //clear board
175  end = 0;
176  //reset some variables
177  victor = '\0';
178  gameFieldCreated = 0; //set to 0, because board was deleted
179  playersTurn = 0; //player1 starts the game
180  coinPosition = 1; //where the coin is actually placed
181  return;
182 }
struct board gameField
Definition: gamefunction.c:11
void clearBoard(struct board *target)
Definition: board.c:43
int coinPosition
Definition: gamefunction.c:15
int end
Definition: gamefunction.c:20
int playersTurn
Definition: gamefunction.c:16
void freeBoard(struct board *target)
Definition: board.c:95
char victor
Definition: gamefunction.c:18
int gameFieldCreated
Definition: gamefunction.c:14

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

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

void clearBoard ( struct board target)

Removes all chips from the board

Parameter
targetAlready existing board.

Definiert in Zeile 43 der Datei board.c.

Benutzt board::content, FIELD_EMPTY und board::numberOfFields.

Wird benutzt von clearAll() und newBoard().

43  {
44  //fills the whole board with space chars
45  int i;
46  for (i = 0; i < target->numberOfFields; i++) {
47  *((target->content) + i) = FIELD_EMPTY;
48  }
49  //add null terminator which allows direct board content debug output
50  *(target->content + i) = '\0';
51 }
const char FIELD_EMPTY
Definition: variables.h:38
unsigned int numberOfFields
Definition: variables.h:46
char * content
Definition: variables.h:47

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

void consoleClear ( )

Clears the console

Definiert in Zeile 428 der Datei system.c.

Wird benutzt von animateBanner(), flushBuffer() und gameFunction().

428  {
429  //should clear the console on most plattforms
430  #ifdef _WIN32
431  //detected windows System (both 32 and 64bit)-->use windows clear
432  system("CLS");
433  #else
434  //detected other (non windows) System-->unse Unix-clear
435  system("clear");
436  #endif
437 
438 }

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

int copyChar ( char *  src,
char *  dst 
)

Copy one character (UTF8-aware) from the source string to the target string and append a null byte afterwards

Parameter
srcSource string pointer.
dstDestination string pointer.
Rückgabe
Returns how many bytes were copied

Definiert in Zeile 114 der Datei system.c.

Benutzt LineItem::length.

Wird benutzt von output().

114  {
115 
116  //determine the length of the string, see http://en.wikipedia.org/wiki/UTF-8#Description
117  int bytes = 0;
118  char slider = *src;
119  while ((slider & 0x80) == 0x80){
120  slider<<=1;
121  bytes++;
122  }
123  bytes+=!bytes;
124 
125  //copy <bytes>-byte long character
126  int cursor = 0;
127  int length = bytes;
128  while (length--){
129  dst[cursor] = src[cursor];
130  cursor++;
131  }
132 
133  //append null byte and return length of copied data
134  dst[cursor] = '\0';
135  return bytes;
136 }

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

void deleteLineItem ( struct LineItem line,
int  deleteAllBelow 
)

Delete the given line from the buffer

Parameter
linePointer to the line struct to delete.
deleteAllBelowDelete all the following lines as well (1) or just the given line (0).

Definiert in Zeile 84 der Datei system.c.

Benutzt display, OutBuffer::lineCount, LineItem::next, LineItem::prev und LineItem::text.

Wird benutzt von flushBuffer() und startBuffer().

84  {
85  if (line==NULL){
86  return;
87  }
88 
89  if (!deleteAllBelow){
90  //fix the chain by connecting the following line to the previous
91  if (line->prev!=NULL){
92  line->prev->next = line->next;
93  }
94  } else {
95  //recursive delete requested
96  if (line->next!=NULL){
97  deleteLineItem(line->next,1);
98  }
99  if (line->prev!=NULL){
100  line->prev->next = NULL;
101  }
102  }
103  free(line->text);
104  free(line);
105  display.lineCount--;
106 }
struct OutBuffer display
Definition: system.c:6
char * text
Definition: variables.h:18
struct LineItem * prev
Definition: variables.h:16
void deleteLineItem(struct LineItem *line, int deleteAllBelow)
Definition: system.c:84
struct LineItem * next
Definition: variables.h:17
int lineCount
Definition: variables.h:24

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

void drawBoard ( struct board target)

Outputs a graphical representation of the given board to the console

Parameter
targetBoard to draw.

Definiert in Zeile 103 der Datei board.c.

Benutzt EXITCODE_OUTOFMEMORY, FIELD_EMPTY, FIELD_PLAYER1, FIELD_PLAYER2, FONT_COIN_EMPTY, FONT_COIN_PLAYER1, FONT_COIN_PLAYER2, FONT_DPIPE_BOTTOM_LEFT, FONT_DPIPE_BOTTOM_RIGHT, FONT_DPIPE_CROSSING, FONT_DPIPE_HORI_BAR, FONT_DPIPE_TBAR_DOWN, FONT_DPIPE_TBAR_LEFT, FONT_DPIPE_TBAR_RIGHT, FONT_DPIPE_TBAR_UP, FONT_DPIPE_TOP_LEFT, FONT_DPIPE_TOP_RIGHT, FONT_DPIPE_VERT_BAR, getField(), board::height, output(), strcatRepeat() und board::width.

Wird benutzt von animateFalling(), gameFunction() und playerAction().

103  {
104  //calculate dimensions of output and reserve memory
105  char* canvas;
106 
107  //4 chars wide per field plus ending bar and newline
108  int charsWidth = target->width*4 + 2;
109  //4 chars wide per field plus stand
110  int charsHeight = target->height*2 + 3;
111  //multiply by 6 for full UTF-8 eventualities plus 1 for string terminator \0
112  int totalSize = charsWidth*charsHeight*6 + 1;
113 
114  canvas = malloc(totalSize * sizeof(char));
115 
116  if (canvas==NULL) {
117  exit(EXITCODE_OUTOFMEMORY);
118  }
119 
120  //writing-to-canvas setup
121  canvas[0] = '\0';
122 
123  int fieldX;
124  int fieldY;
125 
126  //loop over rows and build 2 console lines (grid + values) per loop
127  for(fieldY = target->height - 1; fieldY >= 0; fieldY--) {
128 
129  //grid row
130  for(fieldX = 0; fieldX < target->width; fieldX++) {
131  if (fieldY == target->height - 1 && fieldX == 0) {
132  strcat(canvas,FONT_DPIPE_TOP_LEFT);
133  } else if (fieldY == target->height - 1) {
134  strcat(canvas,FONT_DPIPE_TBAR_DOWN);
135  } else if (fieldX == 0) {
136  strcat(canvas,FONT_DPIPE_TBAR_RIGHT);
137  } else {
138  strcat(canvas,FONT_DPIPE_CROSSING);
139  }
141  }
142 
143  //end of grid row
144  if (fieldY == target->height - 1) {
145  strcat(canvas,FONT_DPIPE_TOP_RIGHT);
146  } else {
147  strcat(canvas,FONT_DPIPE_TBAR_LEFT);
148  }
149  strcat(canvas,"\n");
150 
151  //value row
152  for(fieldX = 0; fieldX < target->width; fieldX++) {
153  strcat(canvas,FONT_DPIPE_VERT_BAR);
154  strcat(canvas," ");
155  char field = getField(target,fieldX,fieldY);
156  if (field==FIELD_EMPTY) strcat(canvas,FONT_COIN_EMPTY);
157  if (field==FIELD_PLAYER1) strcat(canvas,FONT_COIN_PLAYER1);
158  if (field==FIELD_PLAYER2) strcat(canvas,FONT_COIN_PLAYER2);
159  strcat(canvas," ");
160  }
161 
162  //finish value row
163  strcat(canvas,FONT_DPIPE_VERT_BAR);
164  strcat(canvas,"\n");
165 
166  }
167 
168  //build stand
169  for(fieldX = 0; fieldX < target->width; fieldX++) {
170  if (fieldX == 0) {
171  strcat(canvas,FONT_DPIPE_TBAR_RIGHT);
173  } else {
174  strcat(canvas,FONT_DPIPE_TBAR_UP);
176  }
177  }
178  strcat(canvas,FONT_DPIPE_TBAR_LEFT);
179  strcat(canvas,"\n");
180  for(fieldX = 0; fieldX < target->width; fieldX++) {
181  if (fieldX == 0) {
182  strcat(canvas,FONT_DPIPE_BOTTOM_LEFT);
184  } else {
186  }
187  }
188  strcat(canvas,FONT_DPIPE_BOTTOM_RIGHT);
189  strcat(canvas,"\n");
190 
191  //we're done, output the whole thing
192  output("%s",canvas);
193  free(canvas);
194 }
char * FONT_DPIPE_TBAR_RIGHT
Definition: fancyfont.c:10
void strcatRepeat(char *target, const char *source, unsigned int howOften)
Definition: system.c:458
char * FONT_DPIPE_TOP_LEFT
Definition: fancyfont.c:4
char * FONT_DPIPE_TBAR_UP
Definition: fancyfont.c:12
char getField(struct board *target, int x, int y)
Definition: board.c:72
const char FIELD_PLAYER2
Definition: variables.h:40
const char FIELD_PLAYER1
Definition: variables.h:39
const int EXITCODE_OUTOFMEMORY
Definition: variables.h:3
char * FONT_DPIPE_BOTTOM_LEFT
Definition: fancyfont.c:6
const char FIELD_EMPTY
Definition: variables.h:38
char * FONT_DPIPE_TBAR_LEFT
Definition: fancyfont.c:11
unsigned int height
Definition: variables.h:45
char * FONT_COIN_PLAYER2
Definition: fancyfont.c:16
char * FONT_COIN_EMPTY
Definition: fancyfont.c:17
char * FONT_DPIPE_BOTTOM_RIGHT
Definition: fancyfont.c:7
unsigned int width
Definition: variables.h:44
char * FONT_DPIPE_TBAR_DOWN
Definition: fancyfont.c:13
char * FONT_DPIPE_VERT_BAR
Definition: fancyfont.c:8
char * FONT_COIN_PLAYER1
Definition: fancyfont.c:15
char * FONT_DPIPE_HORI_BAR
Definition: fancyfont.c:9
char * FONT_DPIPE_TOP_RIGHT
Definition: fancyfont.c:5
int output(const char *input,...)
Definition: system.c:144
char * FONT_DPIPE_CROSSING
Definition: fancyfont.c:14

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

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

void drawCoin ( int  pos,
char  CoinType 
)

Draws the Coin above the board

Parameter
poswhich position should the coin be drawn to.
coinTypewhich coin should be drawn.
Rückgabe
no return because of void

Definiert in Zeile 291 der Datei gamefunction.c.

Benutzt EXITCODE_OUTOFMEMORY, FIELD_PLAYER1, FONT_COIN_PLAYER1, FONT_COIN_PLAYER2, gameFieldWidth und output().

Wird benutzt von playerAction().

291  {
292  int i;
293  char *canvas;
294  canvas = malloc((gameFieldWidth * 4) * sizeof(char));
295  //if alloc failed return
296  if (canvas==NULL) {
297  exit(EXITCODE_OUTOFMEMORY);
298  }
299  //writing-to-canvas setup (clear string)
300  canvas[0] = '\0';
301  //keep boundaries
302  if (pos < gameFieldWidth || pos > 1) {
303  strcat(canvas,"(");
304  for(i = 1; i <= gameFieldWidth; i++) {
305  //if the coin should be placed draw it
306  if (i == pos) {
307  strcat(canvas," ");
308  if(CoinType == FIELD_PLAYER1){
309  strcat(canvas,FONT_COIN_PLAYER1);
310  }else{
311  strcat(canvas,FONT_COIN_PLAYER2);
312  }
313 
314  strcat(canvas," ");
315  }else{
316  //draw empty
317  strcat(canvas," ");
318  }
319  }
320  strcat(canvas,")\n");
321  //we're done, output the whole thing
322  output("%s",canvas);
323  }
324  //free memory
325  free(canvas);
326  return;
327 }
const char FIELD_PLAYER1
Definition: variables.h:39
const int EXITCODE_OUTOFMEMORY
Definition: variables.h:3
char * FONT_COIN_PLAYER2
Definition: fancyfont.c:16
char * FONT_COIN_PLAYER1
Definition: fancyfont.c:15
int gameFieldWidth
Definition: gamefunction.c:12
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:

void drawLeftArrow ( char *  menu,
int  pPos 
)

This method manipulates the array so the left arrow (to indicate the cursor position) is added to the array.

Parameter
menucontains the pointer to the char-array containing main menu in it's current status
pPoscontains the cursor Position of the cursor

Definiert in Zeile 11 der Datei mainMenu.c.

Wird benutzt von drawMainMenu().

11  {
12 #ifndef DEBUG
13  if (pPos>1) pPos--;
14 #endif // DEBUG
15  menu[pPos*20]='-';
16  menu[pPos*20+1]='>';
17  menu[pPos*20+2]=' ';
18 }

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

void drawMainMenu ( )
void drawRightArrow ( char *  menu,
int  pPos 
)

This method manipulates the array so the right arrow (to indicate the cursor position) is added to the array.

Parameter
menucontains the pointer to the char-array containing main menu in it's current status
pPoscontains the cursor Position of the cursor

Definiert in Zeile 24 der Datei mainMenu.c.

Wird benutzt von drawMainMenu().

24  {
25 #ifndef DEBUG
26  if (pPos>1) pPos--;
27 #endif // DEBUG
28  menu[(pPos+1)*20-2]='-';
29  menu[(pPos+1)*20-3]='<';
30  menu[(pPos+1)*20-4]=' ';
31 }

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

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:

void flushBuffer ( )

Prints the screen (output buffer) and clears the buffer

Definiert in Zeile 188 der Datei system.c.

Benutzt LineItem::align, consoleBufferHeight, consoleBufferWidth, consoleClear(), csbi, deleteLineItem(), display, EXITCODE_BUFFERERROR, EXITCODE_OUTOFMEMORY, FANCY_FONT, OutBuffer::first, FONT_DPIPE_BOTTOM_LEFT, FONT_DPIPE_BOTTOM_RIGHT, FONT_DPIPE_HORI_BAR, FONT_DPIPE_TOP_LEFT, FONT_DPIPE_TOP_RIGHT, FONT_DPIPE_VERT_BAR, OutBuffer::hAlign, initBuffer(), OutBuffer::last, LineItem::length, OutBuffer::lineCount, OutBuffer::maxTextLength, LineItem::next, printfBanner(), strcatRepeat(), LineItem::text, UTF_MULTIPLIER und OutBuffer::vAlign.

Wird benutzt von addFrame(), drawMainMenu(), gameFunction(), mainMenuReactToEnter(), playerAction(), printEmptyBox(), showCredits(), showHallOfShame() und showRules().

188  {
189  if (display.first==NULL || display.last==NULL){
190  exit(EXITCODE_BUFFERERROR);
191  }
192 
193  if(GetConsoleScreenBufferInfo(GetStdHandle( STD_OUTPUT_HANDLE ),&csbi)) {
194  consoleBufferWidth = csbi.dwSize.X;
195  consoleBufferHeight = csbi.dwSize.Y;
196  if(!FANCY_FONT){
197  consoleBufferHeight = 23;
198  }
199  }
200 
201  consoleClear();
202 
203  /*### DEBUGGING START ###*/
204 #ifdef DEBUG
205 // static int flushed = 0;
206 // printf("DEBUG: printing %d-line buffer (%d.)",display.lineCount,++flushed);
207 // struct LineItem* debugchain = display.first;
208 // printf(", chain: %d",(int)debugchain);
209 // do {
210 // printf("(%d)->%d",debugchain->length,(int)debugchain->next);
211 // debugchain = debugchain->next;
212 // } while (debugchain!=NULL);
213 // printf(". [END:%d]\n--------------------------------------------------\n",(int)display.last);
214 #endif // DEBUG
215  /*### DEBUGGING END ###*/
216 
217  int i;
218 
219  if (display.vAlign==0){
220  int marginTopSize = consoleBufferHeight/2 - display.lineCount/2 -1;
221  if (marginTopSize > 5){
223  marginTopSize -= 5;
224  }
225  for(i = 0; i < marginTopSize; i++){
226  printf("\n");
227  }
228  }
229 
230  struct LineItem* current = display.first;
231  const int MAX_CHARS_BORDER = 4;
232  const int MAX_BYTES_PER_LINE = (display.maxTextLength + MAX_CHARS_BORDER + 1) * sizeof(char) * UTF_MULTIPLIER;
233  char* collector = malloc(display.lineCount * MAX_BYTES_PER_LINE + 1);
234  if (collector==NULL){
235  exit(EXITCODE_OUTOFMEMORY);
236  }
237  collector[0] = '\0';
238 
239  //calculate left margin for box position
240  int leftMarginSize = 1;
241  if (display.hAlign==0){
242  leftMarginSize = consoleBufferWidth/2 - display.maxTextLength/2 - 1;
243  }
244  char* leftMargin = malloc(sizeof(char)*leftMarginSize+1);
245  if (leftMargin==NULL){
246  exit(EXITCODE_OUTOFMEMORY);
247  }
248  memset(leftMargin,' ',leftMarginSize);
249  leftMargin[leftMarginSize] = '\0';
250 
251  //draw header line
252  strcat(collector,leftMargin);
253  strcat(collector,FONT_DPIPE_TOP_LEFT);
254  for (i = 0; i < display.maxTextLength; i++){
255  strcat(collector,FONT_DPIPE_HORI_BAR);
256  }
257  strcat(collector,FONT_DPIPE_TOP_RIGHT);
258  strcat(collector,"\n");
259 
260  //temporary:
261  printf("%s",collector);
262  collector[0] = '\0';
263 
264  //draw lines
265  int leftPaddingSize = 0;
266  int rightPaddingSize;
267 
268  while (current!=NULL){
269  //padding calculations
270  switch (current->align){
271  case -1:
272  leftPaddingSize = 0;
273  break;
274  case 0:
275  leftPaddingSize = display.maxTextLength/2 - current->length/2;
276  break;
277  case +1:
278  leftPaddingSize = display.maxTextLength - current->length;
279  break;
280  }
281  rightPaddingSize = display.maxTextLength - current->length - leftPaddingSize;
282 
283  //left border and left padding
284  strcat(collector,leftMargin);
285  strcat(collector,FONT_DPIPE_VERT_BAR);
286  strcatRepeat(collector," ",leftPaddingSize);
287  //text
288  strcat(collector,current->text);
289  //right padding and right border
290  strcatRepeat(collector," ",rightPaddingSize);
291  strcat(collector,FONT_DPIPE_VERT_BAR);
292  strcat(collector,"\n");
293  current = current->next;
294  //temporary:
295  printf("%s",collector);
296  collector[0] = '\0';
297  }
298  //draw footer line
299  strcat(collector,leftMargin);
300  strcat(collector,FONT_DPIPE_BOTTOM_LEFT);
301  for (i = 0; i < display.maxTextLength; i++){
302  strcat(collector,FONT_DPIPE_HORI_BAR);
303  }
304  strcat(collector,FONT_DPIPE_BOTTOM_RIGHT);
305 
306  printf("%s",collector);
307  collector[0] = '\0';
308 
309  free(collector);
310 
312  initBuffer();
313 }
struct OutBuffer display
Definition: system.c:6
int vAlign
Definition: variables.h:25
char * FONT_DPIPE_TOP_LEFT
Definition: fancyfont.c:4
void initBuffer()
Definition: system.c:15
char * text
Definition: variables.h:18
struct LineItem * last
Definition: variables.h:22
const int EXITCODE_OUTOFMEMORY
Definition: variables.h:3
struct LineItem * first
Definition: variables.h:21
char * FONT_DPIPE_BOTTOM_LEFT
Definition: fancyfont.c:6
void printfBanner(int width, int startAt)
Definition: system.c:356
void deleteLineItem(struct LineItem *line, int deleteAllBelow)
Definition: system.c:84
char * FONT_DPIPE_BOTTOM_RIGHT
Definition: fancyfont.c:7
int FANCY_FONT
Definition: fancyfont.c:3
const int EXITCODE_BUFFERERROR
Definition: variables.h:4
int length
Definition: variables.h:13
int consoleBufferWidth
Definition: system.c:8
void strcatRepeat(char *target, const char *source, unsigned int howOften)
Definition: system.c:458
struct LineItem * next
Definition: variables.h:17
int consoleBufferHeight
Definition: system.c:9
int maxTextLength
Definition: variables.h:23
char * FONT_DPIPE_VERT_BAR
Definition: fancyfont.c:8
void consoleClear()
Definition: system.c:428
int hAlign
Definition: variables.h:26
CONSOLE_SCREEN_BUFFER_INFO csbi
Definition: system.c:7
const int UTF_MULTIPLIER
Definition: variables.h:30
char * FONT_DPIPE_HORI_BAR
Definition: fancyfont.c:9
int lineCount
Definition: variables.h:24
int align
Definition: variables.h:15
char * FONT_DPIPE_TOP_RIGHT
Definition: fancyfont.c:5

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

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

void freeBoard ( struct board target)

Frees memory taken by a board (warning: makes it unusable until newBoard() has been called again)

Parameter
targetBoard to free memory.

Definiert in Zeile 95 der Datei board.c.

Benutzt board::content.

Wird benutzt von clearAll().

95  {
96  free(target->content);
97 }
char * content
Definition: variables.h:47

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

void gameFunction ( )

gameFunction first forces users to enter their names and checks them for different mistakes after that startGame() is called within the gameloop, which processes the main game flow last but not least we check for a winner or check for draw and continue to Hall of Shame.

Rückgabe
no return because of void

Definiert in Zeile 28 der Datei gamefunction.c.

Benutzt checkDraw(), clearAll(), consoleClear(), drawBoard(), end, FIELD_PLAYER1, FIELD_PLAYER2, flushBuffer(), gameField, irand(), lower_to_upper(), moves, NO_HIGHLIGHT, output(), player1, player2, playersTurn, showHallOfShame(), startBuffer(), startGame(), updateSaveHoS() und victor.

Wird benutzt von mainMenuReactToEnter().

28  {
29  moves = 0;
30  playersTurn = irand(0,1);
31  char keyStrokeName[2];
32  keyStrokeName[1] = '\0';
33  player1[0] = '\0';
34  player2[0] = '\0';
35  /*
36  player1 name
37  loop as long player did not press enter
38  */
39  do {
40  //check length
41  if (strlen(player1)>=19)
42  break;
43  //buffer
44  startBuffer(50);
45  output("Please enter a name for Player 1: \n");
46  output("> %s_\n", player1);
47  //flush buffer
48  flushBuffer();
49  //wait for a new char and give it to var
50  keyStrokeName[0] = getch();
51  //check for special inputs
52  if ((keyStrokeName[0] == '\b') && (strlen(player1)) > 0) {
53  //if backspace was entered
54  player1[strlen(player1)-1] = '\0'; //set nilbyte to pointer-1
55  }
56  else if (keyStrokeName[0] == '\r') {} //if enter do nothing
57  else if ((keyStrokeName[0] == '\b') && (strlen(player1)) == 0) {} //if string empty and backspace entered do nothing
58  else {
59  keyStrokeName[0] = lower_to_upper(keyStrokeName[0]);
60  strcat(player1, keyStrokeName);
61  }
62  } while(((keyStrokeName[0] != '\r') && (player1[0] != '\0') && !(strlen(player1)>15)) || (strlen(player1)<3));
63  /*
64  player2 name
65  loop as long player did not press enter
66  */
67  do {
68  //check length
69  if (strlen(player1)>=19)
70  break;
71  //buffer
72  startBuffer(55);
73  output("Please enter a name for Player 1: \n");
74  output("%s\n\n", player1);
75  output("Please enter a name for Player 2: \n");
76  output("> %s_\n", player2);
77  //flush buffer
78  flushBuffer();
79  //wait for a new char and give it to var
80  keyStrokeName[0] = getch();
81  //check for special inputs
82  if ((keyStrokeName[0] == '\b') && (strlen(player2)) > 0) {
83  //if backspace was entered
84  player2[strlen(player2)-1] = '\0'; //set nilbyte to pointer-1
85  }
86  else if (keyStrokeName[0] == '\r') {} //if enter do nothing
87  else if ((keyStrokeName[0] == '\b') && (strlen(player2)) == 0) {} //if string empty and backspace entered do nothing
88  else {
89  keyStrokeName[0] = lower_to_upper(keyStrokeName[0]);
90  strcat(player2, keyStrokeName);
91  }
92  } while(((keyStrokeName[0] != '\r') && (player2[0] != '\0') && !(strlen(player2)>15)) || (strlen(player2)<3));
93  //check that player1 and player2 names are not the same
94  if (strcmp(player1, player2)==0) {
95  player2[(strlen(player2))] = '1'; // add a symbolic 1 to the end of the name ;-)
96  player2[(strlen(player2))+1] = '\0';
97  }
98  //start buffer for completion
99  startBuffer(55);
100  output("Thank you %s and %s! The game will start now.\n\nPlease press enter to continue...", player1, player2);
101  flushBuffer();
102  //wait for a key
103  getch();
104  //Loop till the game is done
105  int winnerLine =NO_HIGHLIGHT;
106  while(end == 0){
107  // start the game flow
108  startGame();
109  //check for winners or draw
110  if( victor == FIELD_PLAYER1){
111  end = 1;
112  moves = (moves/2)+(moves%2);
113  consoleClear();
114  startBuffer(50);
116  winnerLine = updateSaveHoS(player1,player2,moves);
117  //output victor
118  output("%s has won!\n\n", player1);
119  output("Press any key to continue to Hall of Shame...\n");
120  //show all
121  flushBuffer();
122  //wait for any keystroke
123  getch();
124  //continue to hall of shame
125  showHallOfShame(winnerLine,winnerLine-4);
126  }
127  else if( victor == FIELD_PLAYER2){
128  end = 1;
129  moves = (moves/2)+(moves%2);
130  consoleClear();
131  startBuffer(50);
133  winnerLine = updateSaveHoS(player2,player1,moves);
134  //output victor
135  output("%s has won!\n\n", player2);
136  output("Press any key to continue to Hall of Shame...\n");
137  //show all
138  flushBuffer();
139  //wait for any keystroke
140  getch();
141  //continue to hall of shame
142  showHallOfShame(winnerLine, winnerLine-4);
143  }
144  if(checkDraw() == 0){
145  end = 1;
146  consoleClear();
147  startBuffer(50);
149  //output draw
150  output("Game ended with a draw...\n\n");
151  output("Press any key to continue to Hall of Shame...\n");
152  //show all
153  flushBuffer();
154  //wait for any keystroke
155  getch();
156  //continue to hall of shame
158  }
159  }
160 
161  //free our game-board
162  clearAll();
163  return;
164 }
char player2[20]
Definition: gamefunction.c:9
void showHallOfShame(int highlight, int startFrom)
Definition: hallofshame.c:13
void consoleClear()
Definition: system.c:428
void startGame()
Definition: gamefunction.c:333
const char FIELD_PLAYER2
Definition: variables.h:40
const char FIELD_PLAYER1
Definition: variables.h:39
struct board gameField
Definition: gamefunction.c:11
void flushBuffer()
Definition: system.c:188
void startBuffer(int maxTextLength)
Definition: system.c:319
int end
Definition: gamefunction.c:20
int updateSaveHoS(char *victor, char *victim, int moves)
Definition: hallofshame.c:101
char lower_to_upper(char ch1)
Definition: system.c:444
int playersTurn
Definition: gamefunction.c:16
int irand(int a, int e)
Definition: gamefunction.c:433
int checkDraw()
Definition: gamefunction.c:419
void clearAll()
Definition: gamefunction.c:171
char victor
Definition: gamefunction.c:18
void drawBoard(struct board *target)
Definition: board.c:103
int moves
Definition: gamefunction.c:19
char player1[20]
Definition: gamefunction.c:8
const int NO_HIGHLIGHT
Definition: variables.h:67
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:

char getField ( struct board target,
int  x,
int  y 
)

Gets what is on a given field of the board (returns FIELD_XYZ constant)

Parameter
targetBoard to read.
xX-coordinate.
yY-coordinate.
Rückgabe
Value of requested field.

Definiert in Zeile 72 der Datei board.c.

Benutzt calcFieldAddress(), FIELD_OUTOFBOUNDS, board::height und board::width.

Wird benutzt von checkDraw(), drawBoard(), neighbourRow() und throwCoin().

72  {
73  if (x<0 || y<0 || x>=target->width || y>=target->height)
74  {
75  return FIELD_OUTOFBOUNDS;
76  }
77  return *calcFieldAddress(target, x, y);
78 }
unsigned int height
Definition: variables.h:45
const char FIELD_OUTOFBOUNDS
Definition: variables.h:41
unsigned int width
Definition: variables.h:44
char * calcFieldAddress(struct board *target, int x, int y)
Definition: board.c:60

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

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 HelloWorld ( )

Definiert in Zeile 5 der Datei dummy.c.

Benutzt output() und testerVariable.

6 {
7  output("Hello World! (%i)\n",testerVariable);
8 }
int testerVariable
Definition: dummy.c:3
int output(const char *input,...)
Definition: system.c:144

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

void initBuffer ( )

Initializes buffering Warning: Only call once at program start

Definiert in Zeile 15 der Datei system.c.

Benutzt display, OutBuffer::first, OutBuffer::hAlign, OutBuffer::last, OutBuffer::lineCount und OutBuffer::vAlign.

Wird benutzt von flushBuffer().

15  {
16  //set text size maximum and insert a first empty line
17  display.first = NULL;
18  display.last = NULL;
19  display.lineCount = 0;
20  display.vAlign = 0;
21  display.hAlign = 0;
22 }
struct OutBuffer display
Definition: system.c:6
int vAlign
Definition: variables.h:25
struct LineItem * last
Definition: variables.h:22
struct LineItem * first
Definition: variables.h:21
int hAlign
Definition: variables.h:26
int lineCount
Definition: variables.h:24

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

struct LineItem* insertNewLineItem ( struct LineItem prev,
struct LineItem next,
int  maxTextLength 
)

Add a new line to the buffer, may be inserted in the middle or appended to the end Info: Horizontal align is inherited from previous line, default -1. Can be changed with setLineAlign() Warning: Use output() to add text, this method is only used internally

Parameter
prevPointer to the previous line struct, NULL appends line to start of buffer.
nextPointer to the next line struct, NULL if this is the last element (most used case).
alignText align for the new line, -1 is left aligned, 0 is centered, +1 is right aligned.
maxTextLengthMaximum number of characters for this line.
Rückgabe
Pointer to new created struct.

Definiert in Zeile 34 der Datei system.c.

Benutzt LineItem::align, LineItem::byteSize, display, EXITCODE_OUTOFMEMORY, OutBuffer::first, OutBuffer::last, LineItem::length, OutBuffer::lineCount, LineItem::next, LineItem::prev, LineItem::text und UTF_MULTIPLIER.

Wird benutzt von output() und startBuffer().

34  {
35 
36  //allocate memory for line item
37  struct LineItem* newLine = malloc(sizeof(struct LineItem));
38  if (newLine==NULL){
40  }
41 
42  //allocate memory for text
43  newLine->text = malloc(maxTextLength * sizeof(char) * UTF_MULTIPLIER + 1);
44  if (newLine->text==NULL){
46  }
47  newLine->text[0] = '\0';
48 
49  //set line properties
50  newLine->align = (prev!=NULL?prev->align:-1);
51  newLine->byteSize = 0;
52  newLine->length = 0;
53 
54  //relink previous item if given
55  newLine->prev = prev;
56  if (prev!=NULL){
57  newLine->prev->next = newLine;
58  }
59 
60  //chain to next item if given
61  newLine->next = next;
62  if (newLine->next!=NULL){
63  newLine->next->prev = newLine;
64  }
65 
66  //update buffer's first&last pointers
67  if (prev==NULL){
68  display.first = newLine;
69  }
70  if (next==NULL){
71  display.last = newLine;
72  }
73 
74  //update line count and return created line
76  return newLine;
77 }
struct OutBuffer display
Definition: system.c:6
int byteSize
Definition: variables.h:14
char * text
Definition: variables.h:18
struct LineItem * prev
Definition: variables.h:16
struct LineItem * last
Definition: variables.h:22
const int EXITCODE_OUTOFMEMORY
Definition: variables.h:3
struct LineItem * first
Definition: variables.h:21
int length
Definition: variables.h:13
struct LineItem * next
Definition: variables.h:17
const int UTF_MULTIPLIER
Definition: variables.h:30
int lineCount
Definition: variables.h:24
int align
Definition: variables.h:15

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

int irand ( int  a,
int  e 
)

Get random integer a<=x<=e

Parameter
aLowest value for x.
eHighest value for x.
Rückgabe
Random integer

Definiert in Zeile 433 der Datei gamefunction.c.

Wird benutzt von gameFunction().

433  {
434  double r = e - a + 1;
435  return a + (int)(r * rand()/(RAND_MAX+1.0));
436 }

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

char lower_to_upper ( char  ch1)

convert string to upper string

Parameter
ch1string to be uppered
Rückgabe
ch2 is upper case char

Definiert in Zeile 444 der Datei system.c.

Wird benutzt von gameFunction().

445 {
446  char ch2;
447 
448  if(ch1 >= 'a' && ch1 <= 'z'){
449  ch2 = ('A' + ch1 - 'a');
450  return ch2;
451  }
452  else{
453  ch2 = ch1;
454  return ch2;
455  }
456 }

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

int mainMenu ( )
Parameter
con
pPos

Definiert in Zeile 62 der Datei mainMenu.c.

Benutzt animateBanner(), animateBox(), con, drawMainMenu() und mainMenuReactToEnter().

Wird benutzt von drawMainMenu() und main().

62  {
63  //describes where the cursor is, default location is 0(NEW GAME)
64  int pPos=0;
65  //as long as this variable is one the loop won't finish
66  con=1;
67  animateBanner(1);
68  animateBox(1,1,25,8);
69  // menu loop
70  while(con){
71  drawMainMenu(pPos);
72  switch(getch()) {
73  case 72: // key up
74  pPos--;
75 #ifndef DEBUG
76  if (pPos==1) pPos--; //settings temporarily disabled
77 #endif // DEBUG
78  break;
79  case 80: // key down
80  pPos++;
81 #ifndef DEBUG
82  if (pPos==1) pPos++; //settings temporarily disabled
83 #endif // DEBUG
84  break;
85  case 77: // key right
86  break;
87  case 75: // key left
88  break;
89  case 13: // key enter
91  break;
92  case 27: //escape
93  // exit(0);
94  con=0;
95  break;
96  }
97  fflush(stdin);
98  if(pPos>5){
99  //cursor is "below" the menu-->get him back
100  pPos=0;
101  } else if(pPos<0){
102  //cursor is the "over" the menu-->get him back
103  pPos=5;
104  }
105 
106  }
107  animateBox(25,8,1,1);
108  animateBanner(0);
109  return 0;
110 }
void animateBox(int wFrom, int hFrom, int wTo, int hTo)
Definition: system.c:392
int con
Definition: variables.h:35
void animateBanner(int slideIn)
Definition: system.c:378

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

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

void mainMenuReactToEnter ( int  pPos)

this method handles the game flow by calling the function corresponding to the cursor posotion within the menu

Parameter
pPoscontains the current position (0:Top,4:bottom)

Definiert in Zeile 115 der Datei mainMenu.c.

Benutzt con, flushBuffer(), gameFunction(), NO_HIGHLIGHT, output(), showCredits(), showHallOfShame(), showRules() und startBuffer().

Wird benutzt von mainMenu().

115  {
116  switch(pPos){
117  case 0:
118  gameFunction();
119  break;
120  case 1:
121  startBuffer(60);
122  output("Settings Menu(Dummy)\nPress any key to return to main\n");
123  flushBuffer();
124  getch();
125  break;
126  case 2:
127  showRules();
128  break;
129  case 3:
131  break;
132  case 4:
133  showCredits();
134  break;
135  case 5:
136  //end game
137  con=0;
138  //exit(0);
139  }
140 }
void showHallOfShame(int highlight, int startFrom)
Definition: hallofshame.c:13
void showRules()
Definition: rules.c:35
void flushBuffer()
Definition: system.c:188
void startBuffer(int maxTextLength)
Definition: system.c:319
void gameFunction()
Definition: gamefunction.c:28
void showCredits()
Definition: credits.c:6
int con
Definition: variables.h:35
const int NO_HIGHLIGHT
Definition: variables.h:67
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 neighbourRow ( int  x,
int  y,
int  xMovement,
int  yMovement,
char  player 
)

Recursive function to sum up the coins which are connected in a row.

Parameter
xX-Position of last dropped coin.
yY-Position of last dropped coin.
xMovementThis is used to build a "vector"
yMovementThis is used to build a "vector"
playerThe cointype to count in a row.
Rückgabe
Number of coins which are connected.

Definiert in Zeile 406 der Datei gamefunction.c.

Benutzt gameField, getField() und neighbourRow().

Wird benutzt von checkForWinner() und neighbourRow().

406  {
407  if (getField(&gameField,x,y) == player){
408  return 1 + neighbourRow(x+xMovement,y+yMovement,xMovement,yMovement,player);
409  }
410  else{
411  return 0;
412  }
413 }
char getField(struct board *target, int x, int y)
Definition: board.c:72
struct board gameField
Definition: gamefunction.c:11
int neighbourRow(int x, int y, int xMovement, int yMovement, char player)
Definition: gamefunction.c:406

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

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

int newBoard ( struct board target,
unsigned int  width,
unsigned int  height 
)

Creates new clean and usable board (including memory allocation) Warning: To resize an existing board call freeBoard() before to prevent memory leak

Parameter
targetPointer to a fresh board structure.
widthBoard width (>=4).
heightBoard height (>=4).
Rückgabe
1 on success, otherwise 0.

Definiert in Zeile 15 der Datei board.c.

Benutzt clearBoard(), board::content, EXITCODE_OUTOFMEMORY, board::height, board::numberOfFields und board::width.

Wird benutzt von startGame().

16 {
17  //sanity check
18  if (width<4 || height<4) {
19  return 0;
20  }
21 
22  //Initialize board
23  target->width = width;
24  target->height = height;
25  target->numberOfFields = width*height;
26  target->content = malloc(target->numberOfFields * sizeof(char) + 1);
27 
28  //abort if memory allocation failed
29  if (target->content == NULL) {
31  }
32 
33  //board is ready, now clear it
34  clearBoard(target);
35 
36  return 1;
37 }
const int EXITCODE_OUTOFMEMORY
Definition: variables.h:3
void clearBoard(struct board *target)
Definition: board.c:43
unsigned int height
Definition: variables.h:45
unsigned int numberOfFields
Definition: variables.h:46
unsigned int width
Definition: variables.h:44
char * content
Definition: variables.h:47

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

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

int output ( const char *  input,
  ... 
)

Add text to output buffer, use EXACTLY like printf()

Parameter
inputFormat string, see printf().
...Parameters, see printf().
Rückgabe
Return value of internal vsnprintf().

Definiert in Zeile 144 der Datei system.c.

Benutzt LineItem::byteSize, copyChar(), display, EXITCODE_BUFFERERROR, OutBuffer::first, insertNewLineItem(), OutBuffer::last, LineItem::length, OutBuffer::maxTextLength, LineItem::next, OUTPUT_MAXBUFFER und LineItem::text.

Wird benutzt von addFrame(), drawBoard(), drawCoin(), drawMainMenu(), gameFunction(), HelloWorld(), mainMenuReactToEnter(), playerAction(), printEmptyBox(), showCredits(), showHallOfShame() und showRules().

144  {
145  if (display.first==NULL || display.last==NULL){
146  exit(EXITCODE_BUFFERERROR);
147  }
148 
149  int result;
150  va_list args;
151  va_start(args, input);
152  va_end(args);
153 
154  //writing to string buffer
155  char buffer[OUTPUT_MAXBUFFER+1];
156  buffer[0] = '\0';
157  result = vsnprintf(buffer, OUTPUT_MAXBUFFER+1, input, args);
158 
159  //keep track of string buffer position and the current line to write to
160  int i = 0;
161  struct LineItem* current = display.last;
162 
163  //loop through buffer and copy to lines
164  while(buffer[i]!='\0'){
165  if (current->length < display.maxTextLength && buffer[i]!='\n'){
166  //line not full yet, copy char
167  int increase = copyChar(buffer+i, current->text+current->byteSize);
168  i += increase;
169  current->byteSize += increase;
170  current->length++;
171  } else {
172  //end line in input: start new line
173  if (buffer[i]=='\n'){
174  i++;
175  }
176  //end line or line full, create new one
177  current->next = insertNewLineItem(current, NULL, display.maxTextLength);
178  current = current->next;
179  }
180  }
181 
182  return result;
183 }
struct OutBuffer display
Definition: system.c:6
int byteSize
Definition: variables.h:14
char * text
Definition: variables.h:18
struct LineItem * last
Definition: variables.h:22
struct LineItem * first
Definition: variables.h:21
const int EXITCODE_BUFFERERROR
Definition: variables.h:4
int length
Definition: variables.h:13
struct LineItem * next
Definition: variables.h:17
int maxTextLength
Definition: variables.h:23
const int OUTPUT_MAXBUFFER
Definition: variables.h:28
struct LineItem * insertNewLineItem(struct LineItem *prev, struct LineItem *next, int maxTextLength)
Definition: system.c:34
int copyChar(char *src, char *dst)
Definition: system.c:114

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

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

void playerAction ( )

playerAction()

  • checks which player's turn it is
  • shows X or O for a game-chip
    • player1 (X) or player2 (O) can move the coin to the left or right (check boundaries, also if row is full) and let it fall (space)
    • stop coin at highest existing coin
    • check if any user has won (function needs to be implemented) according to https://github.com/wagnst/4-gewinnt/issues/10
      Rückgabe
      no return because of void

Definiert in Zeile 194 der Datei gamefunction.c.

Benutzt coinPosition, drawBoard(), drawCoin(), end, EXITCODE_OUTOFMEMORY, FIELD_PLAYER1, FIELD_PLAYER2, flushBuffer(), gameField, gameFieldWidth, moves, output(), player1, player2, playersCoin, playersTurn, setLineAlign(), startBuffer(), throwCoin() und userInput.

Wird benutzt von startGame().

194  {
195  char *spaceBetweenNames;
196  spaceBetweenNames = malloc((gameFieldWidth * 3) * sizeof(char));
197  //if alloc failed return
198  if (spaceBetweenNames==NULL) {
199  exit(EXITCODE_OUTOFMEMORY);
200  }
201  //check which players turn it is and sets the coin
202  playersCoin = '\0';
203  //output buffer
204  startBuffer(50);
205  if (playersTurn == 0) {
206  setLineAlign(-1);
207  output("%s's turn\n\n", player1);
209  }else{
210  setLineAlign(+1);
211  output("%s's turn\n\n", player2);
213  }
214  setLineAlign(0);
215  //draw the coin
217  //draw the board
219  //end buffer
220  flushBuffer();
221  userInput = getch();
222  switch(userInput){
223  //dummy for key left
224  case 75: //left
225  if (coinPosition > 1)
226  coinPosition--;
227  break;
228  //dummy for key right
229  case 77: //right
231  coinPosition++;
232  break;
233  case 27: //escape
234  //go back to menu
235  end = 1;
236  break;
237  //let coin fall
238  case 13: //enter key
240  moves++;
241  break;
242  }
243  free(spaceBetweenNames);
244 }
char player2[20]
Definition: gamefunction.c:9
const char FIELD_PLAYER2
Definition: variables.h:40
const char FIELD_PLAYER1
Definition: variables.h:39
struct board gameField
Definition: gamefunction.c:11
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 coinPosition
Definition: gamefunction.c:15
int end
Definition: gamefunction.c:20
void throwCoin(int pos, char player)
Definition: gamefunction.c:252
char playersCoin
Definition: gamefunction.c:17
int playersTurn
Definition: gamefunction.c:16
void drawCoin(int pos, char CoinType)
Definition: gamefunction.c:291
void drawBoard(struct board *target)
Definition: board.c:103
int moves
Definition: gamefunction.c:19
char player1[20]
Definition: gamefunction.c:8
int gameFieldWidth
Definition: gamefunction.c:12
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:

void printEmptyBox ( int  w,
int  h 
)

Print an empty buffer box with the given size

Parameter
wWidth.
hHeight.

Definiert in Zeile 418 der Datei system.c.

Benutzt flushBuffer(), output() und startBuffer().

Wird benutzt von animateBox().

418  {
419  int i;
420  startBuffer(w);
421  for (i = 0; i < h; i++) output("\n");
422  flushBuffer();
423 }
int output(const char *input,...)
Definition: system.c:144
void startBuffer(int maxTextLength)
Definition: system.c:319
void flushBuffer()
Definition: system.c:188

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

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

void printfBanner ( int  width,
int  startAt 
)

Printf-s ASCII art logo

Parameter
widthWidth for padding with spaces.
startAtValue between 0 and 4 for partial display, see animateBanner().

Definiert in Zeile 356 der Datei system.c.

Benutzt LineItem::length und strcatRepeat().

Wird benutzt von animateBanner() und flushBuffer().

356  {
357  int length = 57;
358  char* banner[5];
359  banner[0] = "_________ _____ _____ __\n";
360  banner[1] = "__ ____/________________________________ /_ __ // /\n";
361  banner[2] = "_ / _ __ \\_ __ \\_ __ \\ _ \\ ___/ __/ _ // /_\n";
362  banner[3] = "/ /___ / /_/ / / / / / / / __/ /__ / /_ /__ __/\n";
363  banner[4] = "\\____/ \\____//_/ /_//_/ /_/\\___/\\___/ \\__/ /_/ \n";
364  int i;
365  char* bannerBuffer = malloc(sizeof(char)*(width+1)*5);
366  bannerBuffer[0] = '\0';
367  for(i = startAt; i<5; i++){
368  strcatRepeat(bannerBuffer," ",width/2-length/2-1);
369  strcat(bannerBuffer,banner[i]);
370  }
371  printf("%s",bannerBuffer);
372 }
int length
Definition: variables.h:13
void strcatRepeat(char *target, const char *source, unsigned int howOften)
Definition: system.c:458

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

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

void setFancy ( )
void setField ( struct board target,
int  x,
int  y,
char  value 
)

Sets what is on a given field of the board (see FIELD_XYZ constants)

Parameter
targetBoard to change.
xX-coordinate.
yY-coordinate.
valueField value (FIELD_EMPTY/FIELD_PLAYER1/FIELD_PLAYER2).

Definiert in Zeile 87 der Datei board.c.

Benutzt calcFieldAddress().

Wird benutzt von throwCoin().

87  {
88  *calcFieldAddress(target, x, y) = value;
89 }
char * calcFieldAddress(struct board *target, int x, int y)
Definition: board.c:60

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

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

void setLineAlign ( int  align)

Change the horizontal align of the current line within the buffer box

Parameter
alignLeft align (-1), centered (0) or right align (+1).

Definiert in Zeile 344 der Datei system.c.

Benutzt LineItem::align, display, EXITCODE_BUFFERERROR und OutBuffer::last.

Wird benutzt von drawMainMenu(), playerAction(), showCredits(), showHallOfShame() und showRules().

344  {
345  if(display.last==NULL){
346  exit(EXITCODE_BUFFERERROR);
347  }
348  display.last->align = align;
349 }
struct OutBuffer display
Definition: system.c:6
struct LineItem * last
Definition: variables.h:22
const int EXITCODE_BUFFERERROR
Definition: variables.h:4
int align
Definition: variables.h:15

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

void showCredits ( )

Show program credits

Definiert in Zeile 6 der Datei credits.c.

Benutzt animateBox(), flushBuffer(), output(), setLineAlign() und startBuffer().

Wird benutzt von mainMenuReactToEnter().

6  {
7  animateBox(25,8,66,17);
8  startBuffer(66);
9  setLineAlign(0);
10  output("____ ____ ____ ___ _ ___ ____ \n| |__/ |___ | \\ | | [__ \n|___ | \\ |___ |__/ | | ___] \n\n");
11  setLineAlign(-1);
12  output(" Niko Berkmann | @n2code\n n2code@users.noreply.github.com\n");
13  setLineAlign(+1);
14  output("Steffen Wagner | @wagnst \nmail@wagnst.de \n");
15  setLineAlign(-1);
16  output(" Marvin Klose | @marvinklose\n marvinklose@gmx.net\n");
17  setLineAlign(+1);
18  output("Michael Wieneke | @momoxd \nmail@momoxd.de \n");
19  setLineAlign(-1);
20  output(" Inga Miadowicz | @IngaMiad\n inga.miadowicz@gmx.de\n");
21  setLineAlign(0);
22  output("\nPress any key to return to main menu.\n");
23  flushBuffer();
24  getch();
25  animateBox(66,17,25,8);
26 }
void animateBox(int wFrom, int hFrom, int wTo, int hTo)
Definition: system.c:392
void setLineAlign(int align)
Definition: system.c:344
void flushBuffer()
Definition: system.c:188
void startBuffer(int maxTextLength)
Definition: system.c:319
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:

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:

void showRules ( )

Method to show the rules.User can leave this screen by pressing any key

Definiert in Zeile 35 der Datei rules.c.

Benutzt flushBuffer(), output(), setLineAlign() und startBuffer().

Wird benutzt von mainMenuReactToEnter().

35  {
36  startBuffer(70);
37  setLineAlign(0);
38  output("[ RULES ]\n\n");
39  setLineAlign(-1);
40  output("This a implementation of the famous two player game \"Connect Four\".\nSo you'll need a friend to play this game :) To win you simply have to\nconnect four of your chips in a row. This row can be horizontal,\nvertical or diagonal,it doesn't matter as long as there are no \"enemy\"\nchips between your chips.\nThe game will automatically end if one you won and take you to the\n\"Hall of Shame\"\n\n");
41  setLineAlign(0);
42  output("[ Press any key to return to main menu ]\n");
43  flushBuffer();
44  getch();
45 }
void setLineAlign(int align)
Definition: system.c:344
void flushBuffer()
Definition: system.c:188
void startBuffer(int maxTextLength)
Definition: system.c:319
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:

void startBuffer ( int  maxTextLength)

Starts a new screen and sets line limit, empties the buffer

Parameter
maxTextLengthSee initBuffer().

Definiert in Zeile 319 der Datei system.c.

Benutzt consoleBufferWidth, deleteLineItem(), display, EXITCODE_BUFFERERROR, EXITCODE_WINDOWERROR, OutBuffer::first, insertNewLineItem(), OutBuffer::last, OutBuffer::lineCount und OutBuffer::maxTextLength.

Wird benutzt von drawMainMenu(), gameFunction(), mainMenuReactToEnter(), playerAction(), printEmptyBox(), showCredits(), showHallOfShame() und showRules().

319  {
320  if (display.first!=NULL || display.last!=NULL){
321  //display not initialized
322  exit(EXITCODE_BUFFERERROR);
323  }
324 
325  if (maxTextLength>consoleBufferWidth-4){
326  //requested buffer too large for window size
327  exit(EXITCODE_WINDOWERROR);
328  }
329 
330  if (maxTextLength<1){
331  exit(EXITCODE_BUFFERERROR);
332  }
333 
334  display.maxTextLength = maxTextLength;
336  display.lineCount = 0;
337  display.first = insertNewLineItem(NULL,NULL,maxTextLength);
338 }
struct OutBuffer display
Definition: system.c:6
struct LineItem * last
Definition: variables.h:22
struct LineItem * first
Definition: variables.h:21
void deleteLineItem(struct LineItem *line, int deleteAllBelow)
Definition: system.c:84
const int EXITCODE_BUFFERERROR
Definition: variables.h:4
int consoleBufferWidth
Definition: system.c:8
int maxTextLength
Definition: variables.h:23
int lineCount
Definition: variables.h:24
const int EXITCODE_WINDOWERROR
Definition: variables.h:5
struct LineItem * insertNewLineItem(struct LineItem *prev, struct LineItem *next, int maxTextLength)
Definition: system.c:34

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

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

void startGame ( )

Function will create a new Board, clears it and calls playerAction()

Rückgabe
no return because of void

Definiert in Zeile 333 der Datei gamefunction.c.

Benutzt gameField, gameFieldCreated, gameFieldHeigth, gameFieldWidth, newBoard() und playerAction().

Wird benutzt von gameFunction().

333  {
334  //checks if field already exists
335  if (gameFieldCreated == 0) {
336  //create new game board (includes clear)
338  //set gameFieldCreated to 1
339  gameFieldCreated = 1;
340  }
341  }
342  //call playerAction to let game begin
343  playerAction();
344 }
struct board gameField
Definition: gamefunction.c:11
void playerAction()
Definition: gamefunction.c:194
int gameFieldHeigth
Definition: gamefunction.c:13
int newBoard(struct board *target, unsigned int width, unsigned int height)
Definition: board.c:15
int gameFieldCreated
Definition: gamefunction.c:14
int gameFieldWidth
Definition: gamefunction.c:12

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

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

void strcatRepeat ( char *  target,
const char *  source,
unsigned int  howOften 
)

Definiert in Zeile 458 der Datei system.c.

Wird benutzt von drawBoard(), flushBuffer() und printfBanner().

458  {
459  int c;
460  for(c = 0; c < howOften; c++){
461  strcat(target,source);
462  }
463 }

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

void throwCoin ( int  pos,
char  player 
)

Checks if a row is full, otherwise throw coin

Parameter
posposition of where coin should be placed (board begins at 0 and coinpos at 1!)
playerChar which contains X oder O according to the player
Rückgabe
no return because of void

Definiert in Zeile 252 der Datei gamefunction.c.

Benutzt checkForWinner(), FIELD_EMPTY, FIELD_PLAYER1, FIELD_PLAYER2, gameField, gameFieldHeigth, getField(), playersTurn, setField() und victor.

Wird benutzt von playerAction().

252  {
253  int lowestCoin = 0, i;
254  //get the lowest field
255  // LOOP DOWN if found, set lowestCoin
256  for(i = 0; i <= gameFieldHeigth-1; i++) {
257  // if we found an empty field, leave this loop and set lowest coin
258  // to the found field, so that game flow can set the next coin here
259  if (getField(&gameField,pos-1,i) == FIELD_EMPTY){
260  lowestCoin = i;
261  break;
262  }
263  }
264  //check if most upper field is filled with coin
266  return;
267  }else{
268  //set the coin for player 1 or 2 to the lowest possible column
269  if(player == FIELD_PLAYER1){
270  setField(&gameField,pos-1,lowestCoin,FIELD_PLAYER1);
271  //added looking for winner here
272  victor = checkForWinner(pos-1, lowestCoin,FIELD_PLAYER1);
273  }else if(player == FIELD_PLAYER2){ //if player 2
274  setField(&gameField,pos-1,lowestCoin,FIELD_PLAYER2);
275  victor = checkForWinner(pos-1, lowestCoin,FIELD_PLAYER2);
276  }
277  }
278  //switch to player 2 or back to 1
279  if (playersTurn == 1)
280  playersTurn = 0;
281  else playersTurn = 1;
282  return;
283 }
char getField(struct board *target, int x, int y)
Definition: board.c:72
const char FIELD_PLAYER2
Definition: variables.h:40
const char FIELD_PLAYER1
Definition: variables.h:39
struct board gameField
Definition: gamefunction.c:11
void setField(struct board *target, int x, int y, char value)
Definition: board.c:87
char checkForWinner(int x, int y, char player)
Definition: gamefunction.c:356
const char FIELD_EMPTY
Definition: variables.h:38
int gameFieldHeigth
Definition: gamefunction.c:13
int playersTurn
Definition: gamefunction.c:16
char victor
Definition: gamefunction.c:18

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: