Matrix and element change and input help
显示 更早的评论
This may be trivial but how do I get for a user input coords as a matrix, and to change the element to another number eg. a tic-tac-toe game player 1 inputs a coord to change a 0 in the matrix to a 1, and player 2 changes a 0 to -1. (player==0 when it is player 1's turn)
my code:
I=input('Number coordinate: ');
for a=1:3
for b=1:3
if B(a,b)==0
if I==1
if player==0
B(1,1)=1;
else
B(1,1)=-1;
end
elseif I==2
if player==0
B(1,2)=1;
else
B(1,2)=-1;
end
elseif I==3
if player==0
B(1,3)=1;
else
B(1,3)=-1;
end
elseif I==4
if player==0
B(2,1)=1;
else
B(2,1)=-1;
end
elseif I==5
if player==0
B(2,2)=1;
else
B(2,2)=-1;
end
elseif I==6
if player==0
B(2,3)=1;
else
B(2,3)=-1;
end
elseif I==7
if player==0
B(3,1)=1;
else
B(3,1)=-1;
end
elseif I==8
if player==0
B(3,2)=1;
else
B(3,2)=-1;
end
elseif I==9
if player==0
B(3,3)=1;
else
B(3,3)=-1;
end
end
else
disp('That is an invalid move')
end
end
end
problems with it: It will let a user set a piece on an already used part of a matrix and will always say that it is an invalid move (even if it is valid)
Could anyone help me to find a good way to do this?
-thanks, Alex
采纳的回答
更多回答(1 个)
David Sanchez
2013-5-23
>>a=input('insert matrix)
>>[1 2 3 4]
类别
在 帮助中心 和 File Exchange 中查找有关 Desktop 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!