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 135343

Submitted on 5 Sep 2012 by Aurelien Queffurust

Correct

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

Test Suite

Test
Code Input and Output
1
Pass
 
%%
%Test Suite
tic
 wins=0; % player wins
 b=zeros(8);% WP 1 BP 3  [WK 2 BK 4]
 b([2 9 11 18 25 27 34 41 43 50 57 59])=3;
 b([6 8 15 22 24 31 38 40 47 54 56 63])=1;
 b_orig=b;
 %mv=zeros(1,2); % [from to promo)]
 %computer_wht=0; % 0 Computer plays wht
 %computer_wht=1; % 1 Computer plays black
 pmv=zeros(1,2); % Move History for record only
 for computer_wht=0:1
  if size(pmv,1)>3
   for i=1:4:size(pmv,1)-4
    fprintf('%2i %2i %2i %2i %2i %2i %2i %2i\n',pmv(i,1:2),pmv(i+1,1:2),pmv(i+2,1:2),pmv(i+3,1:2));
   end
   fprintf('%2i %2i\n',pmv(end-3,:));
   fprintf('%2i %2i\n',pmv(end-2,:));
   fprintf('%2i %2i\n',pmv(end-1,:));
   fprintf('%2i %2i\n',pmv(end,:));
 % Output game 1 moves
  end
  %pmv % Output move history from Game 1
  game_over=false;
  b=b_orig; % Reset for second game
  no_capture=0;
  pmv=zeros(1,2); % [from to promo)] Opponents last move
 while ~game_over
  mvP=zeros(1,2); % [from to]
  % 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]=Checkerz_Shell(0,b,mvP,1); % 0 Wht,... 1 Computer
  else
   [mvP]=Checkerz_Shell(0,b,mvP,2); % 0 Wht  2 is player
  end
  [mv]=Checkerz_Shell(0,b,mvP,3); % 0 Wht,..., 3 Check
  pmv=[pmv;mv(1:2)];
  capture=false;
  % Board changes only occur in Suite code
  if mv(1)~=0 % Valid move determined by mv_chk
    b(mv(2))=b(mv(1)); % potential promotion
    b(mv(1))=0;
    if ismember(mv(2),[9 25 41 57])
     b(mv(2))=2; % Kinged
    end
    if abs(mv(1)-mv(2))>9
     % Piece jumped
     capture=true;
     b((mv(1)+mv(2))/2)=0;
    end
  end % end move
  if isempty(find(b>2)) % White has captured All Blk Pieces
   game_over=true; % change to if comp=wht or blk for win
   if computer_wht==1 % Blk Computer Loses; Player is Wht
    wins=wins+1;
   end
   continue;
  end
  if mv(1)==0 % No legal move or missed jump
   % Game over : White failed to move and Loses
   game_over=true; % change to if comp=wht or blk for win
   if computer_wht==0 % Wht Computer Loses; Player is Blk
    wins=wins+1;
   end
   continue;
  end
  if ~capture
   no_capture=no_capture+1;
   if no_capture>100
       fprintf('Draw 100 moves no capture\n');
       game_over=true;
   end %
  else
   no_capture=1;
  end
  % Black Move
  mvP=[0 0]; % [from to type/promote specials(castle=1/ep=2)]
 if computer_wht==0
   [mvP]=Checkerz_Shell(1,b,mvP,2); % 2 Blk,... 2 is player
  else
   [mvP]=Checkerz_Shell(1,b,mvP,1); % 2 Blk  1 is Computer
 end
  [mv]=Checkerz_Shell(1,b,mvP,3); % 2 Blk,..., 3 Check
 pmv=[pmv;mv(1:2)];
 capture=false;
  % Board changes only occur in Suite code
  if mv(1)~=0 % Valid move determined by mv_chk
    b(mv(2))=b(mv(1)); % potential promotion
    b(mv(1))=0;
    if ismember(mv(2),[8 24 40 56])
     b(mv(2))=4; % Kinged
    end
    if abs(mv(1)-mv(2))>9
     % Piece jumped
     capture=true;
     b((mv(1)+mv(2))/2)=0;
    end
  end % end move
  if isempty([find(b==1)' find(b==2)']) % Black has captured All Wht Pieces
   game_over=true; % change to if comp=wht or blk for win
   if computer_wht==0 % Wht Computer Loses; Player is Blk
    wins=wins+1;
   end
   continue;
  end
  if mv(1)==0 % No legal move or missed jump by Black
   % Game over : Black failed to move correctly and Loses
   game_over=true; % change to if comp=wht or blk for win
   if computer_wht==1 % Blk Computer Loses; Player is Wht
    wins=wins+1;
   end
   continue;
  end
  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
  else
   no_capture=1;
  end % ~capture
 end % While ~game_over
 end % wht_blk
 wins
   if size(pmv,1)>3
    for i=1:4:size(pmv,1)-4
     fprintf('%2i %2i %2i %2i %2i %2i %2i %2i\n',pmv(i,1:2),pmv(i+1,1:2),pmv(i+2,1:2),pmv(i+3,1:2));
    end
    fprintf('%2i %2i\n',pmv(end-3,:));
    fprintf('%2i %2i\n',pmv(end-2,:));
    fprintf('%2i %2i\n',pmv(end-1,:));
    fprintf('%2i %2i\n',pmv(end,:));
 % Output game 2 moves?
  end
wins
toc
 % Player must beat computer twice
 %assert(isequal(wins,2))
Pass=1;
assert(isequal(Pass,1))
wins =
     0
wins =
     0
Elapsed time is 0.070316 seconds.