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

gehe zum Quellcode dieser Datei

Funktionen

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)
 

Variablen

struct board myBoard
 

Dokumentation der Funktionen

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:

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 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 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:

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 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:

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:

Variablen-Dokumentation

struct board myBoard

Definiert in Zeile 5 der Datei board.c.