Skip to content

Commit 36598d5

Browse files
authored
Merge pull request #11 from FlyingWolFox/working
Get last stable commits to master
2 parents 73094b3 + f52e7f7 commit 36598d5

37 files changed

+867
-439
lines changed
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup>
4-
<Filter Include="Arquivos de Origem">
4+
<Filter Include="Source Files">
55
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
66
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
77
</Filter>
8-
<Filter Include="Arquivos de Cabeçalho">
8+
<Filter Include="Header Files">
99
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
1010
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
1111
</Filter>
12-
<Filter Include="Arquivos de Recurso">
13-
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14-
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15-
</Filter>
1612
</ItemGroup>
1713
<ItemGroup>
1814
<ClCompile Include="battleship.c">
19-
<Filter>Arquivos de Origem</Filter>
15+
<Filter>Source Files</Filter>
2016
</ClCompile>
2117
<ClCompile Include="ansi_escapes.c">
22-
<Filter>Arquivos de Origem</Filter>
18+
<Filter>Source Files</Filter>
2319
</ClCompile>
2420
<ClCompile Include="windowsConsoleInteraction.c">
25-
<Filter>Arquivos de Origem</Filter>
21+
<Filter>Source Files</Filter>
2622
</ClCompile>
2723
</ItemGroup>
2824
<ItemGroup>
2925
<ClInclude Include="ansi_escapes.h">
30-
<Filter>Arquivos de Cabeçalho</Filter>
26+
<Filter>Header Files</Filter>
3127
</ClInclude>
3228
<ClInclude Include="windowsConsoleInteraction.h">
33-
<Filter>Arquivos de Cabeçalho</Filter>
29+
<Filter>Header Files</Filter>
3430
</ClInclude>
3531
</ItemGroup>
3632
</Project>

Battleship/battleship.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
#include <stdbool.h>
55
#include "ansi_escapes.h"
66

7+
// handles mouse interaction
78
#include "windowsConsoleInteraction.h"
89
#include <conio.h>
910

11+
// coordinate system
1012
typedef struct Coordinates {
1113
short i, j;
1214
}coord;
1315

16+
// converts the mouse click to board coordinates
1417
coord clickToCoordinates(COORD click)
1518
{
1619
coord retValue;
@@ -38,6 +41,8 @@ coord clickToCoordinates(COORD click)
3841
return retValue;
3942
}
4043

44+
// creates the display board
45+
// putting the character to create an empty board
4146
void createDisplayeGrid(char displayGrid[37][100])
4247
{
4348
for (int i = 0; i < 37; i++)
@@ -58,6 +63,8 @@ void createDisplayeGrid(char displayGrid[37][100])
5863
}
5964
}
6065

66+
// modifyes the display grid putting
67+
// a ship part, submarine or miss in the squares
6168
void modifyDisplayGrid(char displayGrid[37][100], coord coordinates, char symbol)
6269
{
6370
char horizontalBow[3][8] = { {' ', ' ', ' ', ' ', ' ', ' ', ' ', '_'} ,
@@ -159,6 +166,7 @@ void modifyDisplayGrid(char displayGrid[37][100], coord coordinates, char symbol
159166

160167
}
161168

169+
// prints the display
162170
void printDisplayGrid(char displayGrid[37][100])
163171
{
164172
for (int i = 0; i < 37; i++)
@@ -170,6 +178,8 @@ void printDisplayGrid(char displayGrid[37][100])
170178
}
171179
}
172180

181+
// sees if the game ended
182+
// if yes, is because all ships have been found
173183
bool isEnd(char grid[9][11], char playerGrid[9][11])
174184
{
175185
bool isEnd = true;
@@ -182,6 +192,7 @@ bool isEnd(char grid[9][11], char playerGrid[9][11])
182192
return isEnd;
183193
}
184194

195+
// prints the thanks
185196
void thanks(void)
186197
{
187198
puts("Thanks for playing!");
@@ -193,12 +204,13 @@ void thanks(void)
193204

194205
int main(int argc, char** argv)
195206
{
207+
// if there's no argument, shows the usage
196208
if (argc < 2)
197209
{
198210
printf("\tusage: battleship <file>\n");
199211
return -1;
200212
}
201-
213+
// try to open the file
202214
FILE* textFile;
203215
textFile = fopen(argv[1], "r");
204216
if (textFile == NULL)
@@ -208,13 +220,16 @@ int main(int argc, char** argv)
208220
}
209221

210222
char displayGrid[37][100];
211-
char grid[9][11];
212-
char playGrid[9][11];
213-
coord play;
214-
EVENT retEvent;
215-
COORD mouseCoord;
216-
char trashcan[10];
217-
223+
char grid[9][11]; // grid with the complete board
224+
char playGrid[9][11]; // the grid that the player sees
225+
coord play; // play coordinates
226+
EVENT retEvent; // console window events
227+
COORD mouseCoord; // mouse click coordinates
228+
char trashcan[10]; // used to get inputs or freeze execution
229+
230+
// looks if the file is valid and
231+
// formatted correctly, and
232+
// gets the map from the file
218233
{
219234
char getGrid[9][25] = { 0 };
220235
bool pass = true;
@@ -250,6 +265,8 @@ int main(int argc, char** argv)
250265

251266
}
252267

268+
// verifies the windows size
269+
// if it's too small it'll prompt to maximaze it
253270
{
254271
int windowSize[2];
255272
setupConsole();
@@ -270,8 +287,11 @@ int main(int argc, char** argv)
270287

271288
createDisplayeGrid(displayGrid);
272289
printDisplayGrid(displayGrid);
290+
291+
// play loop
273292
for (bool playing = true; playing == true;)
274293
{
294+
// gets the mouse click coordinates
275295
while (true)
276296
{
277297
retEvent.event.mouseEvent = 0xc00;
@@ -281,16 +301,20 @@ int main(int argc, char** argv)
281301
break;
282302
}
283303

304+
// if the mouse click coordinates weren't captured
305+
// (for some reason) it'll loop to get it again
284306
play = clickToCoordinates(mouseCoord);
285307
if (play.i == -1 || play.j == -1)
286308
continue;
287309

310+
// modifies the grid and prints it
288311
modifyDisplayGrid(displayGrid, play, grid[play.i][play.j]);
289312
playGrid[play.i][play.j] = grid[play.i][play.j];
290313
clearScreenToTop;
291314
moveTo(0, 0);
292315
printDisplayGrid(displayGrid);
293316

317+
// verifies if the game ended
294318
if (isEnd(grid, playGrid))
295319
{
296320
puts("Game Over!");
@@ -299,6 +323,7 @@ int main(int argc, char** argv)
299323
}
300324
}
301325

326+
// prints the thanks
302327
thanks();
303328
puts("---Press any key to exit---");
304329
fgets(trashcan, 5, stdin);

Battleship/windowsConsoleInteraction.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ VOID KeyEventProc(KEY_EVENT_RECORD);
99
VOID MouseEventProc(MOUSE_EVENT_RECORD);
1010
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
1111

12+
// Original code is commented
13+
// eventMain was the main of this code
14+
1215
EVENT eventMain(VOID)
1316
{
14-
DWORD cNumRead, fdwMode, i;
17+
DWORD cNumRead, fdwMode, i;
1518
INPUT_RECORD irInBuf[128];
1619
int counter = 0;
1720

@@ -77,6 +80,7 @@ EVENT eventMain(VOID)
7780
return retEvent;
7881
}
7982

83+
// Prints the error when it happens
8084
VOID ErrorExit(LPSTR lpszMessage)
8185
{
8286
fprintf(stderr, "%s\n", lpszMessage);
@@ -88,6 +92,7 @@ VOID ErrorExit(LPSTR lpszMessage)
8892
ExitProcess(0);
8993
}
9094

95+
// Gets keyboard event
9196
VOID KeyEventProc(KEY_EVENT_RECORD ker)
9297
{
9398
/*printf("Key event: ");
@@ -99,6 +104,8 @@ VOID KeyEventProc(KEY_EVENT_RECORD ker)
99104
retEvent.event.keyPressed = ker.bKeyDown;
100105
retEvent.event.keyPressed = ker.uChar.AsciiChar;
101106
}
107+
108+
// Gets mouse event
102109
VOID MouseEventProc(MOUSE_EVENT_RECORD mer)
103110
{
104111
#ifndef MOUSE_HWHEELED
@@ -149,6 +156,8 @@ VOID MouseEventProc(MOUSE_EVENT_RECORD mer)
149156
break;
150157
}
151158
}
159+
160+
// Gets the resize event
152161
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD wbsr)
153162
{
154163
//printf("Resize event\n");

Battleship/windowsConsoleInteraction.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,31 @@
44
#include <windows.h>
55
#include <stdio.h>
66

7+
// stores a mouse or key eventy
78
typedef enum event {
89
_MOUSE_EVENT = 0,
910
_KEY_EVENT
1011
}eventType;
1112

13+
// store an (almost) a entire envent
1214
typedef struct Interaction_Event_t {
13-
eventType regEvent;
15+
eventType regEvent; // type of the event
1416
union eventContent {
1517
struct keyPress {
1618
BOOL keyDown;
1719
char keyPressed;
18-
};
20+
}; // hold a key press or a mouse event
1921
struct mouse {
2022
int mouseEvent;
2123
COORD mouseCoord;
2224
};
2325
}event;
2426
}EVENT;
2527

28+
// as this code was get from the stack overflow
29+
// there was a main
30+
// this was changed to eventMain
31+
// and some functions are also changed
2632
EVENT eventMain(VOID);
2733

28-
#endif // !WINDOWS_CONSOLE_INTERACTION
34+
#endif // WINDOWS_CONSOLE_INTERACTION

0 commit comments

Comments
 (0)