-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path035_connect_four_with_2d_array.c
188 lines (157 loc) · 6.93 KB
/
035_connect_four_with_2d_array.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Connect Four : 6x7 grid with 2D array
#include <stdbool.h>
#include <stdio.h>
char globalHorizontalArray[8];
char globalVerticalArray[7];
char globalDiagonalArrayLeft[((8 > 7) ? 7 : 8)];
char globalDiagonalArrayRight[((8 > 7) ? 7 : 8)];
void extractHorizontalValues(char arr[6][7], int row) {
for (int columnNum = 0; columnNum < 7; columnNum++) {
globalHorizontalArray[columnNum] = arr[row][columnNum];
}
globalHorizontalArray[7] = '\0';
}
void extractVerticalValues(char arr[6][7], int column) {
for (int rowNum = 0; rowNum < 6; rowNum++) {
globalVerticalArray[rowNum] = arr[rowNum][column];
}
globalVerticalArray[6] = '\0';
}
void extractDiagonalValuesFromLeftMargin(char arr[6][7], int rowNumber, int columnChoice) {
while ((rowNumber > 0) && (columnChoice > 0)) {
rowNumber--;
columnChoice--;
}
// at this point, either row or column will be at the boundary (if not both)
int HVIndex = 0;
while ((rowNumber < 6) && (columnChoice < 7)) {
globalDiagonalArrayLeft[HVIndex] = arr[rowNumber][columnChoice];
rowNumber++;
columnChoice++;
HVIndex++;
}
// at this point, the globalDiagonalArrayLeft might not be fully filled up
globalDiagonalArrayLeft[HVIndex] = '\0';
}
void extractDiagonalValuesFromRightMargin(char arr[6][7], int rowNumber, int columnChoice) {
while ((rowNumber > 0) && (columnChoice < 7)) {
rowNumber--;
columnChoice++;
}
// at this point, either row or column will be at the boundary (if not both)
int HVIndex = 0;
while ((rowNumber < 6) && (columnChoice >= 0)) {
globalDiagonalArrayRight[HVIndex] = arr[rowNumber][columnChoice];
rowNumber++;
columnChoice--;
HVIndex++;
}
// at this point, the globalDiagonalArrayRight might not be fully filled up
globalDiagonalArrayRight[HVIndex] = '\0';
}
bool checkIf4PresentInAnyOrder(char arrayOfValues[], char cellValue) {
int indexOfArrayOfValues = 0;
int countIfMin4 = 0;
while (arrayOfValues[indexOfArrayOfValues] != '\0') {
if (arrayOfValues[indexOfArrayOfValues] == cellValue) {
countIfMin4++;
}
indexOfArrayOfValues++;
}
return (countIfMin4 >= 4);
}
bool checkIfWonGivenSingleArray(char arrayOfValues[], int length) {
for (int count = 0; (count + 4) <= length; count++) {
if ((arrayOfValues[count] != ' ') && (arrayOfValues[count] == arrayOfValues[count + 1]) && (arrayOfValues[count] == arrayOfValues[count + 2]) && (arrayOfValues[count] == arrayOfValues[count + 3])) {
return true;
}
}
return false;
}
bool checkIfPlayerHasWon(char arrayOfRows[6][7], int rowNumber, int columnChoice, char cellValue) {
extractHorizontalValues(arrayOfRows, rowNumber);
extractVerticalValues(arrayOfRows, columnChoice);
extractDiagonalValuesFromLeftMargin(arrayOfRows, rowNumber, columnChoice);
extractDiagonalValuesFromRightMargin(arrayOfRows, rowNumber, columnChoice);
bool hRes = checkIf4PresentInAnyOrder(globalHorizontalArray, cellValue) && checkIfWonGivenSingleArray(globalHorizontalArray, 8);
bool vRes = checkIf4PresentInAnyOrder(globalVerticalArray, cellValue) && checkIfWonGivenSingleArray(globalVerticalArray, 7);
bool dResL = checkIf4PresentInAnyOrder(globalDiagonalArrayLeft, cellValue) && checkIfWonGivenSingleArray(globalDiagonalArrayLeft, ((8 > 7) ? 7 : 8));
bool dResR = checkIf4PresentInAnyOrder(globalDiagonalArrayRight, cellValue) && checkIfWonGivenSingleArray(globalDiagonalArrayRight, ((8 > 7) ? 7 : 8));
return (hRes || vRes || dResL || dResR);
}
char playersToken(int playerNumber) {
return (playerNumber == 1) ? 'x' : 'o';
}
void drawGrid(char rows[], char columns[], char arrayOfRows[6][7]) {
printf(" Connect Four: A Game ");
printf("\n Player 1: %c ", playersToken(1));
printf("\n Player 2: %c \n", playersToken(2));
for (int rowCount = 0; rowCount < 6; rowCount++) {
for (int columnCount = 0; columnCount < 7;) {
for (; arrayOfRows[rowCount][columnCount] != '\0';) {
printf("\n %c | %c | %c | %c | %c | %c | %c | %c |", rows[rowCount], arrayOfRows[rowCount][columnCount], arrayOfRows[rowCount][columnCount + 1], arrayOfRows[rowCount][columnCount + 2], arrayOfRows[rowCount][columnCount + 3], arrayOfRows[rowCount][columnCount + 4], arrayOfRows[rowCount][columnCount + 5], arrayOfRows[rowCount][columnCount + 6]);
columnCount += 7;
break;
}
}
printf("\n |-----|-----|-----|-----|-----|-----|-----|");
}
printf("\n\n ");
for (int columnCount = 0; columnCount < 7; columnCount++) {
printf(" %c ", columns[columnCount]);
}
printf("\n\n");
}
bool checkIfCellIsFilled(char cellValue) {
return (cellValue != ' ');
}
bool checkIfColumnIsFilled(char arrayOfRows[6][7], int columnChoice) {
return checkIfCellIsFilled(arrayOfRows[0][columnChoice - 1]);
}
bool markChoiceOnGrid(char rows[], char columns[], char arrayOfRows[6][7], int columnChoice, int playerNumber) {
int rowNumber = 5;
for (; rowNumber >= 0; rowNumber--) {
if (!checkIfCellIsFilled(arrayOfRows[rowNumber][columnChoice - 1])) {
break;
}
}
bool res = checkIfColumnIsFilled(arrayOfRows, columnChoice);
char cellValue = playersToken(playerNumber);
if (res == false) {
arrayOfRows[rowNumber][columnChoice - 1] = cellValue;
}
drawGrid(rows, columns, arrayOfRows);
return checkIfPlayerHasWon(arrayOfRows, rowNumber, (columnChoice - 1), cellValue);
}
int promptForColumnChoice(char arrayOfRows[6][7], int playerNumber) {
int columnChoice;
printf("\n~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ \n \n");
printf("Player %i (%c): Enter your choice of column number: ", playerNumber, playersToken(playerNumber));
scanf("%i", &columnChoice);
printf("\n");
while ((columnChoice < 1) || (7 < columnChoice)) {
printf("Invalid choice. Select another, Player %c: ", playerNumber);
scanf("%i", &columnChoice);
}
bool resultOfCheckIfColumnIsFilled = checkIfColumnIsFilled(arrayOfRows, columnChoice);
while (resultOfCheckIfColumnIsFilled) {
printf("Invalid choice. Select another, Player %c: ", playerNumber);
scanf("%i", &columnChoice);
resultOfCheckIfColumnIsFilled = checkIfColumnIsFilled(arrayOfRows, columnChoice);
}
return columnChoice;
}
int main() {
char rows[] = {'a', 'b', 'c', 'd', 'e', 'f', '\0'};
char columns[] = {'1', '2', '3', '4', '5', '6', '7', '\0'};
char arrayOfRows[6][7] = {{' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' '}, {' ', ' ', ' ', ' ', ' ', ' ', ' '}};
drawGrid(rows, columns, arrayOfRows);
for (int countMoves = 1; countMoves <= 42; countMoves++) {
int playerNumber = (countMoves % 2 != 0) ? 1 : 2;
int columnChoice = promptForColumnChoice(arrayOfRows, playerNumber);
if (markChoiceOnGrid(rows, columns, arrayOfRows, columnChoice, playerNumber)) {
printf("Congrats, Player %i! You have won!\n", playerNumber);
break;
}
}
}