Skip to content

Commit ae22aab

Browse files
committed
Battleship:
-Creation of the project -Basic project setup
1 parent 869763b commit ae22aab

File tree

10 files changed

+454
-33
lines changed

10 files changed

+454
-33
lines changed

Atividades de Aula.sln

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lig 4", "Lig 4\Lig 4.vcxpro
99
EndProject
1010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Forca", "Forca\Forca.vcxproj", "{E42D6209-08C2-41E3-A600-C199645ADC2C}"
1111
EndProject
12-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Batalha Naval", "Batalha Naval\Batalha Naval.vcxproj", "{EE58612E-920A-4BC3-8567-0AEB20070867}"
13-
EndProject
1412
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Simon", "Simon\Simon.vcxproj", "{108814A1-98EE-47AA-8749-2BFDAFC780E0}"
1513
EndProject
1614
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Simon (Linux)", "Simon (Linux)\Simon (Linux).vcxproj", "{21252A61-C6AF-4C67-BBD7-E17DA099F957}"
1715
EndProject
16+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Battleship", "Battleship\Battleship.vcxproj", "{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}"
17+
EndProject
1818
Global
1919
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2020
Debug|ARM = Debug|ARM
@@ -91,26 +91,6 @@ Global
9191
{E42D6209-08C2-41E3-A600-C199645ADC2C}.Release|x64.Build.0 = Release|x64
9292
{E42D6209-08C2-41E3-A600-C199645ADC2C}.Release|x86.ActiveCfg = Release|Win32
9393
{E42D6209-08C2-41E3-A600-C199645ADC2C}.Release|x86.Build.0 = Release|Win32
94-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Debug|ARM.ActiveCfg = Debug|Win32
95-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Debug|ARM64.ActiveCfg = Debug|Win32
96-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Debug|x64.ActiveCfg = Debug|x64
97-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Debug|x64.Build.0 = Debug|x64
98-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Debug|x86.ActiveCfg = Debug|Win32
99-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Debug|x86.Build.0 = Debug|Win32
100-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release StaticCRT|ARM.ActiveCfg = Release|Win32
101-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release StaticCRT|ARM.Build.0 = Release|Win32
102-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release StaticCRT|ARM64.ActiveCfg = Release|Win32
103-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release StaticCRT|ARM64.Build.0 = Release|Win32
104-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release StaticCRT|x64.ActiveCfg = Release|x64
105-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release StaticCRT|x64.Build.0 = Release|x64
106-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release StaticCRT|x86.ActiveCfg = Release|Win32
107-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release StaticCRT|x86.Build.0 = Release|Win32
108-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release|ARM.ActiveCfg = Release|Win32
109-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release|ARM64.ActiveCfg = Release|Win32
110-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release|x64.ActiveCfg = Release|x64
111-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release|x64.Build.0 = Release|x64
112-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release|x86.ActiveCfg = Release|Win32
113-
{EE58612E-920A-4BC3-8567-0AEB20070867}.Release|x86.Build.0 = Release|Win32
11494
{108814A1-98EE-47AA-8749-2BFDAFC780E0}.Debug|ARM.ActiveCfg = Debug|Win32
11595
{108814A1-98EE-47AA-8749-2BFDAFC780E0}.Debug|ARM64.ActiveCfg = Debug|Win32
11696
{108814A1-98EE-47AA-8749-2BFDAFC780E0}.Debug|x64.ActiveCfg = Debug|x64
@@ -167,6 +147,26 @@ Global
167147
{21252A61-C6AF-4C67-BBD7-E17DA099F957}.Release|x86.ActiveCfg = Release|x86
168148
{21252A61-C6AF-4C67-BBD7-E17DA099F957}.Release|x86.Build.0 = Release|x86
169149
{21252A61-C6AF-4C67-BBD7-E17DA099F957}.Release|x86.Deploy.0 = Release|x86
150+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Debug|ARM.ActiveCfg = Debug|Win32
151+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Debug|ARM64.ActiveCfg = Debug|Win32
152+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Debug|x64.ActiveCfg = Debug|x64
153+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Debug|x64.Build.0 = Debug|x64
154+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Debug|x86.ActiveCfg = Debug|Win32
155+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Debug|x86.Build.0 = Debug|Win32
156+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release StaticCRT|ARM.ActiveCfg = Release|x64
157+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release StaticCRT|ARM.Build.0 = Release|x64
158+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release StaticCRT|ARM64.ActiveCfg = Release|x64
159+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release StaticCRT|ARM64.Build.0 = Release|x64
160+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release StaticCRT|x64.ActiveCfg = Release|x64
161+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release StaticCRT|x64.Build.0 = Release|x64
162+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release StaticCRT|x86.ActiveCfg = Release|Win32
163+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release StaticCRT|x86.Build.0 = Release|Win32
164+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release|ARM.ActiveCfg = Release|Win32
165+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release|ARM64.ActiveCfg = Release|Win32
166+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release|x64.ActiveCfg = Release|x64
167+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release|x64.Build.0 = Release|x64
168+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release|x86.ActiveCfg = Release|Win32
169+
{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}.Release|x86.Build.0 = Release|Win32
170170
EndGlobalSection
171171
GlobalSection(SolutionProperties) = preSolution
172172
HideSolutionNode = FALSE

Batalha Naval/batalha_naval.c

Whitespace-only changes.

Batalha Naval/batalha_naval.h

Lines changed: 0 additions & 5 deletions
This file was deleted.

Batalha Naval/Batalha Naval.vcxproj renamed to Battleship/Battleship.vcxproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919
</ProjectConfiguration>
2020
</ItemGroup>
2121
<ItemGroup>
22-
<ClCompile Include="batalha_naval.c" />
22+
<ClCompile Include="ansi_escapes.c" />
23+
<ClCompile Include="battleship.c" />
24+
<ClCompile Include="windowsConsoleInteraction.c" />
2325
</ItemGroup>
2426
<ItemGroup>
25-
<ClInclude Include="batalha_naval.h" />
27+
<ClInclude Include="ansi_escapes.h" />
28+
<ClInclude Include="windowsConsoleInteraction.h" />
2629
</ItemGroup>
2730
<PropertyGroup Label="Globals">
2831
<VCProjectVersion>16.0</VCProjectVersion>
29-
<ProjectGuid>{EE58612E-920A-4BC3-8567-0AEB20070867}</ProjectGuid>
32+
<ProjectGuid>{1896E8DC-0FF8-4DB3-A437-36ACB913CC95}</ProjectGuid>
3033
<Keyword>Win32Proj</Keyword>
31-
<RootNamespace>BatalhaNaval</RootNamespace>
34+
<RootNamespace>Battleship</RootNamespace>
3235
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
3336
</PropertyGroup>
3437
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

Batalha Naval/Batalha Naval.vcxproj.filters renamed to Battleship/Battleship.vcxproj.filters

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@
1515
</Filter>
1616
</ItemGroup>
1717
<ItemGroup>
18-
<ClCompile Include="batalha_naval.c">
18+
<ClCompile Include="battleship.c">
19+
<Filter>Arquivos de Origem</Filter>
20+
</ClCompile>
21+
<ClCompile Include="ansi_escapes.c">
22+
<Filter>Arquivos de Origem</Filter>
23+
</ClCompile>
24+
<ClCompile Include="windowsConsoleInteraction.c">
1925
<Filter>Arquivos de Origem</Filter>
2026
</ClCompile>
2127
</ItemGroup>
2228
<ItemGroup>
23-
<ClInclude Include="batalha_naval.h">
29+
<ClInclude Include="ansi_escapes.h">
30+
<Filter>Arquivos de Cabeçalho</Filter>
31+
</ClInclude>
32+
<ClInclude Include="windowsConsoleInteraction.h">
2433
<Filter>Arquivos de Cabeçalho</Filter>
2534
</ClInclude>
2635
</ItemGroup>

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

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

Battleship/battleship.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include "ansi_escapes.h"
5+
6+
#include "windowsConsoleInteraction.h"
7+
#include <conio.h>
8+
9+
int main(int argc, char** argv)
10+
{
11+
12+
13+
return 0;
14+
}

0 commit comments

Comments
 (0)