Skip to content

Commit 73c3924

Browse files
committed
Refractoring code base
1 parent 60ab8ef commit 73c3924

File tree

9 files changed

+178
-134
lines changed

9 files changed

+178
-134
lines changed

Main.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/*
2-
* A (not to basic) 'Connect 4 Game' game implementation.
2+
* A (not so basic) 'Connect 4 Game' implementation.
33
*
44
* Author: Tommaso Bocchietti
55
* Email: tommaso.bocchietti@gmail.com
66
* Date: 2023-03-30
77
*
8-
* This program let two players play the game of 'Connect 4 Game' on the same computer.
8+
* This program let two players play the 'Connect 4 Game' on the same computer.
99
* 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.
1010
* 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.
1111
* No graphics are provided, this is just a console application.
12+
* It is possible to play personalized versions of the game by passing parameters to the program.
1213
*
1314
* License: MIT
1415
*
@@ -28,46 +29,46 @@
2829
int main(int argc, char *argv[])
2930
{
3031

31-
struct game game = setupGame(argc, argv);
32+
struct Game game = setupGame(argc, argv);
3233

3334
// Game loop
3435
while (true)
3536
{
3637

37-
game.current_move.row = -1;
38-
game.current_move.col = -1;
39-
draw_board(game.board);
38+
game.currentMove.row = -1;
39+
game.currentMove.column = -1;
40+
draw_board(game.gameBoard);
4041

41-
while (game.current_move.row == -1)
42+
while (game.currentMove.row == -1)
4243
{
43-
printf("\n%s (", game.player_name[game.player_index]);
44-
PRINT_COLOURED(game.board.colors[game.player_index], "%c", game.board.symbols[game.player_index]);
44+
printf("\n%s (", game.playerName[game.currentPlayerIndex]);
45+
PRINT_COLOURED(game.gameBoard.playerColors[game.currentPlayerIndex], "%c", game.gameBoard.playerSymbols[game.currentPlayerIndex]);
4546
printf(") selects any column from 1 to 7: ");
46-
while (scanf("%d", &game.current_move.col) != 1)
47+
while (scanf("%d", &game.currentMove.column) != 1)
4748
{
4849
printf("You can enter only integer values: ");
4950
scanf("%*s");
5051
}
51-
game.current_move.col--; // Because of the array index from 0 to 6
52+
game.currentMove.column--; // Because of the array index from 0 to 6
5253

53-
game.current_move.row = insert_into_board(&game.board, game.player_index, game.current_move.col);
54+
game.currentMove.row = insert_into_board(&game.gameBoard, game.currentPlayerIndex, game.currentMove.column);
5455
};
5556

56-
if (check_winner(game.board, game.current_move))
57+
if (check_winner(game.gameBoard, game.currentMove))
5758
{
58-
draw_board(game.board);
59-
printf("\nCongratulations %s, you won! :)", game.player_name[game.player_index]);
59+
draw_board(game.gameBoard);
60+
printf("\nCongratulations %s, you won! :)", game.playerName[game.currentPlayerIndex]);
6061
break;
6162
}
6263

63-
if (check_tie(game.board))
64+
if (check_tie(game.gameBoard))
6465
{
65-
draw_board(game.board);
66+
draw_board(game.gameBoard);
6667
printf("\nOhoh, looks like the game ended in draw...");
6768
break;
6869
}
6970

70-
game.player_index = (game.player_index + 1) % game.n_player;
71+
game.currentPlayerIndex = (game.currentPlayerIndex + 1) % game.numPlayers;
7172
};
7273

7374
printf("\n\n");

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Connect_4_Game
22

3-
This is a **simple implementation of the Connect 4 Game game, developed during high school using basic concepts of C**.
3+
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.
44

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

1818
## How to compile
1919

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

2222
To compile the game, simply run the following command:
2323

2424
```shell
25-
gcc utils.c Main.c -o Connect_4_Game.exe -std=c99
25+
gcc Main.c board.c setup.c utils.c -o Connect_4_Game.exe
2626
```
2727

2828
## How to run
@@ -33,6 +33,21 @@ To run the game, simply run the following command:
3333
./Connect_4_Game
3434
```
3535

36+
### Options / Personalizations
37+
38+
The game has some options that can be used to personalize the game.
39+
40+
- `-h` or `--help`: To show the help message and exit.
41+
- `-b` or `--board`: To set a different board size in the format 'rows x columns'.
42+
- `-p` or `--player`: To set the number of players.
43+
- `-w` or `--winning`: To set the number of symbols to win.
44+
45+
Here is an example of how to use the options set to the deafult values:
46+
47+
```shell
48+
./Connect_4_Game -b 6x7 -p 2 -w 4
49+
```
50+
3651
## Improvements / pull requests
3752

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

47-
4862
Have a nice coding day,
4963

5064
Tommaso :panda_face:

board.c

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
2-
* A simple 'Connect 4 Game' game implementation.
3-
*
42
* Author: Tommaso Bocchietti
53
* Email: tommaso.bocchietti@gmail.com
6-
* Date: 2023-03-30
4+
* Date: 2023-04-01
75
*
8-
* This program let two players play the game of 'Connect 4 Game' on the same computer.
9-
* 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.
10-
* 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.
11-
* No graphics are provided, this is just a console application.
6+
* This file is part of the 'Connect 4 Game' project.
7+
* It contains the main function of the game, such as:
8+
* - Draw the board
9+
* - Insert a symbol into the board
10+
* - Check for wins or ties
1211
*
1312
* License: MIT
1413
*
@@ -24,40 +23,40 @@
2423
#include <stdbool.h>
2524
#include <string.h>
2625

27-
void draw_board(struct board board)
26+
void draw_board(struct Board board)
2827
{
2928
system("@cls || clear");
3029

31-
for (int r = board.n_row - 1; r >= 0; r--)
30+
for (int r = board.numRows - 1; r >= 0; r--)
3231
{
33-
for (int c = 0; c < board.n_col; c++)
32+
for (int c = 0; c < board.numCols; c++)
3433
{
35-
if (board.board[r][c] >= 0)
36-
PRINT_COLOURED(board.colors[board.board[r][c]], " %c ", board.symbols[board.board[r][c]]);
34+
if (board.cells[r][c] >= 0)
35+
PRINT_COLOURED(board.playerColors[board.cells[r][c]], " %c ", board.playerSymbols[board.cells[r][c]]);
3736
else
3837
printf(" . ");
3938
}
4039
printf("\n");
4140
}
4241

43-
for (int i = 0; i < board.n_col; i++)
42+
for (int i = 0; i < board.numCols; i++)
4443
printf("---");
4544
printf("\n");
4645

47-
for (int i = 0; i < board.n_col; i++)
46+
for (int i = 0; i < board.numCols; i++)
4847
i + 1 > 9 ? printf(" %d", i + 1) : printf(" %d ", i + 1);
4948
printf("\n");
5049
}
5150

52-
bool is_insert_valid(struct board board, int selected_column)
51+
bool is_insert_valid(struct Board board, int selected_column)
5352
{
54-
if ((selected_column < 0) || (selected_column > board.n_col - 1))
53+
if ((selected_column < 0) || (selected_column > board.numCols - 1))
5554
{
5655
printf("\nColumn's index out of range");
5756
return false;
5857
}
5958

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

69-
int insert_into_board(struct board *board, int symbol, int selected_column)
68+
int insert_into_board(struct Board *board, int symbol, int selected_column)
7069
{
7170

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

7574
int r = 0;
76-
while (board->board[r][selected_column] != EMPTY_CELL)
75+
while (board->cells[r][selected_column] != EMPTY_CELL)
7776
r++;
78-
board->board[r][selected_column] = symbol;
77+
board->cells[r][selected_column] = symbol;
7978

8079
return r;
8180
}
8281

83-
bool check_winner(struct board board, struct current_move current_move)
82+
bool check_winner(struct Board board, struct BoardCoordinates current_move)
8483
{
8584

86-
int symbol = board.board[current_move.row][current_move.col];
85+
int symbol = board.cells[current_move.row][current_move.column];
8786

8887
for (int i = 0; i <= 3; i++)
8988
{
@@ -92,27 +91,27 @@ bool check_winner(struct board board, struct current_move current_move)
9291
for (int dir = -1; dir <= 1; dir += 2)
9392
{
9493
int r = current_move.row;
95-
int c = current_move.col;
94+
int c = current_move.column;
9695

97-
while ((r >= 0) && (r < board.n_row) && (c >= 0) && (c < board.n_col) && (board.board[r][c] == symbol))
96+
while ((r >= 0) && (r < board.numRows) && (c >= 0) && (c < board.numCols) && (board.cells[r][c] == symbol))
9897
{
9998
count++;
10099
r += dir * (i / 3 - 1);
101100
c += dir * (i % 3 - 1);
102101
}
103102
}
104103

105-
if (--count >= board.n_win_symbol) // Because the central symbol is counted twice
104+
if (--count >= board.numWinSymbols) // Because the central symbol is counted twice
106105
return true;
107106
}
108107

109108
return false;
110109
}
111110

112-
bool check_tie(struct board board)
111+
bool check_tie(struct Board board)
113112
{
114-
for (int c = 0; c < board.n_col; c++)
115-
if (board.board[board.n_row][c] == EMPTY_CELL)
113+
for (int c = 0; c < board.numCols; c++)
114+
if (board.cells[board.numRows - 1][c] == EMPTY_CELL)
116115
return false;
117116

118117
return true;

board.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/*
2-
* A simple 'Connect 4 Game' game implementation.
3-
*
42
* Author: Tommaso Bocchietti
53
* Email: tommaso.bocchietti@gmail.com
6-
* Date: 2023-03-29
4+
* Date: 2023-04-01
75
*
8-
* Utils functions used into the main program.
6+
* This file is part of the 'Connect 4 Game' project.
97
*
108
* License: MIT
119
*
@@ -17,14 +15,14 @@
1715
#ifndef BOARD_H
1816
#define BOARD_H
1917

20-
void draw_board(struct board board);
18+
void draw_board(struct Board board);
2119

22-
bool is_insert_valid(struct board board, int selected_column);
20+
bool is_insert_valid(struct Board board, int selected_column);
2321

24-
int insert_into_board(struct board *board, int symbol, int selected_column);
22+
int insert_into_board(struct Board *board, int symbol, int selected_column);
2523

26-
bool check_winner(struct board board, struct current_move current_move);
24+
bool check_winner(struct Board board, struct BoardCoordinates current_move);
2725

28-
bool check_tie(struct board board);
26+
bool check_tie(struct Board board);
2927

3028
#endif

constant.h

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,49 @@
11
/*
2-
* This file declares the constants used in the program.
3-
* The constants are declared as extern so that they can be used in other files.
4-
* The constants are defined in the file constant.c
2+
* Author: Tommaso Bocchietti
3+
* Email: tommaso.bocchietti@gmail.com
4+
* Date: 2023-04-01
5+
*
6+
* This file is part of the 'Connect 4 Game' project.
7+
* It contains the constants used in the game.
8+
*
9+
* License: MIT
10+
*
511
*/
612

7-
#ifndef CONSTANT_H
8-
#define CONSTANT_H
13+
#ifndef CONSTANTS_H
14+
#define CONSTANTS_H
915

10-
#define MAX_N_PLAYER 10
11-
#define MAX_N_ROW 15
12-
#define MAX_N_COL 15
16+
#define MAX_PLAYERS 10
17+
#define MAX_ROWS 15
18+
#define MAX_COLUMNS 15
19+
#define MAX_WIN_SYMBOLS 10
1320
#define EMPTY_CELL -1
1421

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

17-
struct board
24+
struct Board
1825
{
19-
int n_row;
20-
int n_col;
21-
int n_win_symbol;
22-
int board[MAX_N_ROW][MAX_N_COL];
23-
int symbols[MAX_N_PLAYER];
24-
int colors[MAX_N_PLAYER];
26+
int numRows;
27+
int numCols;
28+
int numWinSymbols;
29+
int cells[MAX_ROWS][MAX_COLUMNS];
30+
int playerSymbols[MAX_PLAYERS];
31+
int playerColors[MAX_PLAYERS];
2532
};
2633

27-
struct current_move
34+
struct BoardCoordinates
2835
{
2936
int row;
30-
int col;
37+
int column;
3138
};
3239

33-
struct game
40+
struct Game
3441
{
35-
int n_player;
36-
int player_index;
37-
struct board board;
38-
char player_name[MAX_N_PLAYER][20];
39-
struct current_move current_move;
42+
int numPlayers;
43+
int currentPlayerIndex;
44+
struct Board gameBoard;
45+
char playerName[MAX_PLAYERS][20];
46+
struct BoardCoordinates currentMove;
4047
};
4148

4249
#endif

0 commit comments

Comments
 (0)