Skip to content

Commit

Permalink
Well, it's a good time I don't push, so I don't remember what I chang…
Browse files Browse the repository at this point in the history
…ed :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
  • Loading branch information
FlyingWolFox committed Nov 18, 2019
1 parent e1e338a commit 3db4e47
Show file tree
Hide file tree
Showing 11 changed files with 729 additions and 42 deletions.
6 changes: 3 additions & 3 deletions Forca/Forca.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\Lig 4\ansi_escapes.c" />
<ClCompile Include="ansi_escapes.c" />
<ClCompile Include="forca.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Lig 4\ansi_escapes.h" />
<ClInclude Include="ansi_escapes.h" />
<ClInclude Include="forca.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
Expand All @@ -38,7 +38,7 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
Expand Down
4 changes: 2 additions & 2 deletions Forca/Forca.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
<ClCompile Include="forca.c">
<Filter>Arquivos de Origem</Filter>
</ClCompile>
<ClCompile Include="..\Lig 4\ansi_escapes.c">
<ClCompile Include="ansi_escapes.c">
<Filter>Arquivos de Origem</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="forca.h">
<Filter>Arquivos de Cabeçalho</Filter>
</ClInclude>
<ClInclude Include="..\Lig 4\ansi_escapes.h">
<ClInclude Include="ansi_escapes.h">
<Filter>Arquivos de Cabeçalho</Filter>
</ClInclude>
</ItemGroup>
Expand Down
101 changes: 101 additions & 0 deletions Forca/ansi_escapes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS 1
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#ifdef _WIN32
// Some old MinGW/CYGWIN distributions don't define this:
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif

static HANDLE stdoutHandle, stdinHandle;
static DWORD outModeInit, inModeInit;

void setupConsole(void) {
DWORD outMode = 0, inMode = 0;
stdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
stdinHandle = GetStdHandle(STD_INPUT_HANDLE);

if (stdoutHandle == INVALID_HANDLE_VALUE || stdinHandle == INVALID_HANDLE_VALUE) {
exit(GetLastError());
}

if (!GetConsoleMode(stdoutHandle, &outMode) || !GetConsoleMode(stdinHandle, &inMode)) {
exit(GetLastError());
}

outModeInit = outMode;
inModeInit = inMode;

// Enable ANSI escape codes
outMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;

// Set stdin as no echo and unbuffered
inMode &= ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);

if (!SetConsoleMode(stdoutHandle, outMode) || !SetConsoleMode(stdinHandle, inMode)) {
exit(GetLastError());
}
}

void restoreConsoleMode(void) {
if (!SetConsoleMode(stdoutHandle, outModeInit) || !SetConsoleMode(stdinHandle, inModeInit)) {
exit(GetLastError());
}
}

void restoreConsole(void) {
// Reset colors
printf("\x1b[0m");

// Reset console mode
if (!SetConsoleMode(stdoutHandle, outModeInit) || !SetConsoleMode(stdinHandle, inModeInit)) {
exit(GetLastError());
}
}
#else

static struct termios orig_term;
static struct termios new_term;

void setupConsole(void) {
tcgetattr(STDIN_FILENO, &orig_term);
new_term = orig_term;

new_term.c_lflag &= ~(ICANON | ECHO);

tcsetattr(STDIN_FILENO, TCSANOW, &new_term);
}

void restoreConsole(void) {
// Reset colors
printf("\x1b[0m");

// Reset console mode
tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
}
#endif

void getCursorPosition(int* row, int* col) {
printf("\x1b[6n");
char buff[128];
int indx = 0;
for (;;) {
int cc = getchar();
buff[indx] = (char)cc;
indx++;
if (cc == 'R') {
buff[indx + 1] = '\0';
break;
}
}
sscanf(buff, "\x1b[%d;%dR", row, col);
fseek(stdin, 0, SEEK_END);
}
115 changes: 115 additions & 0 deletions Forca/ansi_escapes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#pragma once

#include <stdio.h>

enum Colors {
RESET_COLOR,
BLACK_TXT = 30,
RED_TXT,
GREEN_TXT,
YELLOW_TXT,
BLUE_TXT,
MAGENTA_TXT,
CYAN_TXT,
WHITE_TXT,

BLACK_BKG = 40,
RED_BKG,
GREEN_BKG,
YELLOW_BKG,
BLUE_BKG,
MAGENTA_BKG,
CYAN_BKG,
WHITE_BKG
};

enum ClearCodes {
CLEAR_FROM_CURSOR_TO_END,
CLEAR_FROM_CURSOR_TO_BEGIN,
CLEAR_ALL
};

void setupConsole(void);
void restoreConsoleMode(void);
void restoreConsole(void);
void getCursorPosition(int* row, int* col);

static inline void setTextColorRGB(int r, int g, int b) {
printf("\x1b[38;2;%d;%d;%dm", r, g, b);
}

static inline void setTextColor(int code) {
printf("\x1b[%dm", code);
}

static inline void setTextColorBright(int code) {
printf("\x1b[%d;1m", code);
}

static inline void setBackgroundColorRGB(int r, int g, int b) {
printf("\x1b[48;2;%d;%d;%dm", r, g, b);
}

static inline void setBackgroundColor(int code) {
printf("\x1b[%dm", code);
}

static inline void setBackgroundColorBright(int code) {
printf("\x1b[%d;1m", code);
}

static inline void resetColor(void) {
printf("\x1b[%dm", RESET_COLOR);
}

static inline void clearScreen(void) {
printf("\x1b[%dJ", CLEAR_ALL);
}

static inline void clearScreenToBottom(void) {
printf("\x1b[%dJ", CLEAR_FROM_CURSOR_TO_END);
}

static inline void clearScreenToTop(void) {
printf("\x1b[%dJ", CLEAR_FROM_CURSOR_TO_BEGIN);
}

static inline void clearLine(void) {
printf("\x1b[%dK", CLEAR_ALL);
}

static inline void clearLineToRight(void) {
printf("\x1b[%dK", CLEAR_FROM_CURSOR_TO_END);
}

static inline void clearLineToLeft(void) {
printf("\x1b[%dK", CLEAR_FROM_CURSOR_TO_BEGIN);
}

static inline void moveUp(int positions) {
printf("\x1b[%dA", positions);
}

static inline void moveDown(int positions) {
printf("\x1b[%dB", positions);
}

static inline void moveRight(int positions) {
printf("\x1b[%dC", positions);
}

static inline void moveLeft(int positions) {
printf("\x1b[%dD", positions);
}

static inline void moveTo(int row, int col) {
printf("\x1b[%d;%df", row, col);
}

static inline void saveCursorPosition(void) {
printf("\x1b%d", 7);
}

static inline void restoreCursorPosition(void) {
printf("\x1b%d", 8);
}
Loading

0 comments on commit 3db4e47

Please sign in to comment.