26.9.14

Simple Math Game Programming Tutorial ! ^-^ !

     Assalamualaikum, first of all, thank you to our beloved lecturer, Mdm Sharida Hanum, who has put a lot of efforts in teaching us about the Programming in C. This project is part of subject evaluation of Computer II (ISM 2112) for Sciences Programme in CFS IIUM. Here is the members group participated for this project.

1) Hafizul Rahman bin Mohd Sahimi ( 134355 )
2) Wan Hanafi bin Wan Hussain ( 134207 )
3) Muhammmad Aidid Wafi bin Nordzi ( 131732 )
4) Mohammad Aminuddin bin Endut ( 131469 )

 This blog is presenting about the tutorial for creating a simple programming using Programming in C such as data type, arithmetic structure, repetition structure (while loop, do-while loop, for loop, pre/post-test loop, and nested loop) and Function in C. This project is attempt to create a simple gaming program based on C++ language programming. Programming is the process of writing , testing, debugging, and maintaining the source code of computer programs. To initiate the program, user need to compile and execute the program using Bloodshed Dev-C++ compiler. Download here. 

     The user of the program is two different players who participated in the game that need to insert user data. User can do exercises by answering the questions and scoring the possible highest mark.. This program is created to determine which player score the highest mark . 

Beginning of the program

 
User can start the program by choosing any category. (1) for Exercise, (2) for Tutorial, (3) Competition. Then, user need to insert the name for user data.

This is example for Exercise, user need to answer all 10 question to get possible highest score.



This is example for Tutorial, user will be provided the questions with answer so that they can practice the question for Competiton category. Practices make perfect. 


Game Begins !

Now we're in Competition category, this requires two different player to answer the question separately.
 
 
1st player







  
Game Result !


The result is player 2 winning the game as he score the highest mark with 7 answer  correct



     The scenario of the game is quite simple, it requires two different player to answer all questions separately in order to determine which player scores the highest mark. Once the game begins, the player need to insert the user data ( name ), the game will seed the question randomly after the user date have been submitted. The question is answered by insert the correct answer, the answer will appear in the end of the question. Then, the marks will be calculated by how the question be correct in order to determine which player score the highest score.



This is simple flowchart to picture the scenario of the game








This picture below is  an example for gaming coding using C++ language 



To start the coding, we need to create user-defined function and built in function

1) function call
2) function prototype
3) function definition
4) built in function
5) naming the array

To seed the question randomly, we need an input of the random number generation,
( randNo = min + rand( )%( max - min + 1); in function definition.

Here is the example of game coding


Last but not least, may this tutorial helps to create such gaming program.










25.9.14

Coding for the Gaming Program ! ^-^ !

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#define SIZE 2
int Exercise();
int Tutorial();
int Competition ();
int main()
{
   
    int diff;
    char ans;
  
    printf("\t\tAssalamualaikum and welcome to math game");
    printf("\n");
    printf("\n");
    do
    {
    system("cls");
    printf("Please choose category:");
    printf("\n");
    printf("1 for Exercise");
    printf("\n");
    printf("2 for Tutorial");
    printf("\n");
    printf("3 for Competition");
    printf("\n");
    printf("You choose number:");
    scanf("%d",&diff);
    printf("\n");
   
    if(diff==1)
     {
     int e;
     printf("Good Luck and All The Best");
     printf("\n");
     e=Exercise();
     }

    
    else if(diff==2)
    {
    int t;
    t=Tutorial();
    printf("\n");
    }
   
    else if(diff==3)
     {
     int c;
     printf("Good Luck and All The Best");
     printf("\n");
     c=Competition();
     }
   
   
    else
    {
        printf("Invalid!!");
        printf("\n");
        printf("Program is terminated!!");
        printf("\n");
        printf("Please try again later!!");
        printf("\n");   
    }
    fflush(stdin);
    printf("Do you want to continue ?(Y/N)\n");
    scanf("%c",&ans);
    }while(ans=='Y'||ans=='y');
   

   
   
    system("pause");
    return 0;
}

    int Exercise()
    {   int x,y,randno,randno1,k=45,m=23,n=60,o=9,p=11,correct=0,wrong=0;
       
        
        char name[20];
        srand(time(NULL));
        randno=1+rand()%(100-1+1);
        randno1=1+rand()%(100-1+1);
       
   
    printf("Please enter your nickname:");
    scanf("%s",&name);
    printf("Hello %s  \n", name);
    printf("\n");
       
    printf("1)\t%d+%d=",randno,randno1);
    scanf("%d",&x);
    y=randno+randno1;
    printf("\n");
   
    if (x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("2)\t%d-32=",randno1);
    scanf("%d",&x);
    y=randno1-32;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
   
    printf("3)\t%d/1=",randno1);
    scanf("%d",&x);
    y=randno1/1;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("4)\t%d+%d=",randno,k);
    scanf("%d",&x);
    y=randno+k;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("5)\t%d-%d=",m,o);
    scanf("%d",&x);
    y=m-o;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("6)\t%d*%d=",randno1,n);
    scanf("%d",&x);
    y=randno1*n;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("7)\t%d+%d=",p,randno);
    scanf("%d",&x);
    y=p+randno;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("8)\t%d*%d=",k,randno1);
    scanf("%d",&x);
    y=k*randno1;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;   
    }
    printf("\n");
   
    printf("9)\t%d+74=",randno);
    scanf("%d",&x);
    y=randno+74;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("10)\t%d*2=",n);
    scanf("%d",&x);
    y=n*2;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("Total correct answer is %d",correct);
    printf("\n");
    printf("total wrong answer is %d",wrong);
    printf("\n");
    }
   
   
    int Tutorial()
    {
        int randno,randno1,randno2,randno3,randno4,randno5,randno6,m=1,x,y;
        char name[20];
        srand(time(NULL));
        randno=1+rand()%(100-1+1);
        randno1=1+rand()%(100-1+1);
        randno2=1+rand()%(100-1+1);
        randno3=1+rand()%(100-1+1);
        randno4=1+rand()%(100-1+1);
        randno5=1+rand()%(100-1+1);
        randno6=1+rand()%(100-1+1);
       
   
    printf("Please enter your nickname:");
    scanf("%s",&name);
    printf("Hello %s  \n", name);
    printf("\n");
       
       
    printf("1)\t%d+%d",randno,randno1);
    y=randno+randno1;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d+%d=%d",randno,randno1,y);
    printf("\n");
   
    printf("2)\t%d+%d",randno3,randno);
    y=randno3+randno;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d+%d=%d",randno3,randno,y);
    printf("\n");
   
   
    printf("3)\t%d-%d",randno4,randno1);
    y=randno4-randno1;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d-%d=%d",randno4,randno1,y);
    printf("\n");
   
       
    printf("4)\t%d*%d",randno6,randno5);
    y=randno6*randno5;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d*%d=%d",randno6,randno5,y);
    printf("\n");
   
       
    printf("5)\t%d-%d",randno4,randno2);
    y=randno4-randno2;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d-%d=%d",randno4,randno2,y);
    printf("\n");   
   
       
    printf("6)\t%d+%d",randno4,randno1);
    y=randno4+randno1;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d+%d=%d",randno4,randno1,y);
    printf("\n");
   
   
    printf("7)\t%d/%d",randno4,m);
    y=randno4/m;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d/%d=%d",randno4,m,y);
    printf("\n");
   
   
    printf("8)\t%d+%d",randno1,randno6);
    y=randno1+randno6;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d+%d=%d",randno1,randno6,y);
    printf("\n");
   
   
    printf("9)\t%d*%d",randno5,randno3);
    y=randno5*randno3;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d*%d=%d",randno5,randno3,y);
    printf("\n");
   
   
    printf("10)\t%d+%d",randno2,randno4);
    y=randno2+randno4;
    printf("\n");
    printf("\tAnswer:");
    printf("\n");
    printf("\t%d+%d=%d",randno2,randno4,y);
    printf("\n");
   
   
    }
   
    int Competition()
    {
   
    
        int x,y,i,randno,randno1,randno2,randno3,randno4,randno5,randno6,k=45,m=23,n=60,o=9,p=11,correct=0,wrong=0;
        int total[SIZE];
        char name[20];
        srand(time(NULL));
        randno=1+rand()%(100-1+1);
        randno1=1+rand()%(100-1+1);
        randno2=1+rand()%(100-1+1);
        randno3=1+rand()%(100-1+1);
        randno4=1+rand()%(100-1+1);
        randno5=1+rand()%(100-1+1);
        randno6=1+rand()%(100-1+1);
    
     for(i=0;i<SIZE; i++)
    {
    printf("Please enter your name (player %d):",i+1);
    scanf("%s",&name );
        
    printf("1)\t%d+%d=",randno,randno1);
    scanf("%d",&x);
    y=randno+randno1;
    printf("\n");
   
    if (x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("2)\t%d-32=",randno1);
    scanf("%d",&x);
    y=randno1-32;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
   
    printf("3)\t%d/1=",randno1);
    scanf("%d",&x);
    y=randno1/1;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("4)\t%d+%d=",randno,k);
    scanf("%d",&x);
    y=randno+k;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("5)\t%d-%d=",m,o);
    scanf("%d",&x);
    y=m-o;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("6)\t%d*%d=",randno1,n);
    scanf("%d",&x);
    y=randno1*n;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("7)\t%d+%d=",p,randno);
    scanf("%d",&x);
    y=p+randno;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("8)\t%d*%d=",k,randno1);
    scanf("%d",&x);
    y=k*randno1;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;   
    }
    printf("\n");
   
    printf("9)\t%d+74=",randno);
    scanf("%d",&x);
    y=randno+74;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    printf("10)\t%d*2=",n);
    scanf("%d",&x);
    y=n*2;
    printf("\n");
    if(x==y)
    {
    printf("correct!!");
    correct=++correct;
    }
    else
    {
    printf("wrong!!");
    wrong=++wrong;
    }
    printf("\n");
   
    total[i]=correct;
    correct=0;
   
    printf("Total correct answer is %d",total[i]);
    printf("\n");
    printf("total wrong answer is %d",wrong);
    wrong=0;
    printf("\n");
    printf("\n");    
    printf("\n");   
    printf("\n");   
       
     }
    printf("Total answer correct for player 1 is %d\n",total[0]);
    printf("Total answer correct for player 2 is %d\n",total[1]);
    if (total[0]>total[1])
    {
    printf("The winner is player 1");
    printf("\n");
    }
    else if (total[1]>total[0])
    {
    printf("The winner is player 2");
    printf("\n");
    }
    else if(total[0]==total[1])
    {printf("Draw!!");
    printf("\n\n");   
       
    }
   
   
   
   
     }