from The Product Game by MoH@MeD oS@Ma
good game

progame.m
clear all; % Clear all information in memory
clc; % Clear command window

mymenu(); % Display the Introduction

disp('   ') %leave a line
disp('   ') %leave a line
quit=-1; start=-2;
s=0; h=200; s1=0; h1=100; s2=0; h2=180;

%..................................................easy level

disp('                             1)  Easy Level')
disp('   ') %leave a line
disp('    This level start in table 1 ,then 2 and so on.')
disp('    You will take 2 points ( Score ) in each problem')
disp('    If you missed any problem you have 2 tries but you will take nothing in your score')
disp('    For testing the game : ')
disp('    - Enter " off " to skip the current table and move to next table ')
disp('    - Press "Enter" button to skip current level and move to next level')
disp('   ') %leave a line
disp('    Enter "start" to start level      Enter "quit" to end game')
disp('   ')%leave a line
q=input('        ');
disp('   ')%leave a line
if q==start
    for t=[1 2 3 4 5 6 7 8 9 10]
        for a=1:10
            fprintf('  %d * %d = %d\n',t,a),p=input('','s'); % Print the product form
            % The Expression 's' convert the input p to string
            r1=t*a; % r1 ===> MATLAB Answer
            r1=num2str(r1); % Convert MATLAB answer to string
            p=num2str(p); % Convert player answer to string
            % |=======================|
            % | The Comparison System |
            % |=======================|
            if strcmp(p,'quit') == 1 % "strcmp" used for comparing strings instead of == or ~=
                return;
            elseif strcmp(p,'off') == 1
                break;
            end
            % If the player enter nothing
            if isempty(p) == 1 
                disp('  Error!!! ,Expected an input')
                p=input('\n  Enter a valid input :  ','s');
                if strcmp(p,'quit') == 1 ; return; end
                if strcmp(p,r1) == 1
                    disp('     Excellent')
                    s=s+2;
                    fprintf('  Your Score : %d / %d\n', s,h)
                elseif strcmp(p,r1) == 0
                    disp('  False  ,  You have 2 tries')
                    p=input('\n  1-Try entering correct answer:  ','s');
                    if strcmp(p,r1) == 1 ; 1; %To avoid displaying two (Exellent)
                    elseif strcmp(p,r1) == 0
                        disp('  False  ,  You have 1 try')
                        p=input('\n  2-Try entering correct answer:  ','s');
                    end
                    if strcmp(p,r1) == 1
                        disp('     Excellent')
                    elseif strcmp(p,r1) == 0
                        disp('   '),disp('   ') % leave 2 lines
                        disp('                                Game Over ')
                        disp('   '),disp('   ') % leave 2 lines
                        return;
                    end
                end
            % If the player enter an input and True
            elseif strcmp(p,r1) == 1
                disp('     Excellent')
                s=s+2;
                fprintf('  Your Score : %d / %d\n', s,h)
            % If the player enter an input and False
            elseif strcmp(p,r1) == 0
                disp('  False  ,  You have 2 tries')
                p=input('\n  1-Try entering correct answer:  ','s');
                if strcmp(p,r1) == 1  ; 1; %To avoid displaying two (Exellent)
                elseif strcmp(p,r1) == 0
                    disp('  False  ,  You have 1 try')
                    p=input('\n  2-Try entering correct answer:  ','s');
                end
                if strcmp(p,r1) == 1
                    disp('     Excellent')
                elseif strcmp(p,r1) == 0
                    disp('   '),disp('   ') % leave 2 lines
                    disp('                                Game Over ')
                    disp('   '),disp('   ') % leave 2 lines
                    return;
                end
            end % End of Comparison System
        end
    end
elseif q==quit;return;
end

%..................................................medium level

disp('   ')%leave a line
disp('                            2) Medium Level')
disp('   ')%leave a line
disp('    The product numbers in this level will be random')
disp('    You will take 4 points ( Score ) in each problem')
disp('    If you missed any problem you have 2 tries but you will take nothing in your score')
disp('   ')%leave a line
disp('    Enter "start" to start level      Enter "quit" to end game')
disp('   ')%leave a line
q1=input('        ');
disp('   ')%leave a line
if q1==start
    for b=1:25
        x=ceil(rand*12); % This is a technique generates numbers 1:12
        y=ceil(rand*13); % This is a technique generates numbers 1:13
        fprintf('   %d) %d * %d = %d\n',b,x,y),z=input('','s');
        r2=x*y; % r2 ===> MATLAB Answer
        r2=num2str(r2);
        z=num2str(z);
        % |=======================|
        % | The Comparison System |
        % |=======================|
        if strcmp(z,'quit') == 1 ; return;
        elseif strcmp(z,'off') == 1 ; break;
        end
        % If the player enter nothing
        if isempty(z) == 1
            disp('  Error!!! ,Expected an input')
            z=input('\n  Enter a valid input :  ','s');
            if strcmp(z,'quit') == 1 ;return;
            elseif strcmp(z,'off') == 1 ; break;
            end
            if strcmp(z,r2) == 1
                disp('     Excellent')
                s1=s1+4;
                fprintf('  Your Score : %d / %d\n', s1,h1)
            elseif strcmp(z,r2) == 0
                disp('  False  ,  You have 2 tries')
                z=input('\n  1-Try entering correct answer:  ','s');
                if strcmp(z,r2) == 1 ; 1; %To avoid displaying two (Exellent)
                elseif strcmp(z,r2) == 0
                    disp('  False  ,  You have 1 try')
                    z=input('\n  2-Try entering correct answer:  ','s');
                end
                if strcmp(z,r2) == 1
                    disp('     Excellent')
                elseif strcmp(z,r2) == 0
                    disp('   '),disp('   ') % leave 2 lines
                    disp('                                Game Over ')
                    disp('   '),disp('   ') % leave 2 lines
                    return;
                end
            end
        % If the player enter an input and True
        elseif strcmp(z,r2) == 1
            disp('     Excellent')
            s1=s1+4;
            fprintf('  Your Score : %d / %d\n', s1,h1)
        % If the player enter an input and False
        elseif strcmp(z,r2) == 0
            disp('  False  ,  You have 2 tries')
            z=input('\n  1-Try entering correct answer:  ','s');
            if strcmp(z,r2) == 1  ; 1; %To avoid displaying two (Exellent)
            elseif strcmp(z,r2) == 0
                disp('  False  ,  You have 1 try')
                z=input('\n  2-Try entering correct answer:  ','s');
            end
            if strcmp(z,r2) == 1
                disp('     Excellent')
            elseif strcmp(z,r2) == 0
                disp('   '),disp('   ') % leave 2 lines
                disp('                                Game Over ')
                disp('   '),disp('   ') % leave 2 lines
                return;
            end
        end % End of Comparison System
    end
elseif q1==quit;return;
end

% .................................................hard level

disp('   ')%leave a line
disp('                             3) Hard Level')
disp('   ')%leave a line
disp('    The product numbers in this level will be random and in limited time')
disp('    You will take 6 points ( Score ) in each problem')
disp('    If you missed any problem you have 2 tries but you will take nothing in your score')
disp('    You have only 12 sec. to answer each problem ,')
disp('     10 sec. in first try and 5 sec. in second try')
disp('   ')%leave a line
disp('    Enter "start" to start level      Enter "quit" to end level')
disp('   ')%leave a line
q2=input('        ');
disp('   ')%leave a line
if q2 == start
    for c = 1:30
        x1=ceil(rand*14);y1=ceil(rand*15);
        tic   % This expression start a stopwatch
        fprintf('   %d) %d * %d = %d\n',c,x1,y1),z1=input('','s');
        toc   % This expression computes the elapsed time
        r=x1*y1; % r ===> MATLAB Answer
        r=num2str(r);
        z1=num2str(z1);
        % |=======================|
        % | The Comparison System |
        % |=======================|
        if strcmp(z1,'quit') == 1 ;return; end
        % If the first answer takes less than 12 sec.
        if toc <= 12
            % If the player enter nothing
            if isempty(z1) == 1
                disp('  Error!!! ,Expected an input')
                tic
                z1=input('\n  Enter a valid input :  ','s');
                toc
                if toc <= 12
                    if strcmp(z1,r) == 1 ;disp('     Excellent')
                        s2=s2+6;
                        fprintf('  Your Score : %d / %d\n', s2,h2)
                    elseif strcmp(z1,r) == 0
                        disp('  False  ,  You have 2 tries')
                        tic
                        z1=input('\n  1-Try entering correct answer(you have 10 second):  ','s');
                        toc
                        if toc <= 10
                            if strcmp(z1,r) == 1 ; disp('     Excellent')
                            elseif strcmp(z1,r) == 0
                                disp('  False  ,  You have 1 try')
                                tic
                                z1=input('\n  2-Try entering correct answer(you have 5 second):  ','s');
                                toc
                                if toc <= 5
                                    if strcmp(z1,r) == 1;disp('     Excellent')
                                    elseif strcmp(z1,r) == 0
                                        disp('   '),disp('   ')
                                        disp('                                Game Over ')
                                        disp('   '),disp('   ')
                                    end
                                elseif toc > 5
                                    disp('   '),disp('   ')
                                    disp('                                Game Over ')
                                    disp('   '),disp('   ')
                                end
                            end
                        elseif toc > 10
                            disp('   '),disp('   ')
                            disp('                                Game Over ')
                            disp('   '),disp('   ')
                            return;
                        end
                    end
                elseif toc > 12
                    disp('   Time is out, You have 1 try')
                    tic
                    z1=input('\n  3-Try entering an answer(you have 10 second):  ','s');
                    toc
                    if toc <= 10
                        if strcmp(z1,r) == 1;disp('     Excellent')
                        elseif strcmp(z1,r) == 0
                            disp('   '),disp('   ')
                            disp('                                Game Over ')
                            disp('   '),disp('   ')
                            return; 
                        end
                    elseif toc > 10
                        disp('   '),disp('   ')
                        disp('                                Game Over ')
                        disp('   '),disp('   ')
                        return; 
                    end
                end 
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % If the player enter an input and true
            elseif strcmp(z1,r) == 1 ;disp('     Excellent')
                s2=s2+6;
                fprintf('  Your Score : %d / %d\n', s2,h2)
            % If your answer is wrong
            % the game will give you 10 sec. in first try
            % and 5 sec. in second try
            elseif strcmp(z1,r) == 0
                disp('  False  ,  You have 2 tries')
                tic
                z1=input('\n  1-Try entering correct answer(you have 10 second):  ','s');% First try
                toc
                % "10 second" Logic
                if toc <= 10
                    if strcmp(z1,r) == 1 ;disp('     Excellent')
                    elseif strcmp(z1,r) == 0
                        disp('  False  ,  You have 1 try')
                        tic
                        z1=input('\n  2-Try entering correct answer(you have 5 second):  ','s');% Second try
                        toc
                        % "5 second" Logic
                        if toc <= 5
                            if strcmp(z1,r) == 1 ; disp('     Excellent')
                            elseif strcmp(z1,r) == 0
                                disp('   '),disp('   ') % leave 2 lines
                                disp('                                Game Over ')
                                disp('   '),disp('   ') % leave 2 lines
                                return;
                            end
                        elseif toc > 5
                            disp('   '),disp('   ') % leave 2 lines
                            disp('                                Game Over ')
                            disp('   '),disp('   ') % leave 2 lines
                            return;
                        end % End of "5 second" Logic
                    end
                elseif toc > 10
                    disp('   '),disp('   ') % leave 2 lines
                    disp('                                Game Over ')
                    disp('   '),disp('   ') % leave 2 lines
                    return; % End the game
                end  % End of "10 second" Logic
            end
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

            % If the first answer takes more than 12 sec.
        elseif toc > 12
            disp('   Time is out, You have 1 try')
            tic
            z1=input('\n  3-Try entering an answer(you have 10 second):  ','s');
            toc
            if toc <= 10
                if strcmp(z1,r) == 1;disp('     Excellent')
                elseif strcmp(z1,r) == 0
                    disp('   '),disp('   ')
                    disp('                                Game Over ')
                    disp('   '),disp('   ')
                    return; % End the game
                end
            elseif toc > 10
                disp('   '),disp('   ')
                disp('                                Game Over ')
                disp('   '),disp('   ')
                return; % End the game
            end
        end  % End of Comparison System
    end
elseif q2==quit;return;
end

% ------------------------- Logic of Total Score---------------------------

% max(s) ===> score that the player collected in easy level
% max(s1) ===> ===> score that the player collected in medium level
% max(s2) ===> ===> score that the player collected in hard level
% tot ===> the summation of total score
tot=sum(max(s)+ max(s1)+ max(s2));
disp('   ') % leave a lines
fprintf('                     Your Total Score is  :  %d   Points\n',tot)
disp('   '),disp('   ') % leave 2 lines
% 420 ===> score the player have to collect
% If the player collected 420 points or above
if tot >= 420
    disp('                           ! Congratulations !')
    disp('   ') % leave a line
    disp('                             *** You Won ***  ')
% If the player collected less than 420 points
elseif tot < 420
    disp('                   Sorry , You lost , Try again next time')
end
%  -------------------------------------- End   of   Program ---------------------------------------

Contact us at files@mathworks.com