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

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# Solution 93684

Submitted on 30 May 2012 by Richard Zapor

Correct

2094Size
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))
```
```0  0  6 13 59 52 13  4
52 61 15  6 50 59  6 13
59 52  8 15 57 50 13 20
27 13 15  6 52 45  6 20
11 29 22 36 43 29 38 20
50 59 54 36 61 54 47 61
59 52 61 43 34 52  4 11
18  4 20 11  2 20 24 15
52 61 15  6 61 54 63 45
41 50  6 13 20  6 31 22
50 59 22 13  4 22 36 27
59 52 45 59 25 34 27 41
22 31 40 22  9 18 22 13
18 27 13  4 27 36  4 11
36 45 11  2 45 54  2  9
54 63 56 47 63 56 47 38
6 15 38 29 15 24 29 20
56 47 20 11 47 40 11  2
40 31 59 50 31 22 50 57
24 15  9 18 22 13  2  9
15  8  9  2 13  6  2 11
8 15 11  4 15  8  4 13
6 20 18 11 20  2 41 34
8 15 34 27 15  8 27 18
8 15 18 11  2 20 57 50
20 13 50 43 15  8 43 36
13  6 36 29  8 15 29 22
36 29
8 15
29 22
15 29
wins =
2
0  0  6 13 59 52 13  4
52 61 15  6 50 59  6 13
59 52  8 15 57 50 13 20
27 13 15  6 52 45  6 20
11 29 22 36 43 29 38 20
50 59 54 36 61 54 47 61
59 52 61 43 34 52  4 11
18  4 20 11  2 20 24 15
52 61 15  6 61 54 63 45
41 50  6 13 20  6 31 22
50 59 22 13  4 22 36 27
59 52 45 59 25 34 27 41
22 31 40 22  9 18 22 13
18 27 13  4 27 36  4 11
36 45 11  2 45 54  2  9
54 63 56 47 63 56 47 38
6 15 38 29 15 24 29 20
56 63 20 11 63 54 11  2
54 61 59 50 61 52 50 57
52 59  9 18 59 50 41 59
24 31  2  9 31 38  9  2
38 45
2  9
45 52
59 45
wins =
2
Elapsed time is 0.541242 seconds.
```