Professional Documents
Culture Documents
ENG10004 Laboratory Note Week05
ENG10004 Laboratory Note Week05
Page 1 of 5
ENG10004 – Digital and Data Systems Swinburne University of Technology, S2 2021
Page 2 of 5
ENG10004 – Digital and Data Systems Swinburne University of Technology, S2 2021
• The person can input “Rock” or “Paper” or “Scissors” in Command Window or exit the game;
• The program can show the choices of the person and computer as well as the score in each
game; and
Page 3 of 5
ENG10004 – Digital and Data Systems Swinburne University of Technology, S2 2021
Brainstorming:
Program Flowchart
• The problem can be solved by using a while loop since the number of games is not known
before the loop starts;
• Each game result can be determined by a user-defined function combining keyboard input
and random computer choice;
Page 4 of 5
ENG10004 – Digital and Data Systems Swinburne University of Technology, S2 2021
fprintf('The Game starts.\n Enter R for Rock; P for Paper; S for Scissors; Q for
Quit.\n')
while (1)
user_in = input('Please enter R, P, S, or Q: ','s');
user_id = strfind('RPSQ',user_in); % returns the starting index of user_in in string
RPSQ
if (isempty(user_id) || user_id<1 || user_id>4)
fprintf('>>>>Invalid input! Try Again!<<<<\n')
pause(1.5)
elseif (user_id==4)
fprintf('<<You quit the Game. Bye!>>\n')
break;
else
computer_id = randi(3,1);
iresult = rps_game(user_id,computer_id);
fprintf(' You chose -> %s\n Computer choses -> %s\n So -> %s\n', ...
itemtext(user_id,:),itemtext(computer_id,:), ...
wintext(iresult,:));
scores(iresult) = scores(iresult)+1;
fprintf('Your Score:\n Games: %i, Wins: %i, Ties: %i, Losses: %i\n\n', ...
sum(scores),scores);
end
end
rps_game function:
function f = rps_game(user_id,computer_id)
global win_id tie_id lose_id
table = [tie_id lose_id win_id;
win_id tie_id lose_id;
lose_id win_id tie_id];
f = table(user_id,computer_id);
end
Key Points:
Page 5 of 5