Question With Syntax/ Compilation.
12 ビュー (過去 30 日間)
古いコメントを表示
I am trying to fix this matlab code that was written to play a tic tac toe game and I keep getting an error code for syntax and don't know how to fix it. Any help would be appreciated!
Here is the code:
function tictactoe(f_user1,f_user2)
% clear all
% clc
%addpath()
user1=f_user1;
user2=f_user2;
user1wins=0;
user2wins=0;
user2loss=0;
user1loss=0;
ties=0;
user1_forfiet=0;
user2_forfiet=0;
for games=1:1000
%%
board=zeros(3,3);
turn=randi([1 2],1,1);
p=0;
while p==0
turn=turn+1;
if rem(turn,2)~=0
userboard=user1(board,-1);
else
userboard=user2(board,1);
end
p=legalmove(board,userboard,p);
if p~=3
p=winner(userboard);
end
board=userboard;
end
%%
if p==3
if rem(turn,2)==0
user2_forfiet=user2_forfiet+1;
user2loss=user2loss+1;
user1wins=user1wins+1;
else
user1_forfiet=user1_forfiet+1;
user1loss=user1loss+1;
user2wins=user2wins+1;
end
elseif p==2
ties=ties+1;
elseif p==-1
user1wins=user1wins+1;
user2loss=user2loss+1;
else
user2wins=user2wins+1;
user1loss=user1loss+1;
end
end
user1_stats=[user1wins user1loss ties user1_forfiet]
user2_stats=[user2wins user2loss ties user2_forfiet]
function p=legalmove(board,userboard,p)
if any(size(userboard)~=size(board))
p=3;
elseif sum(sum(abs(board-userboard)))~=1;
p=3;
else
p=p;
end
function p = winner(X)
% p = winner(X) returns
% p = 0, no winner yet,
% p = -1, user 1 has won,
% p = 1, user 2 has won,
% p = 2, game is a draw.
for p = [-1 1]
s = 3*p;
win = any(sum(X) == s) || any(sum(X') == s) || ...
sum(diag(X)) == s || sum(diag(fliplr(X))) == s;
if win
return
end
end
p = 2*all(X(:) ~= 0);
function B=game(B,x)
r=randi(3,1);
c=randi(3,1);
while B(r,c) ~= 0
r=randi(3,1);
c=randi(3,1);
end
if B(r,c) == 0
r=randi(1,3);
elseif B(r,c) ~= 0
c=randi(1,3);
end
B(r,c)=x;
2 件のコメント
Steven Lord
2020 年 11 月 27 日
What is the full and exact text (everything displayed in red and/or orange in the Command Window) of the error and/or warning messages you receive when you run your code? The exact text of the messages may include information that will help us determine the problem or at least the next step to take in investigating the problem.
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!