Azzera filtri
Azzera filtri

Please help me perfect code

4 visualizzazioni (ultimi 30 giorni)
Kevin Bafe
Kevin Bafe il 15 Apr 2019
Hello Matlab Gods.
I am curently in the middle of a 'Battleship' homework project i am tryin to take to the next level.
I have a matrix,
target = [0 0 0 0 0 2 2 0 0 0;0 0 1 0 0 0 0 0 0 3;0 4 4 4 4 0 0 0 0 3;5 0 0 6 0 0 0 0 0 3;5 0 0 0 6 0 0 0 0 0;5 0 0 0 0 6 0 0 0 0;5 0 0 0 0 0 6 0 0 0;5 0 0 0 0 0 0 0 6 0;0 0 0 0 0 0 0 0 6 0;0 0 0 0 0 0 0 0 0 0];
How would i go about 3d plotting this x,y,z
where x and y are the position coordenates and z is the number on the matrix, ie, the bigger number the deeper the z value.
I would also like to display my misses interms of the z slope to the ship ( not sure if that makes sense)
If i miss by one opsition i would like a coulour gardient to be shown on 'game' plot.
Any help would be greatly appreciated.
fyi, these tips wont gain me a higer grader as i have already completed required tasks, this is just to make it the best i can interms of my ideas. i will post the code below.
Ps, if you notice anything that can be improved upon, please let me know, i will be sure to share the knowledge!
Thanks!!
Kevin
% I hope it was ok to make my own matrix?? i wants to have controle over
% the diffeent ships.
clear
clc
N=15;
target = [0 0 0 0 0 2 2 0 0 0;0 0 1 0 0 0 0 0 0 3;0 4 4 4 4 0 0 0 0 3;5 0 0 6 0 0 0 0 0 3;5 0 0 0 6 0 0 0 0 0;5 0 0 0 0 6 0 0 0 0;5 0 0 0 0 0 6 0 0 0;5 0 0 0 0 0 0 0 6 0;0 0 0 0 0 0 0 0 6 0;0 0 0 0 0 0 0 0 0 0];
Z = target;
X=Z-2;
Y=Z-4;
surf(X,Y,Z)
subplot(2,1,1)
imagesc(surf)
colormap(winter(100));
colorbar;
title('Battle zone')
caxis([0 6])
%MyColorMap = winter(6);
w=surf(x,y,Z);
[r,c] = find((h.XData==0) & (h.YData==0));
disp(h.ZData(r,c))
surf(x,y,Z)
subplot(2,1,2)
game=zeros(size(surf));
imagesc(game)
colormap(winter(100));
colorbar;
title('Battle zone')
caxis([0 6])
counter=0;
shots=15;
%ship names and values
Cruiser=2;
Submarine=3;
Destroyer=4;
Frigate=5;
Aircraftcarrier=6;
%little intro there to set the scene
y= {'yes','ok','sure','yea'};
u = {'no','nope','nah'};
breif= sprintf('Good evening Captin, a member of our alpha flet have found themselves in deep water,\nwe need an experienced shooter to take over the gunning system and eleminate enemy vessels, this is a dangerous mission, you might not make it back.');
disp(breif)
playoption = input('Do you exept the mission?','s');
if contains(playoption,u)
f=input('\nAre you really not willing to help your comrades captin?','s');
if contains(f,y)
buzzkill = sprintf('You make me sick!');
disp(buzzkill)
return;
end
elseif contains(playoption,y)
Captin=sprintf('I always knew you had it in you, here''s what we are up against!\n');
disp(Captin)
else
disp('Answer me properly captin')
return;
end
Ship = {'Cruiser';'Submarine';'Destroyer'; 'Frigate';'Aircraft carrier'};
Size = [2;3;4;5;6];
T = table(Ship,Size);
disp(T)
ammo=sprintf('Did I mention the ship only has 15 missiles?');
civi=sprintf('Oh, and try not to hit any civilians while you''re at it\n');
disp(civi)
% a few hit strings
hit={'Booom you hit their %s they didnt see that one coming', 'you hit their %s ',' Right on the %s thats some fiine shootin'' captin!','They will be sleepin'' with he fishies soon cheif, you hit their %s'};
numOfhit=length(hit);
Z = randi([1 numOfhit],1);
Hits = sprintf(hit{Z});
% a few miss strings
miss={'miss, i told you to lay off that pirate juice!', 'Miss,do you have two glass eyes?','Miss, my god captin, pull yourself together!','Miss, they are going to get us captin!'};
numOfmiss=length(miss);
v = randi([1 numOfmiss],1);
Miss = sprintf(miss{v});
n=15;
%gameplay
while sum(sum(target))>1
x = input('Enter your x coordinates');
while x > 10
x=input('you''re way out, the cordinates both must be under 10 ,guess again.');
end
y = input('Enter your y coordinates');
while y > 10
y=input('you''re way out, the cordinates both must be under 10, guess again');
end
shots=shots-1;
counter=counter+1;
if target(y,x)==1
Z=1;
game(y,x)=10;
target(y,x)=0;
myColorMap(1,:) = [0, 0, 1];
disp('You just shot a civillian fishing trawler, you better lay low for a while')
return
elseif target(y,x)==2
game(y,x)=2;
Z=2;
target(y,x)=0;
name=('Cruiser');
Hit = sprintf(hit{Z},name);
bingo=nnz(target==2);
if any(target(:) == Cruiser)
disp(Hit)
distroy = sprintf('Only %d more shots and the %s will sink',bingo,name);
disp(distroy)
ax = gca;
ax.XColor = 'red';
else
disp('The Criser has been shot down!')
end
elseif target(y,x)==3
Z=3;
game(y,x)=3;
target(y,x)=0;
name = ('Submarine');
Hit = sprintf(hit{Z},name);
bingo=nnz(target==3);
if any(target(:) == Submarine)
disp(Hit)
distroy = sprintf('Only %d more shots and the %s will sink',bingo,name);
disp(distroy)
else
disp('The Submarine has been distroied!')
end
elseif target(y,x)==4
Z=4;
game(y,x)=4;
target(y,x)=0;
name =('Destroyer');
Hit = sprintf(hit{Z},name);
bingo = nnz(target==4);
if any(target(:) == Destroyer)
disp(Hit)
distroy = sprintf('Only %d more shots and the %s will sink',bingo,name);
disp(distroy)
else
disp('The destroyer has be netralised!')
end
elseif target(y,x)==5
Z=5;
game(y,x)=5;
target(y,x)=0;
name = ('Frigate');
Hit = sprintf(hit{Z},name);
bingo=nnz(target==5);
if any(target(:) == Frigate)
disp(Hit)
distroy = sprintf('Only %d more shots and the %s will sink',bingo,name);
disp(distroy)
else
disp('The Frigate has been shot down!')
end
elseif target(y,x)==6
Z=6;
game(y,x)=6;
target(y,x)=0;
name = ('Aircraft carrier');
Hit = sprintf(hit{Z},name);
bingo=nnz(target==6);
if any(target(:) == Aircraftcarrier)
disp(Hit)
distroy = sprintf('Only %d more shots and the %s will sink',bingo,name);
disp(distroy)
else
disp('The Aircrafter carrier has been shot down!')
end
else
game(y,x)=0;
disp(Miss)
if counter>=10
ammole=sprintf('you have %d missiles left',shots);
disp(ammole)
elseif counter==15
disp('You ran out of ammo!')
return
end
end
subplot(2,1,2);
imagesc(game)
colorbar
title('Battle zone')
caxis([0 6])
end
disp('Well done, you defeated the enemy without any civilian casualities ')

Risposte (0)

Categorie

Scopri di più su Historical Contests in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by