Skip to content

Commit 73094b3

Browse files
authored
Merge pull request #7 from FlyingWolFox/working
Fixing Simon Bug and Comments (Start of them)
2 parents 153d4af + 5fcff18 commit 73094b3

File tree

3 files changed

+70
-56
lines changed

3 files changed

+70
-56
lines changed

Jogo da Velha/Jogo da Velha.c

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ extern void setupConsole(void);
1111
extern void restoreConsoleMode(void);
1212
extern void restoreConsole(void);
1313

14-
/*verifica se alguém venceu e retorna um array[3]
15-
caso verdade, retorna {1, W, C},
16-
sendo W o tipo de vitória:
17-
0 = Horizontal, 1 = Vertical
18-
2 = Diagonal 1 (\), 3 = Diagonal 2 (/)
19-
sendo C uma coordenada, i para vitória
20-
horizontal e j para o resto */
14+
// Verifyes if someone won and return an array[3]
15+
//if someone won, returns {1, W, C}, being:
16+
// W the win type:
17+
// 0, if horizontal
18+
// 1, if vertical
19+
// 2, if diagonal 1 (\)
20+
// 3, if diagonal 2 (/)
21+
// C the coordinate, being:
22+
// row for horizontal win
23+
// column for the rest
2124
int* winVerifyer(int grid[3][3])
2225
{
2326
static int returnValue[3];
2427
int i, j;
2528

29+
// looks for X horizontal wins
2630
for (i = 0; i < 3; i++)
2731
{
2832
if (grid[i][0] == 1)
@@ -37,6 +41,7 @@ int* winVerifyer(int grid[3][3])
3741
}
3842
}
3943

44+
// looks for X vertical wins
4045
for (j = 0; j < 3; j++)
4146
{
4247
if (grid[0][j] == 1)
@@ -51,6 +56,7 @@ int* winVerifyer(int grid[3][3])
5156
}
5257
}
5358

59+
// look for X \ diagonal wins
5460
i = 0;
5561
j = 0;
5662
if (grid[i][j] == 1 && grid[i + 1][j + 1] == 1 && grid[i + 2][j + 2] == 1)
@@ -61,6 +67,7 @@ int* winVerifyer(int grid[3][3])
6167
return returnValue;
6268
}
6369

70+
// look for X / diagonal wins
6471
i = 0;
6572
j = 2;
6673
if (grid[i][j] == 1 && grid[i + 1][j - 1] == 1 && grid[i + 2][j - 2] == 1)
@@ -71,6 +78,7 @@ int* winVerifyer(int grid[3][3])
7178
return returnValue;
7279
}
7380

81+
//looks for O horizontal wins
7482
for (i = 0; i < 3; i++)
7583
{
7684
if (grid[i][0] == -1)
@@ -86,7 +94,7 @@ int* winVerifyer(int grid[3][3])
8694
}
8795

8896

89-
97+
// looks for O vertical wins
9098
for (j = 0; j < 3; j++)
9199
{
92100
if (grid[0][j] == -1)
@@ -101,6 +109,7 @@ int* winVerifyer(int grid[3][3])
101109
}
102110
}
103111

112+
// look for O \ diagonal wins
104113
i = 0;
105114
j = 0;
106115
if (grid[i][j] == -1 && grid[i + 1][j + 1] == -1 && grid[i + 2][j + 2] == -1)
@@ -110,7 +119,8 @@ int* winVerifyer(int grid[3][3])
110119
returnValue[2] = j;
111120
return returnValue;
112121
}
113-
122+
123+
// look for O / diagonal wins
114124
i = 0;
115125
j = 2;
116126
if (grid[i][j] == -1 && grid[i + 1][j - 1] == -1 && grid[i + 2][j - 2] == -1)
@@ -121,25 +131,28 @@ int* winVerifyer(int grid[3][3])
121131
return returnValue;
122132
}
123133

134+
// if no one won returns everything -1
124135
returnValue[0] = -1;
125136
returnValue[1] = -1;
126137
returnValue[2] = -1;
127138
return returnValue;
128139
}
129140

130-
/*verifica se há até 3 casas livres
131-
e retorna as 3 posições, ou menos*/
141+
// Verifyes if exists 3 or less free cells
142+
// and return the coordinate of these cells
132143
int** freeCells(int grid[3][3])
133144
{
134145
static int freePositions[4][2] = { 0 };
135146
int count1;
136147

137148
count1 = 0;
138149

150+
// looks for free cells
139151
for (int i = 0; i < 3; i++)
140152
{
141153
for (int j = 0; j < 3; j++)
142154
{
155+
// if more than 3 free cells are found return all -1
143156
if (count1 > 3)
144157
{
145158
freePositions[2][0] = -1;
@@ -158,6 +171,7 @@ int** freeCells(int grid[3][3])
158171
}
159172
}
160173
}
174+
// no free position was found, set everything to 3
161175
if (freePositions[2][0] == 0 && freePositions[2][1] == 0 && freePositions[1][0] == 0 && freePositions[1][1] == 0 && freePositions[0][0] == 0 && freePositions[0][1] == 0)
162176
{
163177
freePositions[3][0] = 3;
@@ -172,23 +186,23 @@ int** freeCells(int grid[3][3])
172186
return freePositions;
173187
}
174188

175-
176-
/*retem a gridCopy usada na tieVerifyer
177-
para passá-la para a main*/
189+
// gets the tied grid to pass it to main
178190
int** gridTiedReturner(int gridCopy[3][3], int mod)
179191
{
180192
static int gridCopyToReturn[3][3];
181193

194+
// gets the tied grid
182195
if (mod == 0)
183196
memcpy(gridCopyToReturn, gridCopy, sizeof(gridCopyToReturn));
197+
// returns the pereviously got tied grid
184198
if (mod == 1)
185199
return gridCopyToReturn;
186200
return 0;
187201
}
188202

189-
/*verifica se o jogo vai empatar daqui 3, 2 ou 1 jogada(s)
190-
caso empate, retorna 1
191-
caso não, retorna 0*/
203+
// Verifyes if the game will tie in 3, 2 or 1 plays
204+
// if it ties, returns 1
205+
// if not, returns 0
192206
int tieVerifyer(int grid[3][3], int player, int insertionPreference)
193207
{
194208
static int gridCopy[3][3];
@@ -266,7 +280,7 @@ int tieVerifyer(int grid[3][3], int player, int insertionPreference)
266280
return 1;
267281
}
268282

269-
//converte o posicionamento de 1 a 9 por coordenadas
283+
// Converts the position to coordinates
270284
int* convertToCoordinate(int position)
271285
{
272286
static int coordinate[2];
@@ -319,8 +333,7 @@ int* convertToCoordinate(int position)
319333
return coordinate;
320334
}
321335

322-
//converte as coordenadas da grade para coordenadas da
323-
//super grade
336+
// converts the grid coordinates to supergrid coordinates
324337
int* coordinatesToSupergrid(int coordinates[2])
325338
{
326339
static int supergridCoordinates[2];
@@ -359,7 +372,7 @@ int* coordinatesToSupergrid(int coordinates[2])
359372
return supergridCoordinates;
360373
}
361374

362-
//modifica a super grade, botando X ou O
375+
// Modifies the supergrid, putting X or O
363376
void superGridModifier(char supergrid[31][51], int grid[3][3])
364377
{
365378
int supergridCoordinates[2], gridCoordinates[2];
@@ -409,7 +422,7 @@ void superGridModifier(char supergrid[31][51], int grid[3][3])
409422

410423
}
411424

412-
//imprime a grade
425+
// prints the supergrid
413426
void gridPrinter(int grid[3][3], int mod)
414427
{
415428
static char superGrid[31][51];
@@ -630,7 +643,7 @@ void gridPrinter(int grid[3][3], int mod)
630643
}
631644
}
632645

633-
//imprime os agradecimentos
646+
//print the thanks
634647
void thanks()
635648
{
636649
printf("\nMuito Obrigado por Jogar!\n");
@@ -646,23 +659,22 @@ void thanks()
646659

647660
int main(int argc, char* argv[])
648661
{
649-
static int grid[3][3] = { 0 };
650-
int playCoordinates[2] = { 0 }, playPosition, player;
651-
int insertionPreference, symbolPreference;
652-
int win = 0, tie = 0;
653-
int tieFlag = 0;
654-
int freeCellsReturn[4][2];
655-
char keepPlaying;
656-
char trashcan[10];
662+
static int grid[3][3] = { 0 }; //grid creation
663+
int playCoordinates[2] = { 0 }, playPosition, player; // creates the player interaction
664+
int symbolPreference; // this will be used to determine if the player 1 wants X or O
665+
int win = 0, tie = 0; // win and tie flags
666+
int freeCellsReturn[4][2]; // this will hold the return array of the free cells verifyer function
667+
char keepPlaying; // keep playing flag
668+
char trashcan[10]; //used to freeze the program
657669
keepPlaying = 'x';
658670

659-
printf("Iniciando Jogo da Velha!\n");
671+
printf("Starting Tic Tac Toe\n");
660672
printf("To select a position you use:\n");
661673
gridPrinter(grid, 3);
662674
fgets(trashcan, 5, stdin);
663-
printf("Gostaria de jogar o modo: 1- Singleplyer ou 2- Multiplayer\n");
675+
printf("Would you like to play which mode: 1- Singleplyer or 2- Multiplayer\n");
664676
if (scanf("%i", &player) == 1);
665-
printf("Com qual gostaria de jogar? 1- X ou 2- O\n");
677+
printf("Which you'll play with? 1- X ou 2- O\n");
666678
if (scanf("%i", &symbolPreference) == 2);
667679
if (symbolPreference == 2)
668680
symbolPreference = -1;
@@ -682,7 +694,6 @@ int main(int argc, char* argv[])
682694
gridPrinter(grid, 0);
683695
win = 0;
684696
tie = 0;
685-
tieFlag = 0;
686697
while (win == 0 && tie == 0)
687698
{
688699
setupConsole();
@@ -692,7 +703,7 @@ int main(int argc, char* argv[])
692703
gridPrinter(grid, 2);
693704
if (player == 3)
694705
player = 1;
695-
printf("Jogue, Jogador %i\n", player);
706+
printf("Play, Player %i\n", player);
696707

697708
if(scanf("%i", &playPosition) == 1);
698709
memcpy(playCoordinates, convertToCoordinate(playPosition), sizeof(playCoordinates));
@@ -701,7 +712,7 @@ int main(int argc, char* argv[])
701712
break;
702713
if (grid[playCoordinates[0]][playCoordinates[1]] != 0)
703714
{
704-
printf("\nEssa casa já foi marcada!\n");
715+
printf("\nThis cell is already marked!\n");
705716
if (getchar() != NULL);
706717
continue;
707718
}
@@ -716,7 +727,7 @@ int main(int argc, char* argv[])
716727
moveTo(0, 0);
717728
restoreConsole();
718729
gridPrinter(grid, 5);
719-
printf("\nJogador %i venceu!", player);
730+
printf("\nPlayer %i wins!", player);
720731
win = 1;
721732
break;
722733
}
@@ -727,7 +738,7 @@ int main(int argc, char* argv[])
727738
if (freeCellsReturn[0][0] == 3)
728739
{
729740
gridPrinter(gridTiedReturner(grid, 1), 2);
730-
printf("\nDeu velha...\n");
741+
printf("\nTie...\n");
731742
tie = 1;
732743
break;
733744
}
@@ -736,13 +747,13 @@ int main(int argc, char* argv[])
736747
{
737748
memcpy(grid, gridTiedReturner(grid, 1), sizeof(grid));
738749
gridPrinter(gridTiedReturner(grid, 1), 2);
739-
printf("\nDeu velha...\n");
750+
printf("\nTie...\n");
740751
tie = 1;
741752
break;
742753
}
743754
}
744755
}
745-
printf("\nContinuar Jogando?\n s- Sim n- Não\n");
756+
printf("\nContinue playing?\n y- yes n- no\n");
746757
if(getchar() == '\n');
747758
if (scanf("%c", &keepPlaying) == 1);
748759
player = 0;
@@ -752,7 +763,7 @@ int main(int argc, char* argv[])
752763
if (player == 1)
753764
{
754765
int difficulty;
755-
printf("Selecione sua dificuldade: 1- Fácil, 2- Normal, 3- Impossível\n");
766+
printf("Select your difficulty: 1- Easy, 2- Normal, 3- Impossible\n");
756767
if (scanf(" %i", &difficulty) == 1);
757768
difficulty--;
758769
aiStart(grid, -symbolPreference);
@@ -769,16 +780,18 @@ int main(int argc, char* argv[])
769780
gridPrinter(grid, 0);
770781
win = 0;
771782
tie = 0;
772-
tieFlag = 0;
773783
while (win == 0 && tie == 0)
774784
{
775-
printf("\033[2J"); //funciona como system("clear") ou system("cls");
785+
setupConsole();
786+
clearScreenToTop();
787+
moveTo(0, 0);
788+
restoreConsole();
776789
if (player == 3)
777790
player = 1;
778791
if (player == 1)
779792
{
780793
gridPrinter(grid, 2);
781-
printf("Jogue, Jogador!\n");
794+
printf("Play, Player!\n");
782795

783796
if (scanf("%i", &playPosition) == 1);
784797
memcpy(playCoordinates, convertToCoordinate(playPosition), sizeof(playCoordinates));
@@ -787,7 +800,7 @@ int main(int argc, char* argv[])
787800
break;
788801
if (grid[playCoordinates[0]][playCoordinates[1]] != 0)
789802
{
790-
printf("\nEssa casa já foi marcada!\n");
803+
printf("\nThis cell is already marked!\n");
791804
if (getchar() != NULL);
792805
continue;
793806
}
@@ -797,7 +810,7 @@ int main(int argc, char* argv[])
797810
if (freeCellsReturn[0][0] == 3)
798811
{
799812
gridPrinter(grid, 2);
800-
printf("\nDeu velha...\n");
813+
printf("\nTie...\n");
801814
tie = 1;
802815
break;
803816
}
@@ -811,9 +824,9 @@ int main(int argc, char* argv[])
811824
{
812825
gridPrinter(grid, 5);
813826
if (player == 1)
814-
printf("\nVocê venceu!");
827+
printf("\nYou win!");
815828
if (player == 2)
816-
printf("\nVocê Perdeu!\n");
829+
printf("\nYou lose!\n");
817830
win = 1;
818831
break;
819832
}
@@ -824,7 +837,7 @@ int main(int argc, char* argv[])
824837
if (freeCellsReturn[0][0] == 3)
825838
{
826839
gridPrinter(grid, 2);
827-
printf("\nDeu velha...\n");
840+
printf("\nTie...\n");
828841
tie = 1;
829842
break;
830843
}
@@ -833,13 +846,13 @@ int main(int argc, char* argv[])
833846
{
834847
memcpy(grid, gridTiedReturner(grid, 1), sizeof(grid));
835848
gridPrinter(gridTiedReturner(grid, 1), 2);
836-
printf("\nDeu velha...\n");
849+
printf("\nTie...\n");
837850
tie = 1;
838851
break;
839852
}
840853
}
841854
}
842-
printf("\nContinuar Jogando?\n s- Sim n- Não\n");
855+
printf("\nContine playing?\n y- yes n- no\n");
843856
if (getchar() == '\n');
844857
if (scanf("%c", &keepPlaying) == 1);
845858
}

0 commit comments

Comments
 (0)