number placment for battleship game

1 view (last 30 days)
Tarek Eid
Tarek Eid on 18 Mar 2020
Commented: Rik on 18 Mar 2020
Hi all
Im making a Battleship game on Matlab and this is my code so far
BSgrid=zeros(10);
BSgrid(randi(numel(BSgrid)))=1
BSgrid(randi(numel(BSgrid)))=2
BSgrid(randi(numel(BSgrid)))=3
BSgrid(randi(numel(BSgrid)))=4
BSgrid(randi(numel(BSgrid)))=5
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')
The way i want to place the ships is by adding numbers that equal the ships length. For example, if the ships length is 4 units, Then u want to place 4 fours on the grid besides each other and diagonal to each other at random when the code runs
I know how to place a single number on the matrix as seen in the code BSgrid(randi(numel(BSgrid)))=4 but i dont know how to add the other fours besides it randomly as well
Can anyone help me with this?
thanks in advance
  3 Comments
Tarek Eid
Tarek Eid on 18 Mar 2020
Edited: Tarek Eid on 18 Mar 2020
Thanks for your advice Rik
Note that I am a complete beginner on Matlab and thus if i knew how to do the task myself, I would have done it already. Furthermore, I have searched for long hours on how to do this task on the internet but with no luck. I only come here to ask as a last resort
Im asking this question whilst including my code and what I already did and what im missing(which is what the links you sent me tell me to do)Im also not asking you to do my entire homwork assignment for me(to create a batlleship game)I just need help with this task. Telling me to just break it up to small tasks is basic advice and ive already done that on a seperate document. But i just need help on this task, which is why im asking for it here
Finally, I apologize if my question seemed like a (doitforme) kind of question. I should have worded it differently. I never wanted you to write the code for me so that i can copy and paste it on matlab file.
Can i atleast get a hint that may assist me in solving this question?
Rik
Rik on 18 Mar 2020
I assume those BSgrid(randi(numel(BSgrid)))=1 lines are intended to put in the ships. As John suggested, this is a multi-step process.
  1. generate a random coordinate for the bow (or stern) of the ship
  2. generate a direction for the ship (up, down, left, or right)
  3. check if this combination of coordinate and direction will fit on your board and if it doesn't overlap an already placed ship
If it doesn't fit, undo everything and go back to step 1, otherwise you can mark all coordinates with the ship index.
You can use find to convert a logical matrix to row and column indices, and you can use sub2ind and ind2sub to convert back and forth between subscripts and linear indices.
%another hint:
L=false(5,5);L(randi(numel(L)))=true;
[r,c]=find(L);
r=r+(0:3);
L(r,c)=true

Sign in to comment.

Answers (0)

Categories

Find more on Strategy & Logic in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!