Cody

Problem 715. Chezz_015 : Simplified chess

Solution 184057

Submitted on 1 Jan 2013 by Susan
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_move015.m','computer_move.m') urlwrite('http://rmatlabtest.appspot.com/mov_chk.m','mov_chk.m') rehash path toc

ans = /users/msssystem10/ghost_white.m ans = /users/msssystem10/computer_move.m ans = /users/msssystem10/mov_chk.m [Warning: Function /opt/mlsedu/mdcsserver/latest/m/web_common/shadow/license.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 226 In verifyCode at 40 In fevalJSON at 14] [Warning: Function /opt/mlsedu/mdcsserver/latest/m/common/shadow/home.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 226 In verifyCode at 40 In fevalJSON at 14] [Warning: Function /opt/mlsedu/mdcsserver/latest/m/common/shadow/keyboard.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 226 In verifyCode at 40 In fevalJSON at 14] [Warning: Function /opt/mlsedu/mdcsserver/latest/m/common/shadow/more.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 226 In verifyCode at 40 In fevalJSON at 14] [Warning: Function /opt/mlsedu/mdcsserver/latest/m/common/shadow/pause.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In verifyCode>evaluateCode at 226 In verifyCode at 40 In fevalJSON at 14] Elapsed time is 4.456301 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 = 7 6 6 wins = 2 Elapsed time is 0.570429 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,:));

7 6 6 2 3 12 6 5 6 3 4 12 15 14 6 10 11 12 14 13 6 4 13 12 5 4 6 13 14 12 4 11 6 14 23 12 11 18 6 23 32 12 18 25 6 26 27 12 31 30 6 27 28 12 30 29 6 34 35 12 39 38 6 35 36 12 29 36 6 28 29 12 36 35 6 29 38 12 35 42 6 38 47 12 42 49 6 47 56 12 55 54 6 50 51 12 54 53 6 51 52 12 63 62 6 58 59 12 62 61 6 52 61 12 53 52 6 61 62 12 52 59 6 62 63 12 59 58 6 32 24 0 0 0 0 24 16 0 0 0 0 16 8 0 0 0 0 33 25 0 0 0 0 56 48 0 0 0 0 48 40 0 0 0 0 8 15 0 0 0 0 15 22 0 0 0 0 22 29 0 0 0 0 29 36 0 0 0 0 25 33 0 0 0 0 40 47 0 0 0 0 36 43 0 0 0 0 33 42 0 0 0 0 42 49 0 7 6 6 2 3 12 6 5 6 3 4 12 15 14 6 10 11 12 14 13 6 4 13 12 5 4 6 13 14 12 4 11 6 14 23 12 11 18 6 23 32 12 18 25 6 26 27 12 31 30 6 27 28 12 30 29 6 34 35 12 39 38 6 35 36 12 29 36 6 28 29 12 36 35 6 29 38 12 35 42 6 38 47 12 42 49 6 47 56 12 55 54 6 50 51 12 54 53 6 51 52 12 63 62 6 58 59 12 62 61 6 52 61 12 53 52 6 61 62 12 52 59 6 62 63 12 59 58 6 0 0 0 25 17 0 0 0 0 17 9 0 0 0 0 9 1 0 0 0 0 40 32 0 0 0 0 49 41 0 0 0 0 41 33 0 0 0 0 1 10 0 0 0 0 10 19 0 0 0 0 19 28 0 0 0 0 28 37 0 0 0 0 32 40 0 0 0 0 33 42 0 0 0 0 37 46 0 0 0 0 40 47 0 0 0 0 40 47 0 0 0 0 47 56 0