Cody

Solution 188018

Submitted on 8 Jan 2013 by Tomasz
  • Size: 30
  • This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
%% %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
mv = 1 mv = 2 mv = 3 mv = 4 mv = 5 mv = 6 mv = 7 mv = 8 mv = 9 mv = 10 mv = 11 mv = 12 mv = 13 mv = 14 mv = 15 mv = 16 mv = 17 mv = 18 mv = 19 mv = 20 mv = 21 mv = 22 mv = 23 mv = 24 mv = 25 mv = 26 mv = 27 mv = 28 mv = 29 mv = 30 mv = 31 mv = 32 mv = 33 mv = 34 mv = 35 mv = 36 mv = 37 mv = 38 mv = 39 mv = 40 mv = 41 mv = 42 mv = 43 mv = 44 mv = 45 mv = 46 mv = 47 mv = 48 mv = 49 mv = 50 mv = 51 mv = 52 mv = 53 mv = 54 mv = 55 mv = 56 mv = 57 mv = 58 mv = 59 mv = 60 mv = 61 mv = 62 mv = 63 mv = 64 mv = 65 mv = 66 mv = 67 mv = 68 mv = 69 mv = 70 mv = 71 mv = 72 mv = 73 mv = 74 mv = 75 mv = 76 mv = 77 mv = 78 mv = 79 mv = 80 mv = 81 mv = 82 mv = 83 mv = 84 mv = 85 mv = 86 mv = 87 mv = 88 mv = 89 Elapsed time is 0.039737 seconds. bc = 2 0 2 2 2 1 1 1 1 1 2 2 2 0 1 1 1 0 1 1 2 2 2 1 1 0 1 1 1 1 0 2 2 0 1 1 1 1 1 0 2 2 2 0 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 2 2 1 0 1 1 1 1 1 1 2 2 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 1 bp = 2 1 1 1 2 1 1 1 2 0 2 1 1 1 2 1 1 1 2 0 2 1 1 1 2 1 1 1 2 0 1 1 1 1 2 2 2 2 2 0 1 1 1 1 1 1 1 1 2 0 2 1 1 1 2 1 1 1 1 0 2 1 1 1 2 1 1 1 1 0 1 1 1 1 2 1 1 1 1 0 1 1 1 1 1 1 2 2 2 0 1 1 2 2 2 2 1 1 0 0 wins = 1