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!