Skip to content

Commit 378d481

Browse files
committed
Interfaxe e funcionamento básico inplatados e funcionando. Falta o verificador de empate pra commpletar o necessário. Este vai usar o algoritmo minimax, assim como a IA. Tentando ativar True Color no cmd
1 parent 37f878e commit 378d481

File tree

2 files changed

+158
-20
lines changed

2 files changed

+158
-20
lines changed

Lig 4/Lig 4.c

Lines changed: 157 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,105 @@ extern void restoreConsole(void);
1212
Win winVerifyer(int grid[6][7])
1313
{
1414
Win returnValue;
15-
for (int i = 0; i < 6; i++)
15+
returnValue.win = false;
16+
for (int i = 5; i >= 0; i--)
1617
{
17-
for (int j = 0; j < 3; j++)
18+
for (int j = 0; j < 4; j++)
1819
{
1920
if (grid[i][j] == 1 && grid[i][j + 1] == 1 && grid[i][j + 2] == 1 && grid[i][j + 3] == 1)
2021
{
2122
returnValue.win = true;
2223
returnValue.victoryType = 'H';
24+
returnValue.victoryTypeVariation = 1;
2325
returnValue.row = i;
24-
returnValue.row = j;
26+
returnValue.col = j;
2527
}
2628

2729
if (grid[i][j] == -1 && grid[i][j + 1] == -1 && grid[i][j + 2] == -1 && grid[i][j + 3] == -1)
2830
{
2931
returnValue.win = true;
3032
returnValue.victoryType = 'H';
33+
returnValue.victoryTypeVariation = 1;
3134
returnValue.row = i;
32-
returnValue.row = j;
35+
returnValue.col = j;
3336
}
3437
}
38+
}
3539

36-
for (int j = 0; j < 7; j++)
40+
for (int j = 0; j < 7; j++)
41+
{
42+
for (int i = 5; i > 2; i--)
3743
{
38-
for (int i = 0; i < 2; i++)
44+
if (grid[i][j] == 1 && grid[i - 1][j] == 1 && grid[i - 2][j] == 1 && grid[i - 3][j] == 1)
3945
{
40-
if (grid[i][j] == 1 && grid[i + 1][j] == 1 && grid[i + 2][j] == 1 && grid[i + 3][j] == 1)
41-
{
42-
returnValue.win = true;
43-
returnValue.victoryType = 'V';
44-
returnValue.row = i;
45-
returnValue.row = j;
46-
}
46+
returnValue.win = true;
47+
returnValue.victoryType = 'V';
48+
returnValue.victoryTypeVariation = 1;
49+
returnValue.row = i;
50+
returnValue.col = j;
51+
}
52+
53+
if (grid[i][j] == -1 && grid[i - 1][j] == -1 && grid[i - 2][j] == -1 && grid[i - 3][j] == -1)
54+
{
55+
returnValue.win = true;
56+
returnValue.victoryType = 'V';
57+
returnValue.victoryTypeVariation = 1;
58+
returnValue.row = i;
59+
returnValue.col = j;
60+
}
61+
}
62+
63+
}
64+
65+
for (int j = 0; j < 4; j++)
66+
{
67+
for (int i = 5; i > 2; i--)
68+
{
69+
if (grid[i][j] == 1 && grid[i - 1][j + 1] == 1 && grid[i - 2][j + 2] == 1 && grid[i - 3][j + 3] == 1)
70+
{
71+
returnValue.win = true;
72+
returnValue.victoryType = 'D';
73+
returnValue.victoryTypeVariation = 1;
74+
returnValue.row = i;
75+
returnValue.col = j;
76+
}
77+
78+
if (grid[i][j] == -1 && grid[i - 1][j + 1] == -1 && grid[i - 2][j + 2] == -1 && grid[i - 3][j + 3] == -1)
79+
{
80+
returnValue.win = true;
81+
returnValue.victoryType = 'D';
82+
returnValue.victoryTypeVariation = 1;
83+
returnValue.row = i;
84+
returnValue.col = j;
4785
}
4886
}
4987
}
88+
89+
for (int j = 6; j > 2; j--)
90+
{
91+
for (int i = 5; i > 2; i--)
92+
{
93+
if (grid[i][j] == 1 && grid[i - 1][j - 1] == 1 && grid[i - 2][j - 2] == 1 && grid[i - 3][j - 3] == 1)
94+
{
95+
returnValue.win = true;
96+
returnValue.victoryType = 'D';
97+
returnValue.victoryTypeVariation = 2;
98+
returnValue.row = i;
99+
returnValue.col = j;
100+
}
101+
102+
if (grid[i][j] == -1 && grid[i - 1][j - 1] == -1 && grid[i - 2][j - 2] == -1 && grid[i - 3][j - 3] == -1)
103+
{
104+
returnValue.win = true;
105+
returnValue.victoryType = 'D';
106+
returnValue.victoryTypeVariation = 2;
107+
returnValue.row = i;
108+
returnValue.col = j;
109+
}
110+
}
111+
}
112+
113+
return returnValue;
50114
}
51115

52116
void supergridGenerator(char supergrid[37][98])
@@ -83,7 +147,7 @@ void supergridGenerator(char supergrid[37][98])
83147
}
84148
}
85149

86-
void supergridPrinter(char supergrid[37][98], int grid[6][7])
150+
void supergridPrinter(char supergrid[37][98], int grid[6][7], Move winCoordinates[4])
87151
{
88152
for (int i = 0, gridI = 0; i < 37; i++)
89153
{
@@ -119,14 +183,26 @@ void supergridPrinter(char supergrid[37][98], int grid[6][7])
119183
if (grid[gridI][gridJ] == 1)
120184
{
121185
setTextColorBright(BLUE_TXT);
186+
for (int count = 0; count < 4; count++)
187+
{
188+
if (winCoordinates[count].col == gridJ && winCoordinates[count].row == gridI)
189+
setBackgroundColorBright(WHITE_BKG);
190+
}
122191
printf("%c", supergrid[i][j]);
123192
setTextColor(WHITE_TXT);
193+
setBackgroundColor(BLACK_BKG);
124194
}
125195
if (grid[gridI][gridJ] == -1)
126196
{
127197
setTextColorBright(RED_TXT);
198+
for (int count = 0; count < 4; count++)
199+
{
200+
if (winCoordinates[count].col == gridJ && winCoordinates[count].row == gridI)
201+
setBackgroundColor(WHITE_BKG);
202+
}
128203
printf("%c", supergrid[i][j]);
129204
setTextColor(WHITE_TXT);
205+
setBackgroundColor(BLACK_BKG);
130206
}
131207
if (grid[gridI][gridJ] == 0)
132208
printf("%c", supergrid[i][j]);
@@ -188,7 +264,7 @@ void supergridModifyer(char supergrid[37][98], Move modification)
188264

189265
void getTheRow(int grid[6][7], Move *move)
190266
{
191-
for (int i = 6; i >= 0; i--)
267+
for (int i = 5; i >= 0; i--)
192268
{
193269
if (grid[i][move->col] == 0)
194270
{
@@ -207,28 +283,42 @@ int main(int argc, char* argv)
207283
int grid[6][7] = { 0 };
208284
char supergrid[37][98];
209285

210-
supergridGenerator(supergrid);
211286
printf("\n");
212287

213288

214-
for (int mainMenu = 0; mainMenu == 0;)
289+
for (bool mainMenu = true; mainMenu == true;)
215290
{
216291
int player;
217292
char playerNumber[3];
218293
Move playerMove;
219294
char playerMovement[3];
295+
Win winVerifyerReturn;
296+
Move winCoordinates[4];
220297
printf("Gostaria de jogar o modo: 1- Singleplyer ou 2- Multiplayer\n");
221298
if (scanf("%i", &player) == 1);
222299

223300
for (char keepPlaying = 's'; keepPlaying == 's' || keepPlaying == 'S';)
224301
{
302+
supergridGenerator(supergrid);
303+
for (int i = 0; i < 6; i++)
304+
{
305+
for (int j = 0; j < 7; j++)
306+
grid[i][j] = 0;
307+
}
225308
player = 1;
226-
for (int win = 0, tie = 0; win == 0 && tie == 0;)
309+
for (bool win = false, tie = false; win == false && tie == false;)
227310
{
311+
for (int count = 0; count < 4; count++)
312+
{
313+
winCoordinates[count].row = -1;
314+
winCoordinates[count].col = -1;
315+
}
316+
228317
if (player == 3)
229318
player = 1;
230319
setupConsole();
231-
supergridPrinter(supergrid, grid);
320+
printf("/033[90");
321+
supergridPrinter(supergrid, grid, winCoordinates);
232322
restoreConsole();
233323
printf("Jogue jogador %i\n", player);
234324
if(scanf(" %i", &playerMove.col) == 1);
@@ -246,11 +336,58 @@ int main(int argc, char* argv)
246336
if (player == 2)
247337
grid[playerMove.row][playerMove.col] = -1;
248338
supergridModifyer(supergrid, playerMove);
339+
winVerifyerReturn = winVerifyer(grid);
340+
if (winVerifyerReturn.win == true)
341+
{
342+
if (winVerifyerReturn.victoryType == 'H')
343+
{
344+
for (int count = 0; count < 4; count++)
345+
{
346+
winCoordinates[count].row = winVerifyerReturn.row;
347+
winCoordinates[count].col = (winVerifyerReturn.col + count);
348+
}
349+
}
350+
351+
if (winVerifyerReturn.victoryType == 'V')
352+
{
353+
for (int count = 0; count < 4; count++)
354+
{
355+
winCoordinates[count].row = (winVerifyerReturn.row - count);
356+
winCoordinates[count].col = winVerifyerReturn.col;
357+
}
358+
}
359+
360+
if (winVerifyerReturn.victoryType == 'D' && winVerifyerReturn.victoryTypeVariation == 1)
361+
{
362+
for (int count = 0; count < 4; count++)
363+
{
364+
winCoordinates[count].row = (winVerifyerReturn.row - count);
365+
winCoordinates[count].col = (winVerifyerReturn.col + count);
366+
}
367+
}
368+
369+
if (winVerifyerReturn.victoryType == 'D' && winVerifyerReturn.victoryTypeVariation == 2)
370+
{
371+
for (int count = 0; count < 4; count++)
372+
{
373+
winCoordinates[count].row = (winVerifyerReturn.row - count);
374+
winCoordinates[count].col = (winVerifyerReturn.col - count);
375+
}
376+
}
377+
supergridPrinter(supergrid, grid, winCoordinates);
378+
printf("Jogador %i venceu!", player);
379+
win = true;
380+
}
249381
player++;
250382
}
251383

252384
printf("\nContinuar jogando? s-sim n-não\n");
253-
if (scanf(" %c", &keepPlaying) == 1);
385+
if (scanf(" %c", &keepPlaying) == 1)
386+
continue;
387+
printf("Voltar ao menu principal? s-sim n-não\n");
388+
if (getchar() == '\n');
389+
if (getchar() == 'n')
390+
mainMenu = false;
254391
}
255392
}
256393

Lig 4/Lig 4.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct MoveCoordinates {
1010
typedef struct WinInfo {
1111
bool win;
1212
char victoryType;
13+
int victoryTypeVariation;
1314
int row, col;
1415
}Win;
1516

0 commit comments

Comments
 (0)