charcters saved as ASCII in matrix
1 次查看(过去 30 天)
显示 更早的评论
I have a function that works fine
function threeinarow=isWinner(A,s)
count=0;
threeinarow='false';
for i=1:size(A,1)
for j=1:size(A,2)
if strcmp(A(i,j),s)
count=count+1;
if count==size(A,2)
threeinarow='true'
end
end
end
count=0;
end
for i=1:size(A,1)
for j=1:size(A,2)
if strcmp(A(j,i),s)
count=count+1;
if count==size(A,1)
threeinarow='true'
end
end
end
count=0;
end
i=1;
for j=1:size(A,2)
if strcmp(A(j,i),s)
count=count+1;
if count==size(A,1)
threeinarow='true'
end
i=i+1;
end
end
count=0;
i=size(A,1);
for j=1:size(A,2)
if strcmp(A(j,i),s)
count=count+1;
if count==size(A,1)
threeinarow='true'
end
i=i-1;
end
end
I tried to apply the function above to a script that creates the matrix that the function above asks for input in variable A but as I put in a character for one index in the matrix C below like 'x' it gets stored in the matrix as the ASCII-number instead of the character and the function isWinner can't function (at least this seems to be the problem). How do I change this? Here is the function:
C=ones(3,3);
finished=0;
while finished==0
r=input('write roow for x: ');
k=input('write column for for x: ');
while C(r,k)~=1
fprintf('coordinates %d, %d are taken by %c\n', r, k, C(r,k))
r=input('write row for x: ');
k=input('write column for x: ');
end
C(r,k)='x';
check=isWinner(C,'x');
if strcmp(check,'true')
disp('x wins')
finished=1;
end
if finished==0
r=input('write row for 0: ');
k=input('write column for 0: ');
while C(r,k)~=1
fprintf('coordinates %d, %d are taken by %c\n', r, k, C(r,k))
r=input('write row for 0: ');
k=input('write column for 0: ');
end
C(r,k)='0';
check=isWinner(C,'0');
if strcmp(check,'true')
disp('0 wins');
finished=1;
end
end
end
0 个评论
采纳的回答
Walter Roberson
2011-11-6
Do not initialize your
C=ones(3,3);
Instead,
C = char(zeros(3,3));
By the way, have you considered using the logical constants true and false instead of the strings 'true' and 'false' ?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Modify Image Colors 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!