马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
本人刚刚学习matlab,正在编写一个扫雷的程序,还不会gui,是基于字符的,可以自己玩,也可以让电脑来自动扫!
不过在input上遇到了问题,每次运行到input 语句的时候显示如下:
??? Index exceeds matrix dimensions.
Error in ==> minesweeper at 11
xx=input('Please input x!');
希望大家帮个忙!!
程序如下(未完成):
function step=minesweeper(x,y,input)%falied!
tag=input('1-Play by yourself! 2-Play by AI!');
matrix=minemap(x,y,input);
matrix1=zeros(x,y);
matrix1=matrix1-100;%Matrix1 is the matrix that you can see on screen!
disp(matrix1);
have=0;
if tag==1
while check(have,matrix1,matrix)~=input
have=check(have,matrix1,matrix);
xx=input('Please input x!');
yy=input('Please input y!');
ju=input('please input judgement:1 0r 0!');
matrix1=returnmap(matrix,xx,yy,ju);
end
if have==input
disp('You have successed!!');
else
disp('To be continued.......');
end
elseif tag==2
step=autoplay(matrix);
return;
end
function step=autoplay(inmatrix)%failed!
step=1;
[m,n]=size(inmatrix);
x=round(rand*(m-1))+1;
y=round(rand*(n-1))+1;
mat=returnmap(inmatrix,x,y);
if mat==0
step='Failed';
return;
end
while mat~=0 %?????
end
function outmatrix=minemap(x,y,input)%successful!
%First,we have to make the out!
totol=x*y;
out(1,1)=round(rand*(totol-1))+1;
for i=2:1:input
long=length(out);tapp=1;
while tapp==1
tap=1;
number=round(rand*(totol-1))+1;
for j=1:1:long
if out(1,j)==number
tap=0;
break;
end
end
if tap==1
out(1,i)=number;
tapp=0;
end
end
end
%The out has been finished successfully!
outmatrix=zeros(x,y);
long=length(out);
for i=1:1:long
outmatrix(out(i))=-20;
end
xx=zeros(1,y);yy=zeros(x+2,1);
temp=[xx;outmatrix;xx];
outsecond=[yy,temp,yy];
for i=2:1:x+1
for j=2:1:y+1
if outsecond(i,j)<0
outsecond(i-1,j)=outsecond(i-1,j)+1;
outsecond(i+1,j)=outsecond(i+1,j)+1;
outsecond(i,j-1)=outsecond(i,j-1)+1;
outsecond(i,j+1)=outsecond(i,j+1)+1;
outsecond(i-1,j-1)=outsecond(i-1,j-1)+1;
outsecond(i-1,j+1)=outsecond(i-1,j+1)+1;
outsecond(i+1,j-1)=outsecond(i+1,j-1)+1;
outsecond(i+1,j+1)=outsecond(i+1,j+1)+1;
end
end
end
outmatrix=outsecond(2:x+1,2:y+1);
function outmatrix=returnmap(mat,x,y,ju)%failed!
%x and y are coordinates,but ju is a judgement,one means yes but means no!
outmatrix=mat;
if mat(x,y)<0&&ju==0
outmatrix=0;
return;% You have failed!
elseif mat(x,y)>0&&ju==0
%100 means having known but -100 means unknown,88 means maybe a bomb!
outmatrix(x,y)=100;
outmatrix=inmloop(mat,x,y);
elseif mat(x,y)>0&&ju==1
outmatrix(x,y)=88;
elseif mat(x,y)<0&&ju==1
outmatrix(x,y)=88;
end
%to be continued!
function outmatrix=loop(mat,x,y)%failed!
% function of loop is a recursion!
inmat=mat;
tempx=[x-1 x-1 x-1 x x x+1 x+1 x+1];
tempy=[y-1 y y+1 y-1 y+1 y-1 y y+1];
for i=1:1:8
while tempx(i)>0&&tempy(i)>0
%100 means having known but -100 means unknown!
if inmat(tempx(i),tempy(i))==0
temp1=temp(i)-x;temp2=temp(i)-y;
for j=1:1:8
inmat(tempx(j)+temp1,tempy(j)+temp2)=100;%?????
end
inmat=loop(inmat,tempx(i),tempy(i));
else
inmat(tempx(i),tempy(i))=-100;
end
end
end
outmatrix=inmat;
function success=check(have,inmat,matrix)
success=have;
[x,y]=size(inmat);
for i=1:1:x
for j=1:1:y
if inmat(x,y)==100&&matrix(x,y)<0
success=success+1;
end
end
end |