Skip to content

Commit e3226bd

Browse files
committed
Lig 4:
-Optimizations Tic Tac Toe: -Using ansi escape codes now -Optimizations
1 parent 4cee1c0 commit e3226bd

File tree

6 files changed

+251
-31
lines changed

6 files changed

+251
-31
lines changed

Jogo da Velha/Jogo da Velha.c

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
#include "Jogo da Velha.h"
33
#include "ai.h"
44
#include "minimax.h"
5+
#include "ansi_escapes.h"
56
#include <stdio.h>
67
#include <stdlib.h>
78
#include <string.h>
89

10+
extern void setupConsole(void);
11+
extern void restoreConsoleMode(void);
12+
extern void restoreConsole(void);
13+
914
/*verifica se alguém venceu e retorna um array[3]
1015
caso verdade, retorna {1, W, C},
1116
sendo W o tipo de vitória:
@@ -648,16 +653,13 @@ int main(int argc, char* argv[])
648653
int tieFlag = 0;
649654
int freeCellsReturn[4][2];
650655
char keepPlaying;
656+
char trashcan[10];
651657
keepPlaying = 'x';
652658

653659
printf("Iniciando Jogo da Velha!\n");
654-
printf("Como gostaria o tabuleiro? (Alerta, muitas linhas! Pressione enter para continuar)\n");
655-
if(getchar() == '\n');
656-
printf("1: As posições vão de 1 a 9\n");
660+
printf("To select a position you use:\n");
657661
gridPrinter(grid, 3);
658-
printf("\n2: As posições são coordenadas to tipo (x,y)\n");
659-
gridPrinter(grid, 4);
660-
if (scanf("%i", &insertionPreference) == 1);
662+
fgets(trashcan, 5, stdin);
661663
printf("Gostaria de jogar o modo: 1- Singleplyer ou 2- Multiplayer\n");
662664
if (scanf("%i", &player) == 1);
663665
printf("Com qual gostaria de jogar? 1- X ou 2- O\n");
@@ -683,22 +685,18 @@ int main(int argc, char* argv[])
683685
tieFlag = 0;
684686
while (win == 0 && tie == 0)
685687
{
686-
printf("\033[2J"); //funciona como system("clear") ou system("cls");
688+
setupConsole();
689+
clearScreenToTop();
690+
moveTo(0, 0);
691+
restoreConsole();
687692
gridPrinter(grid, 2);
688693
if (player == 3)
689694
player = 1;
690695
printf("Jogue, Jogador %i\n", player);
691-
if (insertionPreference == 1)
692-
{
693-
if (scanf("%i", &playPosition) == 1);
694-
memcpy(playCoordinates, convertToCoordinate(playPosition), sizeof(playCoordinates));
695-
}
696-
if (insertionPreference == 2)
697-
{
698-
if (scanf("%i %i", &playCoordinates[0], &playCoordinates[1]) == 2);
699-
playCoordinates[0]--;
700-
playCoordinates[1]--;
701-
}
696+
697+
if(scanf("%i", &playPosition) == 1);
698+
memcpy(playCoordinates, convertToCoordinate(playPosition), sizeof(playCoordinates));
699+
702700
if (playCoordinates[0] < 0 || playCoordinates[1] < 0)
703701
break;
704702
if (grid[playCoordinates[0]][playCoordinates[1]] != 0)
@@ -713,6 +711,10 @@ int main(int argc, char* argv[])
713711
grid[playCoordinates[0]][playCoordinates[1]] = -symbolPreference;
714712
if ((winVerifyer(grid))[0] == 1)
715713
{
714+
setupConsole();
715+
clearScreenToTop();
716+
moveTo(0, 0);
717+
restoreConsole();
716718
gridPrinter(grid, 5);
717719
printf("\nJogador %i venceu!", player);
718720
win = 1;
@@ -730,7 +732,7 @@ int main(int argc, char* argv[])
730732
break;
731733
}
732734

733-
if (tieVerifyer(grid, player, insertionPreference) == 1)
735+
if (tieVerifyer(grid, player, symbolPreference) == 1)
734736
{
735737
memcpy(grid, gridTiedReturner(grid, 1), sizeof(grid));
736738
gridPrinter(gridTiedReturner(grid, 1), 2);
@@ -777,17 +779,10 @@ int main(int argc, char* argv[])
777779
{
778780
gridPrinter(grid, 2);
779781
printf("Jogue, Jogador!\n");
780-
if (insertionPreference == 1)
781-
{
782-
if (scanf("%i", &playPosition) == 1);
783-
memcpy(playCoordinates, convertToCoordinate(playPosition), sizeof(playCoordinates));
784-
}
785-
if (insertionPreference == 2)
786-
{
787-
if (scanf("%i %i", &playCoordinates[0], &playCoordinates[1]) == 2);
788-
playCoordinates[0]--;
789-
playCoordinates[1]--;
790-
}
782+
783+
if (scanf("%i", &playPosition) == 1);
784+
memcpy(playCoordinates, convertToCoordinate(playPosition), sizeof(playCoordinates));
785+
791786
if (playCoordinates[0] < 0 || playCoordinates[1] < 0)
792787
break;
793788
if (grid[playCoordinates[0]][playCoordinates[1]] != 0)
@@ -834,7 +829,7 @@ int main(int argc, char* argv[])
834829
break;
835830
}
836831

837-
if (tieVerifyer(grid, player, insertionPreference) == 1)
832+
if (tieVerifyer(grid, player, symbolPreference) == 1)
838833
{
839834
memcpy(grid, gridTiedReturner(grid, 1), sizeof(grid));
840835
gridPrinter(gridTiedReturner(grid, 1), 2);

Jogo da Velha/Jogo da Velha.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
</ItemGroup>
2121
<ItemGroup>
2222
<ClCompile Include="ai.c" />
23+
<ClCompile Include="ansi_escapes.c" />
2324
<ClCompile Include="Jogo da Velha.c" />
2425
<ClCompile Include="minimax.c" />
2526
</ItemGroup>
2627
<ItemGroup>
2728
<ClInclude Include="ai.h" />
29+
<ClInclude Include="ansi_escapes.h" />
2830
<ClInclude Include="Jogo da Velha.h" />
2931
<ClInclude Include="minimax.h" />
3032
</ItemGroup>

Jogo da Velha/Jogo da Velha.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<ClCompile Include="minimax.c">
2525
<Filter>Arquivos de Origem</Filter>
2626
</ClCompile>
27+
<ClCompile Include="ansi_escapes.c">
28+
<Filter>Arquivos de Origem</Filter>
29+
</ClCompile>
2730
</ItemGroup>
2831
<ItemGroup>
2932
<ClInclude Include="ai.h">
@@ -35,5 +38,8 @@
3538
<ClInclude Include="minimax.h">
3639
<Filter>Arquivos de Cabeçalho</Filter>
3740
</ClInclude>
41+
<ClInclude Include="ansi_escapes.h">
42+
<Filter>Arquivos de Cabeçalho</Filter>
43+
</ClInclude>
3844
</ItemGroup>
3945
</Project>

Jogo da Velha/ansi_escapes.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#ifdef _WIN32
2+
#define _CRT_SECURE_NO_WARNINGS 1
3+
#include <windows.h>
4+
#else
5+
#include <termios.h>
6+
#include <unistd.h>
7+
#endif
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
#ifdef _WIN32
13+
// Some old MinGW/CYGWIN distributions don't define this:
14+
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
15+
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
16+
#endif
17+
18+
static HANDLE stdoutHandle, stdinHandle;
19+
static DWORD outModeInit, inModeInit;
20+
21+
void setupConsole(void) {
22+
DWORD outMode = 0, inMode = 0;
23+
stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
24+
stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
25+
26+
if (stdoutHandle == INVALID_HANDLE_VALUE || stdinHandle == INVALID_HANDLE_VALUE) {
27+
exit(GetLastError());
28+
}
29+
30+
if (!GetConsoleMode(stdoutHandle, &outMode) || !GetConsoleMode(stdinHandle, &inMode)) {
31+
exit(GetLastError());
32+
}
33+
34+
outModeInit = outMode;
35+
inModeInit = inMode;
36+
37+
// Enable ANSI escape codes
38+
outMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
39+
40+
// Set stdin as no echo and unbuffered
41+
inMode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
42+
43+
if (!SetConsoleMode(stdoutHandle, outMode) || !SetConsoleMode(stdinHandle, inMode)) {
44+
exit(GetLastError());
45+
}
46+
}
47+
48+
void restoreConsoleMode(void) {
49+
if (!SetConsoleMode(stdoutHandle, outModeInit) || !SetConsoleMode(stdinHandle, inModeInit)) {
50+
exit(GetLastError());
51+
}
52+
}
53+
54+
void restoreConsole(void) {
55+
// Reset colors
56+
printf("\x1b[0m");
57+
58+
// Reset console mode
59+
if (!SetConsoleMode(stdoutHandle, outModeInit) || !SetConsoleMode(stdinHandle, inModeInit)) {
60+
exit(GetLastError());
61+
}
62+
}
63+
#else
64+
65+
static struct termios orig_term;
66+
static struct termios new_term;
67+
68+
void setupConsole(void) {
69+
tcgetattr(STDIN_FILENO, &orig_term);
70+
new_term = orig_term;
71+
72+
new_term.c_lflag &= ~(ICANON | ECHO);
73+
74+
tcsetattr(STDIN_FILENO, TCSANOW, &new_term);
75+
}
76+
77+
void restoreConsole(void) {
78+
// Reset colors
79+
printf("\x1b[0m");
80+
81+
// Reset console mode
82+
tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
83+
}
84+
#endif
85+
86+
void getCursorPosition(int* row, int* col) {
87+
printf("\x1b[6n");
88+
char buff[128];
89+
int indx = 0;
90+
for (;;) {
91+
int cc = getchar();
92+
buff[indx] = (char)cc;
93+
indx++;
94+
if (cc == 'R') {
95+
buff[indx + 1] = '\0';
96+
break;
97+
}
98+
}
99+
sscanf(buff, "\x1b[%d;%dR", row, col);
100+
fseek(stdin, 0, SEEK_END);
101+
}

Jogo da Velha/ansi_escapes.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#pragma once
2+
3+
#include <stdio.h>
4+
5+
enum Colors {
6+
RESET_COLOR,
7+
BLACK_TXT = 30,
8+
RED_TXT,
9+
GREEN_TXT,
10+
YELLOW_TXT,
11+
BLUE_TXT,
12+
MAGENTA_TXT,
13+
CYAN_TXT,
14+
WHITE_TXT,
15+
16+
BLACK_BKG = 40,
17+
RED_BKG,
18+
GREEN_BKG,
19+
YELLOW_BKG,
20+
BLUE_BKG,
21+
MAGENTA_BKG,
22+
CYAN_BKG,
23+
WHITE_BKG
24+
};
25+
26+
enum ClearCodes {
27+
CLEAR_FROM_CURSOR_TO_END,
28+
CLEAR_FROM_CURSOR_TO_BEGIN,
29+
CLEAR_ALL
30+
};
31+
32+
void setupConsole(void);
33+
void restoreConsoleMode(void);
34+
void restoreConsole(void);
35+
void getCursorPosition(int* row, int* col);
36+
37+
static inline void setTextColorRGB(int r, int g, int b) {
38+
printf("\x1b[38;2;%d;%d;%dm", r, g, b);
39+
}
40+
41+
static inline void setTextColor(int code) {
42+
printf("\x1b[%dm", code);
43+
}
44+
45+
static inline void setTextColorBright(int code) {
46+
printf("\x1b[%d;1m", code);
47+
}
48+
49+
static inline void setBackgroundColorRGB(int r, int g, int b) {
50+
printf("\x1b[48;2;%d;%d;%dm", r, g, b);
51+
}
52+
53+
static inline void setBackgroundColor(int code) {
54+
printf("\x1b[%dm", code);
55+
}
56+
57+
static inline void setBackgroundColorBright(int code) {
58+
printf("\x1b[%d;1m", code);
59+
}
60+
61+
static inline void resetColor(void) {
62+
printf("\x1b[%dm", RESET_COLOR);
63+
}
64+
65+
static inline void clearScreen(void) {
66+
printf("\x1b[%dJ", CLEAR_ALL);
67+
}
68+
69+
static inline void clearScreenToBottom(void) {
70+
printf("\x1b[%dJ", CLEAR_FROM_CURSOR_TO_END);
71+
}
72+
73+
static inline void clearScreenToTop(void) {
74+
printf("\x1b[%dJ", CLEAR_FROM_CURSOR_TO_BEGIN);
75+
}
76+
77+
static inline void clearLine(void) {
78+
printf("\x1b[%dK", CLEAR_ALL);
79+
}
80+
81+
static inline void clearLineToRight(void) {
82+
printf("\x1b[%dK", CLEAR_FROM_CURSOR_TO_END);
83+
}
84+
85+
static inline void clearLineToLeft(void) {
86+
printf("\x1b[%dK", CLEAR_FROM_CURSOR_TO_BEGIN);
87+
}
88+
89+
static inline void moveUp(int positions) {
90+
printf("\x1b[%dA", positions);
91+
}
92+
93+
static inline void moveDown(int positions) {
94+
printf("\x1b[%dB", positions);
95+
}
96+
97+
static inline void moveRight(int positions) {
98+
printf("\x1b[%dC", positions);
99+
}
100+
101+
static inline void moveLeft(int positions) {
102+
printf("\x1b[%dD", positions);
103+
}
104+
105+
static inline void moveTo(int row, int col) {
106+
printf("\x1b[%d;%df", row, col);
107+
}
108+
109+
static inline void saveCursorPosition(void) {
110+
printf("\x1b%d", 7);
111+
}
112+
113+
static inline void restoreCursorPosition(void) {
114+
printf("\x1b%d", 8);
115+
}

Lig 4/Lig 4.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ int main(int argc, char** argv)
445445
clearScreenToTop();
446446
moveTo(0, 0);
447447
supergridTied(supergrid);
448+
supergridPrinter(supergrid, grid, winCoordinates);
448449
printf("Empate!");
449450
fgets(trashCan, 5, stdin);
450451
}

0 commit comments

Comments
 (0)