![]() |
4-gewinnt
1.0.0
Klassiker "4-gewinnt" als Konsolenanwendung
|
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 LineItem * | insertNewLineItem (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 () |
void animateBanner | ( | int | slideIn | ) |
Animate ASCII art logo.
slideIn | Set 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().
void animateBox | ( | int | wFrom, |
int | hFrom, | ||
int | wTo, | ||
int | hTo | ||
) |
Change the horizontal align of the current line within the buffer box
align | Left align (-1), centered (0) or right align (+1). |
Definiert in Zeile 392 der Datei system.c.
Benutzt printEmptyBox().
Wird benutzt von mainMenu() und showCredits().
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
xPos | the row in which the coin is going to fall |
currBoard | the board with the current game state |
CoinType | variable 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.
char* calcFieldAddress | ( | struct board * | target, |
int | x, | ||
int | y | ||
) |
Internal helper method to calculate the data offset of a given field on the board
target | Board to base calculations on. |
x | X-coordinate. |
y | Y-coordinate. |
Definiert in Zeile 60 der Datei board.c.
Benutzt board::content und board::width.
Wird benutzt von animateFalling(), getField() und setField().
int checkDraw | ( | ) |
Checks if the board is full. Only possible if nobody has won earlier.
Definiert in Zeile 419 der Datei gamefunction.c.
Benutzt FIELD_EMPTY, gameField, gameFieldHeigth, gameFieldWidth und getField().
Wird benutzt von gameFunction().
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.
x | X-Position of last dropped coin. |
y | Y-Position of last dropped coin. |
player | The last coin which was dropped to the game, as the starting point of this algorithm. |
Definiert in Zeile 356 der Datei gamefunction.c.
Benutzt FIELD_EMPTY und neighbourRow().
Wird benutzt von throwCoin().
void clearAll | ( | ) |
Clears all elements which are used within the gameflow Board, loop condition, positions
Definiert in Zeile 171 der Datei gamefunction.c.
Benutzt clearBoard(), coinPosition, end, freeBoard(), gameField, gameFieldCreated, playersTurn und victor.
Wird benutzt von gameFunction().
void clearBoard | ( | struct board * | target | ) |
Removes all chips from the board
target | Already existing board. |
Definiert in Zeile 43 der Datei board.c.
Benutzt board::content, FIELD_EMPTY und board::numberOfFields.
Wird benutzt von clearAll() und newBoard().
void consoleClear | ( | ) |
Clears the console
Definiert in Zeile 428 der Datei system.c.
Wird benutzt von animateBanner(), flushBuffer() und gameFunction().
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
src | Source string pointer. |
dst | Destination string pointer. |
Definiert in Zeile 114 der Datei system.c.
Benutzt LineItem::length.
Wird benutzt von output().
void deleteLineItem | ( | struct LineItem * | line, |
int | deleteAllBelow | ||
) |
Delete the given line from the buffer
line | Pointer to the line struct to delete. |
deleteAllBelow | Delete 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().
void drawBoard | ( | struct board * | target | ) |
Outputs a graphical representation of the given board to the console
target | Board 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().
void drawCoin | ( | int | pos, |
char | CoinType | ||
) |
Draws the Coin above the board
pos | which position should the coin be drawn to. |
coinType | which coin should be drawn. |
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().
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.
menu | contains the pointer to the char-array containing main menu in it's current status |
pPos | contains the cursor Position of the cursor |
Definiert in Zeile 11 der Datei mainMenu.c.
Wird benutzt von drawMainMenu().
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.
menu | contains the pointer to the char-array containing main menu in it's current status |
pPos | contains the cursor Position of the cursor |
Definiert in Zeile 24 der Datei mainMenu.c.
Wird benutzt von drawMainMenu().
int extractMoves | ( | char * | line | ) |
This function extracts the number of moves of the parameter line.
line | Pointer to char array (string). |
Definiert in Zeile 160 der Datei hallofshame.c.
Benutzt moves.
Wird benutzt von updateSaveHoS().
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().
void freeBoard | ( | struct board * | target | ) |
Frees memory taken by a board (warning: makes it unusable until newBoard() has been called again)
target | Board to free memory. |
Definiert in Zeile 95 der Datei board.c.
Benutzt board::content.
Wird benutzt von clearAll().
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.
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().
char getField | ( | struct board * | target, |
int | x, | ||
int | y | ||
) |
Gets what is on a given field of the board (returns FIELD_XYZ constant)
target | Board to read. |
x | X-coordinate. |
y | Y-coordinate. |
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().
int getOldFileLength | ( | ) |
Determine the length of the current HallOfShame.dat file.
Definiert in Zeile 185 der Datei hallofshame.c.
Wird benutzt von updateSaveHoS().
void HelloWorld | ( | ) |
Definiert in Zeile 5 der Datei dummy.c.
Benutzt output() und testerVariable.
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().
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
prev | Pointer to the previous line struct, NULL appends line to start of buffer. |
next | Pointer to the next line struct, NULL if this is the last element (most used case). |
align | Text align for the new line, -1 is left aligned, 0 is centered, +1 is right aligned. |
maxTextLength | Maximum number of characters for this line. |
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().
int irand | ( | int | a, |
int | e | ||
) |
Get random integer a<=x<=e
a | Lowest value for x. |
e | Highest value for x. |
Definiert in Zeile 433 der Datei gamefunction.c.
Wird benutzt von gameFunction().
char lower_to_upper | ( | char | ch1 | ) |
convert string to upper string
ch1 | string to be uppered |
Definiert in Zeile 444 der Datei system.c.
Wird benutzt von gameFunction().
int mainMenu | ( | ) |
con | |
pPos |
Definiert in Zeile 62 der Datei mainMenu.c.
Benutzt animateBanner(), animateBox(), con, drawMainMenu() und mainMenuReactToEnter().
Wird benutzt von drawMainMenu() und main().
void mainMenuReactToEnter | ( | int | pPos | ) |
this method handles the game flow by calling the function corresponding to the cursor posotion within the menu
pPos | contains 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().
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.
x | X-Position of last dropped coin. |
y | Y-Position of last dropped coin. |
xMovement | This is used to build a "vector" |
yMovement | This is used to build a "vector" |
player | The cointype to count in a row. |
Definiert in Zeile 406 der Datei gamefunction.c.
Benutzt gameField, getField() und neighbourRow().
Wird benutzt von checkForWinner() und neighbourRow().
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
target | Pointer to a fresh board structure. |
width | Board width (>=4). |
height | Board height (>=4). |
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().
int output | ( | const char * | input, |
... | |||
) |
Add text to output buffer, use EXACTLY like printf()
input | Format string, see printf(). |
... | Parameters, see printf(). |
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().
void playerAction | ( | ) |
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().
void printEmptyBox | ( | int | w, |
int | h | ||
) |
Print an empty buffer box with the given size
w | Width. |
h | Height. |
Definiert in Zeile 418 der Datei system.c.
Benutzt flushBuffer(), output() und startBuffer().
Wird benutzt von animateBox().
void printfBanner | ( | int | width, |
int | startAt | ||
) |
Printf-s ASCII art logo
width | Width for padding with spaces. |
startAt | Value 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().
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)
target | Board to change. |
x | X-coordinate. |
y | Y-coordinate. |
value | Field value (FIELD_EMPTY/FIELD_PLAYER1/FIELD_PLAYER2). |
Definiert in Zeile 87 der Datei board.c.
Benutzt calcFieldAddress().
Wird benutzt von throwCoin().
void setLineAlign | ( | int | align | ) |
Change the horizontal align of the current line within the buffer box
align | Left 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().
void showCredits | ( | ) |
Show program credits
Definiert in Zeile 6 der Datei credits.c.
Benutzt animateBox(), flushBuffer(), output(), setLineAlign() und startBuffer().
Wird benutzt von mainMenuReactToEnter().
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.
highlight | Highlight a special line with "-> " |
startFrom | The 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().
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().
void startBuffer | ( | int | maxTextLength | ) |
Starts a new screen and sets line limit, empties the buffer
maxTextLength | See 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().
void startGame | ( | ) |
Function will create a new Board, clears it and calls playerAction()
Definiert in Zeile 333 der Datei gamefunction.c.
Benutzt gameField, gameFieldCreated, gameFieldHeigth, gameFieldWidth, newBoard() und playerAction().
Wird benutzt von gameFunction().
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().
void throwCoin | ( | int | pos, |
char | player | ||
) |
Checks if a row is full, otherwise throw coin
pos | position of where coin should be placed (board begins at 0 and coinpos at 1!) |
player | Char which contains X oder O according to the player |
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().
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.
victor | Name of player who won the game. |
vicitim | Name of player who lost. |
moves | Number of steps after the game has finished. |
Definiert in Zeile 101 der Datei hallofshame.c.
Benutzt EXITCODE_OUTOFMEMORY, extractMoves() und getOldFileLength().
Wird benutzt von gameFunction().