# Demo entry 3539644

assa

Submitted by anonymous on Jan 11, 2016 at 08:40
Language: C. Code size: 35.6 kB.

```//-----------------------------------------------------------------------------
// assa.c
//
// This program is kind of 4 in a row. You can play with 2 players, X and O.
// The player switches after the actual player has set his Token (X or O).
// The player who has first 4 of his kind in a horizontal, diagonal or vertical
// has won the game. The gamefield is 7 rows x 6 columns.
// Type "help" in the game for more information.
//
// Group: Group 11, study assistant Pascal Nasahl
//
// Group number: 13299
//
// Authors: Lukas Guenter Buxbaum 1530558
//		  	Martin Gruenwald 1530467
//
// Latest changes: 11.12.2015
//-----------------------------------------------------------------------------
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//-----------------------------------------------------------------------------
///
/// printGameField: It prints the game_field graphically to the terminal.
///                 The gamefield has in front of every line, a
///                 number 1 to 7 and underneath every column a letter A to F
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
///
/// @return No return because of void
//
void printGameField(char game_field, int position_x, int position_y)
{
for (position_y = 0; position_y <= 6; position_y++)
{
printf("%d ", position_y + 1);
for (position_x = 0; position_x <= 5; position_x++)
{
if (position_x < 5)
{
printf("%c  ", game_field[position_y][position_x]);
}
else
{
printf("%c", game_field[position_y][position_x]);
}
}
printf("\n");
if(position_y == 6) //no " " after the last position in a row
{
printf("  A  B  C  D  E  F");
}
}
printf("\n");
}

//-----------------------------------------------------------------------------
///
/// loadGameField: assign the chars from the file to the array game_field
///
/// @param *data_file Is a pointer which points to a variable with
///                   datatype FILE. The variable is the binary file here.
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
///
/// @return No return because of void
//
void loadGameField(FILE *data_file, char game_field, int position_x,
int position_y)
{
for (position_y = 0; position_y <= 6; position_y++)
{
for (position_x = 0; position_x <= 5; position_x++)
{
fscanf(data_file, "%c", &game_field[position_y][position_x]);
}
}
}

//-----------------------------------------------------------------------------
///
/// saveGame: assign the chars from the game_field to the file <your name>
///
/// @param active_player Is 0 or 1, 1 for next player is O (player 2), 0 for X
/// @param save_data_filename Here is the name of the file to save <your name>
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
///
/// @return No return because of void
//
void saveGame(unsigned active_player, char save_data_filename,
char game_field, int position_x, int position_y)
{
FILE *data_file;
data_file = fopen(save_data_filename, "wb");  //write binary
if(data_file == NULL)
{
printf("Error writing data_file.\n");
}

else
{
fprintf(data_file, "%d", active_player);

for (position_y = 0; position_y <= 6; position_y++)
{
for (position_x = 0; position_x <= 5; position_x++)
{
fprintf(data_file, "%c", game_field[position_y][position_x]);
}
}

fclose(data_file);
}
}

//-----------------------------------------------------------------------------
///
/// checkClearFirstRow: Checks the first row (top row) and clears it, if full
///                     with Token. Before clearing, every Token falls down
///                     by 1 row and the last row (lowest) would be eliminated.
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param EMPTY_FIELD Is a const char and stands for a field with no set Token
///
/// @return game_field Which was adapted
//
char checkClearFirstRow(char game_field, int position_x, int position_y,
const char EMPTY_FIELD)
{

if ((strncmp(&game_field, &EMPTY_FIELD, 1) != 0) &&
(strncmp(&game_field, &EMPTY_FIELD, 1) != 0) &&
(strncmp(&game_field, &EMPTY_FIELD, 1) != 0) &&
(strncmp(&game_field, &EMPTY_FIELD, 1) != 0) &&
(strncmp(&game_field, &EMPTY_FIELD, 1) != 0) &&
(strncmp(&game_field, &EMPTY_FIELD, 1) != 0) == 1)
{
for (position_y = 6; position_y >= 1; position_y--)
{
for (position_x = 0; position_x <= 5; position_x++)
{
game_field[position_y][position_x] =
game_field[position_y - 1][position_x];
}
}

for (position_x = 0; position_x < 6; position_x++)
{
game_field[position_x] = EMPTY_FIELD;
}

printf("No more moves possible. Shifting Gameboard.\n");
return game_field;
}

else
{
return game_field;
}
}

//-----------------------------------------------------------------------------
///
/// checkPlayer1Vertical: checks whether the Player 1 has a combination of 4
///                       of his Token Vertically
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
///
/// @return 1 if a vertical combination of Player 1 Tokens ('X') is found
///         0 if no 4 in a column
//
int checkPlayer1Vertical(char game_field, int position_x,
int position_y, const char TOKEN_1)
{

for (position_y = 0; position_y < 4; position_y++)
{
for (position_x = 0; position_x < 7; position_x++)
{
if (((game_field[0 + position_y][position_x] == TOKEN_1) &&
(game_field[1 + position_y][position_x] == TOKEN_1) &&
(game_field[2 + position_y][position_x] == TOKEN_1) &&
(game_field[3 + position_y][position_x] == TOKEN_1)) == 1)
{
return 1;
}
}
}

return 0;
}

//-----------------------------------------------------------------------------
///
/// checkPlayer2Vertical: checks whether the Player 2 has a combination of 4
///                       of his Token Vertically
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
///
/// @return 1 if a vertical combination of Player 2 Tokens ('O') is found
///         0 if no 4 in a column
//
int checkPlayer2Vertical(char game_field, int position_x,
int position_y, const char TOKEN_2)
{

for (position_y = 0; position_y < 4; position_y++)
{
for (position_x = 0; position_x < 7; position_x++)
{
if (((game_field[0 + position_y][position_x] == TOKEN_2) &&
(game_field[1 + position_y][position_x] == TOKEN_2) &&
(game_field[2 + position_y][position_x] == TOKEN_2) &&
(game_field[3 + position_y][position_x] == TOKEN_2)) == 1)
{
return 1;
}
}
}

return 0;
}

//-----------------------------------------------------------------------------
///
/// checkPlayer1Horizontal: checks whether the Player 1 has a combination of 4
///                         of his Token Horizontally
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
///
/// @return 1 if a horizontal combination of Player 1 Tokens ('X') is found
///         0 if not 4 in a row
//
int checkPlayer1Horizontal(char game_field, int position_x,
int position_y,const char TOKEN_1)
{

for (position_x = 0; position_x < 3; position_x++)
{
for (position_y = 0; position_y < 7; position_y++)
{
if (((game_field[position_y][0 + position_x] == TOKEN_1) &&
(game_field[position_y][1 + position_x] == TOKEN_1) &&
(game_field[position_y][2 + position_x] == TOKEN_1) &&
(game_field[position_y][3 + position_x] == TOKEN_1)) == 1)
{
return 1;
}
}
}
return 0;
}

//-----------------------------------------------------------------------------
///
/// checkPlayer2Horizontal: checks whether the Player 2 has a combination of 4
///                         of his Token Horizontally
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
///
/// @return 1 if a vertical combination of Player 2 Tokens ('O') is found
///         0 if no 4 in a row
//
int checkPlayer2Horizontal(char game_field, int position_x,
int position_y, const char TOKEN_2)
{

for (position_x = 0; position_x < 3; position_x++)
{
for (position_y = 0; position_y < 7; position_y++)
{
if (((game_field[position_y][0 + position_x] == TOKEN_2) &&
(game_field[position_y][1 + position_x] == TOKEN_2) &&
(game_field[position_y][2 + position_x] == TOKEN_2) &&
(game_field[position_y][3 + position_x] == TOKEN_2)) == 1)
{
return 1;
}
}
}
return 0;
}

//-----------------------------------------------------------------------------
///
/// checkPlayer1DiagonalPos: checks whether the Player 1 has a combination of 4
///                          of his Token in a positive Diagonal of game_field
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
///
/// @return 1 if a diagonal positive combination of
///         Player 1 Tokens ('X') is found, 0 if no 4 Token of Player 1
//
int checkPlayer1DiagonalPos(char game_field, int position_x,
int position_y, const char TOKEN_1)
{
for (position_y = 0; position_y < 4; position_y++)
{
for (position_x = 0; position_x < 3; position_x++)
{
if (((game_field[3 + position_y][0 + position_x] == TOKEN_1) &&
(game_field[2 + position_y][1 + position_x] == TOKEN_1) &&
(game_field[1 + position_y][2 + position_x] == TOKEN_1) &&
(game_field[0 + position_y][3 + position_x] == TOKEN_1)) == 1)
{
return 1;
}
}
}
return 0;
}

//-----------------------------------------------------------------------------
///
/// checkPlayer2DiagonalPos: checks whether the Player 2 has a combination of 4
///                          of his Token in a positive Diagonal of game_field
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
///
/// @return 1 if a diagonal positive combination of
///         Player 2 Tokens ('O') is found, 0 if no 4 Token of Player 2
//
int checkPlayer2DiagonalPos(char game_field, int position_x,
int position_y, const char TOKEN_2)
{
for (position_y = 0; position_y < 4; position_y++)
{
for (position_x = 0; position_x < 3; position_x++)
{
if (((game_field[3 + position_y][0 + position_x] == TOKEN_2) &&
(game_field[2 + position_y][1 + position_x] == TOKEN_2) &&
(game_field[1 + position_y][2 + position_x] == TOKEN_2) &&
(game_field[0 + position_y][3 + position_x] == TOKEN_2)) == 1)
{
return 1;
}
}
}
return 0;
}

//-----------------------------------------------------------------------------
///
/// checkPlayer1DiagonalNeg: checks whether the Player 1 has a combination of 4
///                          of his Token in a negative Diagonal of game_field
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
///
/// @return 1 if a diagonal negative combination of
///         Player 1 Tokens ('X') is found, 0 if no 4 Token of Player 1
//
int checkPlayer1DiagonalNeg(char game_field, int position_x,
int position_y, const char TOKEN_1)
{
for(position_y = 0; position_y < 4; position_y++)
{
for(position_x = 0; position_x < 3; position_x++)
{
if(((game_field[0 + position_y][0 + position_x] == TOKEN_1) &&
(game_field[1 + position_y][1 + position_x] == TOKEN_1) &&
(game_field[2 + position_y][2 + position_x] == TOKEN_1) &&
(game_field[3 + position_y][3 + position_x] == TOKEN_1)) == 1)
{
return 1;
}
}
}
return 0;
}

//-----------------------------------------------------------------------------
///
/// checkPlayer2DiagonalNeg: checks whether the Player 2 has a combination of 4
///                          of his Token in a negative Diagonal of game_field
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
///
/// @return 1 if a diagonal negative combination of
///         Player 2 Tokens ('O') is found, 0 if no 4 Token of Player 2
//
int checkPlayer2DiagonalNeg(char game_field, int position_x,
int position_y, const char TOKEN_2)
{
for(position_y = 0; position_y < 4; position_y++)
{
for(position_x = 0; position_x < 3; position_x++)
{
if(((game_field[0 + position_y][0 + position_x] == TOKEN_2) &&
(game_field[1 + position_y][1 + position_x] == TOKEN_2) &&
(game_field[2 + position_y][2 + position_x] == TOKEN_2) &&
(game_field[3 + position_y][3 + position_x] == TOKEN_2)) == 1)
{
return 1;
}
}
}
return 0;
}

//-----------------------------------------------------------------------------
///
/// checkVictory: checks every winning combination of every player in this game
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
///
/// @return 1 if a winning combination of 4 Token of player 1 found
///         2 if a winning combination of 4 Token of player 2 found
///         0 if no winning combination of any player found
//
int checkVictory(char game_field, int position_x, int position_y,
const char TOKEN_1, const char TOKEN_2)
{
if (
checkPlayer1Horizontal(game_field, position_x, position_y, TOKEN_1) ||
checkPlayer1Vertical(game_field, position_x, position_y, TOKEN_1) ||
checkPlayer1DiagonalPos(game_field, position_x, position_y, TOKEN_1) ||
checkPlayer1DiagonalNeg(game_field, position_x, position_y, TOKEN_1) == 1
)
{
return 1;
}

if (
checkPlayer2Horizontal(game_field, position_x, position_y, TOKEN_2) ||
checkPlayer2Vertical(game_field, position_x, position_y, TOKEN_2) ||
checkPlayer2DiagonalPos(game_field, position_x, position_y, TOKEN_2) ||
checkPlayer2DiagonalNeg(game_field, position_x, position_y, TOKEN_2) == 1
)
{
return 2;
}

return 0;
}

//-----------------------------------------------------------------------------
///
/// checkMakePossibleMoveA: checks if set Token in A is possible and
///                         sets the Token of actual player there if yes
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
/// @param EMPTY_FIELD Is a const char and stands for a field with no Token
///
/// @return game_field With the new set Token if it was possible.
///                    else: same game_field as before
//
char checkMakePossibleMoveA(char game_field, int position_x,
int position_y, unsigned active_player, const char TOKEN_1,
const char TOKEN_2, const char EMPTY_FIELD)
{

for (position_y = 6; position_y >= 0; position_y--)
{
if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 1) == 1)
{
game_field[position_y] = TOKEN_2;
return game_field;
}

if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 0) == 1)
{
game_field[position_y] = TOKEN_1;
return game_field;
}
}

printf("Invalid move. Try again.\n");
return game_field;
}

//-----------------------------------------------------------------------------
///
/// checkMakePossibleMoveB: checks if set Token in B is possible and
///                         sets the Token of actual player there if yes
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
/// @param EMPTY_FIELD Is a const char and stands for a field with no Token
///
/// @return game_field With the new set Token if it was possible.
///                    else: same game_field as before
//
char checkMakePossibleMoveB(char game_field, int position_x,
int position_y, unsigned active_player, const char TOKEN_1,
const char TOKEN_2, const char EMPTY_FIELD)
{

for (position_y = 6; position_y >= 0; position_y--)
{
if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 1) == 1)
{
game_field[position_y] = TOKEN_2;
return game_field;
}

if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 0) == 1)
{
game_field[position_y] = TOKEN_1;
return game_field;
}
}

printf("Invalid move. Try again.\n");
return game_field;
}

//-----------------------------------------------------------------------------
///
/// checkMakePossibleMoveC: checks if set Token in C is possible and
///                         sets the Token of actual player there if yes
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
/// @param EMPTY_FIELD Is a const char and stands for a field with no Token
///
/// @return game_field With the new set Token if it was possible.
///                    else: same game_field as before
//
char checkMakePossibleMoveC(char game_field, int position_x,
int position_y, unsigned active_player, const char TOKEN_1,
const char TOKEN_2, const char EMPTY_FIELD)
{

for (position_y = 6; position_y >= 0; position_y--)
{
if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 1) == 1)
{
game_field[position_y] = TOKEN_2;
return game_field;
}

if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 0) == 1)
{
game_field[position_y] = TOKEN_1;
return game_field;
}
}

printf("Invalid move. Try again.\n");
return game_field;
}

//-----------------------------------------------------------------------------
///
/// checkMakePossibleMoveD: checks if set Token in D is possible and
///                         sets the Token of actual player there if yes
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
/// @param EMPTY_FIELD Is a const char and stands for a field with no Token
///
/// @return game_field With the new set Token if it was possible.
///                    else: same game_field as before
//
char checkMakePossibleMoveD(char game_field, int position_x,
int position_y, unsigned active_player, const char TOKEN_1,
const char TOKEN_2, const char EMPTY_FIELD)
{

for (position_y = 6; position_y >= 0; position_y--)
{
if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 1) == 1)
{
game_field[position_y] = TOKEN_2;
return game_field;
}

if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 0) == 1)
{
game_field[position_y] = TOKEN_1;
return game_field;
}
}

printf("Invalid move. Try again.\n");
return game_field;
}

//-----------------------------------------------------------------------------
///
/// checkMakePossibleMoveE: checks if set Token in E is possible and
///                         sets the Token of actual player there if yes
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
/// @param EMPTY_FIELD Is a const char and stands for a field with no Token
///
/// @return game_field With the new set Token if it was possible.
///                    else: same game_field as before
//
char checkMakePossibleMoveE(char game_field, int position_x,
int position_y, unsigned active_player, const char TOKEN_1,
const char TOKEN_2, const char EMPTY_FIELD)
{

for (position_y = 6; position_y >= 0; position_y--)
{
if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 1) == 1)
{
game_field[position_y] = TOKEN_2;
return game_field;
}

if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 0) == 1)
{
game_field[position_y] = TOKEN_1;
return game_field;
}
}

printf("Invalid move. Try again.\n");
return game_field;
}

//-----------------------------------------------------------------------------
///
/// checkMakePossibleMoveF: checks if set Token in F is possible and
///                         sets the Token of actual player there if yes
///
/// @param game_field The 2 dimensional array where the set Token are saved
/// @param position_x To determine the column A to F in the gamefield
/// @param position_y To determine the row 1 to 7 in the gamefield
/// @param TOKEN_1 Is a const char and stands for a set Token of Player 1
/// @param TOKEN_2 Is a const char and stands for a set Token of Player 2
/// @param EMPTY_FIELD Is a const char and stands for a field with no Token
///
/// @return game_field With the new set Token if it was possible.
///                    else: same game_field as before
//
char checkMakePossibleMoveF(char game_field, int position_x,
int position_y, unsigned active_player, const char TOKEN_1,
const char TOKEN_2, const char EMPTY_FIELD)
{

for (position_y = 6; position_y >= 0; position_y--)
{
if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 1) == 1)
{
game_field[position_y] = TOKEN_2;
return game_field;
}

if ((strncmp(&game_field[position_y], &EMPTY_FIELD, 1) == 0) &&
(active_player == 0) == 1)
{
game_field[position_y] = TOKEN_1;
return game_field;
}
}

printf("Invalid move. Try again.\n");
return game_field;
}

//-----------------------------------------------------------------------------
///
/// printHelp: shows you the possible commands in this game to set a Token,
///            save a gamescore, quit the game, show the actual game_field
///            or do this here
///
/// @param No parameters for this function
///
/// @return No return because of void
//
void printHelp()
{
printf("Available commands:\n");

printf("set X:   ");
printf("set token of active player in column denoted by X\n");

printf("save X:  ");
printf("write game to the data_file, where X ");
printf("is the name of the data_file\n");

printf("QUIT:    exit the game\n");

printf("SHOW:    displays the game board\n");

printf("HELP:    displays this message\n");
}

//-----------------------------------------------------------------------------
///
/// main: every variable in this program is initialized here
///       (except of FILE *data_file).
///       If started with an valid argument, the game continues depending
///       to the opened file.
///       After correct start, active player has to set a Token.
///       When a winning combination occurs or you write quit or the
///       argument file is not able to be opened the program ends
///
/// @param argc Counted read words at start of program
/// @param argv Is an array of strings with name of data_file in first storage
///
/// @return depending on the command
///         0 if quit the game
///         1 if you start the program with more than 1 argument
///         2 if a player won the game
///         -4 if a argument file cannot be loaded
//
int main(int argc, char* argv[])
{

//_____________________________________________________________________________
//from here...

const char SET_A = "set A";
const char SET_B = "set B";
const char SET_C = "set C";
const char SET_D = "set D";
const char SET_E = "set E";
const char SET_F = "set F";
const char SHOW = "show";
const char QUIT = "quit";
const char HELP = "help";
const char SAVE = "save ";

const char EMPTY_FIELD = '-';
const char TOKEN_1 = 'X';     //Token of Player 1
const char TOKEN_2 = 'O';     //Token of Player 2

//active_player knows whose next turn is it. 0 = actual turn is Player 1
//1 = actual turn is Player 2. First turn is Player 1 ('X') every new game
unsigned active_player = 0;

char *save_data_filename;
char delimiter[] = " ";

unsigned set_Token_1 = 0;
unsigned set_Token_2 = 0;

int position_x = 0;  //position_x is column A to F, position_y is row 1 to 7
int position_y = 0;

char game_field = {{'-','-','-','-','-','-'},
{'-','-','-','-','-','-'},
{'-','-','-','-','-','-'},
{'-','-','-','-','-','-'},
{'-','-','-','-','-','-'},
{'-','-','-','-','-','-'},
{'-','-','-','-','-','-'}};

char command_input;
int general_counter;

for (general_counter = 0; general_counter < 128; general_counter++)
{
command_input[general_counter] = '0';
}

int letter_counter = 0;
char letter_storage = '0';

//_____________________________________________________________________________
//... to here only variable initialization and declaration.

if (argc == 1)  //starts program with no argument
{
printf("ep> ");
}
else if (argc == 2)   //starts program with 1 argument
{
FILE *data_file;
data_file = fopen(argv, "rb");  //reads binary code

if(data_file == NULL)  //if the data_file can't be loaded
{
printf("Error loading data_file.\n");
return -4;
}

else  //load the active_player and gamefield
{
fscanf(data_file, "%d", &active_player);
loadGameField(data_file, game_field, position_x, position_y);
}
fclose(data_file);

printf("ep> ");
}
else  //more than 1 argument
{
printf ("usage: ./assa [savefile]\n");
return 1;
}

//This while is executed as long as not quit or a player won the game.
while (1)
{
letter_storage = '0';   //set to '0' everytime a new gamecycle starts.
letter_counter = 0;   //set to 0 because a new command becomes counted

//command_input becomes overwritten (deleted) by. Now we know what is in there.
for (general_counter = 0; general_counter < 128; general_counter++)
{
command_input[general_counter] = '0';
}

//gets the given characters from the commando line. This characters becomes
//saved in the command_input variable which is compared with commands later
//Everytime the getchar() is used here,
//it gets the next character from the input.
while (letter_storage != '\n')
{
letter_storage = getchar();
command_input[letter_counter] = letter_storage;
letter_counter++;

//The if sets the last read letter (alway <Enter>) to a \0 determiner.
if (letter_storage == '\n')
{
command_input[letter_counter - 1] = '\0';
}
}

//letter counter becomes decremented by one after reading the characters,
//because we dont want to compare the \0 determiner.
letter_counter--;

//In the following the if else if construction compares the input with a
//valid command. If the if/else if comparing condition is true (compare == 0),
//the command becomes executed. If no known command: "invalid command... "
if (strncmp(command_input, SAVE, 5) == 0)
{
//The command_input will be splitted after space (delemiter)
//Everything between first space and \0 becomes data_filename
save_data_filename = strpbrk(command_input, delimiter) + 1;
saveGame(active_player, save_data_filename, game_field,
position_x, position_y);
}

//For the following 6 else if: checks if input equals command
else if (strncmp(command_input, SET_A, letter_counter) == 0 &&
letter_counter != 0)
{
checkMakePossibleMoveA(game_field, position_x, position_y,
active_player, TOKEN_1, TOKEN_2, EMPTY_FIELD);
checkClearFirstRow(game_field, position_x, position_y, EMPTY_FIELD);
printGameField(game_field, position_x, position_y);
}

else if (strncmp(command_input, SET_B, letter_counter) == 0 &&
letter_counter != 0)
{
checkMakePossibleMoveB(game_field, position_x, position_y,
active_player, TOKEN_1, TOKEN_2, EMPTY_FIELD);
checkClearFirstRow(game_field, position_x, position_y, EMPTY_FIELD);
printGameField(game_field, position_x, position_y);
}

else if (strncmp(command_input, SET_C, letter_counter) == 0 &&
letter_counter != 0)
{
checkMakePossibleMoveC(game_field, position_x, position_y,
active_player, TOKEN_1, TOKEN_2, EMPTY_FIELD);
checkClearFirstRow(game_field, position_x, position_y, EMPTY_FIELD);
printGameField(game_field, position_x, position_y);
}

else if (strncmp(command_input, SET_D, letter_counter) == 0 &&
letter_counter != 0)
{
checkMakePossibleMoveD(game_field, position_x, position_y,
active_player, TOKEN_1, TOKEN_2, EMPTY_FIELD);
checkClearFirstRow(game_field, position_x, position_y, EMPTY_FIELD);
printGameField(game_field, position_x, position_y);
}

else if (strncmp(command_input, SET_E, letter_counter) == 0 &&
letter_counter != 0)
{
checkMakePossibleMoveE(game_field, position_x, position_y,
active_player, TOKEN_1, TOKEN_2, EMPTY_FIELD);
checkClearFirstRow(game_field, position_x, position_y, EMPTY_FIELD);
printGameField(game_field, position_x, position_y);
}

else if (strncmp(command_input, SET_F, letter_counter) == 0 &&
letter_counter != 0)
{
checkMakePossibleMoveF(game_field, position_x, position_y,
active_player, TOKEN_1, TOKEN_2, EMPTY_FIELD);
checkClearFirstRow(game_field, position_x, position_y, EMPTY_FIELD);
printGameField(game_field, position_x, position_y);
}

else if (strncmp(command_input, HELP, letter_counter) == 0 &&
letter_counter != 0)
{
printHelp();
}

else if (strncmp(command_input, SHOW, letter_counter) == 0 &&
letter_counter != 0)
{
printGameField(game_field, position_x, position_y);
}

else if (strncmp(command_input, QUIT, letter_counter) == 0 &&
letter_counter != 0)
{
return 0;
}

else
{
printf("Unknown command.\n");
}

set_Token_1 = 0;  //set to zero, so we can compare Tokens (again).
set_Token_2 = 0;  //set to zero, so we can compare Tokens (again).

//This for - construction determines how many Tokens X and O was set.
for (position_x = 0; position_x < 6; position_x++)
{
for (position_y = 0; position_y < 7; position_y++)
{
if (game_field[position_y][position_x] == TOKEN_2)
{
set_Token_2++;
}
else if (game_field[position_y][position_x] == TOKEN_1)
{
set_Token_1++;
}
}
}

//The if else if structure sets the active player to actual player 1 or 0.
if (set_Token_2 < set_Token_1)
{
active_player = 1;
}
else if (set_Token_2 > set_Token_1 || set_Token_1 == set_Token_2)
{
active_player = 0;
}

//This switch case construct checks, whether a player has won the game.
//If not ( = case 0): do nothing and go on.
switch (checkVictory(game_field, position_x, position_y, TOKEN_1, TOKEN_2))
{
case 1:
printf("Player %c has won the game!\n", TOKEN_1);
return 2;   //return 2 if a player won the game
break;

case 2:
printf("Player %c has won the game!\n", TOKEN_2);
return 2;   //return 2 if a player won the game
break;

case 0:
break;
}

printf("ep> ");   //end of while, new game cycle starts after this
}
}
```

This snippet took 0.05 seconds to highlight.

Back to the Entry List or Home.