Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Incorrect

43Size
Leading solution size is 30.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Fail
 
%%
%Test Suite
%Battleship_000
% Carrier/Battleship/Sub/Cruiser/Destroyer
% 5/4/3/3/2  Qty 1/1/2/2/3
% b 0-Unknown 1-Miss 2-Hit
seed=clock;
seed=1000*seed(6);
rng(seed);
game_over=false;
wins=0; % player wins
bp=zeros(10);
bc=zeros(10);
ships_p=ones(10);
ships_c=ones(10);
ship_vec=[5 4 3 3 3 3 2 2 2]; % Length of ships
tic
% initialize Computer Ships
for ship=1:9
 placed=false;
 while~placed
  idx=randi(100);
  [r c]=ind2sub([10 10],idx);
  dwn_rt=randi(2)-1; % 0-down, 1-right
  try % may go beyond board size
   if dwn_rt==0 % down
    blocked=any(ships_c(r:r+ship_vec(ship)-1,c)==2);
   else % right
    blocked=any(ships_c(r,c:c+ship_vec(ship)-1)==2);
   end
  catch
   blocked=true; % invalid placement
  end
  if ~blocked % No ship conflict
   if dwn_rt==0 % down
    ships_c(r:r+ship_vec(ship)-1,c)=2;
   else % right
    ships_c(r,c:c+ship_vec(ship)-1)=2;
   end
   placed=true;
  end
 end % placed
end % ship
% Initialize Player's ships and first move
  try % for invalid mvP values
   [mvP,ships]=Battleship(bp); %
   bp(mvP(1))=ships_c(mvP(1));
    for ship=1:9
      [r c]=ind2sub([10 10],ships(ship,1));
      dwn_rt=ships(ship,2);
      try % may go beyond board size
       if dwn_rt==0 % down
        blocked=any(ships_p(r:r+ship_vec(ship)-1,c)==2);
       else % right
        blocked=any(ships_p(r,c:c+ship_vec(ship)-1)==2);
       end
      catch
       blocked=true; % invalid placement
      end
      if ~blocked % No ship conflict
       if dwn_rt==0 % down
        ships_p(r:r+ship_vec(ship)-1,c)=2;
       else % right
        ships_p(r,c:c+ship_vec(ship)-1)=2;
       end
      end
    end % ship
   if sum(ships_p(:))~=127 % Expect 127 board if all placed
    fprintf('Invalid Ship placement - Game over\n');
    ships
    ships_p
    game_over=true;
   end
  catch
   fprintf('Invalid first respone - Game over\n');
   mvP
   ships
   game_over=true;
  end
% Main Game Loop
 while ~game_over
  % Computer move
  % Author: Richard Z
  % Date: 2012/06/03
  % Battleship_010 bot: Random of Zeros
   avail=find(bc==0);
   mvC=avail(randi(length(avail)));
  %End Battleship bot: Random of Zeros
  bc(mvC)=ships_p(mvC); % Hit=2, Miss=1
  if length(find(bc==2))>=27 % Computer Wins
   break;
  end
  % Player's Second move and thereafter
  try % for invalid mvP values
   [mvP,ships]=Battleship(bp); %
   bp(mvP(1))=ships_c(mvP(1)); % Hit=2, Miss=1
  catch
   fprintf('Ignoring Illegal move %i \n',mvP(1));
  end
  if length(find(bp==2))>=27 % All ships sunk
   wins=1;
   break;
  end
 end % While ~game_over
toc
 % Player must win to Pass
 assert(isequal(wins,1))
%Pass=1;
%assert(isequal(Pass,1));
 bc
 bp
 wins
Error: Undefined function or variable "mvP".