Skip to content

Commit

Permalink
Battleship:
Browse files Browse the repository at this point in the history
-Display grid and it's functions created
-Mouse interactions created
-End verification created
-File opening and verification is done
-File loading is done

-Issue: isn't possible to play yet, problens with it (see Issue #4)
  • Loading branch information
FlyingWolFox committed Nov 25, 2019
1 parent ae22aab commit b754c02
Showing 1 changed file with 241 additions and 0 deletions.
241 changes: 241 additions & 0 deletions Battleship/battleship.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Expand All @@ -6,8 +7,248 @@
#include "windowsConsoleInteraction.h"
#include <conio.h>

typedef struct Coordinates {
short i, j;
}coord;

coord clickToCoordinates(COORD click)
{
coord retValue;

for (int count = 0; count < 37; count += 9)
{
if (click.Y > count && click.Y < count + 9)
retValue.i = count/9;
}
for (int count = 0; count < 109; count += 4)
{
if (click.X > count && click.X < count + 4)
retValue.j = count/4;
}
return retValue;
}

void createDisplayeGrid(char displayGrid[37][109])
{
for (int i = 0; i < 37; i++)
{
for (int j = 0; j < 109; j++)
{
if (j % 9 == 0 || i % 4 == 0)
{
if (i % 4 == 0)
displayGrid[i][j] = ':';

if (j % 9 == 0)
displayGrid[i][j] = '|';
}
else
displayGrid[i][j] = ' ';
}
}
}

void modifyDisplayGrid(char displayGrid[37][109], coord coordinates, char symbol)
{
char horizontalBow[3][8] = { {' ', ' ', ' ', ' ', ' ', ' ', ' ', '_'} ,
{' ', ' ', ' ', ' ', '_', '_', '/', ' '} ,
{' ', ' ', ' ', ' ', '\\', '_', '_', '_'} };

char horizontalMiddle[3][8] = { {'_', '_', '_', '_', '_', '_', '_', '_'} ,
{' ', ' ', '|', '_', 'o', '_', '|', ' '} ,
{'_', '_', '_', '_', '_', '_', '_', '_'} };

char horizontalStern[3][8] = { {'_', '_', '_', ' ', ' ', ' ', ' ', ' '} ,
{' ', ' ', ' ', '|', ' ', ' ', ' ', ' '} ,
{'_', '_', '/', ' ', ' ', ' ', ' ', ' '} };

char verticalBow[3][8] = { {' ', ' ', ' ', '/', '\\', ' ', ' ', ' '} ,
{' ', ' ', '/', '_', '_', '\\', ' ', ' '} ,
{' ', '|', ' ', ' ', ' ', ' ', '|', ' '} };

char verticalMiddle[3][8] = { {' ', '|', ' ', ' ', ' ', ' ', '|', ' '} ,
{' ', '|', ' ', ' ', ' ', ' ', '|', ' '} ,
{' ', '|', ' ', ' ', ' ', ' ', '|', ' '} };

char verticalStern[3][8] = { {' ', '|', ' ', '_', '_', ' ', '|', ' '} ,
{' ', '|', '_', '_', '_', '_', '|', ' '} ,
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '} };

char nope[3][8] = { {' ', ' ', '\\', '_', '_', ' / ', ' ', ' '} ,
{' ', '-', '|', '_', '_', '|', '-', ' '} ,
{' ', ' ', '/', ' ', ' ', '\\', ' ', ' '} };

char submarine[3][18] = { {' ', ' ', ' ', ' ', '_', ' ', ' ', ' '} ,
{' ', '_', '_', '/', 'o', '\\', '_', ' '} ,
{'|', '_', '_', '_', '_', '_', '_', '|'} };

coord start;

for (int count = 0; count < 9; count++)
{
if (coordinates.i == count)
start.i = (count * 4) + 1;
}
for (int count = 0; count < 11; count++)
{
if (coordinates.j == count)
start.j = (count * 9) + 1;
}

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 18; j++)
{
if (symbol == '<')
displayGrid[start.i + i][start.j + j] = horizontalBow[i][j];
if (symbol == '=')
displayGrid[start.i + i][start.j + j] = horizontalMiddle[i][j];
if (symbol == '>')
displayGrid[start.i + i][start.j + j] = horizontalStern[i][j];
if (symbol == '^')
displayGrid[start.i + i][start.j + j] = verticalBow[i][j];
if (symbol == '|')
displayGrid[start.i + i][start.j + j] = verticalMiddle[i][j];
if (symbol == '~')
displayGrid[start.i + i][start.j + j] = verticalStern[i][j];
if (symbol == ' ')
displayGrid[start.i + i][start.j + j] = nope[i][j];
if (symbol == 'o')
displayGrid[start.i + i][start.j + j] = submarine[i][j];
}
}

}

void printDisplayGrid(char displayGrid[37][109])
{
for (int i = 0; i < 37; i++)
{
moveRight(1);
for (int j = 0; j < 109; j++)
printf("%c", displayGrid[i][j]);
printf("\n");
}
}

bool isEnd(char grid[9][11], char playerGrid[9][11])
{
bool isEnd = true;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 11; j++)
if (grid[i][j] != playerGrid[i][j])
isEnd = false;
}
return isEnd;
}

int main(int argc, char** argv)
{
if (argc < 2)
{
printf("\tusage: battleship <file>\n");
return -1;
}

FILE* textFile;
textFile = fopen(argv[1], "r");
if (textFile == NULL)
{
puts("Couldn't open file");
return -2;
}

char displayGrid[37][109];
char grid[9][11];
char playGrid[9][11];
coord play;
EVENT retEvent;
COORD mouseCoord;
char trashcan[10];

{
char getGrid[9][25] = { 0 };
bool pass = true;
for (int count = 0; count < 9; count++)
fgets(getGrid[count], 25, textFile);

for (int count = 0; count < 9; count++)
{
if (getGrid[count][24] != '\0' && getGrid[count][24] != '\n')
{
pass = false;
break;
}
}

if (pass == false)
{
puts("the file isn't formatted correctly, please try again");
return -1;
}

for (int i = 0; i < 9; i++)
{
for (int j = 1; j < 22; j += 2)
grid[i][j - (j/2 + 1)] = getGrid[i][j];
}

for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 11; j++)
playGrid[i][j] = ' ';
}

}

{
int windowSize[2];
setupConsole();
moveTo(999, 999);
getCursorPosition(&windowSize[0], &windowSize[1]);
moveTo(0, 0);

while (windowSize[0] < 40)
{
puts("Please, expand the console window!");
moveTo(999, 999);
getCursorPosition(&windowSize[0], &windowSize[1]);
moveTo(0, 0);
}
clearScreenToBottom();
restoreConsoleMode();
}

createDisplayeGrid(displayGrid);
printDisplayGrid(displayGrid);
for (bool playing = true; playing == true;)
{
while (true)
{
retEvent.event.mouseEvent = 0xc00;
retEvent = eventMain();
mouseCoord = retEvent.event.mouseCoord;
if (retEvent.event.mouseEvent == FROM_LEFT_1ST_BUTTON_PRESSED)
break;
}
printf("%i %i\n", mouseCoord.Y, mouseCoord.X);
play = clickToCoordinates(mouseCoord);
printf("%i %i\n", play.i, play.j);
fgets(trashcan, 5, stdin);

modifyDisplayGrid(displayGrid, play, grid[play.i][play.j]);
clearScreenToTop;
moveTo(0, 0);
printDisplayGrid(displayGrid);

if (isEnd(grid, playGrid))
{
puts("Game Over!");
fgets(trashcan, 5, stdin);
break;
}
}


return 0;
Expand Down

0 comments on commit b754c02

Please sign in to comment.