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.
For this code, we used two kinds of functions: built-in functions and others I have created.
Let's think what MATLAB functions we might need, let me give you a hint:
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.
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:
%Number of questions
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:')
disp("1) You will have "+ num2str(nq) + " questions")
disp("2) You will have only 1 aid")
disp(' It is:')
disp(' 1. *No time')
disp("3) You will have 30 seconds to answer the question")
disp(' Press any key to continue!!!')
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" + ...
"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" + ...
"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" + ...
"d) SadioMané\n" ];
Bag_Answer =[ 'a'
fprintf(Bag_Question(cquestion,1)) % Show the question
Need_help=input('Need help? y/n:','s'); % Do you need help?
disp('Oks, no problem, no time limit for this question')
value=input('Choose the answer:','s');
if(value==Bag_Answer(cquestion)) % Is the answer correct?
Oh! Sorry, I forgot it, the aid allows you to choose no limit time for the question.
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.
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
disp("Question #"+num2str(cquestion)) % Display the game round.
tic % Start time counting.
[Need_help,is_true] =ask_me(cquestion); % Give me your answer and if an aid is needed.
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')
% Is the answer correct?
%% WIN 😀 OR LOSE :S
%% Do you want to quit the game? :S
exit_var=input('Do you want to quit? select 1 for yes and 0 for no:');
disp("* YOU WON:"+num2str(money)+" *")
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.
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.
Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or contact us by emailing your questions to [email protected]. Follow us on LinkedIn Facebook, and Subscribe to our YouTube Channel. If you find any bug or error on this or any other page on our website, please inform us & we will correct it.
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!