-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayGame.m
231 lines (209 loc) · 7.79 KB
/
displayGame.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
function displayGame( playerFleet, opponentFleet, playerHits, playerMisses, opponentHits, opponentMisses, turn )
%displayGame: Creates visual representation of the overall game.
% displayGame( playerFleet, opponentFleet, playerHits, playerMisses,
% opponentHits, opponentMisses, turn ) plots the locations of the ships
% for both sides as well as hit and miss markers for both sides.
%
% Input Arguments:
% playerFleet = The board and ships of the player
% opponentFleet = The board and ships of the opponent
% playerHits = The positions of the player's hits on the opponent
% playerMisses = The position of the player's misses on the opponent
% opponentHits = The positions of the opponent's hits on the player
% opponentMisses = The position of the opponent's misses on the player
% turn = The current turn number
% Prepare Window if turn 0
if ( ~turn )
setupBoard(0);
end
% Draw Opponent Side
if ( ~turn )
% Prepare opponent side if turn 0
setupBoard(1);
else
% Update opponent side otherwise
subplot(2,1,1);
% Clear any excess data
cla;
end
% Draw the grid, visible ships, and marks
hold on;
drawGrid();
drawVisible(opponentFleet,playerHits);
drawMarks(playerHits, playerMisses);
hold off;
% Draw Player Side
if ( ~turn )
% Prepare player side if turn 0
setupBoard(2);
else
% Update player side otherwise
subplot(2,1,2);
% Clear any excess data
cla;
end
% Draw the grid, player ships, and marks
hold on;
drawGrid();
drawFleet(playerFleet);
drawMarks(opponentHits, opponentMisses);
hold off;
end
function setupBoard(num)
%setupBoard: Prepares the figure window and the relevant subplots according
%to the num (functions as mode)
% Prepare the figure window
if ( num == 0 )
% Format the figure (color, position, shape, title)
figure('Color',[0.8 0.8 0.8], 'menu','none',...
'position',[50 100 360 720],...
'name','Battleship','NumberTitle','off');
% Prepare the proper subplot
else
subplot(2,1,num);
% Format the plot (color, titles, axes, shape)
set(gca,'Color',[ 26/255 102/255 153/255 ]);
if ( num == 1 )
title('Opponent Board');
elseif ( num == 2)
title('Player Board');
end
set(gca,'XTick',0.5:10.5,...
'XTickLabel',{'1','2','3','4','5','6','7','8','9','10'},...
'XAxisLocation','top');
set(gca,'YTick',0.5:10.5,....
'YTickLabel',{'A','B','C','D','E','F','G','H','I','J'},...
'YDir','reverse');
%set(gca,'XTick',0.5:10.5,'XAxisLocation','top');
%set(gca,'YTick',0.5:10.5,'YDir','reverse');
axis([ 0 10 0 10 ]);
axis square;
box on;
end
end
function drawGrid()
%drawGrid: Draws the gridlines
for ii = 1:9
plot( ii*ones(1,100), linspace(0,10,100),...
'-','color',[ 62/255 162/255 229/255 ]);
plot( linspace(0,10,100), ii*ones(1,100),...
'-','color',[ 62/255 162/255 229/255 ]);
end
end
function drawMarks(h,m)
%drawMarks: Draws the marks (hits and misses) according to their locations
% Global variables imported
global boardSize;
% Loop across the rows..
for ii = 1:boardSize
% Loop across the cols..
for jj = 1:boardSize
% If a marker is found, then plot according marker
if ( h(ii,jj) )
%hit
plot( jj-0.5, ii-0.5, 'rx', 'markersize', 20);
elseif ( m(ii,jj) )
%miss
plot( jj-0.5, ii-0.5, 'wx', 'markersize', 20);
end
end
end
end
function drawVisible(board, hits)
%drawVisible: Draws the opponents ships only if sunk
% Global variables imported
global boardSize lShip;
% Loop across the rows..
for ii = 1:boardSize
% Loop across the cols..
jj = 1;
while ( jj <= boardSize )
% If a ship segment is found, extract relevant information
if ( board(ii,jj) ~= 0 )
%current ship type
iCS = board(ii,jj);
%length of current ship
lCS = lShip(iCS);
% If next segment in the row contains the ship..
if ( jj+lCS-1 <= boardSize ) && ( board(ii,jj+lCS-1) == iCS )
% ..extract position of the ship to tempoary board 1
tempB1 = zeros(boardSize);
tempB1(ii,jj:jj+lCS-1) = 1;
% ..extract position of corresponding hits to tempoary
% board 2
tempB2 = zeros(boardSize);
tempB2(ii,jj:jj+lCS-1) = hits(ii,jj:jj+lCS-1);
% ..if hits cover the length of the ship, ship has sunk
if ( isequal(tempB1,tempB2) )
% ..display ship accordingly
drawFleet(tempB2)
end
% Alter iterator to jump to the next unseen space
% (note: no need to look through the next few spaces as
% they will register false postives and utilize
% processor as the next spaces have just been checked,
% regardless of if ship was determined sunk or not)
jj = jj + lCS - 1;
end
end
% Update col variable
jj = jj + 1;
end
end
% Loop across the cols..
for ii = 1:boardSize
% Loop across the rows..
jj = 1;
while ( jj <= boardSize )
% If a ship segment is found, extract relevant information
if ( board(jj,ii) ~= 0 )
%current ship type
iCS = board(jj,ii);
%length of current ship
lCS = lShip(iCS);
% If next segment in the col contains the ship..
if ( jj+lCS-1 <= boardSize ) && ( board(jj+lCS-1,ii) == iCS )
% ..extract position of the ship to tempoary board 1
tempB1 = zeros(boardSize);
tempB1(jj:jj+lCS-1,ii) = 1;
% ..extract position of corresponding hits to tempoary
% board 2
tempB2 = zeros(boardSize);
tempB2(jj:jj+lCS-1,ii) = hits(jj:jj+lCS-1,ii);
% ..if hits cover the length of the ship, ship has sunk
if ( isequal(tempB1,tempB2) )
% ..display ship accordingly
drawFleet(tempB2)
end
% Alter iterator to jump to the next unseen space
% (note: no need to look through the next few spaces as
% they will register false postives and utilize
% processor as the next spaces have just been checked,
% regardless of if ship was determined sunk or not)
jj = jj + lCS - 1;
end
end
% Update row variable
jj = jj + 1;
end
end
end
function drawFleet(board)
%drawFleet: Draws the ships according to their location in the data
% Global variables imported
global boardSize;
% Loop across the rows..
for ii = 1:boardSize
% Loop across the cols..
jj = 1;
while ( jj <= boardSize )
% If a ship segment is found, then plot according marker
if board(ii,jj)
plot( jj - 0.5, ii - 0.5,...
'ks', 'markersize', 20, 'markerfacecolor','k');
end
% Update col variable
jj = jj + 1;
end
end
end