fprintf multiple outputs separately

조회 수: 2 (최근 30일)
b
b 2019년 11월 19일
답변: Walter Roberson 2019년 11월 19일
I'm creating the dice game "knockout" and can't figure out how to print out a statement ("Player 1, Player 2, Player 3, etc [contingent on the number of players they specified at the start of the game]... you're still in). How would you go about doing this? This is what I've come up with so far:
%if dice rolls number that was chosen by player, they're knocked out,
%otherwise can continue playing
if dice == chosen_num(player_number)
%HOW TO SEPERATE PLAYER NUMBERS IN COMMAND WINDOW (PLAYER 1, PLAYER 2,
%PLAYER 3?
fprintf(' Player %.0f youre knocked out.\n', player_number)
else fprintf('Player %.0f Youre still in.\n', player_number)
end

채택된 답변

Walter Roberson
Walter Roberson 2019년 11월 19일
still_in_game = true(1, number_of_players);
while any(still_in_game)
for player_number = find(still_in_game)
%if dice rolls number that was chosen by player, they're knocked out,
%otherwise can continue playing
if dice == chosen_num(player_number)
%HOW TO SEPERATE PLAYER NUMBERS IN COMMAND WINDOW (PLAYER 1, PLAYER 2,
%PLAYER 3?
fprintf(' Player %.0f youre knocked out.\n', player_number);
still_in_game(player_number) = false;
end
end
for player_number = find(still_in_game)
fprintf('Player %.0f Youre still in.\n', player_number);
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by