First of I'm a beginner in programming. I am using C.
And I started of this project to make a connect-4 game which can save the look of the board while playing and load it to continue where it was left of.
Basically I managed to get everything to work perfectly to work except to make a save and load function. I need this save function to be able to be accessed while the game is in progress then ask for a what file name the users wants to give it, then the file to be created with that name and contain the look of the board at the time it was saved.
I studied about creating files and writing reading them .. but i dont understand how is ti possible to do what i need(and even if its possible)
here is the code of the whole game: (even if you dont have the answer any tips for the code would be great)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BOARD_ROWS 6
#define BOARD_COLS 7
#define DIM 15
void printBoard(char *board);
int takeTurn(char *board, int player, const char*);
int checkWin(char *board);
int checkFour(char *board, int, int, int, int);
int horizontalCheck(char *board);
int verticalCheck(char *board);
int diagonalCheck(char *board);
int newGame();
int mainMenu();
void nameInput();
char firstPlayer[DIM], secondPlayer[DIM];
int main(){
mainMenu();
}
int newGame(){
const char *PIECES = "XO"; char board[BOARD_ROWS * BOARD_COLS]; memset(board, ' ', BOARD_ROWS * BOARD_COLS);
int turn, done = 0;
for(turn = 0; turn < BOARD_ROWS * BOARD_COLS && !done; turn++){ printBoard(board);
while(!takeTurn(board, turn % 2, PIECES)){ printBoard(board);
puts("**Column full!**\n"); } done = checkWin(board); }
printBoard(board);
if(turn == BOARD_ROWS * BOARD_COLS && !done){ puts("It's a tie!"); } else { turn--; printf("Player %d (%c) wins!\n", turn % 2 + 1, PIECES[turn % 2]); }
return 0;}
void inputName(){
printf("First player enter your name:\n");
getchar(); scanf("%c", firstPlayer);
printf("Second player enter your name:\n");
getchar(); scanf("%c", secondPlayer);
}
void printBoard(char *board){
int row, col; puts("\n ****Connect Four****\n"); for(row = 0; row < BOARD_ROWS; row++){ for(col = 0; col < BOARD_COLS; col++){ printf("| %c ", board[BOARD_COLS * row + col]); }
puts("|");
puts("-----------------------------");
}
puts(" 1 2 3 4 5 6 7\n"); }
int takeTurn(char *board, int player, const char *PIECES){ int row, col = 0;
printf("Player %c (%c):\nEnter number coordinate: ", player + 1, PIECES[player]); while(1){
if(1 != scanf("%d", &col) || col < 1 || col > 7 ){ while(getchar() != '\n'); puts("Number out of bounds! Try again.");
} else {
break; }
}
col--;
for(row = BOARD_ROWS - 1; row >= 0; row--){
if(board[BOARD_COLS * row + col] == ' '){ board[BOARD_COLS * row + col] = PIECES[player]; return 1;
}
}
return 0;
}
int checkWin(char *board){
return (horizontalCheck(board) || verticalCheck(board) || diagonalCheck(board));
}
int checkFour(char *board, int a, int b, int c, int d){
return (board[a] == board[b] && board[b] == board[c] && board[c] == board[d] && board[a] != ' ');
}
int horizontalCheck(char *board){
int row, col, index;
const int WIDTH = 1;
for(row = 0; row < BOARD_ROWS; row++){
for(col = 0; col < BOARD_COLS - 3; col++){
index = BOARD_COLS * row + col; if(checkFour(board, index, index + WIDTH, index + WIDTH * 2, index + WIDTH * 3)){
return 1;
}
}
}
return 0;
}
int verticalCheck(char *board){
int row, col, index;
const int HEIGHT = 7;
for(row = 0; row < BOARD_ROWS - 3; row++){
for(col = 0; col < BOARD_COLS; col++){
index = BOARD_COLS * row + col;
if(checkFour(board, index, index + HEIGHT, index + HEIGHT * 2, index + HEIGHT * 3)){
return 1;
}
}
}
return 0;
}
int diagonalCheck(char *board){
int row, col, index, count = 0;
const int DIAG_RGT = 6, DIAG_LFT = 8;
for(row = 0; row < BOARD_ROWS - 3; row++){
for(col = 0; col < BOARD_COLS; col++){
index = BOARD_COLS * row + col;
if(((count <= 3) && (checkFour(board, index, index + DIAG_LFT, index + DIAG_LFT * 2, index + DIAG_LFT * 3))) ||
((count >= 3) && (checkFour(board, index, index + DIAG_RGT, index + DIAG_RGT * 2, index + DIAG_RGT * 3)))){
return 1;
}
count++;
}
count = 0;
}
return 0;
}
int mainMenu(){
int choiceMainProblem = 0; int choiceMain;
do{
printf("Connect-4 game\n");
printf("1.Play new game\n");
printf("2.Load already existing game\n");
printf("3.Exit the game\n");
printf("\n");
printf("Choose an option(1-3):\n"); scanf("%d", &choiceMain);
if((choiceMain > 3) || (choiceMain <1)){
choiceMainProblem = 1;
printf("Try again values supported are 1 2 3!\n");
}
}
while(choiceMainProblem);
switch(choiceMain){
case 1: inputName(); break;
case 3: return 0;
}
return 0;
}
What I have tried:
I havent tried anything since i dont know how is it possible