Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
%%
%Tic-Tac-Toe
%Demonstration of Multi-Call with adaptive response
wins=0;
Pass=0;
for i=1:50 % Computer First; 51-100 will be Player First
x=zeros(3);
while true % exit on win or filled board
if i<16 % Up bot
p=find(x==0,1);
elseif i<31 % Down bot
p=find(x==0,1,'last');
else % 31 thru 50 % random bot
found=0;
while ~found
p=floor(9*rand)+1;
if x(p)==0,break;end
end
end
% 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,break;end % Computer Wins on its move
% Draw check Post computer move (9th move)
if sum(x(:))>=13,break;end
% Implement player move and Check for Win
p=tic_tac_toe(x);
p=p(1);
if x(p)~=0,break;end % Invalid move - Game over
x(p)=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:50
end % Games 1:50
% Output wins against Computer first
wins
for i=51:100 % Player First; 51-100 Player First
x=zeros(3);
while true % exit on win or filled board
% Implement player move and Check for Win
p=tic_tac_toe(x);
p=p(1);
if x(p)~=0,break;end % Invalid move - Game over
x(p)=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 computer move (9th move)
if sum(x(:))>=13,break;end
if i<66 % Up bot
p=find(x==0,1);
elseif i<81 % Down bot
p=find(x==0,1,'last');
else % 31 thru 50 % random bot
found=0;
while ~found
p=floor(9*rand)+1;
if x(p)==0,break;end
end
end
% 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,break;end % Computer Wins on its move
end % gameover while 51:100
end % Games 51:100
wins
if wins>65,Pass=1;end
assert(isequal(Pass,1))
wins =
47
wins =
97
|
Make the vector [1 2 3 4 5 6 7 8 9 10]
35569 Solvers
707 Solvers
Convert a numerical matrix into a cell array of strings
455 Solvers
Cell Counting: How Many Draws?
581 Solvers
Magic is simple (for beginners)
2756 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!