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 pathships.c
executable file
·128 lines (112 loc) · 3.74 KB
/
ships.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "grids.h"
int check_ships(char **grid, int lines, int columns, int x, int y, int direction, int size)
{
switch (direction)
{
case 1:
if (x < 0 || x + (size - 1) >= columns || y < 0 || y >= lines)
return 1;
break;
case 2:
if (x < 0 || x >= columns || y - (size - 1) < 0 || y >= lines)
return 1;
break;
default:
return 3;
}
for (int i = 0; i < size; ++i)
{
if (direction == 1)
{
if (grid[y][x + i] != ' ')
return 2;
}
else
{
if (grid[y - i][x] != ' ')
return 2;
}
}
return 0;
}
void place_ships(int ship_count, char **ships, int *sizes, char *signs, char **pgrid, char **cgrid, int lines, int columns)
{
for (int i = 0; i < ship_count; ++i)
{
char *ship = ships[i];
int size = sizes[i];
char sign = signs[i];
display_grids(pgrid, cgrid, lines, columns);
int x, y, direction, check;
do
{
x = -1;
y = -1;
do
{
char coordinates[100];
printf("Give the coordinates of the bottom left corner of your %s (of length %d squares): ", ship, size);
scanf("%s", coordinates);
if (strlen(coordinates) != 0 && strlen(coordinates) != 1)
{
x = toupper(coordinates[0]) - 'A';
y = atoi(coordinates + 1) - 1;
}
if (x < 0 || x >= columns || y < 0 || y >= lines)
printf("The position you entered is invalid. It should be between A1 and %c%d.\n", 'A' + (columns - 1), lines);
} while (x < 0 || x >= columns || y < 0 || y >= lines);
do
{
printf("Do you want to place your %s 1. horizontally or 2. vertically? ", ship);
int n = scanf("%d", &direction);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if (direction != 1 && direction != 2)
printf("Invalid direction, please select between horizontal and vertical.\n");
} while (direction != 1 && direction != 2);
check = check_ships(pgrid, lines, columns, x, y, direction, size);
if (check == 1)
printf("Your %s would go outside of the grid. Place it somewhere else.\n", ship);
else if (check == 2)
printf("Your %s would overlap with another ship. Place it somewhere else.\n", ship);
} while (check != 0);
for (int j = 0; j < size; ++j)
{
if (direction == 1)
pgrid[y][x + j] = sign;
else
pgrid[y - j][x] = sign;
}
}
}
void generate_ships(int ship_count, char **ships, int *sizes, char *signs, char **grid, int lines, int columns)
{
for (int i = 0; i < ship_count; ++i)
{
int size = sizes[i];
char sign = signs[i];
int x, y, direction, check;
do
{
direction = rand() % 2 + 1;
x = rand() % (direction == 1? columns - size + 1 : columns);
y = rand() % (direction == 1? lines : lines - size + 1);
check = check_ships(grid, lines, columns, x, y, direction, size);
} while (check != 0);
for (int j = 0; j < size; ++j)
{
if (direction == 1)
grid[y][x + j] = sign;
else
grid[y - j][x] = sign;
}
}
}