-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.c
189 lines (167 loc) · 5.22 KB
/
connect.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
189
#include <stdio.h>
#define ROWS 6
#define COLUMN 7
char arr[ROWS][COLUMN]; // game board
char bar = '|';
// c1 c2 c3 c4 c5 c6 c7
// 0 1 2 3 4 5 6
// 0 {(0,1),(0,3),(0,5),(0,7),(0,9),(0,11),(0,13)},
// 1 {(1,1),(1,3),(1,5),(1,7),(1,9),(1,11),(1,13)},
// 2 {(2,1),(2,3),(2,5),(2,7),(2,9),(2,11),(2,13)},
// 3 {(3,1),(3,3),(3,5),(3,7),(3,9),(3,11),(3,13)},
// 4 {(4,1),(4,3),(4,5),(4,7),(4,9),(4,11),(4,14)},
// 5 {(5,1),(5,3),(5,5),(5,7),(5,9),(5,11),(5,14)}
char checkWinner(int numTokens) {
char players[] = {'R', 'Y'};
for (int i = 0; i < 2; i++) {
char player = players[i];
// Horizontal Win
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLUMN - numTokens + 1; col++) {
int won = 1;
for (int k = 0; k < numTokens; k++) {
if (arr[row][col + k] != player) {
won = 0;
break;
}
}
if (won) return player;
}
}
// Vertical Win
for (int col = 0; col < COLUMN; col++) {
for (int row = 0; row < ROWS - numTokens + 1; row++) {
int won = 1;
for (int k = 0; k < numTokens; k++) {
if (arr[row + k][col] != player) {
won = 0;
break;
}
}
if (won) return player;
}
}
// Diagonal Win (down-right)
for (int row = 0; row < ROWS - numTokens + 1; row++) {
for (int col = 0; col < COLUMN - numTokens + 1; col++) {
int won = 1;
for (int k = 0; k < numTokens; k++) {
if (arr[row + k][col + k] != player) {
won = 0;
break;
}
}
if (won) return player;
}
}
// Diagonal Win (down-left)
for (int row = 0; row < ROWS - numTokens + 1; row++) {
for (int col = numTokens - 1; col < COLUMN; col++) {
int won = 1;
for (int k = 0; k < numTokens; k++) {
if (arr[row + k][col - k] != player) {
won = 0;
break;
}
}
if (won) return player;
}
}
}
return 0; // No winner yet
}
void assignTurn(int column, int player)
{
column--; // accounting that the column is from 0-6 and user enters from 1-7
if (column < 0 || column >= 7) // checks if the column is not out of bounds
{
printf("please enter a number between 1 and 7");
return;
}
for (int i = 5; i >= 0; i--)
{
if (arr[i][column] == 0) // checks an empty element
{
if (player == 1)
{
arr[i][column] = 'R';
return;
}
else if (player == 2)
{
arr[i][column] = 'Y';
return;
}
}
}
printf("Column is full try another one\n"); // print if the column is full
}
void printBoard() // prints the updated baord inbetween games
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
char element = arr[i][j];
if (element == 0)
{
printf("%c", ' ');
}
else
{
printf("%c", element);
}
printf("%c", bar);
}
printf("\n");
}
}
void resetBoard()
{ // reset board to be blank
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMN; j++)
{
arr[i][j] = 0;
}
}
}
int main()
{
int input;
int numTokens;
int playAgain;
do
{
resetBoard(); // Reset the game board before each new game
printf("Enter the number of tokens to win (3, 4, or 5): "); // prompts to get number of tokens
scanf("%d", &numTokens);
while (checkWinner(numTokens) == 0)
{
printBoard();
printf("Red is ready to play. Pick a column (1-7): "); // prompts red for input
scanf("%d", &input);
assignTurn(input, 1);
int winner = checkWinner(numTokens);
if (winner != 0)
{
printf("Congratulations! red wins!\n"); // red is congratulated for winning
break;
}
printBoard();
printf("Yellow is ready to play. Pick a column (1-7): "); // prompt yellow for input
scanf("%d", &input);
assignTurn(input, 2);
winner = checkWinner(numTokens);
if (winner != 0)
{
printf("Congratulations! Yellow wins!\n"); // red is congratulated for winning
break;
}
}
printf("Do you want to play again? (1 for yes, 0 for no): "); // prompts if user wants to play again
scanf("%d", &playAgain);
} while (playAgain);
printf("Thanks for playing!\n");
return 0;
}