I need to create a code to play the game Battleship using matlab. As of right no
ID: 3850413 • Letter: I
Question
I need to create a code to play the game Battleship using matlab. As of right now I have been able to create a computer generated board that is for the computer to use and creates random ships everywhere on the 10x10 board. the only problem I have is I can't figure out how to create the user board in which they use inputs to place their ships onto the board. this is my code so far... I need to make it so that the computer board ships don't overlap and I need to make the user board and all of the game functions.
Compboard=zeros(10);
A=1;
B=2;
C=3;
D=4;
E=5;
F=6;
G=7;
H=8;
I=9;
J=10;
disp('32=sub , 31=cruiser, 4=battleship, 2=destroyer, 5=aircraft carrier')
%--------Placing Ships Comp--------%
%-----destroyer-------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 1) = 2;
sum(Dcol)==0;
%-------aircraft carrier----------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 4) = 5;
sum(Dcol)==0;
%--------BattleShip----------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 3) = 4;
sum(Dcol)==0;
%---Submarine----------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 3) = 32;
sum(Dcol)==0;
%---------------Cruiser-----------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 3) = 31
sum(Dcol)==0;
Explanation / Answer
The code is given below :
Compboard=zeros(10);
A=1;
B=2;
C=3;
D=4;
E=5;
F=6;
G=7;
H=8;
I=9;
J=10;
disp('32=sub , 31=cruiser, 4=battleship, 2=destroyer, 5=aircraft carrier')
%--------Placing Ships Comp--------%
%-----destroyer-------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 1) = 2;
sum(Dcol)==0;
%-------aircraft carrier----------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 4) = 5;
sum(Dcol)==0;
%--------BattleShip----------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 3) = 4;
sum(Dcol)==0;
%---Submarine----------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 3) = 32;
sum(Dcol)==0;
%---------------Cruiser-----------%
ALPHArange = 'A':'J';
Drow = ALPHArange(randi(numel(ALPHArange)));
Dcol = randi(9,1,1);
idx = find(ismember(ALPHArange, Drow));
Compboard(idx, Dcol:Dcol + 3) = 31
sum(Dcol)==0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.