BATTLESHIP game ship placment

4 views (last 30 days)
Tarek Eid
Tarek Eid on 10 Mar 2020
Edited: John D'Errico on 16 Mar 2020
Hi all
im making a battleship game and so far i only have this code
BSgrid=zeros(10);
A=1;
B=2;
C=3;
D=4;
E=5;
F=6;
G=7;
H=8;
I=9;
J=10;
%enter only capital letters!
l=input('enter letter coordinate')
n=input('enter number coordinate')
disp(BSgrid(l,n))
My next step is to actually add the ships( i need 5 ships of lengths 2 3 3 4 5). How do i make them and get the computer to randomly place them on the matrix BSgrid?
Thanks in advance

Accepted Answer

John D'Errico
John D'Errico on 16 Mar 2020
Edited: John D'Errico on 16 Mar 2020
You don't get the compiuter to do anything. You write the program. You are in complete control.
For example, can you randomly choose where one end of a ship will lie? Next, choose a random orientation for the ship. If you know how long the ship is, then you can check to see if the chosen placement for said ship will lie within the bounds of your array, AND if it crosses some other ship. If neither of those things is a problem, then place the ship there, and go on to the next ship.
So where is the problem? Start writing. If a probem seems too complex for you, then break it down into smaller pieces that you can solve. Then put it all together. But you need to do the work.

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!