Skip to content

Commit

Permalink
Refractoring code base
Browse files Browse the repository at this point in the history
  • Loading branch information
Bocchio01 committed Apr 1, 2023
1 parent 60ab8ef commit 73c3924
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 134 deletions.
37 changes: 19 additions & 18 deletions Main.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*
* A (not to basic) 'Connect 4 Game' game implementation.
* A (not so basic) 'Connect 4 Game' implementation.
*
* Author: Tommaso Bocchietti
* Email: [email protected]
* Date: 2023-03-30
*
* This program let two players play the game of 'Connect 4 Game' on the same computer.
* This program let two players play the 'Connect 4 Game' on the same computer.
* The game is played on a 6x7 board, and the first player to get four of his/her symbols in a row (horizontally, vertically or diagonally) wins.
* The players take turns inserting their symbols into the board, and the game ends when one of the players wins, or when the board is full.
* No graphics are provided, this is just a console application.
* It is possible to play personalized versions of the game by passing parameters to the program.
*
* License: MIT
*
Expand All @@ -28,46 +29,46 @@
int main(int argc, char *argv[])
{

struct game game = setupGame(argc, argv);
struct Game game = setupGame(argc, argv);

// Game loop
while (true)
{

game.current_move.row = -1;
game.current_move.col = -1;
draw_board(game.board);
game.currentMove.row = -1;
game.currentMove.column = -1;
draw_board(game.gameBoard);

while (game.current_move.row == -1)
while (game.currentMove.row == -1)
{
printf("\n%s (", game.player_name[game.player_index]);
PRINT_COLOURED(game.board.colors[game.player_index], "%c", game.board.symbols[game.player_index]);
printf("\n%s (", game.playerName[game.currentPlayerIndex]);
PRINT_COLOURED(game.gameBoard.playerColors[game.currentPlayerIndex], "%c", game.gameBoard.playerSymbols[game.currentPlayerIndex]);
printf(") selects any column from 1 to 7: ");
while (scanf("%d", &game.current_move.col) != 1)
while (scanf("%d", &game.currentMove.column) != 1)
{
printf("You can enter only integer values: ");
scanf("%*s");
}
game.current_move.col--; // Because of the array index from 0 to 6
game.currentMove.column--; // Because of the array index from 0 to 6

game.current_move.row = insert_into_board(&game.board, game.player_index, game.current_move.col);
game.currentMove.row = insert_into_board(&game.gameBoard, game.currentPlayerIndex, game.currentMove.column);
};

if (check_winner(game.board, game.current_move))
if (check_winner(game.gameBoard, game.currentMove))
{
draw_board(game.board);
printf("\nCongratulations %s, you won! :)", game.player_name[game.player_index]);
draw_board(game.gameBoard);
printf("\nCongratulations %s, you won! :)", game.playerName[game.currentPlayerIndex]);
break;
}

if (check_tie(game.board))
if (check_tie(game.gameBoard))
{
draw_board(game.board);
draw_board(game.gameBoard);
printf("\nOhoh, looks like the game ended in draw...");
break;
}

game.player_index = (game.player_index + 1) % game.n_player;
game.currentPlayerIndex = (game.currentPlayerIndex + 1) % game.numPlayers;
};

printf("\n\n");
Expand Down
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Connect_4_Game

This is a **simple implementation of the Connect 4 Game game, developed during high school using basic concepts of C**.
This is **a (not so basic) 'Connect 4 Game' implementation**, developed during high school using basic concepts of C and then improved during my free time.

![Initial board screenshot](Img/Initial_Screen.png)
![Winning board screenshot](Img/Winning_Screen.png)
Expand All @@ -17,12 +17,12 @@ The game will end when one of the players connects four pieces in a row, column

## How to compile

To compile the game, you need to have the GCC compiler installed on your machine.
You need to have the GCC compiler installed on your machine and a copy of the <getopt.h> header file.

To compile the game, simply run the following command:

```shell
gcc utils.c Main.c -o Connect_4_Game.exe -std=c99
gcc Main.c board.c setup.c utils.c -o Connect_4_Game.exe
```

## How to run
Expand All @@ -33,6 +33,21 @@ To run the game, simply run the following command:
./Connect_4_Game
```

### Options / Personalizations

The game has some options that can be used to personalize the game.

- `-h` or `--help`: To show the help message and exit.
- `-b` or `--board`: To set a different board size in the format 'rows x columns'.
- `-p` or `--player`: To set the number of players.
- `-w` or `--winning`: To set the number of symbols to win.

Here is an example of how to use the options set to the deafult values:

```shell
./Connect_4_Game -b 6x7 -p 2 -w 4
```

## Improvements / pull requests

Some things that could be improved are:
Expand All @@ -44,7 +59,6 @@ Some things that could be improved are:
Since this repo is still at a very basic level of implementation and complexity, **I would love to see improvements!**
Feel free to fork this repository, make pull requests or do whatever you want with it.


Have a nice coding day,

Tommaso :panda_face:
Expand Down
55 changes: 27 additions & 28 deletions board.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/*
* A simple 'Connect 4 Game' game implementation.
*
* Author: Tommaso Bocchietti
* Email: [email protected]
* Date: 2023-03-30
* Date: 2023-04-01
*
* This program let two players play the game of 'Connect 4 Game' on the same computer.
* The game is played on a 6x7 board, and the first player to get four of his/her symbols in a row (horizontally, vertically or diagonally) wins.
* The players take turns inserting their symbols into the board, and the game ends when one of the players wins, or when the board is full.
* No graphics are provided, this is just a console application.
* This file is part of the 'Connect 4 Game' project.
* It contains the main function of the game, such as:
* - Draw the board
* - Insert a symbol into the board
* - Check for wins or ties
*
* License: MIT
*
Expand All @@ -24,40 +23,40 @@
#include <stdbool.h>
#include <string.h>

void draw_board(struct board board)
void draw_board(struct Board board)
{
system("@cls || clear");

for (int r = board.n_row - 1; r >= 0; r--)
for (int r = board.numRows - 1; r >= 0; r--)
{
for (int c = 0; c < board.n_col; c++)
for (int c = 0; c < board.numCols; c++)
{
if (board.board[r][c] >= 0)
PRINT_COLOURED(board.colors[board.board[r][c]], " %c ", board.symbols[board.board[r][c]]);
if (board.cells[r][c] >= 0)
PRINT_COLOURED(board.playerColors[board.cells[r][c]], " %c ", board.playerSymbols[board.cells[r][c]]);
else
printf(" . ");
}
printf("\n");
}

for (int i = 0; i < board.n_col; i++)
for (int i = 0; i < board.numCols; i++)
printf("---");
printf("\n");

for (int i = 0; i < board.n_col; i++)
for (int i = 0; i < board.numCols; i++)
i + 1 > 9 ? printf(" %d", i + 1) : printf(" %d ", i + 1);
printf("\n");
}

bool is_insert_valid(struct board board, int selected_column)
bool is_insert_valid(struct Board board, int selected_column)
{
if ((selected_column < 0) || (selected_column > board.n_col - 1))
if ((selected_column < 0) || (selected_column > board.numCols - 1))
{
printf("\nColumn's index out of range");
return false;
}

if (board.board[board.n_row - 1][selected_column] != EMPTY_CELL)
if (board.cells[board.numRows - 1][selected_column] != EMPTY_CELL)
{
printf("\nThe selected column is full");
return false;
Expand All @@ -66,24 +65,24 @@ bool is_insert_valid(struct board board, int selected_column)
return true;
}

int insert_into_board(struct board *board, int symbol, int selected_column)
int insert_into_board(struct Board *board, int symbol, int selected_column)
{

if (!is_insert_valid(*board, selected_column))
return -1;

int r = 0;
while (board->board[r][selected_column] != EMPTY_CELL)
while (board->cells[r][selected_column] != EMPTY_CELL)
r++;
board->board[r][selected_column] = symbol;
board->cells[r][selected_column] = symbol;

return r;
}

bool check_winner(struct board board, struct current_move current_move)
bool check_winner(struct Board board, struct BoardCoordinates current_move)
{

int symbol = board.board[current_move.row][current_move.col];
int symbol = board.cells[current_move.row][current_move.column];

for (int i = 0; i <= 3; i++)
{
Expand All @@ -92,27 +91,27 @@ bool check_winner(struct board board, struct current_move current_move)
for (int dir = -1; dir <= 1; dir += 2)
{
int r = current_move.row;
int c = current_move.col;
int c = current_move.column;

while ((r >= 0) && (r < board.n_row) && (c >= 0) && (c < board.n_col) && (board.board[r][c] == symbol))
while ((r >= 0) && (r < board.numRows) && (c >= 0) && (c < board.numCols) && (board.cells[r][c] == symbol))
{
count++;
r += dir * (i / 3 - 1);
c += dir * (i % 3 - 1);
}
}

if (--count >= board.n_win_symbol) // Because the central symbol is counted twice
if (--count >= board.numWinSymbols) // Because the central symbol is counted twice
return true;
}

return false;
}

bool check_tie(struct board board)
bool check_tie(struct Board board)
{
for (int c = 0; c < board.n_col; c++)
if (board.board[board.n_row][c] == EMPTY_CELL)
for (int c = 0; c < board.numCols; c++)
if (board.cells[board.numRows - 1][c] == EMPTY_CELL)
return false;

return true;
Expand Down
16 changes: 7 additions & 9 deletions board.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/*
* A simple 'Connect 4 Game' game implementation.
*
* Author: Tommaso Bocchietti
* Email: [email protected]
* Date: 2023-03-29
* Date: 2023-04-01
*
* Utils functions used into the main program.
* This file is part of the 'Connect 4 Game' project.
*
* License: MIT
*
Expand All @@ -17,14 +15,14 @@
#ifndef BOARD_H
#define BOARD_H

void draw_board(struct board board);
void draw_board(struct Board board);

bool is_insert_valid(struct board board, int selected_column);
bool is_insert_valid(struct Board board, int selected_column);

int insert_into_board(struct board *board, int symbol, int selected_column);
int insert_into_board(struct Board *board, int symbol, int selected_column);

bool check_winner(struct board board, struct current_move current_move);
bool check_winner(struct Board board, struct BoardCoordinates current_move);

bool check_tie(struct board board);
bool check_tie(struct Board board);

#endif
53 changes: 30 additions & 23 deletions constant.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
/*
* This file declares the constants used in the program.
* The constants are declared as extern so that they can be used in other files.
* The constants are defined in the file constant.c
* Author: Tommaso Bocchietti
* Email: [email protected]
* Date: 2023-04-01
*
* This file is part of the 'Connect 4 Game' project.
* It contains the constants used in the game.
*
* License: MIT
*
*/

#ifndef CONSTANT_H
#define CONSTANT_H
#ifndef CONSTANTS_H
#define CONSTANTS_H

#define MAX_N_PLAYER 10
#define MAX_N_ROW 15
#define MAX_N_COL 15
#define MAX_PLAYERS 10
#define MAX_ROWS 15
#define MAX_COLUMNS 15
#define MAX_WIN_SYMBOLS 10
#define EMPTY_CELL -1

#define PRINT_COLOURED(c, f, s) printf("\033[%dm" f "\033[0m", c, s)

struct board
struct Board
{
int n_row;
int n_col;
int n_win_symbol;
int board[MAX_N_ROW][MAX_N_COL];
int symbols[MAX_N_PLAYER];
int colors[MAX_N_PLAYER];
int numRows;
int numCols;
int numWinSymbols;
int cells[MAX_ROWS][MAX_COLUMNS];
int playerSymbols[MAX_PLAYERS];
int playerColors[MAX_PLAYERS];
};

struct current_move
struct BoardCoordinates
{
int row;
int col;
int column;
};

struct game
struct Game
{
int n_player;
int player_index;
struct board board;
char player_name[MAX_N_PLAYER][20];
struct current_move current_move;
int numPlayers;
int currentPlayerIndex;
struct Board gameBoard;
char playerName[MAX_PLAYERS][20];
struct BoardCoordinates currentMove;
};

#endif
Loading

0 comments on commit 73c3924

Please sign in to comment.