-
Notifications
You must be signed in to change notification settings - Fork 1
/
obstruction.c
250 lines (214 loc) · 6.63 KB
/
obstruction.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Author: Muhammed Ali Dilek (xemeds)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BOARD_SIZE 6
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
char board[BOARD_SIZE][BOARD_SIZE];
// Clears the screen
void clear() {
system("clear");
}
// Prints the ASCII art
void print_art() {
printf("\033[0;36m");
printf("░█████╗░██████╗░░██████╗████████╗██████╗░██╗░░░██╗░█████╗░████████╗██╗░█████╗░███╗░░██╗\n");
printf("██╔══██╗██╔══██╗██╔════╝╚══██╔══╝██╔══██╗██║░░░██║██╔══██╗╚══██╔══╝██║██╔══██╗████╗░██║\n");
printf("██║░░██║██████╦╝╚█████╗░░░░██║░░░██████╔╝██║░░░██║██║░░╚═╝░░░██║░░░██║██║░░██║██╔██╗██║\n");
printf("██║░░██║██╔══██╗░╚═══██╗░░░██║░░░██╔══██╗██║░░░██║██║░░██╗░░░██║░░░██║██║░░██║██║╚████║\n");
printf("╚█████╔╝██████╦╝██████╔╝░░░██║░░░██║░░██║╚██████╔╝╚█████╔╝░░░██║░░░██║╚█████╔╝██║░╚███║\n");
printf("░╚════╝░╚═════╝░╚═════╝░░░░╚═╝░░░╚═╝░░╚═╝░╚═════╝░░╚════╝░░░░╚═╝░░░╚═╝░╚════╝░╚═╝░░╚══╝\n");
printf("\033[0m\n");
}
// Introduction to the game
void intro() {
// ASCII art
print_art();
// Intro
printf("\n");
printf("\033[33m\033[4m\033[1mDescription:\n");
printf("\033[0m\033[1m");
printf("\nThe game is played on a grid of 6 x 6. Your symbol is '\033[34m\033[1mX\033[0m\033[1m' and the bots symbol is '\033[31m\033[1mO\033[0m\033[1m'.\n");
printf("You and the bot take turns writing your symbols in a cell. The restriction is that\n");
printf("you can only play in a cell if all its neighbours are empty.\n");
printf("Further explanation: www.papg.com/show?2XMX\n");
printf("\n");
printf("\033[33m\033[4m\033[1mGoal:");
printf("\033[0m\033[1m Leave no place for the bot to move.\n");
printf("\n");
printf("\033[33m\033[1mTo write your symbol in a cell you must specify the location.\n");
printf("\033[33m\033[1mEx:");
printf("\033[0m\033[1m a1 c4 b6\n");
// Start
printf("\nPress Enter to start...\n");
printf("> ");
getchar();
}
// Initializes the board
void init_board() {
for (int y = 0; y < BOARD_SIZE; y++)
{
for (int x = 0; x < BOARD_SIZE; x++)
{
board[y][x] = ' ';
}
}
}
// Prints the board
void print_board() {
clear();
print_art();
printf("\033[0m\033[1m");
printf("\t\t\t\t a b c d e f\n");
printf("\033[32m");
printf("\t\t\t\t╔═══╤═══╤═══╤═══╤═══╤═══╗\n");
for (int y = 0; y < BOARD_SIZE; y++)
{
printf("\033[0m\033[1m");
printf("\t\t\t %i ", y + 1);
printf("\033[32m║");
for (int x = 0; x < BOARD_SIZE; x++)
{
if (board[y][x] == 'X')
printf("\033[34m\033[1m %c ", board[y][x]);
else if (board[y][x] == 'O')
printf("\033[31m\033[1m %c ", board[y][x]);
else
printf("\033[0m\033[1m %c ", board[y][x]);
if (x != BOARD_SIZE - 1)
printf("\033[32m│");
}
printf("\033[32m║\n");
if (y != BOARD_SIZE - 1)
printf("\t\t\t\t╟───┼───┼───┼───┼───┼───╢\n");
}
printf("\t\t\t\t╚═══╧═══╧═══╧═══╧═══╧═══╝\n\n");
printf("\033[0m\033[1m");
printf("\t\t\t==+==+==+==+==+==+==+==+==+==+==+==+==+==\n\n");
}
// Returns 0 if there are no places left to play
int check_board() {
int can_play = 0;
for (int y = 0; y < BOARD_SIZE; y++)
{
for (int x = 0; x < BOARD_SIZE; x++)
{
if (board[y][x] == ' ')
can_play = 1;
}
}
return can_play;
}
// Places a move on the board
void place_move(char symbol, int x, int y) {
// Place the neighbors
// By Reddit user btwiusearch
for (int i = MAX(y - 1, 0); i < MIN(y + 2, BOARD_SIZE); i++) {
for (int j = MAX(x - 1, 0); j < MIN(x + 2, BOARD_SIZE); j++) {
board[i][j] = '#';
}
}
// Place the symbol
board[y][x] = symbol;
}
// Gets a valid user input and sends it to be placed on the board
void user_move() {
char move[3];
int x, y;
// Loop untill a valid move is given
while (1)
{
// Get the move
printf("\t\t\t\t\t> ");
scanf("%2s", move);
// Check the syntax
int len = strlen(move);
if (!(len == 2) || !(97 <= move[0] && move[0] <= 102) || !(49 <= move[1] && move[1] <= 54))
{
print_board();
continue;
}
// Convert the move into integers
// By Reddit user timeforscience
x = move[0] - 'a';
y = move[1] - '1';
// Check if the moves location is empty
if (board[y][x] != ' ')
{
print_board();
continue;
}
// Break the loop
break;
}
// Place the move
place_move('X', x, y);
}
// Chooses a random empty cell and sends it to be placed on the board
void bot_move() {
int empty_cells = 0, x, y, random_cell;
// Find the amount of empty cells
for (int j = 0; j < BOARD_SIZE; j++)
{
for (int i = 0; i < BOARD_SIZE; i++)
{
if (board[j][i] == ' ')
empty_cells++;
}
}
// Choose a random empty cell
random_cell = (999 % empty_cells) + 1;
// Find the random empty cells location
int cell = 0;
for (int j = 0; j < BOARD_SIZE; j++)
{
for (int i = 0; i < BOARD_SIZE; i++)
{
if (board[j][i] == ' ')
{
cell++;
if (cell == random_cell)
{
x = i;
y = j;
}
}
}
}
// Place the bots move on that location
place_move('O', x, y);
}
int main () {
// Clear screen
clear();
// Print the intro
intro();
// Initialize the board
init_board();
// Main loop
while (1)
{
// Print the board
print_board();
// Check if the user has places left to play
if (!check_board())
{
printf("\033[33m\033[1m\t\t\t\t\tYou lose.\033[0m\n\n");
break;
}
// Users move
user_move();
// Print the board
print_board();
// Check if the bot has places left to play
if (!check_board())
{
printf("\033[33m\033[1m\t\t\t\t\tYou win.\033[0m\n\n");
break;
}
// Bots move
bot_move();
}
return 0;
}