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
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(:))

댓글 수: 1

dallas klages
dallas klages 2015년 11월 29일
board1 is a 10x10 matrix.
I found a little success changing it to while max(max(board1))>0
I will try your suggestion.
Thank you!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Board games에 대해 자세히 알아보기

태그

질문:

2015년 11월 29일

댓글:

2015년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by