Don't know how to fix error.

3 次查看(过去 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;
The error is below.
Error: File: Untitled Line: 144 Column: 1
Invalid expression. Check for missing or extra characters.
But I am not sure if there are any other errors after that line is fixed.

采纳的回答

Image Analyst
Image Analyst 2020-11-28
It doesn't like the blank line after the ...
Get rid of it and have this:
win = any(sum(X) == s) || any(sum(X') == s) || ...
sum(diag(X)) == s || sum(diag(fliplr(X))) == s;
Get rid of all the other blank lines between the lines of code. They're not needed and just make your code longer.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Strategy & Logic 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by