question about while loop in code?
显示 更早的评论
so, I have an assignment to make a game for class. I am trying to create the game battleship!
my issue is when I execute the code, it isn't running the while loop at the end. It asks the question about which board the player wants to play beforehand. It just wont run the loop at the end and I am unsure as to why.
board1=menu('Player one, which board would you like to play?','Easy 1','Easy 2','Medium 1','Medium 2','Hard 1','Hard 2');
if board1==1
board1=easyboard1;
elseif board1==2
board1=easyboard2;
elseif board1==3
board1=mediumboard1;
elseif board1==4
board1= mediumboard2;
elseif board1==5
board1= hardboard1;
else board1==6
board1=hardboard2;
end
board2=zeros(10);
board3=board1;
disp(board2)
count=0;
while max(board1)>0 | max(board3)>0
if mod(count,2) == 0
player1(board1)
count=count+1;
else
computerplayer(board1)
count=count+1;
end
end
回答(1 个)
Walter Roberson
2015-11-29
0 个投票
What is size(board1) ? If board1 is a 2D array then max(board1) would be a vector and your while would be testing a vector condition. When you test a vector using if or while then the vector is only considered true of all elements of the vector or true. Perhaps you want to test max(board1(:))
类别
在 帮助中心 和 File Exchange 中查找有关 Board games 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!