question about while loop in code?

3 views (last 30 days)
dallas klages
dallas klages on 29 Nov 2015
Commented: dallas klages on 29 Nov 2015
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

Answers (1)

Walter Roberson
Walter Roberson on 29 Nov 2015
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 Comment
dallas klages
dallas klages on 29 Nov 2015
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!

Sign in to comment.

Categories

Find more on Board games in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!