Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 111251

Submitted on 12 Jul 2012 by Susan

Correct

237Size
Leading solution size is 56.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

This solution is outdated. To rescore this solution, log in.

Test Suite

Test
Code Input and Output
1
Pass
 
%%
% Load in the routines that play the game and check move validity
tic
urlwrite('http://tinyurl.com/matlab-chezz-shell','Chezz_Shell.m') ;
urlwrite('http://tinyurl.com/matlab-chezz-ghost','ghost_white.m')
urlwrite('http://tinyurl.com/matlab-chezz-Cmove000','computer_move.m')
urlwrite('http://tinyurl.com/matlab-chezz-movchk','mov_chk.m')
rehash path
toc
ans =
/users/msssystem1/ghost_white.m
ans =
/users/msssystem1/computer_move.m
ans =
/users/msssystem1/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 189
  In verifyCode at 37
  In fevalJSON at 14}
{Warning: Function
/opt/mlsedu/mdcsserver/latest/m/web_common/shadow/graphicsAndGuis/uicontrol.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 189
  In verifyCode at 37
  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 189
  In verifyCode at 37
  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 189
  In verifyCode at 37
  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 189
  In verifyCode at 37
  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 189
  In verifyCode at 37
  In fevalJSON at 14}
Elapsed time is 6.727390 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
 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))
wins =
     2
Elapsed time is 0.214042 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  2  3 12  0  0  0
 3  4 12  0  0  0  4  5 12
 0  0  0  5  6 12  0  0  0
 6 15 12  0  0  0 15 24 12
 0  0  0 10 11 12  0  0  0
11 12 12  0  0  0 12 13 12
 0  0  0 13 14 12  0  0  0
14 23 12  0  0  0 23 32 12
 0  0  0 18 19 12  0  0  0
19 20 12  0  0  0 20 21 12
 0  0  0 21 22 12  0  0  0
22 31 12
 0  0  0
31 40 12
 7  6  6  0  0  0  6  5  6
 0  0  0  5  4  6  0  0  0
 4  3  6  0  0  0  3 10  6
 0  0  0 10 17  6  0  0  0
15 14  6  0  0  0 14 13  6
 0  0  0 13 12  6  0  0  0
12 11  6  0  0  0 11 18  6
 0  0  0 18 25  6  0  0  0
23 22  6  0  0  0 22 21  6
 0  0  0 21 20  6  0  0  0
20 19  6  0  0  0 19 26  6
19 26  6
 0  0  0
26 33  6