In: Advanced Math
Here is the code for lovedicegame.m
clc; clear all;
if input('Do you want to play lovedice game? (Y/N) ', 's') ==
'N'
return
end
out = 'Y';
while out == 'Y'
nuser = readdice(); % User input.
ndice = genrandomdice(); % Dice number.
comparedice(nuser, ndice);
out = input('Play again (Y/N)? ', 's');
if out == 'N'
disp('Mabuti naman at natauhan ka na!')
end
end
First we ask the player if he/she wants to play, If so, we continue the game, otherwise the code stops. Next we ask for the input and generate the random dice. Later we compare the results and print the corresponding message. Finally, we ask if the player wants to play again.
Here are the codes for readice.m,genrandomdice.m and comparedice.m respectively
function nuser = readdice()
nuser = input('Enter your dice: ');
end
function ndice = genrandomdice()
ndice = randi(6);
disp(['My dice result: ' num2str(ndice)])
end
function comparedice(nuser, ndice)
if nuser > ndice
disp('Mahahanap mo na ang forever mo. Sana all!')
elseif nuser < ndice
disp('Gising na friend, di ka niya mahal!')
else
disp('Move on na ghorl')
end
end
We use the function input('prompt') to ask for an input and the function disp('message') to display a message.
We run the code for the values 3,1,4,1,6 and get the output
Do you want to play lovedice game? (Y/N) Y
Enter your dice: 3
My dice result: 6
Gising na friend, di ka niya mahal!
Play again (Y/N)? Y
Enter your dice: 1
My dice result: 4
Gising na friend, di ka niya mahal!
Play again (Y/N)? Y
Enter your dice: 4
My dice result: 1
Mahahanap mo na ang forever mo. Sana all!
Play again (Y/N)? Y
Enter your dice: 1
My dice result: 6
Gising na friend, di ka niya mahal!
Play again (Y/N)? Y
Enter your dice: 6
My dice result: 6
Move on na ghorl
Play again (Y/N)? N
Mabuti naman at natauhan ka na!