-
Notifications
You must be signed in to change notification settings - Fork 0
/
bTests.m
206 lines (165 loc) · 7.06 KB
/
bTests.m
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
clear; clc; close all; format shortG; format compact;
%verifyBoard: Runs verification on all the boards in the "Test Boards"
%folder, and outputs the legal result for each (correct or wrong).
global boardSize iShip lShip tShip;
bList = dir(['Test Boards','\*.txt']);
bLength = length(bList);
bNames = cell(1,bLength);
for kk = 1:bLength
bNames{kk} = bList(kk).name;
end
for kk = 1:bLength
board = load(['Test Boards\',bNames{kk}]);
% verifyBoard()
%Found Ships
%Set up variable for "sightings"
fShip = zeros(1,length(iShip));
%Keep track of sightings in rows
rFind = zeros(1,length(iShip));
% Traverse the Board
%Look through for completed ships in the ROWS
for ii = 1:boardSize
jj = 1;
while ( jj <= boardSize )
%Found something
if board(ii,jj) ~= 0
iCS = board(ii,jj);
lCS = lShip(iCS);
%fprintf('Discovery!\n %i at b(%i,%i).\n It''s a %s (length of %i).\n',iCS,ii,jj,tShip{iCS},lCS);
%return;
%Has it been found already
if ( fShip(iCS) )
%fprintf('Not new!\n Ship type has already been found!\n');
else
%fShip(iCS) = 1;
%fprintf('New!\n Ship type has been added to seen!\n');
end
if ( fShip(iCS) == 0 )
%Check if space in row is sufficient
aSpace = jj + lCS - 1;
if ( aSpace == 10)
%fprintf('Space:\n There is exactly enough space for the ship!\n');
aLength = 1;
elseif ( aSpace < 10 )
%fprintf('Space:\n There is more than enough space open!\n');
aLength = 0;
elseif ( aSpace > 10 )
%fprintf('Space:\n There is NOT enough space for the ship!\n');
aLength = -1;
end
%Check if ship is in fact in row
switch aLength
case -1
%fprintf('error\n');
case 0
sTemp = board(ii, jj:jj + lCS);
fTemp = ( iCS == sTemp );
if all(fTemp)
%fprintf('Wrong!\n Ship is longer than it should be!\n');
elseif all(fTemp(1:end-1))
%fprintf('Correct!\n Entire ship was found.\n');
fShip(iCS) = fShip(iCS) + 1;
rFind(iCS) = rFind(iCS) + 1;
%Move to next empty space
jj = jj + lCS - 1;
end
case 1
sTemp = board(ii, jj:jj + lCS - 1);
fTemp = ( iCS == sTemp );
if all(fTemp)
%fprintf('Correct!\n Entire ship was found.\n');
fShip(iCS) = fShip(iCS) + 1;
rFind(iCS) = rFind(iCS) + 1;
%Move to next empty space
jj = jj + lCS - 1;
end
end
%jj = jj + lCS;
elseif ( fShip(iCS) > 1 )
fShip(iCS) = fShip(iCS) + 1;
%fprintf('Wrong!\n Too many ships of this type!\n');
%return;
end
%fprintf('\n');
end
%Move to next empty space
jj = jj + 1;
end
end
%Look through for completed ships in the COLS
for ii = 1:boardSize
jj = 1;
while ( jj <= boardSize )
%Found something
if board(jj,ii) ~= 0
iCS = board(jj,ii);
lCS = lShip(iCS);
%fprintf('Discovery!\n %i at b(%i,%i).\n It''s a %s (length of %i).\n',iCS,jj,ii,tShip{iCS},lCS);
%return;
%Has it been found already
if ( fShip(iCS) )
%fprintf('Not new!\n Ship type has already been found!\n');
else
%fShip(iCS) = 1;
%fprintf('New!\n Ship type has been added to seen!\n');
end
if ( fShip(iCS) == 0 )
%Check if space in row is sufficient
aSpace = jj + lCS - 1;
if ( aSpace == 10)
%fprintf('Space:\n There is exactly enough space for the ship!\n');
aLength = 1;
elseif ( aSpace < 10 )
%fprintf('Space:\n There is more than enough space open!\n');
aLength = 0;
elseif ( aSpace > 10 )
%fprintf('Space:\n There is NOT enough space for the ship!\n');
aLength = -1;
end
%Check if ship is in fact in row
switch aLength
case -1
%fprintf('error\n');
case 0
sTemp = board(jj:jj + lCS, ii)';
fTemp = ( iCS == sTemp );
if all(fTemp)
%fprintf('Wrong!\n Ship is longer than it should be!.\n');
elseif all(fTemp(1:end-1))
%fprintf('Correct!\n Entire ship was found.\n');
fShip(iCS) = fShip(iCS) + 1;
rFind(iCS) = rFind(iCS) + 1;
end
case 1
sTemp = board(jj:jj + lCS - 1, ii)';
fTemp = ( iCS == sTemp );
if all(fTemp)
%fprintf('Correct!\n Entire ship was found.\n');
fShip(iCS) = fShip(iCS) + 1;
rFind(iCS) = rFind(iCS) + 1;
end
end
%Move to next empty space
jj = jj + lCS - 1;
elseif ( ( fShip(iCS) == 1 ) && ( rFind(iCS) == 1 ) )
%fprintf('Already found!\n Ship was found during the row sweep.\n');
elseif ( fShip(iCS) > 1 )
fShip(iCS) = fShip(iCS) + 1;
%r = 0;
%fprintf('Wrong!\n Too many ships of this type!\n');
end
%fprintf('\n');
end
jj = jj + 1;
end
end
r = all(fShip == 1);
fprintf('RESULT FOR BOARD %i (%s):\n',kk,bNames{kk});
if ( r )
fprintf(' CORRECT! All ships were found.\n');
else
fprintf(' WRONG! Board does not conform to the rules.\n');
end
fprintf('\n');
end
clear;