-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
501 lines (427 loc) · 12.3 KB
/
main.c
File metadata and controls
501 lines (427 loc) · 12.3 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Project made by:
// - Ema3nto
// - Giovanni Bellini
/*
TODO LIST
☼ Change table charactewr for windows
☼ Adjust print for windows
☼ Make pointer movement even for windows
THE CODE BETWEEN THE
*****************************************************
COMMENT IS TO CHECK/TRY
THE ADDED CODE HAS THE <- COMMENT ON THE SIDE
*/
#include "inc.h"
#include "structure.c"
#include "winOrLose.c"
#include "menu.c"
/*****************************************************/
/*WHEN EXECUTED WITH GCC IT DON'T GIVE ANY ERROR*//*
#if _WIN64 || _WIN32 // <-
static int set_wide_stream(FILE *stream) // <-
{
return _setmode(_fileno(stream), _O_U16TEXT); // <-
}
setlocale(LC_ALL, ""); // <-
set_wide_stream(stdout); // <-
*/
// #endif // <-
/*****************************************************/
/**************** VARIABILI GLOBALI ****************/
/*struct pos{
int X;
int Y;
};*/
int main(void);
struct pos cursorPos = {5, 11};
char campo[][79] =
{
"┌───────┬───────┬───────┐",
"│ │ │ │▒",
"| | | |▒",
"│ │ │ │▒",
"├───────┼───────┼───────┤▒",
"│ │ │ │▒",
"| | | |▒",
"│ │ │ │▒",
"├───────┼───────┼───────┤▒",
"│ │ │ │▒",
"| | | |▒",
"│ │ │ │▒",
"└───────┴───────┴───────┘▒",
" ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒"
};
struct pos pedine[] =
{
// 0, 1, 2, 3, 4, 5 this are the name of the pawn
//0 1 2
{5, 3}, {13, 3}, {21, 3},
//3 4 5
{5, 11}, {13, 11}, {21, 11}
};
int selected = 3;
int mosse = 0;
int mosseMacchina = 0;
int isStart = 1;
/**************** GETCH ****************/
#ifdef __linux__ // this functions are only for linux
static struct termios old, current;
void initTermios()
{
tcgetattr(0, &old); /* grab old terminal i/o settings */
current = old; /* make new settings same as old settings */
current.c_lflag &= ~ICANON; /* disable buffered i/o */
current.c_lflag &= ~ECHO; /* set no echo mode */
tcsetattr(0, TCSANOW, ¤t); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
/* Read 1 character without echo */
char getch(void)
{
char ch;
initTermios();
ch = getchar();
resetTermios();
return ch;
}
#endif
/**************** FUNZIONI ****************/
void resetPartita()
{
// reset the match from the menù and return in start game 2 or more times
cursorPos.X = 5;
cursorPos.Y = 11;
int w = 0;
for(int i=3; i<=11;i+=8)
{
for(int j=5;j<=21;j+=8)
{
pedine[w].X = j;
pedine[w].Y = i;
w++;
}
}
selected = 3;
mosse = 0;
mosseMacchina = 0;
}
void drawBoard()
{
int a = 1;
char check; //check if in the box there is a space
for(int i = 0; i < sizeof(campo)/sizeof(campo[0]); ++i)
{
for(int j = 0; j < strlen(campo[4]); ++j)
{
check = 0;
for(int k = 0; k < sizeof(pedine)/sizeof(pedine[0]); ++k)
{
if(pedine[k].X-1 == j && pedine[k].Y-1 == i)
{
//print pawn
check |= 1;
printf("%d", k);
a *= 3;
break;
}
}
if(!check)
{
printf("%c", campo[i][j]);
}
}
if(i==1)
printf("\tUser Moves: %d", mosse);
if(i==2)
printf("\tMachine Moves: %d", mosseMacchina);
puts(""); // go in new line
}
}
void moveCursor(char ch)
{
switch(ch)
{
case 'a': // move to sx
if((cursorPos.X - 8) >= 5)
cursorPos.X -= 8;
break;
case 's': // move to bottom
if((cursorPos.Y + 4) <= 11)
cursorPos.Y += 4;
break;
case 'd': // move to dx
if((cursorPos.X + 8) <= 21)
cursorPos.X += 8;
break;
case 'w': // move to top
if((cursorPos.Y - 4) >= 3)
cursorPos.Y -= 4;
break;
}
}
int selection(struct pos input, int is3to6) // check if the input is on one of the pawns of the opponent
{
int i = (is3to6)? 3 : 0;
int a = (is3to6)? 6 : 3;
for(; i < a; i++)
{
if(input.X == pedine[i].X && input.Y == pedine[i].Y)
{
return i;
}
}
return -1;
}
int iselement(struct pos position) // check if in the input position there is an opponent pawn, if there is return the position of the pawn
{
for(int i=0 ; i < 6; ++i)
{
if(position.X == pedine[i].X && position.Y == pedine[i].Y)
{
return i;
}
}
return -1;
}
int rules(struct pos input, int upOrDown)
{
if(input.X == pedine[selected].X && input.Y == pedine[selected].Y + upOrDown && iselement(input) == -1)
{
// if he moves by 1 position and there aren't pawns
return 1;
}
else if(input.X == pedine[selected].X + 8 && input.Y == pedine[selected].Y + upOrDown && iselement(input) != -1)
{
// if he moves in oblique dx by 1 and there is an opponent pawn
return 1;
}
else if(input.X == pedine[selected].X - 8 && input.Y == pedine[selected].Y + upOrDown && iselement(input) != -1)
{
// if he moves in oblique sx by 1 and there is an opponent pawn
return 1;
}
return 0;
}
int piazza()
{
if(selected != -1 && selection(cursorPos, 1) == -1 && rules(cursorPos, -4)) // check if the selected pawn is of the player, check if thecursor is on one of the player pawns, check if the rules are respected
{
int index = iselement(cursorPos); // index of opponent's pawn (if it is present)
pedine[selected].X = cursorPos.X; // update the X & Y of the selected pawns
pedine[selected].Y = cursorPos.Y;
if(index != -1) // if the index is valid
{
//printf("\npiazza: %d\n", index);
// delete the opponent's pawn
pedine[index].X = -1;
pedine[index].Y = -1;
}
selected = -1; // reset the selection
mosse++;
CLEAR;
drawBoard();
return 1;
}
return 0;
}
int option(char ch)
{
// user turn
switch(ch)
{
case 'a': // move to sx
case 's': // move to bottom
case 'd': // move to dx
case 'w': // move to top
moveCursor(ch);
break;
case 32: // to select the pawn [space]
selected = selection(cursorPos, 1);
break;
case 10: // to place the pawn [enter]
return piazza();
case 27:
CLEAR;
main();
break;
}
return 0;
}
int isColorelement(int pIndex, struct pos arr[], int from, int to)
{
for(int i = from; i < to; ++i)
{
if(pedine[pIndex].X == arr[i].X && pedine[pIndex].Y == arr[i].Y)
{
return 0;
}
}
return 1;
}
struct pToRemove machine()
{
++mosseMacchina;
struct pToRemove out;
// computer turn
for(int i = 0; i < sizeof(boxArr)/sizeof(boxArr[0]); ++i)
{
int j;
for(j = 0; j < 6; ++j)
{
if(isColorelement(j, boxArr[i].board,(j < 3) ? 0 : 3, (j < 3) ? 3 : 6))
{
break;
}
}
if(j == 6)
{
// if the i configuration is the actual
int n_mossa;
do{
n_mossa = rand() % boxArr[i].howmany;
}while(boxArr[i].active[n_mossa]==0);
// repeat the selection in the case of the extracted move is disabled
int pedina_attuale;
int pedinaDAmov = iselement(boxArr[i].moves[n_mossa][0]);
pedina_attuale = iselement(boxArr[i].moves[n_mossa][1]);
if(pedina_attuale != -1)
{
//if in the position to moves, is already there a pawn, the latter will be eaten
pedine[pedina_attuale].X = -1;
pedine[pedina_attuale].Y = -1;
}
//moves the pawn to the agreed position
pedine[pedinaDAmov].X = boxArr[i].moves[n_mossa][1].X;
pedine[pedinaDAmov].Y = boxArr[i].moves[n_mossa][1].Y;
CLEAR;
drawBoard();
out.n_box = i;
out.pallino = n_mossa;
return out;
}
}
}
int win(char isUser)
{
int i = (isUser) ? 0 : 3;
int j = (isUser) ? 3 : 0;
int cicli = (isUser) ? 3 : 6;
int cicli2 = (isUser) ? 6 : 3;
int endBoard = (isUser) ? 3 : 11;
// if all the pawns of the user/pc are eaten
for(; i < cicli; ++i)
{
if(pedine[i].X != -1)
{
break;
}
}
if(i == ((isUser) ? 3 : 6))
{
//if someone has lost all the pawns
return 1;
}
// if at leas one of the pawns have reached the end of the board
for(; j < cicli2; ++j)
{
if(pedine[j].Y == endBoard)
{
return 1;
}
}
//check if a move is possible for the opponent
i = (isUser) ? 0 : 3;
int upOrDown = (isUser) ? 4 : -4;
for(; i < cicli; ++i)
{ // foreach opponent's pawn in the field
selected = i;
struct pos tmp = pedine[selected];
if(tmp.X == -1) // if the pawn isn't in the field move on to the next one
continue;
tmp.Y += upOrDown;
if(tmp.Y > 0)
{
for(int otto = -8; otto <= 8; otto += 8)
{
if(((tmp.X == 5 && otto < 0) || (tmp.X == 21 && otto > 0)))
{
continue;
}
tmp.X += otto;
//printf("sel %d %d --", pedine[selected].X, pedine[selected].Y);
//printf("%d %d\n", tmp.X, tmp.Y);
if(selection(tmp, !isUser) == -1 && rules(tmp, upOrDown))
{
// if only 1 pawn can moves return 0
/*i = cicli + 1;
break;*/
selected= -1;
return 0;
}
}
}
}
selected = -1;
if(i == cicli)
{
// the opponent's pawn cannot moves
return 1;
}
return 0;
}
/**************** MAIN LOOP ****************/
int main(void)
{
int c=0;
resetPartita();// reset previous game's info
/*
* since the main is called after the end of the match
* we need to reset the match, and doesn't show the splash screen for ever, moreover it has to show the menù
*/
if(isStart) // if is the start
printTitle(); // print title
isStart = 0;
menu(); // print the menu
readFile(); // read file to get the winning moves
FFLUSHSTDOUT;
srand(time(NULL));
CLEAR;
drawBoard(); // draw field
FFLUSHSTDOUT;
struct pToRemove p;
while(1)
{
do{
printf("\033[%d;%dH", cursorPos.Y, cursorPos.X);
c = option(getch()); // user turn
}while(c==0);
if(win(1))
{
if(boxArr[p.n_box].howmany != 1)
{
boxArr[p.n_box].active[p.pallino] = 0;
}
remove("./data.db");
for(int i = 0; i < sizeof(boxArr)/sizeof(boxArr[0]); ++i)
{
writeFile(boxArr[i].active, sizeof(boxArr[i].active)/sizeof(boxArr[i].active[0]));
}
//user win
printWin();
main();
}
SLEEP(300);
p = machine();
if(win(0))
{
//pc win
printLose();
main();
}
}
return 0;
}