Cody

Solution 300861

Submitted on 10 Aug 2013 by Alfonso Nieto-Castanon
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
%% %Tic Tac Toe : Never Lose draws=0; wins=0; Pass=1; for i=1:2000 % Computer First; 2001-4000 Player First if Pass==0,break;end x=zeros(3); while true % exit on win or filled board found=0; while ~found p=floor(9*rand)+1; if x(p)==0,break;end end % Random unless Win presents itself v0=find(x==0)'; t1=x; t1(t1==2)=-2; % Player pips set to -2 % Check for Win Option wc=find(sum(t1)==2); % column with a win if ~isempty(wc) p=intersect((1:3)+3*(wc-1),v0); end wr=find(sum(t1,2)==2); % row with a win if ~isempty(wr) p=intersect((1:3:7)+(wr-1),v0); end % Diagonal Win Options if x(5)==1 if x(1)==1 && x(9)==0,p=9;end if x(9)==1 && x(1)==0,p=1;end if x(3)==1 && x(7)==0,p=7;end if x(7)==1 && x(3)==0,p=3;end end % end of Win check % Implement Computer move and Check for Win x(p)=1; wt=x; wt(wt==2)=0; win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==3); if win % Computer Wins on its move : Match Over Pass=0; x break; end % Draw check Post computer move (9th move) if sum(x(:))>=13, draws=draws+1; break; end % Implement player move and Check for Win p=tic_tac_toe(x); if x(p(1))~=0,Pass=0;end % Invalid move - Game over x(p(1))=2; wt=x; wt(wt==1)=0; win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==6); if win wins=wins+1; break; end % Player Wins on their move end % gameover while 1:2000 end % Games 1:2000 for i=1:2000 % Player First; 2001-40000 Player First if Pass==0,break;end x=zeros(3); while true % exit on win or filled board % Implement player move and Check for Win p=tic_tac_toe(x); if x(p(1))~=0,Pass=0;end % Invalid move - Game over x(p(1))=2; wt=x; wt(wt==1)=0; win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==6); if win wins=wins+1; break; end % Player Wins on their move % Draw check Post Player move (9th move) if sum(x(:))>=13, draws=draws+1; break; end % Computer Move found=0; while ~found p=floor(9*rand)+1; if x(p)==0,break;end end % Random unless Win presents itself v0=find(x==0)'; t1=x; t1(t1==2)=-2; % Player pips set to -2 % Check for Win Option wc=find(sum(t1)==2); % column with a win if ~isempty(wc) p=intersect((1:3)+3*(wc-1),v0); end wr=find(sum(t1,2)==2); % row with a win if ~isempty(wr) p=intersect((1:3:7)+(wr-1),v0); end % Diagonal Win Options if x(5)==1 if x(1)==1 && x(9)==0,p=9;end if x(9)==1 && x(1)==0,p=1;end if x(3)==1 && x(7)==0,p=7;end if x(7)==1 && x(3)==0,p=3;end end % end of Win check % Implement Computer move and Check for Win x(p)=1; wt=x; wt(wt==2)=0; win=any([sum(wt) sum(wt,2)' trace(wt) trace(fliplr(wt))]==3); if win % Computer Wins on its move : Match Over Pass=0; x break; end end % gameover while 1:2000 end % Games 1:2000 wins draws Pass assert(isequal(Pass,1))
wins = 3584 draws = 416 Pass = 1