-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_four.c
169 lines (148 loc) · 6.29 KB
/
connect_four.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#include <conio.h> // this library doesnt exist on linux
char column[43];
char playerTokenPlacement[43]; // save the placement of the player's token
void board();
void checkwin();
const int numberOfslotsInOneRow = 7;
const int yes = 1;
const int no = 0;
const int firstSlot = 1;
const int lastSlot = 42;
int shouldGameContinue = yes;
int player = 1;
int isDraw = no;
int main() {
const int firstSlotRow = firstSlot;
for(int i = 0; i < 43; i++) {
column[i] = 'O';
playerTokenPlacement[i] = 'O';
}
board();
while(shouldGameContinue == yes) {
int choice;
int infiniteLoop = yes;
int playersTurn = ((player % 2) == 0) ? 2 : 1;
while(infiniteLoop == yes) {
int shouldSkip = no;
printf("\nplayer %d enter a number: ", playersTurn);
choice = getche() - 48;
/* ---------- verifying player's answer ---------- */
for(int slotRow = firstSlotRow; slotRow <= numberOfslotsInOneRow + 1; slotRow++) {
if(slotRow == numberOfslotsInOneRow + 1) {
board();
printf("\nType a number between 1 and 7\n");
shouldSkip = yes;
break;
}
else {
if(choice == slotRow) {
if(column[slotRow + (7 * 0)] != 'O') { // if the first slot is full it means that the whole column is full
board();
printf("\nthose slots are full, pick another slot\n");
shouldSkip = yes;
break;
}
else break;
}
}
}
/* ---------- logic of the game ---------- */
if(shouldSkip == no) {
for(int i = firstSlotRow; i <= numberOfslotsInOneRow; i++) {
if(choice == i) {
int slot = i + 35; // bottom slot
for(int j = 1; j <= 6; j++) {
if(column[slot] == 'O') {
column[slot] = '0';
playerTokenPlacement[slot] = ((player % 2) == 0) ? '2' : '1';
infiniteLoop = no;
break;
}
else {
slot -= numberOfslotsInOneRow; // going from the bottom slot to the top in the same column
}
}
}
}
board();
checkwin();
player++;
}
}
}
if(isDraw == yes) {
printf("\nIt's a draw !\n");
}
else {
printf("\nPlayer %d is the winner !\n", ((player % 2) != 0) ? 2 : 1);
}
printf("\nPress Any Key to Exit...\n");
getch();
}
void board() {
system("cls"); // clear terminal
printf("\n\n Connect 4\n\n");
printf("\033[0mplayer 1: \033[1;31m0 \033[0mplayer 2: \033[0;34m0\n\n");
printf(" \033[0;33m1 2 3 4 5 6 7\n");
printf(" ");
for(int slot = firstSlot; slot <= lastSlot; slot++) {
printf("\033[0m|");
if(playerTokenPlacement[slot] == '1') printf(" \033[1;31m%c ",column[slot]);
else if(playerTokenPlacement[slot] == '2') printf(" \033[0;34m%c ",column[slot]);
else printf(" \033[0m%c ",column[slot]);
if((slot % numberOfslotsInOneRow) == 0) {
printf("\033[0m|\n");
printf(" ");
}
}
}
void checkwin() {
int checkAnswer = ((player % 2) == 0) ? '2' : '1';
int token;
for(token = firstSlot; token <= 21; token++) { // vertical
if((playerTokenPlacement[token + (7 * 0)] == checkAnswer && playerTokenPlacement[token + (7 * 1)] == checkAnswer &&
playerTokenPlacement[token + (7 * 2)] == checkAnswer && playerTokenPlacement[token + (7 * 3)] == checkAnswer)) {
shouldGameContinue = no;
return;
}
}
for(token = firstSlot; token <= 39; token++) { // horizontal
if((token >= 1 && token <= 4) || (token >= 8 && token <= 11) || (token >= 15 && token <= 18) ||
(token >= 22 && token <= 32) || (token >= 36 && token <= 39)) {
if((playerTokenPlacement[token + 0] == checkAnswer && playerTokenPlacement[token + 1] == checkAnswer &&
playerTokenPlacement[token + 2] == checkAnswer && playerTokenPlacement[token + 3] == checkAnswer)) {
shouldGameContinue = no;
return;
}
} // skip these numbers
}
for(token = firstSlot; token <= 18; token++) { // diagonal left
if((token >= 1 && token <= 4) || (token >= 8 && token <= 11) || (token >= 15 && token <= 18)) {
if((playerTokenPlacement[token + (8 * 0)] == checkAnswer && playerTokenPlacement[token + (8 * 1)] == checkAnswer &&
playerTokenPlacement[token + (8 * 2)] == checkAnswer && playerTokenPlacement[token + (8 * 3)] == checkAnswer)) {
shouldGameContinue = no;
return;
}
} // skip these numbers
}
for(token = 4; token <= 21; token++) { // diagonal right
if((token >= 4 && token <= 7) || (token >= 11 && token <= 14) || (token >= 18 && token <= 21)) {
if((playerTokenPlacement[token + (6 * 0)] == checkAnswer && playerTokenPlacement[token + (6 * 1)] == checkAnswer &&
playerTokenPlacement[token + (6 * 2)] == checkAnswer && playerTokenPlacement[token + (6 * 3)] == checkAnswer)) {
shouldGameContinue = no;
return;
}
} // skip these numbers
}
int topSlots = 0;
for(token = firstSlot; token <= numberOfslotsInOneRow; token++) {
if(column[token] != 'O') topSlots++;
if(topSlots == numberOfslotsInOneRow) {
isDraw = yes;
shouldGameContinue = no;
return;
}
}
}