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