This repository has been archived by the owner on Dec 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·91 lines (75 loc) · 2.83 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "utils.h"
#include "menus.h"
#include "grids.h"
#include "ships.h"
#include "shooting.h"
#include "constants.h"
int main()
{
srand(time(NULL));
int lines = DEFAULT_LINES, columns = DEFAULT_COLUMNS, ship_count = DEFAULT_SHIP_COUNT, turn = 0, X_shooting = 0, Y_shooting = 0;
bool shooting = false;
char **ships = (char **) malloc(sizeof(char *) * ship_count);
int *sizes = (int *) malloc(sizeof(int) * ship_count);
char *signs = (char *) malloc(sizeof(char) * ship_count);
for (int i = 0; i < ship_count; ++i)
{
ships[i] = (char *) malloc(sizeof(char) * 100);
for (int j = 0; j < strlen(DEFAULT_SHIPS[i]) + 1; ++j)
ships[i][j] = DEFAULT_SHIPS[i][j];
sizes[i] = DEFAULT_SIZES[i];
signs[i] = DEFAULT_SIGNS[i];
}
char **pgrid, **cgrid;
create_grids(&pgrid, &cgrid, lines, columns);
int screen;
do
{
screen = menu(&ship_count, &ships, &sizes, &signs, &pgrid, &cgrid, &lines, &columns, &turn, &shooting, &X_shooting, &Y_shooting);
switch (screen)
{
case 1:
{
place_ships(ship_count, ships, sizes, signs, pgrid, cgrid, lines, columns);
generate_ships(ship_count, ships, sizes, signs, cgrid, lines, columns);
display_grids(pgrid, cgrid, lines, columns);
int month, day, year;
do
{
month = 0;
day = 0;
year = 0;
printf("When did you sail for the last time? ");
int n = scanf("%d/%d/%d", &month, &day, &year);
if (n < 3)
{
char null[100];
scanf("%s", null);
printf("Invalid date, please enter one in the format MM/DD/YYYY.\n");
} else if (day < 1 || day > get_days_in_month(month, year))
printf("This date doesn't exist, please enter a valid one.\n");
} while (day < 1 || day > get_days_in_month(month, year));
printf("Well, the computer never sailed... So you will start first.\n");
turn = 0;
screen = 2;
}
case 2:
{
screen = shooting_stage(ship_count, ships, sizes, signs, pgrid, cgrid, lines, columns, turn, shooting, X_shooting, Y_shooting);
free_grids(pgrid, cgrid, lines, columns);
create_grids(&pgrid, &cgrid, lines, columns);
turn = 0;
shooting = false;
X_shooting = 0;
Y_shooting = 0;
}
break;
}
} while (screen != 0);
return 0;
}