charcters saved as ASCII in matrix

1 回表示 (過去 30 日間)
Tor Fredrik Hove
Tor Fredrik Hove 2011 年 11 月 6 日
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

採用された回答

Walter Roberson
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' ?
  2 件のコメント
Tor Fredrik Hove
Tor Fredrik Hove 2011 年 11 月 6 日
thank you very much!
I wonder why the answer sheet for the assignment works. They start up with a 3 times 3 zero-matrix but still the game works. It is here:
function vinner = tripptrapp
Brett = [0 0 0; 0 0 0; 0 0 0];
spiller = 'x';
vinner = 0;
while (tellPlasser(Brett) > 0 && vinner == 0)
rad = input(sprintf('%s, choose row: ', spiller));
kolonne = input(sprintf(' %s, choose column: ', spiller));
if (Brett(rad,kolonne) == 0)
Brett(rad,kolonne) = spiller;
if (erVinnerfasit(spiller, Brett))
vinner = spiller;
disp(sprintf(' %s won!', spiller));
end
if (spiller == 'x')
spiller = 'o';
else
spiller = 'x';
end
else
disp('already taken coordinates!');
end
end
if (vinner == 0)
disp('It''s a tie!');
end
Tor Fredrik Hove
Tor Fredrik Hove 2011 年 11 月 6 日
it uses theese two functions:
function vinner = erVinnerfasit(spiller, Brett)
vinner = 0;
diagonalt_fra_hoyre = 0;
diagonalt_fra_venstre = 0;
for i=1:size(Brett)
horisontalt = 0;
vertikalt = 0;
for j=1:size(Brett, 2)
if (Brett(i,j) == spiller)
horisontalt = horisontalt + 1;
end
if (Brett(j,i) == spiller)
vertikalt = vertikalt + 1;
end
if (i==j && Brett(j, i) == spiller)
diagonalt_fra_venstre = diagonalt_fra_venstre + 1;
end
if (((i==3 && j==1) || (i==2 && j==2) || (i==1 && j==3)) &&
Brett(i,j)==spiller)
diagonalt_fra_hoyre = diagonalt_fra_hoyre + 1;
end
end
if (diagonalt_fra_venstre == 3 || diagonalt_fra_hoyre == 3 ||
vertikalt == 3 || horisontalt == 3)
vinner = 1;
break;
end
end
the second function it uses:
function out=tellPlasser(A)
[N M]=size(A);
num=0;
for i=1:N
for j=1:M
if A(i,j)==0
num=num+1;
end
end
end
out=num;

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeModify Image Colors についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by