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

Correct

17Size
Leading solution size is 2.
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
 
%%
% Core Code based upon TRON Cody 327 by Alfonso Nieto-Castanon
% Tweaked to have 50 paired matches that swap starting positions
% 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));
 % 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'; % Move S on error
       end
          params=evalin('caller','params');
          rand('state',params.state);
          % red's next movement
          i=find(params.board==2);
          [nill,j]=max(~params.board(i+params.D)+.5*(params.board(i-params.D)==-2));
          d2=params.str(j);
          % 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 2*score
scr=max(0,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));
%fh=fopen('tron.m','wt');
%fprintf(fh,'%s\n',repmat('1;',[1,floor(scr)]) );
%fclose(fh);
91 wins; 2 ties; 7 losses
2
Pass
 
%%
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);