{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2025-12-14T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":740,"title":"Battleship_010 : (TM)  Classic Game - Methodical Bot (100 move max)","description":"The Classic Battleship(TM) game implemented between a computer bot and a player's bot.\r\n\r\n\u003chttp://en.wikipedia.org/wiki/Battleship_%28game%29 Battleship\u003e\r\n\r\nYou and your opponent have 9 ships of various sizes.\r\n\r\nThese Ships/Sizes/Qty are Carrier-5, Battleship-4, Submarine-3(qty 2), Cruiser-3(qty 2), Destroyer-2(qty-3).\r\n\r\nThe board is 10x10. (index 1-100)\r\nUnknown=0, Miss=1, Hit=2\r\n\r\n*The Play:*\r\n\r\nPlayer places his ships on the board. Ships may not overlap but may touch.\r\n\r\nShips array is [9,2] where 1 is the Carrier and 9 is a Destroyer. \r\n\r\n[start_idx, orientation; start_idx, orientation...]. An orientation of 0 is Down and a 1 is Right. \r\n\r\nShips[1 1;...] places the Carrier in cells [1 11 22 33 44]\r\n\r\nPlayer takes a shot on the board - idx 1:100.\r\n\r\nThe computer bot will take a shot if he has any ships remaining.\r\n\r\nThe player will see an updated board for his next shot if he has any ships remaining.\r\n\r\n*Pass: Win*\r\n\r\nBattleship_bot_010 randomly picks from Zero sectors. \r\n","description_html":"\u003cp\u003eThe Classic Battleship™ game implemented between a computer bot and a player's bot.\u003c/p\u003e\u003cp\u003e\u003ca href=\"http://en.wikipedia.org/wiki/Battleship_%28game%29\"\u003eBattleship\u003c/a\u003e\u003c/p\u003e\u003cp\u003eYou and your opponent have 9 ships of various sizes.\u003c/p\u003e\u003cp\u003eThese Ships/Sizes/Qty are Carrier-5, Battleship-4, Submarine-3(qty 2), Cruiser-3(qty 2), Destroyer-2(qty-3).\u003c/p\u003e\u003cp\u003eThe board is 10x10. (index 1-100)\r\nUnknown=0, Miss=1, Hit=2\u003c/p\u003e\u003cp\u003e\u003cb\u003eThe Play:\u003c/b\u003e\u003c/p\u003e\u003cp\u003ePlayer places his ships on the board. Ships may not overlap but may touch.\u003c/p\u003e\u003cp\u003eShips array is [9,2] where 1 is the Carrier and 9 is a Destroyer.\u003c/p\u003e\u003cp\u003e[start_idx, orientation; start_idx, orientation...]. An orientation of 0 is Down and a 1 is Right.\u003c/p\u003e\u003cp\u003eShips[1 1;...] places the Carrier in cells [1 11 22 33 44]\u003c/p\u003e\u003cp\u003ePlayer takes a shot on the board - idx 1:100.\u003c/p\u003e\u003cp\u003eThe computer bot will take a shot if he has any ships remaining.\u003c/p\u003e\u003cp\u003eThe player will see an updated board for his next shot if he has any ships remaining.\u003c/p\u003e\u003cp\u003e\u003cb\u003ePass: Win\u003c/b\u003e\u003c/p\u003e\u003cp\u003eBattleship_bot_010 randomly picks from Zero sectors.\u003c/p\u003e","function_template":"function [mv,ships] = Battleship(b)\r\n % b is 10x10; idx 1:100\r\n % output mv is idx 1 thru 100\r\n % b: 0-open ocean; 1-miss shot, 2-Hit\r\n % ships: Placement of player ships\r\n % ships are initialized on first turn sum(b(:))==0\r\n % Place 9 ships [idx1 0/1;idx2 0/1;...idx9 0/1]\r\n % Ship lengths : 5/4/3/3/3/3/2/2/2\r\n % Ship orientation : 0-Vert Down; 1-Horiz Right \r\n % ships=[1 1;2 1;...8 1;99 0] \r\n % Places 8 ships on Left edge, one down in bottom rt corner\r\n % ships may not overlap - Loss\r\n \r\n% Need ships each call or will Lose; Ships placed only on first move\r\n  ships=[1 1;2 1;3 1;4 1;5 1;6 1;7 1;8 1;99 0]; \r\n \r\n % randomly pick an open spot (same as Battleship_bot_000)\r\n mv=randi(100);\r\n \r\nend % Battleship\r\n","test_suite":"%%\r\n%Test Suite\r\n%Battleship_000\r\n% Carrier/Battleship/Sub/Cruiser/Destroyer\r\n% 5/4/3/3/2  Qty 1/1/2/2/3\r\n% b 0-Unknown 1-Miss 2-Hit\r\nseed=clock;\r\nseed=1000*seed(6);\r\nrng(seed);\r\ngame_over=false;\r\nwins=0; % player wins\r\nbp=zeros(10);\r\nbc=zeros(10);\r\nships_p=ones(10);\r\nships_c=ones(10);\r\nship_vec=[5 4 3 3 3 3 2 2 2]; % Length of ships\r\n\r\ntic\r\n% initialize Computer Ships\r\nfor ship=1:9\r\n placed=false;\r\n while~placed\r\n  idx=randi(100);\r\n  [r c]=ind2sub([10 10],idx);\r\n  dwn_rt=randi(2)-1; % 0-down, 1-right\r\n  try % may go beyond board size\r\n   if dwn_rt==0 % down\r\n    blocked=any(ships_c(r:r+ship_vec(ship)-1,c)==2);\r\n   else % right\r\n    blocked=any(ships_c(r,c:c+ship_vec(ship)-1)==2);\r\n   end\r\n  catch\r\n   blocked=true; % invalid placement\r\n  end\r\n  if ~blocked % No ship conflict\r\n   if dwn_rt==0 % down\r\n    ships_c(r:r+ship_vec(ship)-1,c)=2;\r\n   else % right\r\n    ships_c(r,c:c+ship_vec(ship)-1)=2;\r\n   end\r\n   placed=true;\r\n  end \r\n end % placed\r\nend % ship\r\n \r\n% Initialize Player's ships and first move\r\n  try % for invalid mvP values\r\n   [mvP,ships]=Battleship(bp); % \r\n   bp(mvP(1))=ships_c(mvP(1));\r\n   \r\n    for ship=1:9\r\n      [r c]=ind2sub([10 10],ships(ship,1));\r\n      dwn_rt=ships(ship,2); \r\n      try % may go beyond board size\r\n       if dwn_rt==0 % down\r\n        blocked=any(ships_p(r:r+ship_vec(ship)-1,c)==2);\r\n       else % right\r\n        blocked=any(ships_p(r,c:c+ship_vec(ship)-1)==2);\r\n       end\r\n      catch\r\n       blocked=true; % invalid placement\r\n      end\r\n      if ~blocked % No ship conflict\r\n       if dwn_rt==0 % down\r\n        ships_p(r:r+ship_vec(ship)-1,c)=2;\r\n       else % right\r\n        ships_p(r,c:c+ship_vec(ship)-1)=2;\r\n       end\r\n      end \r\n    end % ship\r\n   \r\n   if sum(ships_p(:))~=127 % Expect 127 board if all placed\r\n    fprintf('Invalid Ship placement - Game over\\n');\r\n    ships\r\n    ships_p\r\n    game_over=true;\r\n   end\r\n   \r\n  catch\r\n   fprintf('Invalid first respone - Game over\\n');\r\n   mvP\r\n   ships\r\n   game_over=true;\r\n  end\r\n\r\n% Main Game Loop\r\n\r\n while ~game_over \r\n  % Computer move\r\n  % Author: Richard Z\r\n  % Date: 2012/06/03\r\n  % Battleship_010 bot: Random of Zeros\r\n  \r\n   avail=find(bc==0);\r\n   mvC=avail(randi(length(avail))); \r\n  %End Battleship bot: Random of Zeros\r\n  \r\n  bc(mvC)=ships_p(mvC); % Hit=2, Miss=1\r\n\r\n  if length(find(bc==2))\u003e=27 % Computer Wins\r\n   break;\r\n  end\r\n  \r\n  % Player's Second move and thereafter\r\n  try % for invalid mvP values\r\n   [mvP,ships]=Battleship(bp); % \r\n   bp(mvP(1))=ships_c(mvP(1)); % Hit=2, Miss=1\r\n  catch\r\n   fprintf('Ignoring Illegal move %i \\n',mvP(1));\r\n  end\r\n  \r\n  if length(find(bp==2))\u003e=27 % All ships sunk\r\n   wins=1;\r\n   break;\r\n  end\r\n  \r\n end % While ~game_over\r\n\r\ntoc\r\n \r\n % Player must win to Pass\r\n assert(isequal(wins,1))\r\n%Pass=1;\r\n%assert(isequal(Pass,1));\r\n\r\n bc\r\n bp\r\n wins","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":4,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-04T04:28:43.000Z","updated_at":"2012-06-04T04:57:27.000Z","published_at":"2012-06-04T04:56:04.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Classic Battleship™ game implemented between a computer bot and a player's bot.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Battleship_%28game%29\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eBattleship\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou and your opponent have 9 ships of various sizes.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThese Ships/Sizes/Qty are Carrier-5, Battleship-4, Submarine-3(qty 2), Cruiser-3(qty 2), Destroyer-2(qty-3).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe board is 10x10. (index 1-100) Unknown=0, Miss=1, Hit=2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eThe Play:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePlayer places his ships on the board. Ships may not overlap but may touch.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eShips array is [9,2] where 1 is the Carrier and 9 is a Destroyer.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e[start_idx, orientation; start_idx, orientation...]. An orientation of 0 is Down and a 1 is Right.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eShips[1 1;...] places the Carrier in cells [1 11 22 33 44]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePlayer takes a shot on the board - idx 1:100.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe computer bot will take a shot if he has any ships remaining.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe player will see an updated board for his next shot if he has any ships remaining.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ePass: Win\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBattleship_bot_010 randomly picks from Zero sectors.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":740,"title":"Battleship_010 : (TM)  Classic Game - Methodical Bot (100 move max)","description":"The Classic Battleship(TM) game implemented between a computer bot and a player's bot.\r\n\r\n\u003chttp://en.wikipedia.org/wiki/Battleship_%28game%29 Battleship\u003e\r\n\r\nYou and your opponent have 9 ships of various sizes.\r\n\r\nThese Ships/Sizes/Qty are Carrier-5, Battleship-4, Submarine-3(qty 2), Cruiser-3(qty 2), Destroyer-2(qty-3).\r\n\r\nThe board is 10x10. (index 1-100)\r\nUnknown=0, Miss=1, Hit=2\r\n\r\n*The Play:*\r\n\r\nPlayer places his ships on the board. Ships may not overlap but may touch.\r\n\r\nShips array is [9,2] where 1 is the Carrier and 9 is a Destroyer. \r\n\r\n[start_idx, orientation; start_idx, orientation...]. An orientation of 0 is Down and a 1 is Right. \r\n\r\nShips[1 1;...] places the Carrier in cells [1 11 22 33 44]\r\n\r\nPlayer takes a shot on the board - idx 1:100.\r\n\r\nThe computer bot will take a shot if he has any ships remaining.\r\n\r\nThe player will see an updated board for his next shot if he has any ships remaining.\r\n\r\n*Pass: Win*\r\n\r\nBattleship_bot_010 randomly picks from Zero sectors. \r\n","description_html":"\u003cp\u003eThe Classic Battleship™ game implemented between a computer bot and a player's bot.\u003c/p\u003e\u003cp\u003e\u003ca href=\"http://en.wikipedia.org/wiki/Battleship_%28game%29\"\u003eBattleship\u003c/a\u003e\u003c/p\u003e\u003cp\u003eYou and your opponent have 9 ships of various sizes.\u003c/p\u003e\u003cp\u003eThese Ships/Sizes/Qty are Carrier-5, Battleship-4, Submarine-3(qty 2), Cruiser-3(qty 2), Destroyer-2(qty-3).\u003c/p\u003e\u003cp\u003eThe board is 10x10. (index 1-100)\r\nUnknown=0, Miss=1, Hit=2\u003c/p\u003e\u003cp\u003e\u003cb\u003eThe Play:\u003c/b\u003e\u003c/p\u003e\u003cp\u003ePlayer places his ships on the board. Ships may not overlap but may touch.\u003c/p\u003e\u003cp\u003eShips array is [9,2] where 1 is the Carrier and 9 is a Destroyer.\u003c/p\u003e\u003cp\u003e[start_idx, orientation; start_idx, orientation...]. An orientation of 0 is Down and a 1 is Right.\u003c/p\u003e\u003cp\u003eShips[1 1;...] places the Carrier in cells [1 11 22 33 44]\u003c/p\u003e\u003cp\u003ePlayer takes a shot on the board - idx 1:100.\u003c/p\u003e\u003cp\u003eThe computer bot will take a shot if he has any ships remaining.\u003c/p\u003e\u003cp\u003eThe player will see an updated board for his next shot if he has any ships remaining.\u003c/p\u003e\u003cp\u003e\u003cb\u003ePass: Win\u003c/b\u003e\u003c/p\u003e\u003cp\u003eBattleship_bot_010 randomly picks from Zero sectors.\u003c/p\u003e","function_template":"function [mv,ships] = Battleship(b)\r\n % b is 10x10; idx 1:100\r\n % output mv is idx 1 thru 100\r\n % b: 0-open ocean; 1-miss shot, 2-Hit\r\n % ships: Placement of player ships\r\n % ships are initialized on first turn sum(b(:))==0\r\n % Place 9 ships [idx1 0/1;idx2 0/1;...idx9 0/1]\r\n % Ship lengths : 5/4/3/3/3/3/2/2/2\r\n % Ship orientation : 0-Vert Down; 1-Horiz Right \r\n % ships=[1 1;2 1;...8 1;99 0] \r\n % Places 8 ships on Left edge, one down in bottom rt corner\r\n % ships may not overlap - Loss\r\n \r\n% Need ships each call or will Lose; Ships placed only on first move\r\n  ships=[1 1;2 1;3 1;4 1;5 1;6 1;7 1;8 1;99 0]; \r\n \r\n % randomly pick an open spot (same as Battleship_bot_000)\r\n mv=randi(100);\r\n \r\nend % Battleship\r\n","test_suite":"%%\r\n%Test Suite\r\n%Battleship_000\r\n% Carrier/Battleship/Sub/Cruiser/Destroyer\r\n% 5/4/3/3/2  Qty 1/1/2/2/3\r\n% b 0-Unknown 1-Miss 2-Hit\r\nseed=clock;\r\nseed=1000*seed(6);\r\nrng(seed);\r\ngame_over=false;\r\nwins=0; % player wins\r\nbp=zeros(10);\r\nbc=zeros(10);\r\nships_p=ones(10);\r\nships_c=ones(10);\r\nship_vec=[5 4 3 3 3 3 2 2 2]; % Length of ships\r\n\r\ntic\r\n% initialize Computer Ships\r\nfor ship=1:9\r\n placed=false;\r\n while~placed\r\n  idx=randi(100);\r\n  [r c]=ind2sub([10 10],idx);\r\n  dwn_rt=randi(2)-1; % 0-down, 1-right\r\n  try % may go beyond board size\r\n   if dwn_rt==0 % down\r\n    blocked=any(ships_c(r:r+ship_vec(ship)-1,c)==2);\r\n   else % right\r\n    blocked=any(ships_c(r,c:c+ship_vec(ship)-1)==2);\r\n   end\r\n  catch\r\n   blocked=true; % invalid placement\r\n  end\r\n  if ~blocked % No ship conflict\r\n   if dwn_rt==0 % down\r\n    ships_c(r:r+ship_vec(ship)-1,c)=2;\r\n   else % right\r\n    ships_c(r,c:c+ship_vec(ship)-1)=2;\r\n   end\r\n   placed=true;\r\n  end \r\n end % placed\r\nend % ship\r\n \r\n% Initialize Player's ships and first move\r\n  try % for invalid mvP values\r\n   [mvP,ships]=Battleship(bp); % \r\n   bp(mvP(1))=ships_c(mvP(1));\r\n   \r\n    for ship=1:9\r\n      [r c]=ind2sub([10 10],ships(ship,1));\r\n      dwn_rt=ships(ship,2); \r\n      try % may go beyond board size\r\n       if dwn_rt==0 % down\r\n        blocked=any(ships_p(r:r+ship_vec(ship)-1,c)==2);\r\n       else % right\r\n        blocked=any(ships_p(r,c:c+ship_vec(ship)-1)==2);\r\n       end\r\n      catch\r\n       blocked=true; % invalid placement\r\n      end\r\n      if ~blocked % No ship conflict\r\n       if dwn_rt==0 % down\r\n        ships_p(r:r+ship_vec(ship)-1,c)=2;\r\n       else % right\r\n        ships_p(r,c:c+ship_vec(ship)-1)=2;\r\n       end\r\n      end \r\n    end % ship\r\n   \r\n   if sum(ships_p(:))~=127 % Expect 127 board if all placed\r\n    fprintf('Invalid Ship placement - Game over\\n');\r\n    ships\r\n    ships_p\r\n    game_over=true;\r\n   end\r\n   \r\n  catch\r\n   fprintf('Invalid first respone - Game over\\n');\r\n   mvP\r\n   ships\r\n   game_over=true;\r\n  end\r\n\r\n% Main Game Loop\r\n\r\n while ~game_over \r\n  % Computer move\r\n  % Author: Richard Z\r\n  % Date: 2012/06/03\r\n  % Battleship_010 bot: Random of Zeros\r\n  \r\n   avail=find(bc==0);\r\n   mvC=avail(randi(length(avail))); \r\n  %End Battleship bot: Random of Zeros\r\n  \r\n  bc(mvC)=ships_p(mvC); % Hit=2, Miss=1\r\n\r\n  if length(find(bc==2))\u003e=27 % Computer Wins\r\n   break;\r\n  end\r\n  \r\n  % Player's Second move and thereafter\r\n  try % for invalid mvP values\r\n   [mvP,ships]=Battleship(bp); % \r\n   bp(mvP(1))=ships_c(mvP(1)); % Hit=2, Miss=1\r\n  catch\r\n   fprintf('Ignoring Illegal move %i \\n',mvP(1));\r\n  end\r\n  \r\n  if length(find(bp==2))\u003e=27 % All ships sunk\r\n   wins=1;\r\n   break;\r\n  end\r\n  \r\n end % While ~game_over\r\n\r\ntoc\r\n \r\n % Player must win to Pass\r\n assert(isequal(wins,1))\r\n%Pass=1;\r\n%assert(isequal(Pass,1));\r\n\r\n bc\r\n bp\r\n wins","published":true,"deleted":false,"likes_count":0,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":4,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-04T04:28:43.000Z","updated_at":"2012-06-04T04:57:27.000Z","published_at":"2012-06-04T04:56:04.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Classic Battleship™ game implemented between a computer bot and a player's bot.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Battleship_%28game%29\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eBattleship\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou and your opponent have 9 ships of various sizes.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThese Ships/Sizes/Qty are Carrier-5, Battleship-4, Submarine-3(qty 2), Cruiser-3(qty 2), Destroyer-2(qty-3).\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe board is 10x10. (index 1-100) Unknown=0, Miss=1, Hit=2\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eThe Play:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePlayer places his ships on the board. Ships may not overlap but may touch.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eShips array is [9,2] where 1 is the Carrier and 9 is a Destroyer.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e[start_idx, orientation; start_idx, orientation...]. An orientation of 0 is Down and a 1 is Right.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eShips[1 1;...] places the Carrier in cells [1 11 22 33 44]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ePlayer takes a shot on the board - idx 1:100.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe computer bot will take a shot if he has any ships remaining.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe player will see an updated board for his next shot if he has any ships remaining.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ePass: Win\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eBattleship_bot_010 randomly picks from Zero sectors.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"bot\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"bot\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"bot\"","","\"","bot","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f7727507800\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f7727507760\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f77275055a0\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f7727507e40\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f7727507da0\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f7727507a80\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f77275079e0\u003e":"tag:\"bot\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f77275079e0\u003e":"tag:\"bot\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"search","password":"J3bGPZzQ7asjJcCk","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"bot\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"bot\"","","\"","bot","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f7727507800\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f7727507760\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f77275055a0\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f7727507e40\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f7727507da0\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f7727507a80\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f77275079e0\u003e":"tag:\"bot\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f77275079e0\u003e":"tag:\"bot\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":740,"difficulty_rating":"unrated"}]}}