E) Design a well-packaged MATLAB program, with user-friendly interface for the following game: [20 Marks] (i) Two players, A and B, play a game called Lotto by Two Players. The Lotto machine generates a random lotto number in the range 0-50. They tak

3 次查看(过去 30 天)
Design a well-packaged MATLAB program, with user-friendly interface for the following game: [20 Marks] (i) Two players, A and B, play a game called Lotto by Two Players. The Lotto machine generates a random lotto number in the range 0-50. They take turns choosing any number, positive or negative to build-up their total to the lotto number. Player A starts and may choose any number. After each move, the number chosen is added to a common running total. If the total exceeds the lotto number, a message indicating that the total is too high should be displayed, similarly the opposite if the number is too low. For example, a random lotto number of 45 is generated (hidden). A starts with 25(total=25), the script now displays that the total is too low. B then chooses 25(total=50). The script now says the total is too high. A chooses -10 (total 40), and so forth. Write a script file to simulate the game that allows each player to correct the total 20 times. If there is no matching total after 20 chances for each player, the player whose previous guess brought the total closest to the lotto number wins, otherwise they share the lotto winnings( if both equally closer).
  3 个评论
MHLAWAKHE VATSHA
MHLAWAKHE VATSHA 2019-10-16
clc;
clear;
L = randi([0 50])
total_sum = 0
chances = 0
A = input('choose number')
B = input('choose number')
if A == L
fprintf('\A won\n')
elseif B == L
fprintf('\B won \n')
else
total_sum = A + B + total_sum
chances = chances + 1
end
if total_sum < L
fprintf('\numrber too low\n')
elseif total_sum > L
fprintf('\number toohigh\n')
else
fprint('\ the play that brought the number closer to L wins if non both win and share the win\m')
while chances <= 20
end
end

请先登录,再进行评论。

回答(1 个)

MHLAWAKHE VATSHA
MHLAWAKHE VATSHA 2019-10-17
clc;
clear;
L = randi([0 50])
total_sum = 0
chances = 0
A = 0
B = 0
while chances <= 20
A = input('choose number')
B = input('choose number')
if total_sum < L
fprintf('\n total too low\n')
elseif total_sum > L
fprintf('\n total too high\n')
else
end
if A == L || B == L
end
total_sum = total_sum + A + B;
chances = chances + 1;
end
if total_sum == L || total_sum ~= L
fprintf('\n the player that brought the total closest to L wins if both of the player they share the win\n')
end

类别

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