Skip to content

Commit e3953ca

Browse files
committed
Tentaiva de conserta a IA, mas o modo normal ainda não funciona direito
1 parent 070834b commit e3953ca

File tree

2 files changed

+71
-20
lines changed

2 files changed

+71
-20
lines changed

Jogo da Velha/Jogo da Velha.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,13 @@ int main(int argc, char* argv[])
797797
}
798798
if (player == 1)
799799
grid[playCoordinates[0]][playCoordinates[1]] = symbolPreference;
800+
if (freeCellsReturn[0][0] == 3)
801+
{
802+
gridPrinter(grid, 2);
803+
printf("\nDeu velha...\n");
804+
tie = 1;
805+
break;
806+
}
800807
if (player == 2)
801808
{
802809
memcpy(playCoordinates, aiPlay(grid, difficulty), sizeof(playCoordinates));

Jogo da Velha/ai.c

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ int** winPositionVerifyer(int aiGrid[3][3], int symbol)
1919

2020
for (i = 0; i < 3; i++)
2121
{
22-
if (aiGrid[i][0] == symbol && aiGrid[i][1] == symbol && aiGrid[i][2] !=-symbol)
22+
if (aiGrid[i][0] == symbol && aiGrid[i][1] == symbol && aiGrid[i][2] == 0)
2323
{
2424
returnValue[count1][0] = 2;
2525
returnValue[count1][1] = 0;
2626
returnValue[count1][2] = i;
2727
count1++;
2828
}
2929

30-
if (aiGrid[i][0] == symbol && aiGrid[i][1] != -symbol && aiGrid[i][2] == symbol)
30+
if (aiGrid[i][0] == symbol && aiGrid[i][1] == 0 && aiGrid[i][2] == symbol)
3131
{
3232
returnValue[count1][0] = 1;
3333
returnValue[count1][1] = 0;
3434
returnValue[count1][2] = i;
3535
count1++;
3636
}
3737

38-
if (aiGrid[i][0] !=-symbol && aiGrid[i][1] == symbol && aiGrid[i][2] == symbol)
38+
if (aiGrid[i][0] == 0 && aiGrid[i][1] == symbol && aiGrid[i][2] == symbol)
3939
{
4040
returnValue[count1][0] = 0;
4141
returnValue[count1][1] = 0;
@@ -46,23 +46,23 @@ int** winPositionVerifyer(int aiGrid[3][3], int symbol)
4646

4747
for (j = 0; j < 3; j++)
4848
{
49-
if (aiGrid[0][j] == symbol && aiGrid[1][j] == symbol && aiGrid[2][j] !=-symbol)
49+
if (aiGrid[0][j] == symbol && aiGrid[1][j] == symbol && aiGrid[2][j] == 0)
5050
{
5151
returnValue[count1][0] = 2;
5252
returnValue[count1][1] = 1;
5353
returnValue[count1][2] = j;
5454
count1++;
5555
}
5656

57-
if (aiGrid[0][j] == symbol && aiGrid[1][j] !=-symbol && aiGrid[2][j] == symbol)
57+
if (aiGrid[0][j] == symbol && aiGrid[1][j] == 0 && aiGrid[2][j] == symbol)
5858
{
5959
returnValue[count1][0] = 1;
6060
returnValue[count1][1] = 1;
6161
returnValue[count1][2] = j;
6262
count1++;
6363
}
6464

65-
if (aiGrid[0][j] !=-symbol && aiGrid[1][j] == symbol && aiGrid[2][j] == symbol)
65+
if (aiGrid[0][j] == 0 && aiGrid[1][j] == symbol && aiGrid[2][j] == symbol)
6666
{
6767
returnValue[count1][0] = 0;
6868
returnValue[count1][1] = 1;
@@ -73,23 +73,23 @@ int** winPositionVerifyer(int aiGrid[3][3], int symbol)
7373

7474
i = 0;
7575
j = 0;
76-
if (aiGrid[i][j] == symbol && aiGrid[i + 1][j + 1] == symbol && aiGrid[i + 2][j + 2] != -symbol)
76+
if (aiGrid[i][j] == symbol && aiGrid[i + 1][j + 1] == symbol && aiGrid[i + 2][j + 2] == 0)
7777
{
7878
returnValue[count1][0] = 2;
7979
returnValue[count1][1] = 2;
80-
returnValue[count1][2] = j;
80+
returnValue[count1][2] = (j + 2);
8181
count1++;
8282
}
8383

84-
if (aiGrid[i][j] == symbol && aiGrid[i + 1][j + 1] != -symbol && aiGrid[i + 2][j + 2] == symbol)
84+
if (aiGrid[i][j] == symbol && aiGrid[i + 1][j + 1] == 0 && aiGrid[i + 2][j + 2] == symbol)
8585
{
8686
returnValue[count1][0] = 1;
8787
returnValue[count1][1] = 2;
88-
returnValue[count1][2] = j;
88+
returnValue[count1][2] = (j + 1);
8989
count1++;
9090
}
9191

92-
if (aiGrid[i][j] != -symbol && aiGrid[i + 1][j + 1] == symbol && aiGrid[i + 2][j + 2] == symbol)
92+
if (aiGrid[i][j] == 0 && aiGrid[i + 1][j + 1] == symbol && aiGrid[i + 2][j + 2] == symbol)
9393
{
9494
returnValue[count1][0] = 0;
9595
returnValue[count1][1] = 2;
@@ -99,23 +99,23 @@ int** winPositionVerifyer(int aiGrid[3][3], int symbol)
9999

100100
i = 0;
101101
j = 2;
102-
if (aiGrid[i][j] == symbol && aiGrid[i + 1][j - 1] == symbol && aiGrid[i + 2][j - 2] != -symbol)
102+
if (aiGrid[i][j] == symbol && aiGrid[i + 1][j - 1] == symbol && aiGrid[i + 2][j - 2] == 0)
103103
{
104104
returnValue[count1][0] = 2;
105105
returnValue[count1][1] = 3;
106-
returnValue[count1][2] = j;
106+
returnValue[count1][2] = (j - 2);
107107
count1++;
108108
}
109109

110-
if (aiGrid[i][j] == symbol && aiGrid[i + 1][j - 1] != -symbol && aiGrid[i + 2][j - 2] == symbol)
110+
if (aiGrid[i][j] == symbol && aiGrid[i + 1][j - 1] == 0 && aiGrid[i + 2][j - 2] == symbol)
111111
{
112112
returnValue[count1][0] = 1;
113113
returnValue[count1][1] = 3;
114-
returnValue[count1][2] = j;
114+
returnValue[count1][2] = (j - 1);
115115
count1++;
116116
}
117117

118-
if (aiGrid[i][j] != -symbol && aiGrid[i + 1][j - 1] == symbol && aiGrid[i + 2][j - 2] == symbol)
118+
if (aiGrid[i][j] == 0 && aiGrid[i + 1][j - 1] == symbol && aiGrid[i + 2][j - 2] == symbol)
119119
{
120120
returnValue[count1][0] = 0;
121121
returnValue[count1][1] = 3;
@@ -418,6 +418,22 @@ int* aiPlay(int grid[3][3], int difficulty)
418418
{
419419
freeWinPositions[count][0] = 2;
420420
freeWinPositions[count][1] = 1;
421+
freeWinPositions[count][2] = (j + 2);
422+
count++;
423+
}
424+
425+
if (aiGrid[i][j] == 0 && aiGrid[i + 1][j + 1] == aiSymbol && aiGrid[i + 2][j + 2] == 0)
426+
{
427+
freeWinPositions[count][0] = 1;
428+
freeWinPositions[count][1] = 1;
429+
freeWinPositions[count][2] = (j + 1);
430+
count++;
431+
}
432+
433+
if (aiGrid[i][j] == aiSymbol && aiGrid[i + 1][j + 1] == 0 && aiGrid[i + 2][j + 2] == 0)
434+
{
435+
freeWinPositions[count][0] = 0;
436+
freeWinPositions[count][1] = 1;
421437
freeWinPositions[count][2] = j;
422438
count++;
423439
}
@@ -428,10 +444,27 @@ int* aiPlay(int grid[3][3], int difficulty)
428444
{
429445
freeWinPositions[count][0] = 2;
430446
freeWinPositions[count][1] = 1;
447+
freeWinPositions[count][2] = (j - 2);
448+
count++;
449+
}
450+
451+
if (aiGrid[i][j] == 0 && aiGrid[i + 1][j - 1] == aiSymbol && aiGrid[i + 2][j - 2] == 0)
452+
{
453+
freeWinPositions[count][0] = 1;
454+
freeWinPositions[count][1] = 1;
455+
freeWinPositions[count][2] = (j - 1);
456+
count++;
457+
}
458+
459+
if (aiGrid[i][j] == aiSymbol && aiGrid[i + 1][j - 1] == 0 && aiGrid[i + 2][j - 2] == 0)
460+
{
461+
freeWinPositions[count][0] = 0;
462+
freeWinPositions[count][1] = 1;
431463
freeWinPositions[count][2] = j;
432464
count++;
433465
}
434466

467+
435468
if (freeWinPositions[1][2] == 0 && freeWinPositions[0][2] != 0)
436469
{
437470
if (possibleWin[0][1] == 0)
@@ -452,14 +485,20 @@ int* aiPlay(int grid[3][3], int difficulty)
452485
if (possibleWin[0][1] == 2)
453486
{
454487
aiPlayCoordinates[0] = randPostion(possibleWin[0][0]);
455-
aiPlayCoordinates[1] = randPostion(possibleWin[0][0]);
488+
aiPlayCoordinates[1] = aiPlayCoordinates[0];
456489
return aiPlayCoordinates;
457490
}
458491

459492
if (possibleWin[0][1] == 3)
460493
{
461494
aiPlayCoordinates[0] = randPostion(possibleWin[0][0]);
462-
aiPlayCoordinates[1] = randPostion(possibleWin[0][2]);
495+
496+
if (aiPlayCoordinates[0] == 0)
497+
aiPlayCoordinates[1] = (aiPlayCoordinates[0] + 2);
498+
if (aiPlayCoordinates[0] == 1)
499+
aiPlayCoordinates[1] = (aiPlayCoordinates[0]);
500+
if (aiPlayCoordinates[0] == 2)
501+
aiPlayCoordinates[1] = (aiPlayCoordinates[0] - 2);
463502
return aiPlayCoordinates;
464503
}
465504
}
@@ -491,14 +530,19 @@ int* aiPlay(int grid[3][3], int difficulty)
491530
if (possibleWin[winPossibilitiesCount][1] == 2)
492531
{
493532
aiPlayCoordinates[0] = randPostion(possibleWin[winPossibilitiesCount][0]);
494-
aiPlayCoordinates[1] = randPostion(possibleWin[winPossibilitiesCount][0]);
533+
aiPlayCoordinates[1] = (aiPlayCoordinates[0]);
495534
return aiPlayCoordinates;
496535
}
497536

498537
if (possibleWin[winPossibilitiesCount][1] == 3)
499538
{
500539
aiPlayCoordinates[0] = randPostion(possibleWin[winPossibilitiesCount][0]);
501-
aiPlayCoordinates[1] = randPostion(possibleWin[winPossibilitiesCount][2]);
540+
if (aiPlayCoordinates[0] == 0)
541+
aiPlayCoordinates[1] = (aiPlayCoordinates[0] + 2);
542+
if (aiPlayCoordinates[0] == 1)
543+
aiPlayCoordinates[1] = (aiPlayCoordinates[0]);
544+
if (aiPlayCoordinates[0] == 2)
545+
aiPlayCoordinates[1] = (aiPlayCoordinates[0] - 2);
502546
return aiPlayCoordinates;
503547
}
504548
}

0 commit comments

Comments
 (0)