Cody

Solution 107661

Submitted on 7 Jul 2012 by Richard Zapor
  • Size: 56
  • This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
%% % Load in the routines that play the game and check move validity tic urlwrite('http://rmatlabtest.appspot.com/Chezz_Shell.m','Chezz_Shell.m') ; urlwrite('http://rmatlabtest.appspot.com/ghost_white.m','ghost_white.m') urlwrite('http://rmatlabtest.appspot.com/computer_move000.m','computer_move.m') urlwrite('http://rmatlabtest.appspot.com/mov_chk.m','mov_chk.m') rehash path toc
ans = /users/msssystem19/ghost_white.m ans = /users/msssystem19/computer_move.m ans = /users/msssystem19/mov_chk.m [Warning: Function clc has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function diary has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function license has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function mex has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function home has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function keyboard has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function more has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function pause has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] [Warning: Function simulink has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 238 In verifyCode at 43 In fevalJSON at 14] Elapsed time is 0.364523 seconds.
2   Pass
%% % Play Two Chezz games % Player must win Twice to Pass global pmvout1 pmvout2 tic wins=0; % player wins b=zeros(8);% P 1/7 R 2/8 N 3/9 B 4/10 Q 5/11 K 6/12 b(2,:)=7; b(1,:)=[8 9 10 11 12 10 9 8]; % Player 7:12 b(7,:)=1; b(8,:)=b(1,:)-6; % Computer 1:6 b_orig=b; %mv=zeros(1,3); % [from to promo)] %computer_wht=0; % 0 Computer plays wht %computer_wht=1; % 1 Computer plays black pmv=zeros(1,3); % [from to promo)] Opponents last move castle=[1 1 1 1 1 1]; % False move call to satisfy Cody Rqmt of TestSuite match Ref Solution mv=player_move(b,pmv,castle) for computer_wht=0:1 pmvout1=pmv; % Store game 1 moves dbg=0; game_over=false; b=b_orig; no_capture=0; b_hist=zeros(102,8,8); pmv=zeros(1,3); % [from to promo)] Opponents last move castle=[1 1 1 1 1 1]; % History of if ever moved w/b Castles/kings % idx 8 40 64 1 33 57 while ~game_over mvP=zeros(1,3); % [from to type/promote)] % Shell 0=Blk,1=Wht;Board;move,prev move; % function (1 Play Comp, 2 Player, 3 Check mv) % White move if computer_wht==0 [mvP]=Chezz_Shell(0,b,mvP,pmv,castle,1); % 0 Wht,... 1 Computer else [mvP]=Chezz_Shell(0,b,mvP,pmv,castle,2); % 0 Wht 2 is player end [mv]=Chezz_Shell(0,b,mvP,pmv(end,:),castle,3); % 0 Wht,..., 3 Check pmv=[pmv;mv(1:3)]; capture=false; % Board changes only occur in Suite code if mv(1)~=0 % Valid move determined by mv_chk if mv(4)==0 % Normal moves and promotions if b(mv(2))~=0,capture=true;end b(mv(2))=b(mv(1)); % potential promotion b(mv(1))=0; if ismember(b(mv(2)),[1 7]) if ismember(mv(2),[1:8:57 8:8:64]) b(mv(2))=mv(3); % Place promoted selection end end else % ep or castle by White if mv(4)==1 % castle 0-0 or 0-0-0 b(mv(1))=0; b(mv(2))=6; if mv(2)==24 b(8)=0;b(32)=2; else b(64)=0;b(48)=2; end end % castle if mv(4)==2 % ep capture=true; b(mv(1))=0; b(mv(2))=1; % White Pawn b(mv(2)+1)=0; % Take pawn that passed end % end ep end % move implemented end % end move %b % display board after move if isempty(find(b==12,1)) % Game over : Black King Captured game_over=true; % change to if comp=wht or blk for win if computer_wht==1 % Blk Computer King; Player is Wht captured Blk King wins=wins+1; end continue; end castle=castle.*logical([b(8)==2 b(40) b(64)==2 b(1)==8 b(33) b(57)==8]); if ~capture no_capture=no_capture+1; if no_capture>100 fprintf('Draw 100 moves no capture\n'); game_over=true; end % move is b and w b_hist(no_capture,:,:)=b; else no_capture=1; b_hist=b_hist*0; b_hist(no_capture,:,:)=b; end % Black Move mvP=zeros(1,4); % [from to type/promote specials(castle=1/ep=2)] if computer_wht==0 [mvP]=Chezz_Shell(1,b,mvP,pmv,castle,2); % 2 Blk,... 2 is player else [mvP]=Chezz_Shell(1,b,mvP,pmv,castle,1); % 2 Blk 1 is Computer end [mv]=Chezz_Shell(1,b,mvP,pmv(end,:),castle,3); % 2 Blk,..., 3 Check pmv=[pmv;mv(1:3)]; capture=false; % Board changes only occur in Suite code if mv(1)~=0 % Valid move determined by mv_chk if mv(4)==0 % Normal moves and promotions if b(mv(2))~=0,capture=true;end b(mv(2))=b(mv(1)); % potential promotion b(mv(1))=0; if ismember(b(mv(2)),[1 7]) if ismember(mv(2),[1:8:57 8:8:64]) b(mv(2))=mv(3); % Place promoted selection end end else % ep or castle by Black if mv(4)==1 % castle 0-0 or 0-0-0 b(mv(1))=0; b(mv(2))=12; if mv(2)==49 % Blk 0-0 b(57)=0;b(41)=8; else % Blk 0-0-0 b(1)=0;b(25)=8; end end % castle if mv(4)==2 % ep by Black capture=true; b(mv(1))=0; b(mv(2))=mv(3); % White passed black on prior move b(mv(2)-1)=0; end % end ep end % move implemented end % end move % b % figure(1);imagesc(b,[0 12]);axis equal; if isempty(find(b==6,1)) % Game over : Blk captured White King game_over=true; if computer_wht==0 % Wht Computer King; Player is Blk captured Wht King wins=wins+1; end continue end castle=castle.*logical([b(8)==2 b(40) b(64)==2 b(1)==8 b(33) b(57)==8]); if ~capture no_capture=no_capture+1; if no_capture>100 fprintf('Draw 100 moves no capture\n'); game_over=true; end % move is b and w b_hist(no_capture,:,:)=b; % Check for 3 position repetition for i=1:no_capture-1 cdelta=0; for j=i+1:no_capture delta=(b_hist(i,:,:)-b_hist(j,:,:)); if sum(abs(delta(:)))==0 cdelta=cdelta+1; end end if cdelta>=7 % repetitions 3 identical setups fprintf('Game over due to repetition\n'); game_over=true; end end % rep check loop else no_capture=1; b_hist=b_hist*0; b_hist(no_capture,:,:)=b; end % ~capture end % While ~game_over end % wht_blk pmvout2=pmv; wins % Player must win Twice to Pass toc assert(isequal(wins,2))
mv = 39 38 0 wins = 2 Elapsed time is 0.072045 seconds.
3   Pass
%% global pmvout1 pmvout2 % Output moves for games % Output game 2 moves pmv=pmvout1; for i=2:3:size(pmv,1)-3 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)); end fprintf('%2i %2i %2i\n',pmv(end-2,:)); fprintf('%2i %2i %2i\n',pmv(end-1,:)); fprintf('%2i %2i %2i\n',pmv(end,:)); % Output game 2 moves pmv=pmvout2; for i=2:3:size(pmv,1)-3 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)); end % repeat of moves fprintf('%2i %2i %2i\n',pmv(end-2,:)); fprintf('%2i %2i %2i\n',pmv(end-1,:)); fprintf('%2i %2i %2i\n',pmv(end,:));
0 0 0 34 35 0 0 0 0 25 61 0 0 0 0 61 47 0 61 47 0 0 0 0 47 40 0 39 38 0 0 0 0 32 60 0 0 0 0 60 42 0 0 0 0 60 42 0 0 0 0 42 33 0