Why isn't IF statement stopping Wile loop

조회 수: 1 (최근 30일)
shaan khokhar
shaan khokhar 2020년 3월 12일
댓글: shaan khokhar 2020년 3월 12일
Hi all,
I was wondering why this if statement isn't stopping my code, i have tried everything but it doesn't seem to work:
if player1score >= 50 || player2score >= 50
if player1score > player2score
fprintf("%s Wins The Game!! \n Here are the final scores: \n %s's Score: %d \n %s's Score: %d \n", player1, player1, player2,player1score, player2score);
else
fprintf("%s Wins The Game!! \n Here are the final scores: \n %s's Score: %d \n %s's Score: %d \n", player2, player1, player2,player1score, player2score);
end
gamestate = 0;
player_turn = 0;
end
You can see my full code segment down below to see if that's causing the issue:
player_turn = 1;
player1score = 0;
player2score = 0;
global gamestate
gamestate = 1;
player1 = input('Enter Name For Player 1: ','s');
player2 = input('Enter Name For Player 2: ','s');
play = "";
while gamestate == 1
if player1score >= 50 || player2score >= 50
if player1score > player2score
fprintf("%s Wins The Game!! \n Here are the final scores: \n %s's Score: %d \n %s's Score: %d \n", player1, player1, player2,player1score, player2score);
else
fprintf("%s Wins The Game!! \n Here are the final scores: \n %s's Score: %d \n %s's Score: %d \n", player2, player1, player2,player1score, player2score);
end
gamestate = 0;
player_turn = 0;
end
while player_turn == 1
fprintf("\n\n%s's Turn \n", player1);
output = gameturn();
player1score = player1score + output(3);
if output(1) == output(2)
fprintf("Dice 1 Score: %d \nDice 2 Score: %d \nTotal Score: %d \n",output(1),output(2),player1score);
fprintf("You got a double!! Roll again\n\n");
else
fprintf("Dice 1 Score: %d \nDice 2 Score: %d \nTotal Score: %d \n",output(1),output(2),player1score);
player_turn = 2;
end
while player_turn == 2
fprintf("\n\n%s's Turn \n", player2);
output = gameturn();
player2score = player2score + output(3);
if output(1) == output(2)
fprintf("Dice 1 Score: %d \nDice 2 Score: %d \nTotal Score: %d \n",output(1),output(2),player2score);
fprintf("You got a double!! Roll again \n\n");
else
play = "";
fprintf("Dice 1 Score: %d \nDice 2 Score: %d \nTotal Score: %d \n\n",output(1),output(2),player2score);
fprintf("Totals: \n%s's Total: %d \n%s's Total: %d\n",player1,player1score,player2,player2score);
while play == ""
play = input('Do you Want To Continue Playing? (Y or N)','s');
play = lower(play);
if play == "y"
player_turn = 1;
elseif play == "n"
gamestate = 0;
player_turn = 0;
else
play = "";
disp("Invalid Input")
end
end
end
end
end
end
function x = gameturn()
d1 = randi([1,6]);
d2 = randi([1,6]);
total = d1+d2;
x = [d1,d2,total];
end

채택된 답변

Jon
Jon 2020년 3월 12일
편집: Jon 2020년 3월 12일
You never break out of the inner loop where you alternate between players, so the condition doesn't get checked
Try putting in some break points and single stepping through the code and you will see what is going on.
If you haven't used the MATLAB debug tool, I highly recommend learning about it. It will very quickly help you see all kinds of problems. Note that you can hover above variables when it stops at a break point to see there values. You can also select a line of code and right click to evaluate it to see exactly what it gives.
  댓글 수: 2
Jon
Jon 2020년 3월 12일
I think if you reararrange your code like this it does what you want, or at least is much closer. Note you also had some mistakes on your final output lines to provide the scores. These are corrected here too.
It would be good if you could figure out how to restructure the code further so that you don't have such deeply nested loops. Even if it works, it is hard to understand the code with such deeply nested loops.
player_turn = 1;
player1score = 0;
player2score = 0;
global gamestate
gamestate = 1;
player1 = input('Enter Name For Player 1: ','s');
player2 = input('Enter Name For Player 2: ','s');
play = "";
while gamestate == 1
while player_turn == 1
fprintf("\n\n%s's Turn \n", player1);
output = gameturn();
player1score = player1score + output(3);
if output(1) == output(2)
fprintf("Dice 1 Score: %d \nDice 2 Score: %d \nTotal Score: %d \n",output(1),output(2),player1score);
fprintf("You got a double!! Roll again\n\n");
else
fprintf("Dice 1 Score: %d \nDice 2 Score: %d \nTotal Score: %d \n",output(1),output(2),player1score);
player_turn = 2;
end
while player_turn == 2
fprintf("\n\n%s's Turn \n", player2);
output = gameturn();
player2score = player2score + output(3);
if output(1) == output(2)
fprintf("Dice 1 Score: %d \nDice 2 Score: %d \nTotal Score: %d \n",output(1),output(2),player2score);
fprintf("You got a double!! Roll again \n\n");
else
play = "";
fprintf("Dice 1 Score: %d \nDice 2 Score: %d \nTotal Score: %d \n\n",output(1),output(2),player2score);
fprintf("Totals: \n%s's Total: %d \n%s's Total: %d\n",player1,player1score,player2,player2score);
if player1score >= 50 || player2score >= 50
if player1score > player2score
fprintf("%s Wins The Game!! \n Here are the final scores: \n %s's Score: %d \n %s's Score: %d \n", player1, player1, player1score, player2, player2score);
else
fprintf("%s Wins The Game!! \n Here are the final scores: \n %s's Score: %d \n %s's Score: %d \n", player2, player1, player1score, player2, player2score);
gamestate = 0;
player_turn = 0;
end
else
while play == ""
play = input('Do you Want To Continue Playing? (Y or N)','s');
play = lower(play);
if play == "y"
player_turn = 1;
elseif play == "n"
gamestate = 0;
player_turn = 0;
else
play = "";
disp("Invalid Input")
end
end
end
end
end
end
end
function x = gameturn()
d1 = randi([1,6]);
d2 = randi([1,6]);
total = d1+d2;
x = [d1,d2,total];
end
shaan khokhar
shaan khokhar 2020년 3월 12일
Thank you for your help. I do agree my code is somewhat of a mess. But, the breaks helped i forgot they existed and i shall try this new version of code :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Video games에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by