Skip to content

Commit 3db4e47

Browse files
committed
Well, it's a good time I don't push, so I don't remember what I changed :P
Tic Tac Toe: - AI action order changed from Oponent-2 -> AI-2 -> rest to AI-2 -> Oponent-2 -> rest, so AI see if itself wins before seeing if the oponente will win -Some corrections (nothing especific) Lig 4: -Some corrections (nothing especific) -There's a Run-time check error #2 when Ai looks for ties, somewhere, happens some times, some not Hang Man: -Made Rest: -Nothing
1 parent e1e338a commit 3db4e47

File tree

11 files changed

+729
-42
lines changed

11 files changed

+729
-42
lines changed

Forca/Forca.vcxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
</ProjectConfiguration>
2020
</ItemGroup>
2121
<ItemGroup>
22-
<ClCompile Include="..\Lig 4\ansi_escapes.c" />
22+
<ClCompile Include="ansi_escapes.c" />
2323
<ClCompile Include="forca.c" />
2424
</ItemGroup>
2525
<ItemGroup>
26-
<ClInclude Include="..\Lig 4\ansi_escapes.h" />
26+
<ClInclude Include="ansi_escapes.h" />
2727
<ClInclude Include="forca.h" />
2828
</ItemGroup>
2929
<PropertyGroup Label="Globals">
@@ -38,7 +38,7 @@
3838
<ConfigurationType>Application</ConfigurationType>
3939
<UseDebugLibraries>true</UseDebugLibraries>
4040
<PlatformToolset>v142</PlatformToolset>
41-
<CharacterSet>Unicode</CharacterSet>
41+
<CharacterSet>MultiByte</CharacterSet>
4242
</PropertyGroup>
4343
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
4444
<ConfigurationType>Application</ConfigurationType>

Forca/Forca.vcxproj.filters

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
<ClCompile Include="forca.c">
1919
<Filter>Arquivos de Origem</Filter>
2020
</ClCompile>
21-
<ClCompile Include="..\Lig 4\ansi_escapes.c">
21+
<ClCompile Include="ansi_escapes.c">
2222
<Filter>Arquivos de Origem</Filter>
2323
</ClCompile>
2424
</ItemGroup>
2525
<ItemGroup>
2626
<ClInclude Include="forca.h">
2727
<Filter>Arquivos de Cabeçalho</Filter>
2828
</ClInclude>
29-
<ClInclude Include="..\Lig 4\ansi_escapes.h">
29+
<ClInclude Include="ansi_escapes.h">
3030
<Filter>Arquivos de Cabeçalho</Filter>
3131
</ClInclude>
3232
</ItemGroup>

Forca/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+
}

Forca/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+
}

0 commit comments

Comments
 (0)