Initial commit; 2017 me stupidly didn't use free(), so that needs fixing.
Fixed memory issue and did some cleanup Removed VS code trash
This commit is contained in:
parent
5749e24299
commit
aba952c8a1
|
@ -0,0 +1,6 @@
|
||||||
|
FROM gcc:8.4.0
|
||||||
|
|
||||||
|
WORKDIR /opt
|
||||||
|
COPY src/** /opt
|
||||||
|
RUN gcc -std=c11 -o connectfour connectfour.c file_utils.c gamelogic.c arghandler.c
|
||||||
|
CMD ["./connectfour"]
|
|
@ -3,7 +3,7 @@
|
||||||
#include "arghandler.h"
|
#include "arghandler.h"
|
||||||
|
|
||||||
/************************************************************************************
|
/************************************************************************************
|
||||||
* @author Scoots
|
* @author Amber
|
||||||
* @version 3/03/2017
|
* @version 3/03/2017
|
||||||
* @see arghandler.h
|
* @see arghandler.h
|
||||||
*
|
*
|
||||||
|
@ -53,21 +53,20 @@ int winlen;
|
||||||
/*
|
/*
|
||||||
* Saved filename for loading.
|
* Saved filename for loading.
|
||||||
*/
|
*/
|
||||||
char* loadFile;
|
char *loadFile;
|
||||||
|
|
||||||
struct argp_option options[] = {
|
struct argp_option options[] = {
|
||||||
{ "width", 'w', "WIDTH", 0, "Width dimension for board." },
|
{"width", 'w', "WIDTH", 0, "Width dimension for board."},
|
||||||
{ "height", 'h', "HEIGHT", 0, "Height dimension for board." },
|
{"height", 'h', "HEIGHT", 0, "Height dimension for board."},
|
||||||
{ "square", 's', "SQUARE", 0, "Dimensions for a square board (width and height will be overrided)." },
|
{"square", 's', "SQUARE", 0, "Dimensions for a square board (width and height will be overrided)."},
|
||||||
{ "connect", 'c', "CONNECT", 0, "Length to win the game." },
|
{"connect", 'c', "CONNECT", 0, "Length to win the game."},
|
||||||
{ "load", 'l', "LOAD", 0, "Load a previous saved game." },
|
{"load", 'l', "LOAD", 0, "Load a previous saved game."},
|
||||||
{ 0 }
|
{0}};
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Required arguments for the argp struct.
|
* Required arguments for the argp struct.
|
||||||
*/
|
*/
|
||||||
struct argp argp = { options, parse_opt, args_doc, doc };
|
struct argp argp = {options, parse_opt, args_doc, doc};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Argument values for flags.
|
* Argument values for flags.
|
||||||
|
@ -81,9 +80,11 @@ struct arguments arguments;
|
||||||
* @param arg, the argument being passed.
|
* @param arg, the argument being passed.
|
||||||
* @param state, the state of parser.
|
* @param state, the state of parser.
|
||||||
*/
|
*/
|
||||||
error_t parse_opt(int key, char* arg, struct argp_state *state) {
|
error_t parse_opt(int key, char *arg, struct argp_state *state)
|
||||||
|
{
|
||||||
struct arguments *arguments = state->input;
|
struct arguments *arguments = state->input;
|
||||||
switch (key) {
|
switch (key)
|
||||||
|
{
|
||||||
case 'w':
|
case 'w':
|
||||||
arguments->width = atoi(arg);
|
arguments->width = atoi(arg);
|
||||||
break;
|
break;
|
||||||
|
@ -110,9 +111,10 @@ error_t parse_opt(int key, char* arg, struct argp_state *state) {
|
||||||
* @param argc, number of command line arguments.
|
* @param argc, number of command line arguments.
|
||||||
* @param argv, the command line arguments.
|
* @param argv, the command line arguments.
|
||||||
*/
|
*/
|
||||||
void setup(int argc, char** argv) {
|
void setup(int argc, char **argv)
|
||||||
wid= 0;
|
{
|
||||||
hei= 0;
|
wid = 0;
|
||||||
|
hei = 0;
|
||||||
sqr = 0;
|
sqr = 0;
|
||||||
winlen = 0;
|
winlen = 0;
|
||||||
loadFile = NULL;
|
loadFile = NULL;
|
||||||
|
@ -124,7 +126,8 @@ void setup(int argc, char** argv) {
|
||||||
sqr = arguments.square;
|
sqr = arguments.square;
|
||||||
winlen = arguments.winL;
|
winlen = arguments.winL;
|
||||||
|
|
||||||
if (arguments.loadFile != NULL) {
|
if (arguments.loadFile != NULL)
|
||||||
|
{
|
||||||
loadFile = arguments.loadFile;
|
loadFile = arguments.loadFile;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,7 +3,7 @@
|
||||||
#include <argp.h>
|
#include <argp.h>
|
||||||
|
|
||||||
/************************************************************************************
|
/************************************************************************************
|
||||||
* @author Scoots
|
* @author Amber
|
||||||
* @version 3/03/2017
|
* @version 3/03/2017
|
||||||
* @see argp.h
|
* @see argp.h
|
||||||
*
|
*
|
||||||
|
@ -33,27 +33,28 @@ int winlen;
|
||||||
/*
|
/*
|
||||||
* Saved filename for loading.
|
* Saved filename for loading.
|
||||||
*/
|
*/
|
||||||
char* loadFile;
|
char *loadFile;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Argument values for flags.
|
* Argument values for flags.
|
||||||
*/
|
*/
|
||||||
struct arguments {
|
struct arguments
|
||||||
|
{
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
int square;
|
int square;
|
||||||
int winL;
|
int winL;
|
||||||
char* loadFile;
|
char *loadFile;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Function that sets up values post-parsing.
|
* Function that sets up values post-parsing.
|
||||||
*/
|
*/
|
||||||
error_t parse_opt(int key, char* arg, struct argp_state *state);
|
error_t parse_opt(int key, char *arg, struct argp_state *state);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parses arguments.
|
* Parses arguments.
|
||||||
*/
|
*/
|
||||||
void setup(int argc, char** argv);
|
void setup(int argc, char **argv);
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -5,8 +5,8 @@
|
||||||
#include "arghandler.h"
|
#include "arghandler.h"
|
||||||
|
|
||||||
/************************************************************************************
|
/************************************************************************************
|
||||||
* @author Scoots
|
* @author Amber
|
||||||
* @version 3/03/2017
|
* @version 2/23/2021
|
||||||
* @see gamelogic.h
|
* @see gamelogic.h
|
||||||
* @see arghandler.h
|
* @see arghandler.h
|
||||||
*
|
*
|
||||||
|
@ -25,68 +25,87 @@
|
||||||
* @param argv, the command line arguments.
|
* @param argv, the command line arguments.
|
||||||
* @return 0 if the operation failed and 1 if it succeeded.
|
* @return 0 if the operation failed and 1 if it succeeded.
|
||||||
*/
|
*/
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
// Variables and array setup
|
// Variables and array setup
|
||||||
char* input = malloc(100 * sizeof(char));
|
char *input = malloc(100 * sizeof(char));
|
||||||
int player = 1;
|
int player = 1;
|
||||||
setup(argc, argv);
|
setup(argc, argv);
|
||||||
|
|
||||||
// Fixes invalid height or width arguments
|
// Fixes invalid height or width arguments
|
||||||
if (hei <= 0 || hei > 2147483647 || wid <= 0 || wid > 2147483647) {
|
if (hei <= 0 || hei > 2147483647 || wid <= 0 || wid > 2147483647)
|
||||||
|
{
|
||||||
setHeight(7);
|
setHeight(7);
|
||||||
setWidth(7);
|
setWidth(7);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
setHeight(hei);
|
setHeight(hei);
|
||||||
setWidth(wid);
|
setWidth(wid);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fixes invalid win length arguments
|
// Fixes invalid win length arguments
|
||||||
if (winlen <= 0 || winlen > 2147483647) {
|
if (winlen <= 0 || winlen > 2147483647)
|
||||||
|
{
|
||||||
setWinLength(4);
|
setWinLength(4);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
setWinLength(winlen);
|
setWinLength(winlen);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fixes invalid square dimension arguments
|
// Fixes invalid square dimension arguments
|
||||||
if (sqr > 0 && sqr <= 2147483647) {
|
if (sqr > 0 && sqr <= 2147483647)
|
||||||
|
{
|
||||||
setHeight(sqr);
|
setHeight(sqr);
|
||||||
setWidth(sqr);
|
setWidth(sqr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Either starts new game or loads from argument
|
// Either starts new game or loads from argument
|
||||||
printf("%sWelcome to Connect Four!\nType 'save' to save, 'load' to load, and 'quit' to quit.\n%s", "\x1B[32m", "\033[0m");
|
printf("%sWelcome to Connect Four!\nType 'save' to save, 'load' to load, and 'quit' to quit.\n%s", "\x1B[32m", "\033[0m");
|
||||||
if (loadFile != NULL) {
|
if (loadFile != NULL)
|
||||||
if (loadGame(loadFile) == 0) {
|
{
|
||||||
|
if (loadGame(loadFile) == 0)
|
||||||
|
{
|
||||||
printf("\nInvalid file.\n");
|
printf("\nInvalid file.\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
printBoard();
|
printBoard();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
populateBoard();
|
populateBoard();
|
||||||
printBoard();
|
printBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
// While loop for the game logic
|
// While loop for the game logic
|
||||||
while (1) {
|
while (1)
|
||||||
|
{
|
||||||
// Checks if player 1 has won
|
// Checks if player 1 has won
|
||||||
if (checkWin('o') == 1) {
|
if (checkWin('o') == 1)
|
||||||
|
{
|
||||||
printf("Player 1 wins!\n");
|
printf("Player 1 wins!\n");
|
||||||
printf("Would you like to play again? Type 'yes' for yes or 'no' for no.\n");
|
printf("Would you like to play again? Type 'yes' for yes or 'no' for no.\n");
|
||||||
while (1) {
|
while (1)
|
||||||
|
{
|
||||||
scanf("%s", input);
|
scanf("%s", input);
|
||||||
if ((strcmp("yes", input) == 0)) {
|
if ((strcmp("yes", input) == 0))
|
||||||
|
{
|
||||||
printf("\n");
|
printf("\n");
|
||||||
populateBoard();
|
populateBoard();
|
||||||
player = 1;
|
player = 1;
|
||||||
printBoard();
|
printBoard();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ((strcmp("no", input) == 0)) {
|
if ((strcmp("no", input) == 0))
|
||||||
|
{
|
||||||
|
free(input);
|
||||||
|
clean();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("Invalid input.\n");
|
printf("Invalid input.\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -94,20 +113,29 @@ int main(int argc, char** argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if player 2 has won
|
// Checks if player 2 has won
|
||||||
if (checkWin('x') == 1) {
|
if (checkWin('x') == 1)
|
||||||
|
{
|
||||||
printf("Player 2 wins!\n");
|
printf("Player 2 wins!\n");
|
||||||
printf("Would you like to play again? Type 'yes' for yes or 'no' for no.\n");
|
printf("Would you like to play again? Type 'yes' for yes or 'no' for no.\n");
|
||||||
while (1) {
|
while (1)
|
||||||
|
{
|
||||||
scanf("%s", input);
|
scanf("%s", input);
|
||||||
if ((strcmp("yes", input) == 0)) {
|
if ((strcmp("yes", input) == 0))
|
||||||
|
{
|
||||||
populateBoard();
|
populateBoard();
|
||||||
player = 1;
|
player = 1;
|
||||||
printBoard();
|
printBoard();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
break;
|
break;
|
||||||
} if ((strcmp("no", input) == 0)) {
|
}
|
||||||
|
if ((strcmp("no", input) == 0))
|
||||||
|
{
|
||||||
|
free(input);
|
||||||
|
clean();
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("Invalid input.\n");
|
printf("Invalid input.\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -120,12 +148,16 @@ int main(int argc, char** argv) {
|
||||||
scanf("%s", input);
|
scanf("%s", input);
|
||||||
|
|
||||||
// For quitting
|
// For quitting
|
||||||
if (strcmp("quit", input) == 0) {
|
if (strcmp("quit", input) == 0)
|
||||||
|
{
|
||||||
|
free(input);
|
||||||
|
clean();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Saving functionality
|
// Saving functionality
|
||||||
if (strcmp("save", input) == 0) {
|
if (strcmp("save", input) == 0)
|
||||||
|
{
|
||||||
printf("Input a filename:\n");
|
printf("Input a filename:\n");
|
||||||
scanf("%s", input);
|
scanf("%s", input);
|
||||||
saveGame(input);
|
saveGame(input);
|
||||||
|
@ -134,13 +166,17 @@ int main(int argc, char** argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loading functionality
|
// Loading functionality
|
||||||
if (strcmp("load", input) == 0) {
|
if (strcmp("load", input) == 0)
|
||||||
|
{
|
||||||
printf("Input a filename:\n");
|
printf("Input a filename:\n");
|
||||||
scanf("%s", input);
|
scanf("%s", input);
|
||||||
if (loadGame(loadFile) == 0) {
|
if (loadGame(loadFile) == 0)
|
||||||
|
{
|
||||||
printf("\nInvalid file.\n");
|
printf("\nInvalid file.\n");
|
||||||
continue;
|
continue;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printBoard();
|
printBoard();
|
||||||
printf("\nGame loaded.\n");
|
printf("\nGame loaded.\n");
|
||||||
continue;
|
continue;
|
||||||
|
@ -148,24 +184,32 @@ int main(int argc, char** argv) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks to make sure input is valid
|
// Checks to make sure input is valid
|
||||||
if (atoi(input) < 1 || atoi(input) > width) {
|
if (atoi(input) < 1 || atoi(input) > width)
|
||||||
|
{
|
||||||
printf("Invalid input. Input another column:\n");
|
printf("Invalid input. Input another column:\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks to make sure input is valid
|
// Checks to make sure input is valid
|
||||||
if (placeChip(atoi(input) - 1, player) == 0) {
|
if (placeChip(atoi(input) - 1, player) == 0)
|
||||||
|
{
|
||||||
printf("Invalid input. Input another column:\n");
|
printf("Invalid input. Input another column:\n");
|
||||||
} else {
|
}
|
||||||
if (player == 1) {
|
else
|
||||||
|
{
|
||||||
|
if (player == 1)
|
||||||
|
{
|
||||||
player = 2;
|
player = 2;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
player = 1;
|
player = 1;
|
||||||
}
|
}
|
||||||
printBoard();
|
printBoard();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(input);
|
||||||
|
clean();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
#include "file_utils.h"
|
#include "file_utils.h"
|
||||||
|
|
||||||
/************************************************************************************
|
/************************************************************************************
|
||||||
* @author Scoots
|
* @author Amber
|
||||||
* @version 3/03/2017
|
* @version 3/03/2017
|
||||||
* @see file_utils.h
|
* @see file_utils.h
|
||||||
*
|
*
|
|
@ -2,7 +2,7 @@
|
||||||
#define FILE_UTILS
|
#define FILE_UTILS
|
||||||
|
|
||||||
/************************************************************************************
|
/************************************************************************************
|
||||||
* @author Scoots
|
* @author Amber
|
||||||
* @version 3/03/2017
|
* @version 3/03/2017
|
||||||
*
|
*
|
||||||
* Header file for file utilities for reading in a file to an array and then
|
* Header file for file utilities for reading in a file to an array and then
|
|
@ -5,8 +5,8 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
/************************************************************************************
|
/************************************************************************************
|
||||||
* @author Scoots
|
* @author Amber
|
||||||
* @version 3/03/2017
|
* @version 2/23/2021
|
||||||
* @see gamelogic.h
|
* @see gamelogic.h
|
||||||
*
|
*
|
||||||
* Implementation for the Connect Four game logic. Includes all of the functions
|
* Implementation for the Connect Four game logic. Includes all of the functions
|
||||||
|
@ -19,7 +19,7 @@
|
||||||
/*
|
/*
|
||||||
* Doubly array that will store the game board.
|
* Doubly array that will store the game board.
|
||||||
*/
|
*/
|
||||||
char** gameBoard;
|
char **gameBoard;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Height of the game board.
|
* Height of the game board.
|
||||||
|
@ -43,7 +43,8 @@ int winLength;
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int setHeight(int h) {
|
int setHeight(int h)
|
||||||
|
{
|
||||||
height = h;
|
height = h;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -55,7 +56,8 @@ int setHeight(int h) {
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int setWidth(int w) {
|
int setWidth(int w)
|
||||||
|
{
|
||||||
width = w;
|
width = w;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -67,7 +69,8 @@ int setWidth(int w) {
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int setWinLength(int l) {
|
int setWinLength(int l)
|
||||||
|
{
|
||||||
winLength = l;
|
winLength = l;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -78,14 +81,22 @@ int setWinLength(int l) {
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int printBoard() {
|
int printBoard()
|
||||||
for (int i = 0; i < height; i++) {
|
{
|
||||||
for (int j = 0; j < width; j++) {
|
for (int i = 0; i < height; i++)
|
||||||
if (gameBoard[i][j] == 'o') {
|
{
|
||||||
|
for (int j = 0; j < width; j++)
|
||||||
|
{
|
||||||
|
if (gameBoard[i][j] == 'o')
|
||||||
|
{
|
||||||
printf("\x1b[34m%c\x1b[0m", gameBoard[i][j]);
|
printf("\x1b[34m%c\x1b[0m", gameBoard[i][j]);
|
||||||
} else if (gameBoard[i][j] == 'x') {
|
}
|
||||||
|
else if (gameBoard[i][j] == 'x')
|
||||||
|
{
|
||||||
printf("\x1b[31m%c\x1b[0m", gameBoard[i][j]);
|
printf("\x1b[31m%c\x1b[0m", gameBoard[i][j]);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
printf("%c", gameBoard[i][j]);
|
printf("%c", gameBoard[i][j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,13 +112,17 @@ int printBoard() {
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int populateBoard() {
|
int populateBoard()
|
||||||
gameBoard = malloc(sizeof(char*) * height);
|
{
|
||||||
for (int i = 0; i < height; i++) {
|
gameBoard = malloc(sizeof(char *) * height);
|
||||||
|
for (int i = 0; i < height; i++)
|
||||||
|
{
|
||||||
gameBoard[i] = malloc(sizeof(char) * width);
|
gameBoard[i] = malloc(sizeof(char) * width);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < height; i++) {
|
for (int i = 0; i < height; i++)
|
||||||
for (int j = 0; j < width; j++) {
|
{
|
||||||
|
for (int j = 0; j < width; j++)
|
||||||
|
{
|
||||||
gameBoard[i][j] = '*';
|
gameBoard[i][j] = '*';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,25 +140,33 @@ int populateBoard() {
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int placeChip(int col, int p) {
|
int placeChip(int col, int p)
|
||||||
|
{
|
||||||
// Handles bad arguments
|
// Handles bad arguments
|
||||||
if (col > width - 1 || col < 0) {
|
if (col > width - 1 || col < 0)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks to make sure the move is valid and places if so
|
// Checks to make sure the move is valid and places if so
|
||||||
for (int i = height - 1; i >= 0; i--) {
|
for (int i = height - 1; i >= 0; i--)
|
||||||
if (i > 0 && (gameBoard[i][col] == 'o' || gameBoard[i][col] == 'x')) {
|
{
|
||||||
|
if (i > 0 && (gameBoard[i][col] == 'o' || gameBoard[i][col] == 'x'))
|
||||||
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if (i == 0 && gameBoard[i][col] != '*') {
|
else if (i == 0 && gameBoard[i][col] != '*')
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
if (p == 1) {
|
{
|
||||||
|
if (p == 1)
|
||||||
|
{
|
||||||
gameBoard[i][col] = 'o';
|
gameBoard[i][col] = 'o';
|
||||||
}
|
}
|
||||||
if (p == 2) {
|
if (p == 2)
|
||||||
|
{
|
||||||
gameBoard[i][col] = 'x';
|
gameBoard[i][col] = 'x';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -161,14 +184,15 @@ int placeChip(int col, int p) {
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int saveGame(char* filename) {
|
int saveGame(char *filename)
|
||||||
|
{
|
||||||
// Variables and character array setup
|
// Variables and character array setup
|
||||||
char* buffer;
|
char *buffer;
|
||||||
int count = 33;
|
int count = 33;
|
||||||
buffer = malloc((sizeof(char) * width * height) + (33 * sizeof(char)));
|
buffer = malloc((sizeof(char) * width * height) + (33 * sizeof(char)));
|
||||||
char* widthVal = malloc(10 * sizeof(char));
|
char *widthVal = malloc(10 * sizeof(char));
|
||||||
char* heightVal = malloc(10 * sizeof(char));
|
char *heightVal = malloc(10 * sizeof(char));
|
||||||
char* winVal = malloc(10 * sizeof(char));
|
char *winVal = malloc(10 * sizeof(char));
|
||||||
|
|
||||||
// Stores board parameter and win length as strings
|
// Stores board parameter and win length as strings
|
||||||
sprintf(widthVal, "%010d", width);
|
sprintf(widthVal, "%010d", width);
|
||||||
|
@ -212,12 +236,15 @@ int saveGame(char* filename) {
|
||||||
buffer[32] = '\n';
|
buffer[32] = '\n';
|
||||||
|
|
||||||
// Saves the state of the board to the buffer
|
// Saves the state of the board to the buffer
|
||||||
for (int i = 0; i < height; i++) {
|
for (int i = 0; i < height; i++)
|
||||||
for (int j = 0; j < width; j++) {
|
{
|
||||||
|
for (int j = 0; j < width; j++)
|
||||||
|
{
|
||||||
buffer[count] = gameBoard[i][j];
|
buffer[count] = gameBoard[i][j];
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
if (count >(width * height + 32)) {
|
if (count > (width * height + 32))
|
||||||
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,6 +254,11 @@ int saveGame(char* filename) {
|
||||||
// Writes the buffer into memory.
|
// Writes the buffer into memory.
|
||||||
write_file(filename, buffer, (sizeof(char) * width * height) + (33 * sizeof(char)));
|
write_file(filename, buffer, (sizeof(char) * width * height) + (33 * sizeof(char)));
|
||||||
|
|
||||||
|
// Free up reserved memory.
|
||||||
|
free(widthVal);
|
||||||
|
free(heightVal);
|
||||||
|
free(winVal);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,14 +271,16 @@ int saveGame(char* filename) {
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int loadGame(char* filename) {
|
int loadGame(char *filename)
|
||||||
|
{
|
||||||
// Variables
|
// Variables
|
||||||
char* buffer;
|
char *buffer;
|
||||||
int count = 33;
|
int count = 33;
|
||||||
char* sub;
|
char *sub;
|
||||||
|
|
||||||
// Reads in the previous game to memory
|
// Reads in the previous game to memory
|
||||||
if (read_file(filename, &buffer) == 0) {
|
if (read_file(filename, &buffer) == 0)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,13 +297,18 @@ int loadGame(char* filename) {
|
||||||
populateBoard();
|
populateBoard();
|
||||||
|
|
||||||
// Copies past game's state to board.
|
// Copies past game's state to board.
|
||||||
for (int i = 0; i < height; i++) {
|
for (int i = 0; i < height; i++)
|
||||||
for (int j = 0; j < width; j++) {
|
{
|
||||||
|
for (int j = 0; j < width; j++)
|
||||||
|
{
|
||||||
gameBoard[i][j] = buffer[count];
|
gameBoard[i][j] = buffer[count];
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Free up reserved memory.
|
||||||
|
free(sub);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,32 +320,41 @@ int loadGame(char* filename) {
|
||||||
* @return 0 if the player passed has not won, and 1 if the
|
* @return 0 if the player passed has not won, and 1 if the
|
||||||
* player passed has won.
|
* player passed has won.
|
||||||
*/
|
*/
|
||||||
int checkWin(char p) {
|
int checkWin(char p)
|
||||||
|
{
|
||||||
// Variables for the diagonal check algorithm
|
// Variables for the diagonal check algorithm
|
||||||
int row = 0;
|
int row = 0;
|
||||||
int col = 0;
|
int col = 0;
|
||||||
|
|
||||||
// Handles 1 by 1 boards
|
// Handles 1 by 1 boards
|
||||||
if (height == 1 && width == 1 && winLength == 1 ) {
|
if (height == 1 && width == 1 && winLength == 1)
|
||||||
if (gameBoard[0][0] == p) {
|
{
|
||||||
|
if (gameBoard[0][0] == p)
|
||||||
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Checks horizontally
|
// Checks horizontally
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (int i = 0; i < height; i++) {
|
for (int i = 0; i < height; i++)
|
||||||
for (int j = 0; j < width; j++) {
|
{
|
||||||
if (gameBoard[i][j] == p) {
|
for (int j = 0; j < width; j++)
|
||||||
|
{
|
||||||
|
if (gameBoard[i][j] == p)
|
||||||
|
{
|
||||||
count++;
|
count++;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
if (count == winLength) {
|
if (count == winLength)
|
||||||
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -315,14 +363,20 @@ int checkWin(char p) {
|
||||||
|
|
||||||
// Checks vertically
|
// Checks vertically
|
||||||
count = 0;
|
count = 0;
|
||||||
for (int i = 0; i < width; i++) {
|
for (int i = 0; i < width; i++)
|
||||||
for (int j = 0; j < height; j++) {
|
{
|
||||||
if (gameBoard[j][i] == p) {
|
for (int j = 0; j < height; j++)
|
||||||
|
{
|
||||||
|
if (gameBoard[j][i] == p)
|
||||||
|
{
|
||||||
count++;
|
count++;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
if (count == winLength) {
|
if (count == winLength)
|
||||||
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -331,22 +385,29 @@ int checkWin(char p) {
|
||||||
|
|
||||||
// Checks negative slope diagonal
|
// Checks negative slope diagonal
|
||||||
count = 0;
|
count = 0;
|
||||||
for (int i = 0; i < height; i++) {
|
for (int i = 0; i < height; i++)
|
||||||
for (int j = 0; j < width; j++) {
|
{
|
||||||
|
for (int j = 0; j < width; j++)
|
||||||
|
{
|
||||||
row = i;
|
row = i;
|
||||||
col = j;
|
col = j;
|
||||||
count = 0;
|
count = 0;
|
||||||
while (row < height && col < width) {
|
while (row < height && col < width)
|
||||||
if (gameBoard[row][col] == p) {
|
{
|
||||||
|
if (gameBoard[row][col] == p)
|
||||||
|
{
|
||||||
count++;
|
count++;
|
||||||
row++;
|
row++;
|
||||||
col++;
|
col++;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
row++;
|
row++;
|
||||||
col++;
|
col++;
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
if (count == winLength) {
|
if (count == winLength)
|
||||||
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -355,22 +416,29 @@ int checkWin(char p) {
|
||||||
|
|
||||||
// Checks positive slope diagonal
|
// Checks positive slope diagonal
|
||||||
count = 0;
|
count = 0;
|
||||||
for (int i = 0; i < height; i++) {
|
for (int i = 0; i < height; i++)
|
||||||
for (int j = 0; j < width; j++) {
|
{
|
||||||
|
for (int j = 0; j < width; j++)
|
||||||
|
{
|
||||||
row = i;
|
row = i;
|
||||||
col = j;
|
col = j;
|
||||||
count = 0;
|
count = 0;
|
||||||
while (row >= 0 && col < width) {
|
while (row >= 0 && col < width)
|
||||||
if (gameBoard[row][col] == p) {
|
{
|
||||||
|
if (gameBoard[row][col] == p)
|
||||||
|
{
|
||||||
count++;
|
count++;
|
||||||
row--;
|
row--;
|
||||||
col++;
|
col++;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
row--;
|
row--;
|
||||||
col++;
|
col++;
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
if (count == winLength) {
|
if (count == winLength)
|
||||||
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -390,7 +458,27 @@ int checkWin(char p) {
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int modifyPostition(int row, int col, char c) {
|
int modifyPostition(int row, int col, char c)
|
||||||
|
{
|
||||||
gameBoard[row][col] = c;
|
gameBoard[row][col] = c;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees up any reserved memory from
|
||||||
|
* the game session.
|
||||||
|
*/
|
||||||
|
int clean()
|
||||||
|
{
|
||||||
|
if (gameBoard != NULL)
|
||||||
|
{
|
||||||
|
free(gameBoard);
|
||||||
|
for (int i = 0; i < height; i++)
|
||||||
|
{
|
||||||
|
if (gameBoard[i] != NULL)
|
||||||
|
{
|
||||||
|
free(gameBoard[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,8 +2,8 @@
|
||||||
#define GAME_LOGIC
|
#define GAME_LOGIC
|
||||||
|
|
||||||
/************************************************************************************
|
/************************************************************************************
|
||||||
* @author Scoots
|
* @author Amber
|
||||||
* @version 3/03/2017
|
* @version 2/23/2021
|
||||||
*
|
*
|
||||||
* Header file for the Connect Four game logic.
|
* Header file for the Connect Four game logic.
|
||||||
*************************************************************************************/
|
*************************************************************************************/
|
||||||
|
@ -11,7 +11,7 @@
|
||||||
/*
|
/*
|
||||||
* Doubly array that will store the game board.
|
* Doubly array that will store the game board.
|
||||||
*/
|
*/
|
||||||
char** gameBoard;
|
char **gameBoard;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Height of the game board.
|
* Height of the game board.
|
||||||
|
@ -91,7 +91,7 @@ int placeChip(int col, int p);
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int saveGame(char* filename);
|
int saveGame(char *filename);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Loads a previous game. Reloads a previous game's
|
* Loads a previous game. Reloads a previous game's
|
||||||
|
@ -102,7 +102,7 @@ int saveGame(char* filename);
|
||||||
* @return 0 if the operation failed, and 1 if the operation
|
* @return 0 if the operation failed, and 1 if the operation
|
||||||
* succeeded.
|
* succeeded.
|
||||||
*/
|
*/
|
||||||
int loadGame(char* filename);
|
int loadGame(char *filename);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Checks if the current state of the board is a victory
|
* Checks if the current state of the board is a victory
|
||||||
|
@ -126,4 +126,10 @@ int checkWin(char p);
|
||||||
*/
|
*/
|
||||||
int modifyPostition(int row, int col, char c);
|
int modifyPostition(int row, int col, char c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees up any reserved memory from
|
||||||
|
* the game session.
|
||||||
|
*/
|
||||||
|
int clean();
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue