# Who wants to be a millionaire? A Game in MATLAB

May 26, 2020

By  Edward Rodriguez

`lang: en_US`

## Introduction

Hello again, this time I want to ask you a question, wouldn't you like to be a millionaire? I have good news for you. No, I'm not going to teach you how to be one, but I can teach you how to be one in a game, don't you like it? Give me a chance. Let me show you. What do you think if we create this game using MATLAB? Let's do it. For this, some points will be covered: we’ are starting with some useful functions, then we see some game features, and finally, an example is shown.

## Used Functions

For this code, we used two kinds of functions: built-in functions and others I have created.

## Built-in functions

Let's think what MATLAB functions we might need, let me give you a hint:

• disp()
• fprintf()
• tic()
• toc()
• input()
• pause()

Ok, it seems easy, I have used these functions in other tasks, is that all? Actually no! But calm down, take it easy. Three more functions are needed: rules(), ask_me(), play(). Let’s see each other.

## Author’s functions

##### Code: rules()

You know, what would be a game without rules? Well, this function adds ones, the game is designed to not be difficult, this function includes the number of questions as well as aids and other things, let's see:

function rules()
%Number of questions
nq=5;
disp('Hello! and welcome to this: Who Wants To Be Millionaire Game! ')
%Print the information about game's rules.
disp('Here you have the rules:')
fprintf('\n')
fprintf('\n')

disp("1) You will have "+ num2str(nq) + " questions")
disp("2) You will have only 1 aid")
disp('   It is:')
fprintf('\n')

disp(' 1.  *No time')
fprintf('\n')
disp("3) You will have 30 seconds to answer the question")
fprintf('\n')
fprintf('\n')
fprintf('\n')
disp('         Press any key to continue!!!')
end

Did you see? Only 5 questions and only 1 aid, of course! By the way, you only have 30 seconds to answer.

This is an interesting function. It has one input and two outputs, the input is a number, this corresponds to the question depending on your level. As you know, there are 5 questions, so the input will be a number between 1 to 5. Now, two outputs are expected. The first is the variable result (contain information about the answer) and second Need_help (you know, in case you have selected this aid).

The function looks (partially) like is shown below:

Bag_Question=["This game is called:\n\n" + ...
"a) Who wants to be millionaire? \n"   + ...
"b) Who wants to be MATLAB Helper? \n"    + ...
"c) Is this a game? \n"   + ...
"d) None\n"

"Boost converter belongs to\n" + ...
"a) AC-DC converter\n" + ...
"b) DC-AC converter\n" + ...
"c) DC-DC converter\n" + ...
"d) AC-AC converter\n"

"In a discontinuous working DC-DC converter\n" + ...
"a) The current is not discontinuous\n" + ...
"b) The current is negative\n" + ...
"c) The current is sinusoidal \n" + ...
"d) The current fluctuates and sometimes goes down to zero during a cycle\n"

"For a first-order continuous system, we have the next poles, which represent the faster (and stable) response? \n" + ...
"a)2\n" + ...
"b)-2\n" + ...
"c)1\n" + ...
"d)-1\n"

"A control system has the following characteristic equation: as^2+bs+c, the system is unstable\n" + ...
"a) Only if a, b, cis all positive\n" + ...
"b) Only if a, b, cis all negative\n" + ...
"c) If at least, one of a, b, c or c is negative and the others are positive\n" + ...
"d) It is not possible to answer the question\n"

"Who is the MATLAB Helper founder? \n" + ...
"a) Edward Rodriguez\n" + ...
"b) Gunjan Gupta\n" + ...
"c) Nitin Mane\n" + ...

'c';
'd';
'b';
'c'];

fprintf(Bag_Question(cquestion,1))          % Show the question
pause(1)

Need_help=input('Need help? y/n:','s');      % Do you need help?

if(Need_help=='y')
disp('Oks, no problem, no time limit for this question')
else
end

result=1;
else
result=0;
end

end

Oh! Sorry, I forgot it, the aid allows you to choose no limit time for the question.

##### Code: play()

This is the main function. It uses a while statement. Into while the game is executed. The game will continue since the player does not decide to quit, get a wrong answer of if s/he has not completed all the questions, this information is updated here. Finally, the earned money is shown.

function play()

correct  =0;                                           % Correct answer counter
cquestion=1;                                        % Question counter
wrong    =0;                                          % Wrong answer counter
nq       =5;                                              % Number of questions
time_over=30;                                      % Maximum time
money    =0;                                         % Starting money
exit_var =0;                                           % Do you want quit the game? why?

while((correct<nq)&&(wrong==0)&&(exit_var==0))    % Conditions to remain in the game

fprintf('\n')

disp("Question #"+num2str(cquestion))                  % Display the game round.
tic                                                                                    % Start time counting.
your_time=toc                                                              % Finish time counting.

if((your_time>time_over)&&(Need_help=='n'))        % Did your answer be in the requested time? Let's see
disp('Time is over!!! ... You lose :s')
break
else

end

%% Updating

if (is_true==1)
correct=correct+1;
else
wrong  =wrong+1;
end

cquestion=cquestion+1;

%% WIN 😀 OR LOSE :S

if (wrong>0)
fprintf('\n')
disp('You lose')
elseif(correct<nq)
fprintf('\n')
disp('ok!!!')
else
fprintf('\n')
disp('You win!!!')
end

%% Do you want to quit the game? :S

exit_var=input('Do you want to quit? select 1 for yes and 0 for no:');
money   =money+1000*correct;

end
fprintf('\n')
fprintf('\n')
fprintf('*********************\n')
disp("* YOU WON:"+num2str(money)+"         *")
fprintf('*********************\n')
end

## Example

When you run the code, you will probably get this:

As you see, questions are displayed, the time is shown too and finally the money you have won.

## Conclusion

MATLAB is very useful software, it can be used for academic tasks, research, data processing, and even for playing games. We have learned how to create a game by creating functions, using control flow statements (while loop), and some conditional structures (if-else).

Did you find some helpful content from our video or article and now looking for its code, model, or application? You can purchase the specific Title, if available, and instantly get the download link.

If you are looking for free help, you can post your comment below & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we recommend you book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our training modules.

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

Education is our future. MATLAB is our feature. Happy MATLABing!

#### Edward Rodriguez

My Name is Edward Rodriguez, I am 22 years old and I am from Cúcuta, Colombia (not Columbia). I studied Electronics Engineering at the Industrial of Santander University (UIS) in Bucaramanga, Colombia. I like listening to Salsa music and playing soccer in my free time!!!

#### Connect with MATLAB Helper ®

Use Website Chat or WhatsApp at +91-8104622179

Watch our Latest Video