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

Solution 99542

Submitted on 18 Jun 2012 by Richard Zapor

Correct

2Size
`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

```% Passing Criteria modified to W-Ties/4 > Losses
% 10% of all matches against perfect bot are guaranteed losses
global scr
doDraw=false; %set to true for display
%doDraw=true; %set to true for display
params=struct('n',50,'str','NESW','D',[-1,50,1,-50],'wins',[0,0,0]);
y=clock;
rand('state',10000*y(6));
map50=str2num('reshape([ones(1,101)*2 ones(1,46)*3 2 2 zeros(1,2202) 3 4 4 ones(1,47) 3 4 zeros(1,97)],50,50)+(reshape([zeros(1,53) ones(1,44)*3 zeros(1,6) ones(1,44)*4 zeros(1,6) 3 ones(1,43)*4 zeros(1,6) ones(1,43)*2 3 zeros(1,2106) ones(1,44)*2 zeros(1,6) ones(1,44)*2 zeros(1,53)],50,50))''');
% rand('state',0); % Modify to make non-repeating
for nboards=1:50
% initialize board
params.board=zeros(params.n);
params.board([1,end],:)=-3;
params.board(:,[1,end])=-3;
[i,j]=find(params.board|1); % creates idx  to [r,c] via magic
[nill,pos1]=max((i==2|j==2)+~params.board(:)+rand(params.n*params.n,1));
params.board(pos1)=1;
[nill,pos2]=max((i==2|j==2)+~params.board(:)+rand(params.n*params.n,1));
params.board(pos2)=2;
orig_pos1=pos1;
orig_pos2=pos2;
for game=1:2
if game==2 % Play reversed starting positions
params.board=zeros(params.n);
params.board([1,end],:)=-3;
params.board(:,[1,end])=-3;
pos1=orig_pos2;
pos2=orig_pos1;
params.board(pos1)=1;
params.board(pos2)=2;
end
while 1
% blue's next movement
params.state=rand('state');
assignin('caller','params',params);
try
d1=tron(params.board); % Player Move
catch
d1='S';
end
params=evalin('caller','params');
rand('state',params.state);
% red's next movement
% Creator: Richard Zapor
% BotName: Map50 vs Clockwise Bot
% Date: 6/8/2012
str='NESW';
%  map50=str2num('reshape([ones(1,101)*2 ones(1,46)*3 2 2 zeros(1,2202) 3 4 4 ones(1,47) 3 4 zeros(1,97)],50,50)+(reshape([zeros(1,53) ones(1,44)*3 zeros(1,6) ones(1,44)*4 zeros(1,6) 3 ones(1,43)*4 zeros(1,6) ones(1,43)*2 3 zeros(1,2106) ones(1,44)*2 zeros(1,6) ones(1,44)*2 zeros(1,53)],50,50))''');
try
move=str(map50(find(params.board==2)));
catch
move='S';
end
d2=move;
% evaluate movements
d1=findstr(params.str,d1);
d2=findstr(params.str,d2);
assert(~isempty(d1));
params.board(pos1)=-1;
params.board(pos2)=-2;
pos1=pos1+params.D(d1);
pos2=pos2+params.D(d2);
if pos1==pos2||(params.board(pos1)&&params.board(pos2)), %tie
params.wins(2)=params.wins(2)+1;
if doDraw, disp('tie'); end
break;
elseif params.board(pos1), %lose
if doDraw, disp('lose'); end
%pause(0.5)
params.wins(3)=params.wins(3)+1;
break;
elseif params.board(pos2), %win
if doDraw, disp('win'); end
params.wins(1)=params.wins(1)+1;
break;
end
params.board(pos1)=1;
params.board(pos2)=2;
if doDraw
image(4+params.board);
axis equal off;
colormap([1,1,1;.5,0,0;0,0,.5;0,0,0;0,0,1;1,0,0]);
set(gcf,'color','k');
drawnow;
end
end
if doDraw, pause; end
end % game
end % nboards
disp(sprintf('%d wins; %d ties; %d losses',params.wins));
% Make the size equal to 100-W-T/4  Perfect=0, 100 L scores 200
scr=100-params.wins(1)-params.wins(2)/4;
% W-T/4 > Losses to Pass
assert(params.wins(1)-params.wins(2)/4>params.wins(3),sprintf('%d wins; %d ties; %d losses',params.wins));
```
```99 wins; 0 ties; 1 losses
```
2
Pass

```%%
% Implement tron.m resizing to scr when assert W-T/4>L  Fails
global scr
feval(@assignin,'caller','score',floor(2*scr));
%fh=fopen('tron.m','wt');
%fprintf(fh,'%s\n',repmat('1;',[1,floor(scr)]) );
%fclose(fh);
```