-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect4.c
142 lines (125 loc) · 4.43 KB
/
connect4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdbool.h>
#define ROWS 6
#define COLUMNS 7
typedef struct {
char gameBoard[ROWS][COLUMNS];
int currentPlayer;
int totalMoves;
} Connect4Game;
void initialize(Connect4Game *game) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
game->gameBoard[i][j] = ' ';
}
}
game->currentPlayer = 1;
game->totalMoves = 0;
}
void printGameBoard(Connect4Game *game) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
printf("| %c ", game->gameBoard[i][j]);
}
printf("|\n");
}
for (int j = 0; j < COLUMNS; j++) {
printf(" %d ", j + 1);
}
printf("\n");
}
bool isColumnFull(Connect4Game *game, int col) {
return game->gameBoard[0][col] != ' ';
}
bool checkWin(Connect4Game *game, char playerSymbol) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (game->gameBoard[i][j] == playerSymbol) {
// Check horizontally
if (j + 3 < COLUMNS &&
game->gameBoard[i][j + 1] == playerSymbol &&
game->gameBoard[i][j + 2] == playerSymbol &&
game->gameBoard[i][j + 3] == playerSymbol) {
return true;
}
// Check vertically
if (i + 3 < ROWS &&
game->gameBoard[i + 1][j] == playerSymbol &&
game->gameBoard[i + 2][j] == playerSymbol &&
game->gameBoard[i + 3][j] == playerSymbol) {
return true;
}
// Check diagonally (up-right)
if (i + 3 < ROWS && j + 3 < COLUMNS &&
game->gameBoard[i + 1][j + 1] == playerSymbol &&
game->gameBoard[i + 2][j + 2] == playerSymbol &&
game->gameBoard[i + 3][j + 3] == playerSymbol) {
return true;
}
// Check diagonally (up-left)
if (i + 3 < ROWS && j - 3 >= 0 &&
game->gameBoard[i + 1][j - 1] == playerSymbol &&
game->gameBoard[i + 2][j - 2] == playerSymbol &&
game->gameBoard[i + 3][j - 3] == playerSymbol) {
return true;
}
}
}
}
return false;
}
void dropPiece(Connect4Game *game, int col) {
int row = ROWS - 1;
while (row >= 0 && game->gameBoard[row][col] != ' ') {
row--;
}
if (row >= 0) {
game->gameBoard[row][col] = (game->currentPlayer == 1) ? 'X' : 'O';
game->currentPlayer = 3 - game->currentPlayer;
game->totalMoves++;
}
}
void displayGameExplanation() {
printf("Welcome to Connect 4!\n");
printf("Players choose X or O. Drop your discs into the grid, starting in the middle or at the edge, ");
printf("to stack your discs upwards, horizontally, or diagonally. Use strategy to block opponents while ");
printf("aiming to be the first player to get 4 in a row to win.\n");
}
int connect4() {
Connect4Game game;
initialize(&game);
int selectedColumn;
bool validMove = false;
displayGameExplanation();
while (game.totalMoves < ROWS * COLUMNS) {
printGameBoard(&game);
do {
printf("Player %d, enter column (1-%d): ", game.currentPlayer, COLUMNS);
if (scanf("%d", &selectedColumn) != 1 || selectedColumn < 1 || selectedColumn > COLUMNS) {
while (getchar() != '\n');
} else {
selectedColumn--;
if (!isColumnFull(&game, selectedColumn)) {
validMove = true;
} else {
printf("Column %d is full. Try again.\n", selectedColumn + 1);
}
}
} while (!validMove);
dropPiece(&game, selectedColumn);
if (checkWin(&game, 'X')) {
printGameBoard(&game);
printf("Player 1 (X) wins!\n");
break;
} else if (checkWin(&game, 'O')) {
printGameBoard(&game);
printf("Player 2 (O) wins!\n");
break;
}
}
if (game.totalMoves == ROWS * COLUMNS) {
printGameBoard(&game);
printf("It's a draw!\n");
}
return 0;
}