Skip to content

Commit 2c21747

Browse files
committed
Lig 4:
-Now uses ansi escape codes to get the terminal windows size, now it's not OS dependente anymore
1 parent 5879900 commit 2c21747

File tree

2 files changed

+25
-130
lines changed

2 files changed

+25
-130
lines changed

Lig 4/Lig 4.c

Lines changed: 16 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,10 @@
55
#include "ansi_escapes.h"
66
#include "lig 4.h"
77

8-
#ifdef _WIN32
9-
#define windows
10-
#include <Windows.h>
11-
#include <Wincon.h>
12-
#include <ConsoleApi2.h>
13-
148
extern void setupConsole(void);
159
extern void restoreConsoleMode(void);
1610
extern void restoreConsole(void);
1711

18-
#endif //define to just compile windows code
19-
20-
#ifdef __APPLE__
21-
#include "TargetConditionals.h"
22-
#if TARGET_OS_SIMULATOR
23-
# pragma message("You're compiling in the Xcode (OS_SIMULATOR), some terminal interaction may not work\n");
24-
#elif TARGET_OS_IPHONE
25-
# pragma message("You're compiling in the iPhone, some terminal interaction may not work\n");
26-
#elif TARGET_OS_MAC
27-
# pragma message("You're compiling in the Mac OS X, some terminal interaction may not work\n");
28-
#else
29-
# pragma message("You're compiling in a Unknown Apple platform, some terminal interaction may not work")
30-
#endif
31-
# pragma message("If you run into problems, cut the #define linux from the code under the __APPLE__ definition\n");
32-
#define linux
33-
#include <sys/ioctl.h>
34-
#include <unistd.h>
35-
#endif // Define linux headers on apple devices. No guarantee of work.
36-
37-
#ifndef __linux__
38-
#ifndef __APPLE__
39-
#ifndef _WIN32
40-
41-
42-
43-
#define unknonwOS
44-
#define linux
45-
# pragma message("Compiling in a Unknown system (or even on UNIX based system)\n")
46-
# pragma message("Using some linux terminal interation. So some terminal interations and ANSI escapes may not work\n")
47-
# pragma message("If you run into problems, cut the #define linux from the code under the unknowSO definition\n")
48-
#endif // !_WIN32
49-
50-
#endif // !__APPLE__
51-
52-
#endif // Define linux headers on other OS's. No guarantee of work.
53-
54-
#ifdef __linux__
55-
#ifdef linux
56-
#pragma message("You're compiling with Linux support. Make sure to activate the code and headers defined to run at linux, as it's marked as comment")
57-
#endif
58-
#infdef linux
59-
#define linux
60-
#pragma message("You're compiling on Linux. Make sure to activate the code defined and headers to run at linux, as it's marked as comment")
61-
//#include <sys/ioctl.h>
62-
//#include <unistd.h>
63-
#endif //define to just compile linux code
64-
6512
extern void supergridTied(char supergrid[37][98]);
6613
extern bool minimaxTie(int grid[6][7], int nextplayer);
6714
extern Move minimaxPlay(int grid[6][7], int nextplayer);
@@ -361,72 +308,30 @@ int main(int argc, char** argv)
361308
Move playerMove;
362309
Win winVerifyerReturn;
363310
Move winCoordinates[4];
364-
bool isMaximazed = false;
365-
bool legacyMode = true;
366-
367311
//used to freeze the program instead of getchar
368312
char trashCan[10];
369313

370-
setupConsole();
371-
setBackgroundColorRGB(20, 20, 20);
372-
restoreConsole();
373-
printf("Você vai precisar maximizar a janela do console para poder jogar\n");
374-
375-
// Windows mode
376-
#ifdef windows
377-
while (1) {
378-
CONSOLE_SCREEN_BUFFER_INFO csbi;
379-
int columns, rows;
380-
381-
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
382-
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
383-
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
384-
385-
if (rows < 41 || columns < 101)
386-
{
387-
printf("A janela não está no tamanha adequado! Tente aumenta-la ou maximizá-la\n");
388-
printf("Caso você não consiga aumentar mais digite \'n\'\n");
389-
fgets(trashCan, 5, stdin);
390-
if (trashCan[0] == 'n')
391-
{
392-
printf("Ok. Rodando no modo legacy\n");
393-
legacyMode = true;
394-
break;
395-
}
396-
}
397-
398-
if (rows > 40 && columns > 100)
399-
{
400-
legacyMode = false;
401-
break;
402-
}
403-
}
404-
#endif
405-
406-
// Linux mode
407-
/*#ifdef linux
408-
while (1)
409314
{
410-
struct winsize w;
411-
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
315+
int windowSize[2];
316+
setupConsole();
317+
moveTo(999, 999);
318+
getCursorPosition(&windowSize[0], &windowSize[1]);
319+
moveTo(0, 0);
412320

413-
if (w.ws_row < 41 && w.ws_col < 101)
321+
while (windowSize[0] < 41 || windowSize[1] < 101)
414322
{
415-
printf("A janela não está no tamanha adequado! Tente aumenta-la ou maximizá-la\n");
416-
printf("Caso você não consiga aumentar mais digite \'n\'");
417-
fgets(trashCan, 5, stdin);
418-
if (trashCan[0] == 'n')
419-
{
420-
printf("Ok. Rodando no modo legacy");
421-
legacyMode = true;
422-
break;
423-
}
323+
puts("You'll need to expand the windows to play");
324+
moveTo(999, 999);
325+
getCursorPosition(&windowSize[0], &windowSize[1]);
326+
moveTo(0, 0);
424327
}
425-
426-
if (w.ws_row > 40 && w.ws_col > 100)
427-
break;
328+
clearScreenToBottom();
329+
restoreConsoleMode();
428330
}
429-
#endif*/
331+
332+
setupConsole();
333+
setBackgroundColorRGB(20, 20, 20);
334+
restoreConsole();
430335

431336
printf("Gostaria de jogar o modo: 1- Singleplayer ou 2- Multiplayer\n");
432337
fgets(trashCan, 5, stdin);
@@ -456,16 +361,6 @@ int main(int argc, char** argv)
456361
setupConsole();
457362
setBackgroundColorRGB(20, 20, 20);
458363

459-
if (legacyMode == false)
460-
{
461-
moveTo(0, 0);
462-
clearScreenToBottom();
463-
moveTo(0, 0);
464-
}
465-
466-
else
467-
clearScreen();
468-
469364
supergridPrinter(supergrid, grid, winCoordinates);
470365
restoreConsoleMode();
471366
printf("Jogue jogador %i\n", player);

Lig 4/minimax.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "minimax.h"
22
#include <stdbool.h>
33

4-
54
int player;
65
extern Win winVerifyer(int grid[6][7]);
76
extern void getTheRow(int grid[6][7], Move* move);
@@ -53,7 +52,7 @@ int bestValReturner(int bestVal, int mod)
5352
return -1;
5453
}
5554

56-
int** tiedGridReturner(int aiGrid[6][7], int mod)
55+
void tiedGridReturner(int aiGrid[6][7], int mod)
5756
{
5857
static int tiedGrid[6][7];
5958
if (mod == 0)
@@ -65,8 +64,13 @@ int** tiedGridReturner(int aiGrid[6][7], int mod)
6564
}
6665
}
6766
if (mod == 1)
68-
return tiedGrid;
69-
return tiedGrid;
67+
{
68+
for (int i = 0; i < 6; i++)
69+
{
70+
for (int j = 0; j < 7; j++)
71+
aiGrid[i][j] = tiedGrid[i][j];
72+
}
73+
}
7074
}
7175

7276
bool isMovesLeft(int aiGrid[6][7])
@@ -320,11 +324,7 @@ bool minimaxTie(int grid[6][7], int nextplayer)
320324
}
321325
findBestMove(aiGrid);
322326
if (bestValReturner(1, 1) == 0) {
323-
for (int i = 0; i < 6; i++)
324-
{
325-
for (int j = 0; j < 7; j++)
326-
grid[i][j] = aiGrid[i][j];
327-
}
327+
tiedGridReturner(aiGrid, 1);
328328
return true;
329329
}
330330
else

0 commit comments

Comments
 (0)