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
1012typedef struct Coordinates {
1113 short i , j ;
1214}coord ;
1315
16+ // converts the mouse click to board coordinates
1417coord 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
4146void 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
6168void 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
162170void 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
173183bool 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
185196void thanks (void )
186197{
187198 puts ("Thanks for playing!" );
@@ -193,12 +204,13 @@ void thanks(void)
193204
194205int 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 );
0 commit comments