-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
178 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
|
@@ -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"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
|
@@ -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; | ||
|
@@ -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++) | ||
{ | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.