{"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":739,"title":"Battleship_000 : (TM)  Classic Game - Easy computer bot","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_000 randomly fires. \r\n\r\nThere is a Bernoulli probability question if this strategy could ever win against anything but another random bot.\r\n\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_000 randomly fires.\u003c/p\u003e\u003cp\u003eThere is a Bernoulli probability question if this strategy could ever win against anything but another random bot.\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","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  % Battleship_000 bot: Random\r\n  mvC=randi(100);\r\n  % End Battleship_000 bot: Random\r\n  \r\n  bc(mvC)=ships_p(mvC); % Hit=2, Miss=1\r\n\r\n %figure(1);imagesc(bc,[-2 2]);axis equal;pause(0.1)  \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 %figure(2);imagesc(bp,[-2 2]);axis equal\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":5,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":24,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-04T03:25:36.000Z","updated_at":"2026-01-13T14:38:54.000Z","published_at":"2012-06-04T03:50:52.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_000 randomly fires.\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\u003eThere is a Bernoulli probability question if this strategy could ever win against anything but another random bot.\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\"}]}"},{"id":702,"title":"Play Tic Tac Toe : No Losses Allowed","description":"Let's play a friendly game of Tic Tac Toe \r\n\r\nCreate an efficient algorithm to Never Lose.\r\n\r\nThe Player may be either the first or second to play.\r\n\r\nInitial board is zeros(3). Computer moves are 1s and Player moves are 2s.\r\n\r\nPlayer output is an integer 1:9 to identify array index position\r\n\r\n 1 4 7\r\n 2 5 8\r\n 3 6 9\r\n\r\nExample game play:\r\n\r\n Input:        [1 0 0; 0 0 0; 0 0 0]  % Computer playing first in this example\r\n Output:        5                     % Player return value\r\n Yields :      [1 0 0; 0 2 0; 0 0 0]  % Computer then picks position 2.\r\n Second Input: [1 0 0; 1 2 0; 0 0 0]  % Computer's second move shown.\r\n\r\nPassing score is never losing.\r\n\r\nThe contest engine is almost Random - about 4000 times. (\u003c12% of Time-Out)\r\n\r\nCreating the perfect tic-tac-toe engine seemed unnecessary.\r\n\r\nYour Wins/Losses/Draws will be shown.\r\n\r\nOn a loss the losing board will be shown\r\n\r\nFollow-up game will be WOPR:Global Thermonuclear War.","description_html":"\u003cp\u003eLet's play a friendly game of Tic Tac Toe\u003c/p\u003e\u003cp\u003eCreate an efficient algorithm to Never Lose.\u003c/p\u003e\u003cp\u003eThe Player may be either the first or second to play.\u003c/p\u003e\u003cp\u003eInitial board is zeros(3). Computer moves are 1s and Player moves are 2s.\u003c/p\u003e\u003cp\u003ePlayer output is an integer 1:9 to identify array index position\u003c/p\u003e\u003cpre\u003e 1 4 7\r\n 2 5 8\r\n 3 6 9\u003c/pre\u003e\u003cp\u003eExample game play:\u003c/p\u003e\u003cpre\u003e Input:        [1 0 0; 0 0 0; 0 0 0]  % Computer playing first in this example\r\n Output:        5                     % Player return value\r\n Yields :      [1 0 0; 0 2 0; 0 0 0]  % Computer then picks position 2.\r\n Second Input: [1 0 0; 1 2 0; 0 0 0]  % Computer's second move shown.\u003c/pre\u003e\u003cp\u003ePassing score is never losing.\u003c/p\u003e\u003cp\u003eThe contest engine is almost Random - about 4000 times. (\u0026lt;12% of Time-Out)\u003c/p\u003e\u003cp\u003eCreating the perfect tic-tac-toe engine seemed unnecessary.\u003c/p\u003e\u003cp\u003eYour Wins/Losses/Draws will be shown.\u003c/p\u003e\u003cp\u003eOn a loss the losing board will be shown\u003c/p\u003e\u003cp\u003eFollow-up game will be WOPR:Global Thermonuclear War.\u003c/p\u003e","function_template":"function p = tic_tac_toe(x)\r\n  p = 1;\r\nend","test_suite":"%%\r\n%Tic Tac Toe : Never Lose\r\ndraws=0;\r\nwins=0;\r\nPass=1;\r\nfor i=1:2000 % Computer First;  2001-4000 Player First\r\n if Pass==0,break;end\r\n x=zeros(3);\r\n while true % exit on win or filled board\r\n   found=0;\r\n   while ~found\r\n    p=floor(9*rand)+1;\r\n    if x(p)==0,break;end\r\n   end\r\n\r\n% Random unless Win presents itself\r\n  v0=find(x==0)';\r\n  t1=x;\r\n  t1(t1==2)=-2; % Player pips set to -2\r\n% Check for Win Option\r\n wc=find(sum(t1)==2); % column with a win\r\n if ~isempty(wc)\r\n  p=intersect((1:3)+3*(wc-1),v0);\r\n end\r\n wr=find(sum(t1,2)==2); % row with a win\r\n if ~isempty(wr)\r\n  p=intersect((1:3:7)+(wr-1),v0);\r\n end\r\n% Diagonal Win Options\r\nif x(5)==1\r\n if x(1)==1 \u0026\u0026 x(9)==0,p=9;end\r\n if x(9)==1 \u0026\u0026 x(1)==0,p=1;end\r\n if x(3)==1 \u0026\u0026 x(7)==0,p=7;end\r\n if x(7)==1 \u0026\u0026 x(3)==0,p=3;end\r\nend\r\n% end of Win check\r\n   \r\n   \r\n   \r\n  % Implement Computer move and Check for Win\r\n  x(p)=1;\r\n  wt=x;\r\n  wt(wt==2)=0;\r\n  win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==3);\r\n  if win % Computer Wins on its move : Match Over\r\n   Pass=0;\r\n   x\r\n   break;\r\n  end \r\n  \r\n  % Draw check Post computer move (9th move)\r\n  if sum(x(:))\u003e=13,\r\n   draws=draws+1;\r\n   break;\r\n  end\r\n  \r\n  % Implement player move and Check for Win\r\n  p=tic_tac_toe(x);\r\n  if x(p(1))~=0,Pass=0;end % Invalid move - Game over\r\n  x(p(1))=2;\r\n  wt=x;\r\n  wt(wt==1)=0;\r\n  win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==6);\r\n  if win\r\n   wins=wins+1;\r\n   break;\r\n  end % Player Wins on their move\r\n  \r\n  \r\n end % gameover while 1:2000\r\nend % Games 1:2000\r\n\r\n\r\nfor i=1:2000 % Player First;  2001-40000 Player First\r\n if Pass==0,break;end\r\n x=zeros(3);\r\n while true % exit on win or filled board\r\n  % Implement player move and Check for Win\r\n  p=tic_tac_toe(x);\r\n  if x(p(1))~=0,Pass=0;end % Invalid move - Game over\r\n  x(p(1))=2;\r\n  wt=x;\r\n  wt(wt==1)=0;\r\n  win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==6);\r\n  if win\r\n   wins=wins+1;\r\n   break;\r\n  end % Player Wins on their move\r\n  \r\n  \r\n  % Draw check Post Player move (9th move)\r\n  if sum(x(:))\u003e=13,\r\n   draws=draws+1;\r\n   break;\r\n  end\r\n  \r\n  % Computer Move\r\n   found=0;\r\n   while ~found\r\n    p=floor(9*rand)+1;\r\n    if x(p)==0,break;end\r\n   end\r\n  \r\n% Random unless Win presents itself\r\n  v0=find(x==0)';\r\n  t1=x;\r\n  t1(t1==2)=-2; % Player pips set to -2\r\n% Check for Win Option\r\n wc=find(sum(t1)==2); % column with a win\r\n if ~isempty(wc)\r\n  p=intersect((1:3)+3*(wc-1),v0);\r\n end\r\n wr=find(sum(t1,2)==2); % row with a win\r\n if ~isempty(wr)\r\n  p=intersect((1:3:7)+(wr-1),v0);\r\n end\r\n% Diagonal Win Options\r\nif x(5)==1\r\n if x(1)==1 \u0026\u0026 x(9)==0,p=9;end\r\n if x(9)==1 \u0026\u0026 x(1)==0,p=1;end\r\n if x(3)==1 \u0026\u0026 x(7)==0,p=7;end\r\n if x(7)==1 \u0026\u0026 x(3)==0,p=3;end\r\nend\r\n% end of Win check\r\n   \r\n  % Implement Computer move and Check for Win\r\n  x(p)=1;\r\n  wt=x;\r\n  wt(wt==2)=0;\r\n  win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==3);\r\n  if win % Computer Wins on its move : Match Over\r\n   Pass=0;\r\n   x\r\n   break;\r\n  end \r\n  \r\n  \r\n end % gameover while 1:2000\r\nend % Games 1:2000\r\nwins\r\ndraws\r\nPass\r\nassert(isequal(Pass,1))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":11,"test_suite_updated_at":"2012-05-23T04:19:50.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-05-19T23:49:07.000Z","updated_at":"2026-01-04T12:38:00.000Z","published_at":"2012-05-20T00:36:44.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\u003eLet's play a friendly game of Tic Tac Toe\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\u003eCreate an efficient algorithm to Never Lose.\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 may be either the first or second to 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\u003eInitial board is zeros(3). Computer moves are 1s and Player moves are 2s.\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 output is an integer 1:9 to identify array index position\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ 1 4 7\\n 2 5 8\\n 3 6 9]]\u003e\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\u003eExample game play:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input:        [1 0 0; 0 0 0; 0 0 0]  % Computer playing first in this example\\n Output:        5                     % Player return value\\n Yields :      [1 0 0; 0 2 0; 0 0 0]  % Computer then picks position 2.\\n Second Input: [1 0 0; 1 2 0; 0 0 0]  % Computer's second move shown.]]\u003e\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\u003ePassing score is never losing.\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 contest engine is almost Random - about 4000 times. (\u0026lt;12% of Time-Out)\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\u003eCreating the perfect tic-tac-toe engine seemed unnecessary.\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\u003eYour Wins/Losses/Draws will be shown.\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\u003eOn a loss the losing board will be shown\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\u003eFollow-up game will be WOPR:Global Thermonuclear War.\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\"}]}"},{"id":726,"title":"Chezz_020 Pawn Attack and Slaying King","description":"Chezz:...A simplified Chess game. Two game match with future multiple skill levels via evolutionary upgrades.\r\n\r\nSimplified the rules to implement rapid move check. Normal Chess moves like \"a4\", Castling, and En Passant are accepted. Moves are simple vectors [idx_from idx_to promo] where promo is for pawns reaching the 8th rank. Piece names are numeric 1-6 White (P,R,N,B,Q,K) and Black is 7-12. Board empty space is 0.\r\n\r\nChezz unique rules: 1) To Win must remove opponent's King.. 2) Lose Turn if try an impossible move.. 3) Checks are ignored - allow move into check, castle out of check, castle thru/into check.. 4) Pawn promote to any piece, including opponent and vacuum.. 5) Six position repetition is a Draw..6) 50 moves No captures is a Draw..\r\n\r\n\r\n*Inputs:* (b,pmv,castle)\r\n\r\nPlayer always appears to be White. However, Black may apparently move first.\r\n\r\nb (8x8) array with empty=0, White Pieces 1-6, Blk Pieces 7-12\r\n\r\npmv [from to promo] vector of both players moves (needed for en passant option). Illegal move creates a [0 0 0] pmv value. Size [2*moves,3]\r\n\r\ncastle: 6 pt logical vector to show castling options. 1 if piece is eligible. Positions are [8 40 64 1 33 57] for [WQR WK WKR BQR BK BKR]. . . eg castle mv [40 56 0]\r\n\r\n\r\n\r\n*Output:* mv  [from to pawn_new_type] where from/to are 1-64 and pawn_new is 0 thru 12 if pawn is on final row.\r\n\r\n\r\n\r\nThe Test Suite will oversee two games with the Player as Black(moving second) and then as White.\r\n\r\n*Passing:* Player must Win both games\r\n\r\n*Evolution:* Winning player algorithms will be hosted as Chezz_xyz based on their wins over other Chezz algorithms. Champions and the basis will be fully credited in Chezz updates. Champions must be able to play self and Not Cody timeout.\r\n\r\nChezz_020: Pawn Attack then Mad King Attack\r\n\r\nLink update 12/27/12","description_html":"\u003cp\u003eChezz:...A simplified Chess game. Two game match with future multiple skill levels via evolutionary upgrades.\u003c/p\u003e\u003cp\u003eSimplified the rules to implement rapid move check. Normal Chess moves like \"a4\", Castling, and En Passant are accepted. Moves are simple vectors [idx_from idx_to promo] where promo is for pawns reaching the 8th rank. Piece names are numeric 1-6 White (P,R,N,B,Q,K) and Black is 7-12. Board empty space is 0.\u003c/p\u003e\u003cp\u003eChezz unique rules: 1) To Win must remove opponent's King.. 2) Lose Turn if try an impossible move.. 3) Checks are ignored - allow move into check, castle out of check, castle thru/into check.. 4) Pawn promote to any piece, including opponent and vacuum.. 5) Six position repetition is a Draw..6) 50 moves No captures is a Draw..\u003c/p\u003e\u003cp\u003e\u003cb\u003eInputs:\u003c/b\u003e (b,pmv,castle)\u003c/p\u003e\u003cp\u003ePlayer always appears to be White. However, Black may apparently move first.\u003c/p\u003e\u003cp\u003eb (8x8) array with empty=0, White Pieces 1-6, Blk Pieces 7-12\u003c/p\u003e\u003cp\u003epmv [from to promo] vector of both players moves (needed for en passant option). Illegal move creates a [0 0 0] pmv value. Size [2*moves,3]\u003c/p\u003e\u003cp\u003ecastle: 6 pt logical vector to show castling options. 1 if piece is eligible. Positions are [8 40 64 1 33 57] for [WQR WK WKR BQR BK BKR]. . . eg castle mv [40 56 0]\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e mv  [from to pawn_new_type] where from/to are 1-64 and pawn_new is 0 thru 12 if pawn is on final row.\u003c/p\u003e\u003cp\u003eThe Test Suite will oversee two games with the Player as Black(moving second) and then as White.\u003c/p\u003e\u003cp\u003e\u003cb\u003ePassing:\u003c/b\u003e Player must Win both games\u003c/p\u003e\u003cp\u003e\u003cb\u003eEvolution:\u003c/b\u003e Winning player algorithms will be hosted as Chezz_xyz based on their wins over other Chezz algorithms. Champions and the basis will be fully credited in Chezz updates. Champions must be able to play self and Not Cody timeout.\u003c/p\u003e\u003cp\u003eChezz_020: Pawn Attack then Mad King Attack\u003c/p\u003e\u003cp\u003eLink update 12/27/12\u003c/p\u003e","function_template":"function mv=player_move(b,pmv,castle)\r\n% Author: Richard Z\r\n% Bot name: First Pawn then King Attack\r\n% Chezz_020\r\n% http://tinyurl.com/matlab-chezz-Cmove020\r\n mv=zeros(1,3); % [from to promo] \r\n% This is your opponent\r\n\r\n pieces=1:6;\r\n enemy=7:12;\r\n \r\n king_mv=[7 8 9 -1 1 -9 -8 -7]; %\r\n king_attack=[-9 -8 -7 -1 1 7 8 9]; %\r\n \r\n pv=find(b==1); % vector of Pawns\r\n no_move=true;\r\n ptr=0;\r\n \r\n % loop thru piece types\r\n \r\n while no_move % Pawn\r\n  ptr=ptr+1;\r\n  if ptr\u003elength(pv),break;end\r\n  [r c]=ind2sub([8 8],pv(ptr));\r\n  if ismember(r,[1 8]),continue;end % end row for a P\r\n  if c==1\r\n   if ismember(b(pv(ptr)+7),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)+7 6];\r\n    return;\r\n   end\r\n  end\r\n  if c==8\r\n   if ismember(b(pv(ptr)-9),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)-9 6];\r\n    return;\r\n   end\r\n  end\r\n  if c\u003c8 \u0026\u0026 c\u003e1\r\n   if ismember(b(pv(ptr)+7),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)+7 6];\r\n    return;\r\n   end\r\n   if ismember(b(pv(ptr)-9),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)-9 6];\r\n    return;\r\n   end\r\n  end\r\n  \r\n  % Move fwd\r\n  if b(pv(ptr)-1)==0\r\n   mv=[pv(ptr) pv(ptr)-1 6];\r\n   return;\r\n  end\r\n  \r\n end % while no move Pawn\r\n\r\n% No pawn move found; Move on to King\r\n\r\n% No pawn moves left: Attack with King\r\n\r\npv=find(b==6); % Find Kings\r\nno_move=true;\r\nptr=0;\r\n\r\nwhile no_move % King captures\r\n  ptr=ptr+1;\r\n  if ptr\u003elength(pv),break;end\r\n  [r c]=ind2sub([8 8],pv(ptr));\r\n  for i=1:8 % 8 King attacks\r\n   if (pv(ptr)+king_attack(i)\u003e64) || (pv(ptr)+king_attack(i)\u003c1),continue;end\r\n   if r==1 \u0026\u0026 ismember(i,[1 4 6]),continue;end % make ~ismem 1:64\r\n   if r==8 \u0026\u0026 ismember(i,[3 5 8]),continue;end\r\n   if c==1 \u0026\u0026 ismember(i,[1 2 3]),continue;end\r\n   if c==8 \u0026\u0026 ismember(i,[6 7 8]),continue;end\r\n   if ismember(b(pv(ptr)+king_attack(i)),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)+king_attack(i) 0]; % errant r1 attack idx 1 to idx 8\r\n    return; % Attack found\r\n   end\r\n  end % 8 King attacks\r\nend % while King capture search\r\n\r\n% No King capture move found\r\nmyk=pv(1); % First King\r\nek=find(b==max(enemy),1); %Find an enemy king\r\n[kr1 kc1]=ind2sub([8 8],myk);\r\n[er1 ec1]=ind2sub([8 8],ek);\r\nif er1\u003c=kr1 \u0026\u0026 ec1\u003e=kc1,king_mv=[7 8 -1 9 -9 -8 1 -7];end\r\nif er1\u003e=kr1 \u0026\u0026 ec1\u003e=kc1,king_mv=[9 8  1 -7 7 -8 -1 -9];end\r\nif er1\u003c=kr1 \u0026\u0026 ec1\u003c=kc1,king_mv=[-9 -1 -8 -7 7 8 1 9];end\r\nif er1\u003e=kr1 \u0026\u0026 ec1\u003c=kc1,king_mv=[-7 1 -8 9 -9 8 -1 7];end\r\nfor i=1:8\r\n if kr1==1 \u0026\u0026 ismember(king_mv(i),[-9 -1 7]),continue;end\r\n if kr1==8 \u0026\u0026 ismember(king_mv(i),[-7 1 9]),continue;end\r\n if kc1==1 \u0026\u0026 ismember(king_mv(i),[-9 -8 -7]),continue;end\r\n if kc1==8 \u0026\u0026 ismember(king_mv(i),[7 8 9]),continue;end\r\n if ismember(b(myk+king_mv(i)),0) % empty\r\n  mv=[myk myk+king_mv(i) 0];\r\n  return; % path found\r\n end\r\nend\r\n% Hopefully don't get here as mv=[0 0 0]\r\n\r\nend % End of Computer Champion Bot","test_suite":"%%\r\n% Load in the routines that play the game and check move validity\r\ntic\r\nurlwrite('http://rmatlabtest.appspot.com/Chezz_Shell.m','Chezz_Shell.m') ;\r\nurlwrite('http://rmatlabtest.appspot.com/ghost_white.m','ghost_white.m') \r\nurlwrite('http://rmatlabtest.appspot.com/computer_move020.m','computer_move.m') \r\nurlwrite('http://rmatlabtest.appspot.com/mov_chk.m','mov_chk.m') \r\nrehash path\r\ntoc\r\n%%\r\n% Play Two Chezz games\r\n% Player must win Twice to Pass\r\nglobal pmvout1 pmvout2\r\ntic\r\n wins=0; % player wins\r\n b=zeros(8);% P 1/7 R 2/8 N 3/9 B 4/10 Q 5/11 K 6/12 \r\n b(2,:)=7;\r\n b(1,:)=[8 9 10 11 12 10 9 8]; % Player 7:12\r\n b(7,:)=1;\r\n b(8,:)=b(1,:)-6; % Computer 1:6\r\n b_orig=b;\r\n \r\n %mv=zeros(1,3); % [from to promo)]  \r\n %computer_wht=0; % 0 Computer plays wht\r\n %computer_wht=1; % 1 Computer plays black\r\n \r\n pmv=zeros(1,3); % [from to promo)] Opponents last move\r\n castle=[1 1 1 1 1 1];\r\n % False move call to satisfy Cody Rqmt of TestSuite match Ref Solution\r\n mv=player_move(b,pmv,castle)\r\n  \r\n for computer_wht=0:1\r\n  pmvout1=pmv; % Store game 1 moves\r\n  \r\n  dbg=0;\r\n  game_over=false;\r\n  b=b_orig;\r\n  no_capture=0;\r\n  b_hist=zeros(102,8,8);\r\n  pmv=zeros(1,3); % [from to promo)] Opponents last move\r\n  castle=[1 1 1 1 1 1]; % History of if ever moved w/b Castles/kings\r\n %  idx 8 40 64  1 33 57\r\n \r\n while ~game_over\r\n  mvP=zeros(1,3); % [from to type/promote)] \r\n  % Shell 0=Blk,1=Wht;Board;move,prev move;\r\n  % function (1 Play Comp, 2 Player, 3 Check mv)\r\n  \r\n  % White move\r\n  if computer_wht==0\r\n   [mvP]=Chezz_Shell(0,b,mvP,pmv,castle,1); % 0 Wht,... 1 Computer\r\n  else\r\n   [mvP]=Chezz_Shell(0,b,mvP,pmv,castle,2); % 0 Wht  2 is player\r\n  end\r\n  \r\n  \r\n  [mv]=Chezz_Shell(0,b,mvP,pmv(end,:),castle,3); % 0 Wht,..., 3 Check\r\n  pmv=[pmv;mv(1:3)];\r\n  capture=false;\r\n  % Board changes only occur in Suite code\r\n  if mv(1)~=0 % Valid move determined by mv_chk\r\n   if mv(4)==0 % Normal moves and promotions\r\n    if b(mv(2))~=0,capture=true;end\r\n     b(mv(2))=b(mv(1)); % potential promotion\r\n     b(mv(1))=0;\r\n     if ismember(b(mv(2)),[1 7])\r\n      if ismember(mv(2),[1:8:57 8:8:64])\r\n       b(mv(2))=mv(3); % Place promoted selection\r\n      end\r\n     end\r\n     \r\n   else % ep or castle by White\r\n    if mv(4)==1 % castle 0-0 or 0-0-0\r\n      b(mv(1))=0;\r\n      b(mv(2))=6;\r\n      if mv(2)==24\r\n        b(8)=0;b(32)=2;  \r\n      else\r\n        b(64)=0;b(48)=2;\r\n      end    \r\n    end % castle\r\n    if mv(4)==2 % ep\r\n      capture=true;\r\n      b(mv(1))=0;\r\n      b(mv(2))=1; % White Pawn\r\n      b(mv(2)+1)=0; % Take pawn that passed\r\n    end % end ep\r\n   end % move implemented\r\n  end % end move\r\n  \r\n  %b % display board after move\r\n  \r\n  if isempty(find(b==12,1))\r\n   % Game over : Black King Captured\r\n   game_over=true; % change to if comp=wht or blk for win\r\n   if computer_wht==1 % Blk Computer King; Player is Wht captured Blk King \r\n    wins=wins+1;\r\n   end\r\n   continue;\r\n  end\r\n  \r\n  castle=castle.*logical([b(8)==2 b(40) b(64)==2 b(1)==8 b(33) b(57)==8]);\r\n  \r\n  if ~capture\r\n   no_capture=no_capture+1;\r\n   if no_capture\u003e100\r\n       fprintf('Draw 100 moves no capture\\n');\r\n       game_over=true;\r\n   end % move is b and w\r\n   b_hist(no_capture,:,:)=b;\r\n  else\r\n   no_capture=1;\r\n   b_hist=b_hist*0;\r\n   b_hist(no_capture,:,:)=b;\r\n  end\r\n   \r\n  % Black Move\r\n  mvP=zeros(1,4); % [from to type/promote specials(castle=1/ep=2)] \r\n if computer_wht==0\r\n   [mvP]=Chezz_Shell(1,b,mvP,pmv,castle,2); % 2 Blk,... 2 is player\r\n  else\r\n   [mvP]=Chezz_Shell(1,b,mvP,pmv,castle,1); % 2 Blk  1 is Computer\r\n end\r\n  \r\n  [mv]=Chezz_Shell(1,b,mvP,pmv(end,:),castle,3); % 2 Blk,..., 3 Check\r\n  \r\n pmv=[pmv;mv(1:3)];\r\n capture=false;\r\n  % Board changes only occur in Suite code\r\n  if mv(1)~=0 % Valid move determined by mv_chk\r\n   if mv(4)==0 % Normal moves and promotions\r\n    if b(mv(2))~=0,capture=true;end\r\n    b(mv(2))=b(mv(1)); % potential promotion\r\n    b(mv(1))=0;\r\n    if ismember(b(mv(2)),[1 7])\r\n     if ismember(mv(2),[1:8:57 8:8:64])\r\n      b(mv(2))=mv(3); % Place promoted selection\r\n     end\r\n    end\r\n   else % ep or castle by Black\r\n    if mv(4)==1 % castle 0-0 or 0-0-0\r\n      b(mv(1))=0;\r\n      b(mv(2))=12;\r\n      if mv(2)==49 % Blk 0-0\r\n        b(57)=0;b(41)=8;  \r\n      else % Blk 0-0-0\r\n        b(1)=0;b(25)=8;\r\n      end    \r\n    end % castle\r\n    if mv(4)==2 % ep by Black\r\n      capture=true;\r\n      b(mv(1))=0;\r\n      b(mv(2))=mv(3);\r\n    % White passed black on prior move\r\n      b(mv(2)-1)=0; \r\n    end % end ep\r\n   end % move implemented\r\n  end % end move\r\n  \r\n%   b\r\n%   figure(1);imagesc(b,[0 12]);axis equal;\r\n  \r\n  if isempty(find(b==6,1))\r\n   % Game over : Blk captured White King\r\n   game_over=true;\r\n   if computer_wht==0 % Wht Computer King; Player is Blk captured Wht King\r\n    wins=wins+1;\r\n   end\r\n   continue\r\n  end\r\n  \r\n  castle=castle.*logical([b(8)==2 b(40) b(64)==2 b(1)==8 b(33) b(57)==8]);\r\n  \r\n  if ~capture\r\n   no_capture=no_capture+1;\r\n   if no_capture\u003e100\r\n       fprintf('Draw 100 moves no capture\\n');\r\n       game_over=true;\r\n   end % move is b and w\r\n   b_hist(no_capture,:,:)=b;\r\n   % Check for 3 position repetition\r\n   for i=1:no_capture-1\r\n    cdelta=0;\r\n    for j=i+1:no_capture\r\n     delta=(b_hist(i,:,:)-b_hist(j,:,:));\r\n     if sum(abs(delta(:)))==0\r\n      cdelta=cdelta+1;\r\n     end\r\n    end\r\n    if cdelta\u003e=7 % repetitions 3 identical setups\r\n     fprintf('Game over due to repetition\\n');\r\n     game_over=true;\r\n    end\r\n   end % rep check loop \r\n  else\r\n   no_capture=1;\r\n   b_hist=b_hist*0;\r\n   b_hist(no_capture,:,:)=b;\r\n  end % ~capture\r\n  \r\n end % While ~game_over\r\n \r\n end % wht_blk\r\n\r\npmvout2=pmv;\r\nwins\r\n \r\n% Player must win Twice to Pass\r\ntoc\r\nassert(isequal(wins,2))\r\n\r\n%%\r\nglobal pmvout1 pmvout2\r\n% Output moves for games\r\n\r\n% Output game 2 moves\r\npmv=pmvout1;\r\n   for i=2:3:size(pmv,1)-3\r\n    fprintf('%2i %2i %2i %2i %2i %2i %2i %2i %2i \\n',pmv(i,1:3),pmv(i+1,1:3),pmv(i+2,1:3));\r\n   end\r\n   fprintf('%2i %2i %2i\\n',pmv(end-2,:));\r\n   fprintf('%2i %2i %2i\\n',pmv(end-1,:));\r\n   fprintf('%2i %2i %2i\\n',pmv(end,:));\r\n \r\n% Output game 2 moves\r\npmv=pmvout2;\r\n for i=2:3:size(pmv,1)-3\r\n  fprintf('%2i %2i %2i %2i %2i %2i %2i %2i %2i \\n',pmv(i,1:3),pmv(i+1,1:3),pmv(i+2,1:3));\r\n  end\r\n% repeat of moves\r\n fprintf('%2i %2i %2i\\n',pmv(end-2,:));\r\n fprintf('%2i %2i %2i\\n',pmv(end-1,:));\r\n fprintf('%2i %2i %2i\\n',pmv(end,:));","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":1,"test_suite_updated_at":"2012-12-27T22:44:29.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-05-28T06:59:32.000Z","updated_at":"2012-12-27T22:44:29.000Z","published_at":"2012-05-28T07:41:09.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\u003eChezz:...A simplified Chess game. Two game match with future multiple skill levels via evolutionary upgrades.\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\u003eSimplified the rules to implement rapid move check. Normal Chess moves like \\\"a4\\\", Castling, and En Passant are accepted. Moves are simple vectors [idx_from idx_to promo] where promo is for pawns reaching the 8th rank. Piece names are numeric 1-6 White (P,R,N,B,Q,K) and Black is 7-12. Board empty space is 0.\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\u003eChezz unique rules: 1) To Win must remove opponent's King.. 2) Lose Turn if try an impossible move.. 3) Checks are ignored - allow move into check, castle out of check, castle thru/into check.. 4) Pawn promote to any piece, including opponent and vacuum.. 5) Six position repetition is a Draw..6) 50 moves No captures is a Draw..\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\u003eInputs:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (b,pmv,castle)\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 always appears to be White. However, Black may apparently move first.\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\u003eb (8x8) array with empty=0, White Pieces 1-6, Blk Pieces 7-12\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\u003epmv [from to promo] vector of both players moves (needed for en passant option). Illegal move creates a [0 0 0] pmv value. Size [2*moves,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\u003ecastle: 6 pt logical vector to show castling options. 1 if piece is eligible. Positions are [8 40 64 1 33 57] for [WQR WK WKR BQR BK BKR]. . . eg castle mv [40 56 0]\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\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e mv [from to pawn_new_type] where from/to are 1-64 and pawn_new is 0 thru 12 if pawn is on final row.\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 Test Suite will oversee two games with the Player as Black(moving second) and then as White.\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\u003ePassing:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Player must Win both games\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\u003eEvolution:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Winning player algorithms will be hosted as Chezz_xyz based on their wins over other Chezz algorithms. Champions and the basis will be fully credited in Chezz updates. Champions must be able to play self and Not Cody timeout.\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\u003eChezz_020: Pawn Attack then Mad King Attack\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\u003eLink update 12/27/12\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\"}]}"},{"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":739,"title":"Battleship_000 : (TM)  Classic Game - Easy computer bot","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_000 randomly fires. \r\n\r\nThere is a Bernoulli probability question if this strategy could ever win against anything but another random bot.\r\n\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_000 randomly fires.\u003c/p\u003e\u003cp\u003eThere is a Bernoulli probability question if this strategy could ever win against anything but another random bot.\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","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  % Battleship_000 bot: Random\r\n  mvC=randi(100);\r\n  % End Battleship_000 bot: Random\r\n  \r\n  bc(mvC)=ships_p(mvC); % Hit=2, Miss=1\r\n\r\n %figure(1);imagesc(bc,[-2 2]);axis equal;pause(0.1)  \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 %figure(2);imagesc(bp,[-2 2]);axis equal\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":5,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":24,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-04T03:25:36.000Z","updated_at":"2026-01-13T14:38:54.000Z","published_at":"2012-06-04T03:50:52.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_000 randomly fires.\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\u003eThere is a Bernoulli probability question if this strategy could ever win against anything but another random bot.\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\"}]}"},{"id":702,"title":"Play Tic Tac Toe : No Losses Allowed","description":"Let's play a friendly game of Tic Tac Toe \r\n\r\nCreate an efficient algorithm to Never Lose.\r\n\r\nThe Player may be either the first or second to play.\r\n\r\nInitial board is zeros(3). Computer moves are 1s and Player moves are 2s.\r\n\r\nPlayer output is an integer 1:9 to identify array index position\r\n\r\n 1 4 7\r\n 2 5 8\r\n 3 6 9\r\n\r\nExample game play:\r\n\r\n Input:        [1 0 0; 0 0 0; 0 0 0]  % Computer playing first in this example\r\n Output:        5                     % Player return value\r\n Yields :      [1 0 0; 0 2 0; 0 0 0]  % Computer then picks position 2.\r\n Second Input: [1 0 0; 1 2 0; 0 0 0]  % Computer's second move shown.\r\n\r\nPassing score is never losing.\r\n\r\nThe contest engine is almost Random - about 4000 times. (\u003c12% of Time-Out)\r\n\r\nCreating the perfect tic-tac-toe engine seemed unnecessary.\r\n\r\nYour Wins/Losses/Draws will be shown.\r\n\r\nOn a loss the losing board will be shown\r\n\r\nFollow-up game will be WOPR:Global Thermonuclear War.","description_html":"\u003cp\u003eLet's play a friendly game of Tic Tac Toe\u003c/p\u003e\u003cp\u003eCreate an efficient algorithm to Never Lose.\u003c/p\u003e\u003cp\u003eThe Player may be either the first or second to play.\u003c/p\u003e\u003cp\u003eInitial board is zeros(3). Computer moves are 1s and Player moves are 2s.\u003c/p\u003e\u003cp\u003ePlayer output is an integer 1:9 to identify array index position\u003c/p\u003e\u003cpre\u003e 1 4 7\r\n 2 5 8\r\n 3 6 9\u003c/pre\u003e\u003cp\u003eExample game play:\u003c/p\u003e\u003cpre\u003e Input:        [1 0 0; 0 0 0; 0 0 0]  % Computer playing first in this example\r\n Output:        5                     % Player return value\r\n Yields :      [1 0 0; 0 2 0; 0 0 0]  % Computer then picks position 2.\r\n Second Input: [1 0 0; 1 2 0; 0 0 0]  % Computer's second move shown.\u003c/pre\u003e\u003cp\u003ePassing score is never losing.\u003c/p\u003e\u003cp\u003eThe contest engine is almost Random - about 4000 times. (\u0026lt;12% of Time-Out)\u003c/p\u003e\u003cp\u003eCreating the perfect tic-tac-toe engine seemed unnecessary.\u003c/p\u003e\u003cp\u003eYour Wins/Losses/Draws will be shown.\u003c/p\u003e\u003cp\u003eOn a loss the losing board will be shown\u003c/p\u003e\u003cp\u003eFollow-up game will be WOPR:Global Thermonuclear War.\u003c/p\u003e","function_template":"function p = tic_tac_toe(x)\r\n  p = 1;\r\nend","test_suite":"%%\r\n%Tic Tac Toe : Never Lose\r\ndraws=0;\r\nwins=0;\r\nPass=1;\r\nfor i=1:2000 % Computer First;  2001-4000 Player First\r\n if Pass==0,break;end\r\n x=zeros(3);\r\n while true % exit on win or filled board\r\n   found=0;\r\n   while ~found\r\n    p=floor(9*rand)+1;\r\n    if x(p)==0,break;end\r\n   end\r\n\r\n% Random unless Win presents itself\r\n  v0=find(x==0)';\r\n  t1=x;\r\n  t1(t1==2)=-2; % Player pips set to -2\r\n% Check for Win Option\r\n wc=find(sum(t1)==2); % column with a win\r\n if ~isempty(wc)\r\n  p=intersect((1:3)+3*(wc-1),v0);\r\n end\r\n wr=find(sum(t1,2)==2); % row with a win\r\n if ~isempty(wr)\r\n  p=intersect((1:3:7)+(wr-1),v0);\r\n end\r\n% Diagonal Win Options\r\nif x(5)==1\r\n if x(1)==1 \u0026\u0026 x(9)==0,p=9;end\r\n if x(9)==1 \u0026\u0026 x(1)==0,p=1;end\r\n if x(3)==1 \u0026\u0026 x(7)==0,p=7;end\r\n if x(7)==1 \u0026\u0026 x(3)==0,p=3;end\r\nend\r\n% end of Win check\r\n   \r\n   \r\n   \r\n  % Implement Computer move and Check for Win\r\n  x(p)=1;\r\n  wt=x;\r\n  wt(wt==2)=0;\r\n  win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==3);\r\n  if win % Computer Wins on its move : Match Over\r\n   Pass=0;\r\n   x\r\n   break;\r\n  end \r\n  \r\n  % Draw check Post computer move (9th move)\r\n  if sum(x(:))\u003e=13,\r\n   draws=draws+1;\r\n   break;\r\n  end\r\n  \r\n  % Implement player move and Check for Win\r\n  p=tic_tac_toe(x);\r\n  if x(p(1))~=0,Pass=0;end % Invalid move - Game over\r\n  x(p(1))=2;\r\n  wt=x;\r\n  wt(wt==1)=0;\r\n  win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==6);\r\n  if win\r\n   wins=wins+1;\r\n   break;\r\n  end % Player Wins on their move\r\n  \r\n  \r\n end % gameover while 1:2000\r\nend % Games 1:2000\r\n\r\n\r\nfor i=1:2000 % Player First;  2001-40000 Player First\r\n if Pass==0,break;end\r\n x=zeros(3);\r\n while true % exit on win or filled board\r\n  % Implement player move and Check for Win\r\n  p=tic_tac_toe(x);\r\n  if x(p(1))~=0,Pass=0;end % Invalid move - Game over\r\n  x(p(1))=2;\r\n  wt=x;\r\n  wt(wt==1)=0;\r\n  win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==6);\r\n  if win\r\n   wins=wins+1;\r\n   break;\r\n  end % Player Wins on their move\r\n  \r\n  \r\n  % Draw check Post Player move (9th move)\r\n  if sum(x(:))\u003e=13,\r\n   draws=draws+1;\r\n   break;\r\n  end\r\n  \r\n  % Computer Move\r\n   found=0;\r\n   while ~found\r\n    p=floor(9*rand)+1;\r\n    if x(p)==0,break;end\r\n   end\r\n  \r\n% Random unless Win presents itself\r\n  v0=find(x==0)';\r\n  t1=x;\r\n  t1(t1==2)=-2; % Player pips set to -2\r\n% Check for Win Option\r\n wc=find(sum(t1)==2); % column with a win\r\n if ~isempty(wc)\r\n  p=intersect((1:3)+3*(wc-1),v0);\r\n end\r\n wr=find(sum(t1,2)==2); % row with a win\r\n if ~isempty(wr)\r\n  p=intersect((1:3:7)+(wr-1),v0);\r\n end\r\n% Diagonal Win Options\r\nif x(5)==1\r\n if x(1)==1 \u0026\u0026 x(9)==0,p=9;end\r\n if x(9)==1 \u0026\u0026 x(1)==0,p=1;end\r\n if x(3)==1 \u0026\u0026 x(7)==0,p=7;end\r\n if x(7)==1 \u0026\u0026 x(3)==0,p=3;end\r\nend\r\n% end of Win check\r\n   \r\n  % Implement Computer move and Check for Win\r\n  x(p)=1;\r\n  wt=x;\r\n  wt(wt==2)=0;\r\n  win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==3);\r\n  if win % Computer Wins on its move : Match Over\r\n   Pass=0;\r\n   x\r\n   break;\r\n  end \r\n  \r\n  \r\n end % gameover while 1:2000\r\nend % Games 1:2000\r\nwins\r\ndraws\r\nPass\r\nassert(isequal(Pass,1))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":11,"test_suite_updated_at":"2012-05-23T04:19:50.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-05-19T23:49:07.000Z","updated_at":"2026-01-04T12:38:00.000Z","published_at":"2012-05-20T00:36:44.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\u003eLet's play a friendly game of Tic Tac Toe\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\u003eCreate an efficient algorithm to Never Lose.\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 may be either the first or second to 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\u003eInitial board is zeros(3). Computer moves are 1s and Player moves are 2s.\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 output is an integer 1:9 to identify array index position\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ 1 4 7\\n 2 5 8\\n 3 6 9]]\u003e\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\u003eExample game play:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[ Input:        [1 0 0; 0 0 0; 0 0 0]  % Computer playing first in this example\\n Output:        5                     % Player return value\\n Yields :      [1 0 0; 0 2 0; 0 0 0]  % Computer then picks position 2.\\n Second Input: [1 0 0; 1 2 0; 0 0 0]  % Computer's second move shown.]]\u003e\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\u003ePassing score is never losing.\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 contest engine is almost Random - about 4000 times. (\u0026lt;12% of Time-Out)\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\u003eCreating the perfect tic-tac-toe engine seemed unnecessary.\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\u003eYour Wins/Losses/Draws will be shown.\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\u003eOn a loss the losing board will be shown\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\u003eFollow-up game will be WOPR:Global Thermonuclear War.\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\"}]}"},{"id":726,"title":"Chezz_020 Pawn Attack and Slaying King","description":"Chezz:...A simplified Chess game. Two game match with future multiple skill levels via evolutionary upgrades.\r\n\r\nSimplified the rules to implement rapid move check. Normal Chess moves like \"a4\", Castling, and En Passant are accepted. Moves are simple vectors [idx_from idx_to promo] where promo is for pawns reaching the 8th rank. Piece names are numeric 1-6 White (P,R,N,B,Q,K) and Black is 7-12. Board empty space is 0.\r\n\r\nChezz unique rules: 1) To Win must remove opponent's King.. 2) Lose Turn if try an impossible move.. 3) Checks are ignored - allow move into check, castle out of check, castle thru/into check.. 4) Pawn promote to any piece, including opponent and vacuum.. 5) Six position repetition is a Draw..6) 50 moves No captures is a Draw..\r\n\r\n\r\n*Inputs:* (b,pmv,castle)\r\n\r\nPlayer always appears to be White. However, Black may apparently move first.\r\n\r\nb (8x8) array with empty=0, White Pieces 1-6, Blk Pieces 7-12\r\n\r\npmv [from to promo] vector of both players moves (needed for en passant option). Illegal move creates a [0 0 0] pmv value. Size [2*moves,3]\r\n\r\ncastle: 6 pt logical vector to show castling options. 1 if piece is eligible. Positions are [8 40 64 1 33 57] for [WQR WK WKR BQR BK BKR]. . . eg castle mv [40 56 0]\r\n\r\n\r\n\r\n*Output:* mv  [from to pawn_new_type] where from/to are 1-64 and pawn_new is 0 thru 12 if pawn is on final row.\r\n\r\n\r\n\r\nThe Test Suite will oversee two games with the Player as Black(moving second) and then as White.\r\n\r\n*Passing:* Player must Win both games\r\n\r\n*Evolution:* Winning player algorithms will be hosted as Chezz_xyz based on their wins over other Chezz algorithms. Champions and the basis will be fully credited in Chezz updates. Champions must be able to play self and Not Cody timeout.\r\n\r\nChezz_020: Pawn Attack then Mad King Attack\r\n\r\nLink update 12/27/12","description_html":"\u003cp\u003eChezz:...A simplified Chess game. Two game match with future multiple skill levels via evolutionary upgrades.\u003c/p\u003e\u003cp\u003eSimplified the rules to implement rapid move check. Normal Chess moves like \"a4\", Castling, and En Passant are accepted. Moves are simple vectors [idx_from idx_to promo] where promo is for pawns reaching the 8th rank. Piece names are numeric 1-6 White (P,R,N,B,Q,K) and Black is 7-12. Board empty space is 0.\u003c/p\u003e\u003cp\u003eChezz unique rules: 1) To Win must remove opponent's King.. 2) Lose Turn if try an impossible move.. 3) Checks are ignored - allow move into check, castle out of check, castle thru/into check.. 4) Pawn promote to any piece, including opponent and vacuum.. 5) Six position repetition is a Draw..6) 50 moves No captures is a Draw..\u003c/p\u003e\u003cp\u003e\u003cb\u003eInputs:\u003c/b\u003e (b,pmv,castle)\u003c/p\u003e\u003cp\u003ePlayer always appears to be White. However, Black may apparently move first.\u003c/p\u003e\u003cp\u003eb (8x8) array with empty=0, White Pieces 1-6, Blk Pieces 7-12\u003c/p\u003e\u003cp\u003epmv [from to promo] vector of both players moves (needed for en passant option). Illegal move creates a [0 0 0] pmv value. Size [2*moves,3]\u003c/p\u003e\u003cp\u003ecastle: 6 pt logical vector to show castling options. 1 if piece is eligible. Positions are [8 40 64 1 33 57] for [WQR WK WKR BQR BK BKR]. . . eg castle mv [40 56 0]\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e mv  [from to pawn_new_type] where from/to are 1-64 and pawn_new is 0 thru 12 if pawn is on final row.\u003c/p\u003e\u003cp\u003eThe Test Suite will oversee two games with the Player as Black(moving second) and then as White.\u003c/p\u003e\u003cp\u003e\u003cb\u003ePassing:\u003c/b\u003e Player must Win both games\u003c/p\u003e\u003cp\u003e\u003cb\u003eEvolution:\u003c/b\u003e Winning player algorithms will be hosted as Chezz_xyz based on their wins over other Chezz algorithms. Champions and the basis will be fully credited in Chezz updates. Champions must be able to play self and Not Cody timeout.\u003c/p\u003e\u003cp\u003eChezz_020: Pawn Attack then Mad King Attack\u003c/p\u003e\u003cp\u003eLink update 12/27/12\u003c/p\u003e","function_template":"function mv=player_move(b,pmv,castle)\r\n% Author: Richard Z\r\n% Bot name: First Pawn then King Attack\r\n% Chezz_020\r\n% http://tinyurl.com/matlab-chezz-Cmove020\r\n mv=zeros(1,3); % [from to promo] \r\n% This is your opponent\r\n\r\n pieces=1:6;\r\n enemy=7:12;\r\n \r\n king_mv=[7 8 9 -1 1 -9 -8 -7]; %\r\n king_attack=[-9 -8 -7 -1 1 7 8 9]; %\r\n \r\n pv=find(b==1); % vector of Pawns\r\n no_move=true;\r\n ptr=0;\r\n \r\n % loop thru piece types\r\n \r\n while no_move % Pawn\r\n  ptr=ptr+1;\r\n  if ptr\u003elength(pv),break;end\r\n  [r c]=ind2sub([8 8],pv(ptr));\r\n  if ismember(r,[1 8]),continue;end % end row for a P\r\n  if c==1\r\n   if ismember(b(pv(ptr)+7),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)+7 6];\r\n    return;\r\n   end\r\n  end\r\n  if c==8\r\n   if ismember(b(pv(ptr)-9),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)-9 6];\r\n    return;\r\n   end\r\n  end\r\n  if c\u003c8 \u0026\u0026 c\u003e1\r\n   if ismember(b(pv(ptr)+7),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)+7 6];\r\n    return;\r\n   end\r\n   if ismember(b(pv(ptr)-9),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)-9 6];\r\n    return;\r\n   end\r\n  end\r\n  \r\n  % Move fwd\r\n  if b(pv(ptr)-1)==0\r\n   mv=[pv(ptr) pv(ptr)-1 6];\r\n   return;\r\n  end\r\n  \r\n end % while no move Pawn\r\n\r\n% No pawn move found; Move on to King\r\n\r\n% No pawn moves left: Attack with King\r\n\r\npv=find(b==6); % Find Kings\r\nno_move=true;\r\nptr=0;\r\n\r\nwhile no_move % King captures\r\n  ptr=ptr+1;\r\n  if ptr\u003elength(pv),break;end\r\n  [r c]=ind2sub([8 8],pv(ptr));\r\n  for i=1:8 % 8 King attacks\r\n   if (pv(ptr)+king_attack(i)\u003e64) || (pv(ptr)+king_attack(i)\u003c1),continue;end\r\n   if r==1 \u0026\u0026 ismember(i,[1 4 6]),continue;end % make ~ismem 1:64\r\n   if r==8 \u0026\u0026 ismember(i,[3 5 8]),continue;end\r\n   if c==1 \u0026\u0026 ismember(i,[1 2 3]),continue;end\r\n   if c==8 \u0026\u0026 ismember(i,[6 7 8]),continue;end\r\n   if ismember(b(pv(ptr)+king_attack(i)),enemy) % capture\r\n    mv=[pv(ptr) pv(ptr)+king_attack(i) 0]; % errant r1 attack idx 1 to idx 8\r\n    return; % Attack found\r\n   end\r\n  end % 8 King attacks\r\nend % while King capture search\r\n\r\n% No King capture move found\r\nmyk=pv(1); % First King\r\nek=find(b==max(enemy),1); %Find an enemy king\r\n[kr1 kc1]=ind2sub([8 8],myk);\r\n[er1 ec1]=ind2sub([8 8],ek);\r\nif er1\u003c=kr1 \u0026\u0026 ec1\u003e=kc1,king_mv=[7 8 -1 9 -9 -8 1 -7];end\r\nif er1\u003e=kr1 \u0026\u0026 ec1\u003e=kc1,king_mv=[9 8  1 -7 7 -8 -1 -9];end\r\nif er1\u003c=kr1 \u0026\u0026 ec1\u003c=kc1,king_mv=[-9 -1 -8 -7 7 8 1 9];end\r\nif er1\u003e=kr1 \u0026\u0026 ec1\u003c=kc1,king_mv=[-7 1 -8 9 -9 8 -1 7];end\r\nfor i=1:8\r\n if kr1==1 \u0026\u0026 ismember(king_mv(i),[-9 -1 7]),continue;end\r\n if kr1==8 \u0026\u0026 ismember(king_mv(i),[-7 1 9]),continue;end\r\n if kc1==1 \u0026\u0026 ismember(king_mv(i),[-9 -8 -7]),continue;end\r\n if kc1==8 \u0026\u0026 ismember(king_mv(i),[7 8 9]),continue;end\r\n if ismember(b(myk+king_mv(i)),0) % empty\r\n  mv=[myk myk+king_mv(i) 0];\r\n  return; % path found\r\n end\r\nend\r\n% Hopefully don't get here as mv=[0 0 0]\r\n\r\nend % End of Computer Champion Bot","test_suite":"%%\r\n% Load in the routines that play the game and check move validity\r\ntic\r\nurlwrite('http://rmatlabtest.appspot.com/Chezz_Shell.m','Chezz_Shell.m') ;\r\nurlwrite('http://rmatlabtest.appspot.com/ghost_white.m','ghost_white.m') \r\nurlwrite('http://rmatlabtest.appspot.com/computer_move020.m','computer_move.m') \r\nurlwrite('http://rmatlabtest.appspot.com/mov_chk.m','mov_chk.m') \r\nrehash path\r\ntoc\r\n%%\r\n% Play Two Chezz games\r\n% Player must win Twice to Pass\r\nglobal pmvout1 pmvout2\r\ntic\r\n wins=0; % player wins\r\n b=zeros(8);% P 1/7 R 2/8 N 3/9 B 4/10 Q 5/11 K 6/12 \r\n b(2,:)=7;\r\n b(1,:)=[8 9 10 11 12 10 9 8]; % Player 7:12\r\n b(7,:)=1;\r\n b(8,:)=b(1,:)-6; % Computer 1:6\r\n b_orig=b;\r\n \r\n %mv=zeros(1,3); % [from to promo)]  \r\n %computer_wht=0; % 0 Computer plays wht\r\n %computer_wht=1; % 1 Computer plays black\r\n \r\n pmv=zeros(1,3); % [from to promo)] Opponents last move\r\n castle=[1 1 1 1 1 1];\r\n % False move call to satisfy Cody Rqmt of TestSuite match Ref Solution\r\n mv=player_move(b,pmv,castle)\r\n  \r\n for computer_wht=0:1\r\n  pmvout1=pmv; % Store game 1 moves\r\n  \r\n  dbg=0;\r\n  game_over=false;\r\n  b=b_orig;\r\n  no_capture=0;\r\n  b_hist=zeros(102,8,8);\r\n  pmv=zeros(1,3); % [from to promo)] Opponents last move\r\n  castle=[1 1 1 1 1 1]; % History of if ever moved w/b Castles/kings\r\n %  idx 8 40 64  1 33 57\r\n \r\n while ~game_over\r\n  mvP=zeros(1,3); % [from to type/promote)] \r\n  % Shell 0=Blk,1=Wht;Board;move,prev move;\r\n  % function (1 Play Comp, 2 Player, 3 Check mv)\r\n  \r\n  % White move\r\n  if computer_wht==0\r\n   [mvP]=Chezz_Shell(0,b,mvP,pmv,castle,1); % 0 Wht,... 1 Computer\r\n  else\r\n   [mvP]=Chezz_Shell(0,b,mvP,pmv,castle,2); % 0 Wht  2 is player\r\n  end\r\n  \r\n  \r\n  [mv]=Chezz_Shell(0,b,mvP,pmv(end,:),castle,3); % 0 Wht,..., 3 Check\r\n  pmv=[pmv;mv(1:3)];\r\n  capture=false;\r\n  % Board changes only occur in Suite code\r\n  if mv(1)~=0 % Valid move determined by mv_chk\r\n   if mv(4)==0 % Normal moves and promotions\r\n    if b(mv(2))~=0,capture=true;end\r\n     b(mv(2))=b(mv(1)); % potential promotion\r\n     b(mv(1))=0;\r\n     if ismember(b(mv(2)),[1 7])\r\n      if ismember(mv(2),[1:8:57 8:8:64])\r\n       b(mv(2))=mv(3); % Place promoted selection\r\n      end\r\n     end\r\n     \r\n   else % ep or castle by White\r\n    if mv(4)==1 % castle 0-0 or 0-0-0\r\n      b(mv(1))=0;\r\n      b(mv(2))=6;\r\n      if mv(2)==24\r\n        b(8)=0;b(32)=2;  \r\n      else\r\n        b(64)=0;b(48)=2;\r\n      end    \r\n    end % castle\r\n    if mv(4)==2 % ep\r\n      capture=true;\r\n      b(mv(1))=0;\r\n      b(mv(2))=1; % White Pawn\r\n      b(mv(2)+1)=0; % Take pawn that passed\r\n    end % end ep\r\n   end % move implemented\r\n  end % end move\r\n  \r\n  %b % display board after move\r\n  \r\n  if isempty(find(b==12,1))\r\n   % Game over : Black King Captured\r\n   game_over=true; % change to if comp=wht or blk for win\r\n   if computer_wht==1 % Blk Computer King; Player is Wht captured Blk King \r\n    wins=wins+1;\r\n   end\r\n   continue;\r\n  end\r\n  \r\n  castle=castle.*logical([b(8)==2 b(40) b(64)==2 b(1)==8 b(33) b(57)==8]);\r\n  \r\n  if ~capture\r\n   no_capture=no_capture+1;\r\n   if no_capture\u003e100\r\n       fprintf('Draw 100 moves no capture\\n');\r\n       game_over=true;\r\n   end % move is b and w\r\n   b_hist(no_capture,:,:)=b;\r\n  else\r\n   no_capture=1;\r\n   b_hist=b_hist*0;\r\n   b_hist(no_capture,:,:)=b;\r\n  end\r\n   \r\n  % Black Move\r\n  mvP=zeros(1,4); % [from to type/promote specials(castle=1/ep=2)] \r\n if computer_wht==0\r\n   [mvP]=Chezz_Shell(1,b,mvP,pmv,castle,2); % 2 Blk,... 2 is player\r\n  else\r\n   [mvP]=Chezz_Shell(1,b,mvP,pmv,castle,1); % 2 Blk  1 is Computer\r\n end\r\n  \r\n  [mv]=Chezz_Shell(1,b,mvP,pmv(end,:),castle,3); % 2 Blk,..., 3 Check\r\n  \r\n pmv=[pmv;mv(1:3)];\r\n capture=false;\r\n  % Board changes only occur in Suite code\r\n  if mv(1)~=0 % Valid move determined by mv_chk\r\n   if mv(4)==0 % Normal moves and promotions\r\n    if b(mv(2))~=0,capture=true;end\r\n    b(mv(2))=b(mv(1)); % potential promotion\r\n    b(mv(1))=0;\r\n    if ismember(b(mv(2)),[1 7])\r\n     if ismember(mv(2),[1:8:57 8:8:64])\r\n      b(mv(2))=mv(3); % Place promoted selection\r\n     end\r\n    end\r\n   else % ep or castle by Black\r\n    if mv(4)==1 % castle 0-0 or 0-0-0\r\n      b(mv(1))=0;\r\n      b(mv(2))=12;\r\n      if mv(2)==49 % Blk 0-0\r\n        b(57)=0;b(41)=8;  \r\n      else % Blk 0-0-0\r\n        b(1)=0;b(25)=8;\r\n      end    \r\n    end % castle\r\n    if mv(4)==2 % ep by Black\r\n      capture=true;\r\n      b(mv(1))=0;\r\n      b(mv(2))=mv(3);\r\n    % White passed black on prior move\r\n      b(mv(2)-1)=0; \r\n    end % end ep\r\n   end % move implemented\r\n  end % end move\r\n  \r\n%   b\r\n%   figure(1);imagesc(b,[0 12]);axis equal;\r\n  \r\n  if isempty(find(b==6,1))\r\n   % Game over : Blk captured White King\r\n   game_over=true;\r\n   if computer_wht==0 % Wht Computer King; Player is Blk captured Wht King\r\n    wins=wins+1;\r\n   end\r\n   continue\r\n  end\r\n  \r\n  castle=castle.*logical([b(8)==2 b(40) b(64)==2 b(1)==8 b(33) b(57)==8]);\r\n  \r\n  if ~capture\r\n   no_capture=no_capture+1;\r\n   if no_capture\u003e100\r\n       fprintf('Draw 100 moves no capture\\n');\r\n       game_over=true;\r\n   end % move is b and w\r\n   b_hist(no_capture,:,:)=b;\r\n   % Check for 3 position repetition\r\n   for i=1:no_capture-1\r\n    cdelta=0;\r\n    for j=i+1:no_capture\r\n     delta=(b_hist(i,:,:)-b_hist(j,:,:));\r\n     if sum(abs(delta(:)))==0\r\n      cdelta=cdelta+1;\r\n     end\r\n    end\r\n    if cdelta\u003e=7 % repetitions 3 identical setups\r\n     fprintf('Game over due to repetition\\n');\r\n     game_over=true;\r\n    end\r\n   end % rep check loop \r\n  else\r\n   no_capture=1;\r\n   b_hist=b_hist*0;\r\n   b_hist(no_capture,:,:)=b;\r\n  end % ~capture\r\n  \r\n end % While ~game_over\r\n \r\n end % wht_blk\r\n\r\npmvout2=pmv;\r\nwins\r\n \r\n% Player must win Twice to Pass\r\ntoc\r\nassert(isequal(wins,2))\r\n\r\n%%\r\nglobal pmvout1 pmvout2\r\n% Output moves for games\r\n\r\n% Output game 2 moves\r\npmv=pmvout1;\r\n   for i=2:3:size(pmv,1)-3\r\n    fprintf('%2i %2i %2i %2i %2i %2i %2i %2i %2i \\n',pmv(i,1:3),pmv(i+1,1:3),pmv(i+2,1:3));\r\n   end\r\n   fprintf('%2i %2i %2i\\n',pmv(end-2,:));\r\n   fprintf('%2i %2i %2i\\n',pmv(end-1,:));\r\n   fprintf('%2i %2i %2i\\n',pmv(end,:));\r\n \r\n% Output game 2 moves\r\npmv=pmvout2;\r\n for i=2:3:size(pmv,1)-3\r\n  fprintf('%2i %2i %2i %2i %2i %2i %2i %2i %2i \\n',pmv(i,1:3),pmv(i+1,1:3),pmv(i+2,1:3));\r\n  end\r\n% repeat of moves\r\n fprintf('%2i %2i %2i\\n',pmv(end-2,:));\r\n fprintf('%2i %2i %2i\\n',pmv(end-1,:));\r\n fprintf('%2i %2i %2i\\n',pmv(end,:));","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":1,"test_suite_updated_at":"2012-12-27T22:44:29.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-05-28T06:59:32.000Z","updated_at":"2012-12-27T22:44:29.000Z","published_at":"2012-05-28T07:41:09.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\u003eChezz:...A simplified Chess game. Two game match with future multiple skill levels via evolutionary upgrades.\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\u003eSimplified the rules to implement rapid move check. Normal Chess moves like \\\"a4\\\", Castling, and En Passant are accepted. Moves are simple vectors [idx_from idx_to promo] where promo is for pawns reaching the 8th rank. Piece names are numeric 1-6 White (P,R,N,B,Q,K) and Black is 7-12. Board empty space is 0.\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\u003eChezz unique rules: 1) To Win must remove opponent's King.. 2) Lose Turn if try an impossible move.. 3) Checks are ignored - allow move into check, castle out of check, castle thru/into check.. 4) Pawn promote to any piece, including opponent and vacuum.. 5) Six position repetition is a Draw..6) 50 moves No captures is a Draw..\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\u003eInputs:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (b,pmv,castle)\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 always appears to be White. However, Black may apparently move first.\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\u003eb (8x8) array with empty=0, White Pieces 1-6, Blk Pieces 7-12\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\u003epmv [from to promo] vector of both players moves (needed for en passant option). Illegal move creates a [0 0 0] pmv value. Size [2*moves,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\u003ecastle: 6 pt logical vector to show castling options. 1 if piece is eligible. Positions are [8 40 64 1 33 57] for [WQR WK WKR BQR BK BKR]. . . eg castle mv [40 56 0]\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\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e mv [from to pawn_new_type] where from/to are 1-64 and pawn_new is 0 thru 12 if pawn is on final row.\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 Test Suite will oversee two games with the Player as Black(moving second) and then as White.\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\u003ePassing:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Player must Win both games\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\u003eEvolution:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Winning player algorithms will be hosted as Chezz_xyz based on their wins over other Chezz algorithms. Champions and the basis will be fully credited in Chezz updates. Champions must be able to play self and Not Cody timeout.\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\u003eChezz_020: Pawn Attack then Mad King Attack\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\u003eLink update 12/27/12\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\"}]}"},{"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:\"multi-call\"","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:\"multi-call\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"multi-call\"","","\"","multi-call","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f73636e41b8\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f73636e4118\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f73636e2ef8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f73636e4898\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f73636e4758\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f73636e4578\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f73636e4258\u003e":"tag:\"multi-call\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f73636e4258\u003e":"tag:\"multi-call\""},"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:\"multi-call\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"multi-call\"","","\"","multi-call","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f73636e41b8\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f73636e4118\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f73636e2ef8\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f73636e4898\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f73636e4758\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f73636e4578\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f73636e4258\u003e":"tag:\"multi-call\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f73636e4258\u003e":"tag:\"multi-call\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":739,"difficulty_rating":"easy"},{"id":702,"difficulty_rating":"hard"},{"id":726,"difficulty_rating":"unrated"},{"id":740,"difficulty_rating":"unrated"}]}}