UVa 100 : 3n+1 DP solution

//C++ 5.3.0 - GNU C++ Compiler with options: -lm -lcrypt -O2 -pipe -DONLINE_JUDGE

#include<stdio.h>
#define MX 1000000
#define SWAP(X, Y) do { typeof(X) TMP = X; X = Y; Y = TMP; } while (0)

unsigned long DP[MX];
unsigned long tmp,max,k,t;

unsigned long cycleLen(unsigned long i,unsigned long n,unsigned long cnt)
{

    if(i<MX && DP[i]){ DP[n]=cnt+DP[i];return DP[n]; }

    if(i==1) { DP[n]=cnt;return DP[n]; }

    if(i%2 == 0) return cycleLen(i/2,n,cnt+1);

return cycleLen(3*i+1,n,cnt+1);
}


unsigned long mxLen(unsigned long i,unsigned long j)
{
    if(i>j) SWAP(i,j);

    max=cycleLen(i,i,0)+1;

    for(k=i+1;k<=j;k++){
        t=cycleLen(k,k,0)+1;
        if(max<t) max=t;
    }
    return max;
}

int main()
{
    unsigned long i,j,mx;
    //freopen("INPUT.TXT","r",stdin);
    //freopen("OUTPUT.TXT","w",stdout);

    while(scanf("%lu%lu",&i,&j)==2)
    {
        mx=mxLen(i,j);
        printf("%lu %lu %lu\n",i,j,mx);
    }
    return 0;
}


UVa - 755 487--3279

//C++ 5.3.0 - GNU C++ Compiler with options: -lm -lcrypt -O2 -pipe -DONLINE_JUDGE
#include<stdio.h>
#include<string.h>
#include<stdbool.h>

#define mmset(X) memset(X,0,sizeof(X))

unsigned int a[10000000];
bool flags[10000000];
bool dupes[10000000];
bool noDupes;

unsigned int map[200];

unsigned long idx,iA;
unsigned long cases;
int cPhn,lPhn,lParsed,iParsed,iPhn,tmpInd,mappedInd,iTmp;
char phn[1000],buffer[10];
char parsed[1000];





int main(){


    for(iTmp=0;iTmp<=9;iTmp++) map[iTmp]=iTmp;


    map[17]=2;map[18]=2;map[19]=2;
    map[20]=3;map[21]=3;map[22]=3;
    map[23]=4;map[24]=4;map[25]=4;
    map[26]=5;map[27]=5;map[28]=5;
    map[29]=6;map[30]=6;map[31]=6;
    map[32]=7;map[34]=7;map[35]=7;
    map[36]=8;map[37]=8;map[38]=8;
    map[39]=9;map[40]=9;map[41]=9;

//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);   


    scanf("%lu",&cases);

    while(cases--){

        noDupes=true;
        memset(a, 0, sizeof(a));
        memset(flags, false, sizeof(flags));
        memset(dupes, false, sizeof(dupes));


        scanf("%d",&cPhn);

        while(cPhn--){
            scanf("%s",phn);
            lPhn=strlen(phn);
            iParsed=0;

            for(iPhn=0;iPhn<lPhn;iPhn++){
                if(phn[iPhn] != '-'){
                    parsed[iParsed++]=phn[iPhn];

                }

            }
            parsed[iParsed]='\0';
            lParsed=strlen(parsed);


            idx=0;

            for(iParsed=0;iParsed<lParsed;iParsed++){

                tmpInd=parsed[iParsed]-'0';

                mappedInd=map[tmpInd];
                idx=10*idx;
                idx=idx+mappedInd;
            }

            if(dupes[idx]){

                a[idx] += 1;
            }
            else if(flags[idx]){

                a[idx] += 1;noDupes=false;
                dupes[idx]=true;
            }
            else{
                a[idx] += 1;
                flags[idx]=true;
            }

        }


        for(iA=0;iA<=9999999;iA++){

            if(noDupes){printf("No duplicates.\n");break;}

            if(dupes[iA]){
                sprintf(buffer, "%07lu",iA);
                printf("%c%c%c-%c%c%c%c %u\n",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5],buffer[6],a[iA]);
            }

        }

        if(cases != 0) printf("\n");
    }
}

UVa - 621 Secret Research

#include<stdio.h>
#include<string.h>

char s[2000];
int n,l,c;

inline int plus() {   return !strcmp(s, "1") || !strcmp(s, "4") || !strcmp(s, "78"); }

inline int minus() {   return s[l - 2] == '3' && s[l - 1] == '5'; }

inline int star() {   return s[0] == '9' && s[l - 1] == '4'; }


int main() {

    char r[]="+-*";
    int (*pattern[3])()={plus,minus,star};

//        freopen("input.txt","r",stdin);
//        freopen("output.txt","w",stdout);   

    scanf("%d",&n);
    while(n--){
        scanf("%s",s);
        l=strlen(s);

        for(c=0;c<3;c++){
            if((*pattern[c])()) {printf("%c\n",r[c]);break;}
        }

        if(c==3) printf("?\n");
    }
    return 0;
}

UVa - 11827 Maximum GCD

//java solution

import java.io.*;
import java.util.*;

class Main
{


    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dinamic instance
        myWork.Begin();            // the true entry point
    }
int gcd(int a,int b) {
while (b > 0) {
a = a % b;
b=a-b+(a=b); }
return a;
}
    void Begin()
    {
 try
 {
 
Scanner sc=new Scanner(System.in);
int n;
ArrayList dts,a;
dts=new ArrayList(150);
a=new ArrayList(150);
n=Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++)
{
dts.add(sc.nextLine());
}
for(int k=0;k<n;k++)
{
Scanner scc=new Scanner(dts.get(k).toString());
while(scc.hasNext()) a.add(scc.next());
int max=1,t,t1,t2;
for(int i=0;i<a.size();i++)
for(int j=i+1;j<a.size();j++)
{
t1=Integer.parseInt(a.get(i).toString());
t2=Integer.parseInt(a.get(j).toString());
t=gcd(t1,t2);
if(max<t) max=t;
}
System.out.println(max);
a.clear();
}
sc.close();
}
 catch(Exception e)
 {
  System.exit(0);
 }
    }


}

UVa - 10183 How many Fibs?

//java solution

import java.util.*;
import java.util.regex.*;
import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.math.*;
import java.text.*;

class Main
{


    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dynamic instance
        myWork.Begin();            // the true entry point
    }

    void Begin()
    {
Scanner sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter pr=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
/*
try
 {
   sc=new Scanner(new File("INPUT.TXT"));
 pr=new PrintWriter(new File("OUTPUT.TXT"));
}
 catch(Exception e)
 {
  System.exit(0);
 }*/
  ArrayList abc=new ArrayList(500);
  abc.add(new BigInteger("1"));
  abc.add(new BigInteger("2"));
  BigInteger b1=new BigInteger("1");
  BigInteger b2=new BigInteger("2");
  BigInteger tmp=new BigInteger("0");
  BigInteger tmp2=new BigInteger("0");
  BigInteger a=new BigInteger("0");
  BigInteger b=new BigInteger("0");
  BigInteger lpcntr=new BigInteger("0");
  BigInteger cntr=new BigInteger("0");
  int x=0,y=0,t=0;
  for(int i=2;i<501;i++)
  {
   tmp=b2;
   b2=b1.add(b2);
   b1=tmp;
   abc.add(i,b2);
  }
  Collections.sort(abc);
  for(;;)
  {
t=0;
a=sc.nextBigInteger();
   b=sc.nextBigInteger();
   if(a.compareTo(BigInteger.ZERO)==0 && b.compareTo(BigInteger.ZERO)==0) break;
  
  
   if(a.compareTo(b)>0)
   {
   tmp2=a;a=b;b=tmp2;
   }
   x=Collections.binarySearch(abc,a);
   if(x<0){x=x+1;x = -1*x;t=t+1;}
   y=Collections.binarySearch(abc,b);
   if(y<0){y=y+1;y = -1*y; y=y-1;t=t+1;}  
   if(a.compareTo(b)==0 && t==0)
   pr.printf("1%n");
   else
   pr.printf("%d%n",(y-x)+1);
  }
    pr.close();
sc.close();
  }


}

Simple 2D Racing Game with C/C++

Simple 2D Racing Game with C\C++ : The following , is a simple 2D racing game , created , using the BGI graphics library of Turbo C/C++ 3.0 :


While running this program , make sure that, you used the correct path for the BGI graphics driver .For example : if Turbo C/C++ is installed in the following location "C:\TC" , then the BGI graphics path should be "C:\\TC\\BGI" :


Screenshot :







C++ Codes :


#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include <dos.h>

#include <ctype.h>

#define   r   20

#define  tr   35

#define  jr   25

int x=140,y=400;

int num;

FILE *fp1,*fp2;

int ccar1=2,ccar2=11,ccar3=9,ccar4=12,ctruck=15,cjeep=11,cbus=6;

long int score=0;

void *car,*car1,*road,*truck,*jeep,*bus,*car2,*car3,*car4;

char mnmn[5]={'M','A','S','U','M'};

struct record{

int rscore;

char rname[30];

};

struct record record_book[10];

void init_list(void)

{

int t;

for(t=0;t<10;t++)

record_book[t].rscore=0;

}

void rcd_info()

{

FILE *fp;

int t;

int i,k;

char M1[10],M2[30],M3[5];

if((fp=fopen("C:\\RCDBOOK.MYG","r"))==NULL)

{

fp=fopen("C:\\RCDBOOK.MYG","w");

for(i=500,t=0;t<10;i-=50,t++)

{

record_book[t].rscore=i;

for(k=0;k<5;k++)

record_book[t].rname[k]=mnmn[k];

}

fwrite(record_book,sizeof record_book,1,fp);

fclose(fp);

fp=fopen("C:\\RCDBOOK.MYG","r");

}

fread(record_book,sizeof record_book,1,fp);

setcolor(11);

settextstyle(0,0,2);

outtextxy(210,220,"Score");

outtextxy(80,220,"Name");

setcolor(11);

settextstyle(0,0,1);

for(t=0;t<10;t++)

{

sprintf(M3,"%d)",t+1);

if(t==9)

outtextxy(60,245+12*t,M3);

else

outtextxy(65,245+12*t,M3);

}

setcolor(10);

for(t=0;t<10;t++)

{

 if(record_book[t].rscore)

 {

  sprintf(M1,"%d",record_book[t].rscore);

  sprintf(M2,"%s",record_book[t].rname);

  if(record_book[t].rscore<100)

  outtextxy(235,245+12*t,M1);

  else if(record_book[t].rscore>=1000)

  outtextxy(225,245+12*t,M1);

  else if(record_book[t].rscore>=10000)

  outtextxy(220,245+12*t,M1);

  else

  outtextxy(230,245+12*t,M1);



  outtextxy(90,245+12*t,M2);

  }

 }

 fclose(fp);

}

void entry(int mscore)

{

FILE *fp;

int t,k;

int i,nnn=0;

char m1[10],m2[30];

if((fp=fopen("C:\\RCDBOOK.MYG","r"))==NULL)

{

fp=fopen("C:\\RCDBOOK.MYG","w");

for(i=500,t=0;t<10;i-=50,t++)

{

record_book[t].rscore=i;

for(k=0;k<5;k++)

record_book[t].rname[k]=mnmn[k];

}

fwrite(record_book,sizeof(record_book),1,fp);

fclose(fp);

fp=fopen("C:\\RCDBOOK.MYG","r");

}

fread(record_book,sizeof(record_book),1,fp);

for(t=0;t<10;t++)

{

 if(mscore>record_book[t].rscore)

 {

  fclose(fp);

  fp=fopen("C:\\RCDBOOK.MYG","w");

  for(i=10-1;i>=t;i--)

  {

  record_book[i].rscore=record_book[i-1].rscore;

  for(k=0;k<30;k++)

  record_book[i].rname[k]=record_book[i-1].rname[k];

  }

  record_book[t].rscore=mscore;

  nnn=1;

  settextstyle(0,0,2);

  outtextxy(100,100," You have a high score.");

  outtextxy(100,120,"Enter your name:");

  gotoxy(30,10);

  scanf("%s",record_book[t].rname);

  fwrite(record_book,sizeof(record_book),1,fp);

  fclose(fp);

  fp=fopen("C:\\RCDBOOK.MYG","r");

  fread(record_book,sizeof record_book,1,fp);

  break;

  }



}

cleardevice();

if(nnn==1)

{

setcolor(9);

settextstyle(10,0,6);

outtextxy(60,2,"High Scores");

setcolor(3);

settextstyle(0,0,3);

outtextxy(82,140,"NAME");

outtextxy(362,140,"SCORE");

setcolor(2);

settextstyle(0,0,2);

  for(i=0;i<10;i++)

{

 if(record_book[i].rscore)

 {

  sprintf(m1,"%d",record_book[i].rscore);

  outtextxy(382,180+25*i,m1);

  sprintf(m2,"%s",record_book[i].rname);

  outtextxy(92,180+25*i,m2);

  }

 }

  while(!kbhit())

  {

  setcolor(11);

  sprintf(m1,"%d",record_book[t].rscore);

  outtextxy(382,180+25*t,m1);

  sprintf(m2,"%s",record_book[t].rname);

  outtextxy(92,180+25*t,m2);

  delay(350);

  setcolor(0);

  sprintf(m1,"%d",record_book[t].rscore);

  outtextxy(382,180+25*t,m1);

  sprintf(m2,"%s",record_book[t].rname);

  outtextxy(92,180+25*t,m2);

  delay(350);

  }

  }

  fclose(fp);

}

music(int type)

{

float octave[7]={130.81,146.83,164.81,174.61,196,220,246.94};

int n,i;

switch(type)

{

 case 1:

 for(i=0;i<7;i++)

 {

  sound(octave[i]*8);

  delay(30);

  }

 nosound();

 break;

 case 2:

 fflush(stdin);

 if(getch()==0)

 getch();

 for(i=0;i<15;i++)

 {

  n=random(7);

  sound(octave[n]*4);

  delay(100);

  }

  nosound();

 break;

 case 3:

 while(!kbhit())

 {

  n=random(7);

  sound(octave[n]*4);

  delay(100);

  }

 fflush(stdin);

 if(getch()==0)

 getch();

 nosound();

 break;

 case 4:

 for(i=4;i>=0;i--)

 {

  sound(octave[i]*4);

  delay(15);

  }

  nosound();

  break;

 case 5:

 sound(octave[6]*2);

 delay(20);

 nosound();

 }

 return(type);

 }

void gameover()

{

char *word,B4[100],M1[30];

cleardevice();

setcolor(3);

settextstyle(0,HORIZ_DIR,3);

outtextxy(200,200,"GAME OVER");

music(2);

cleardevice();

sprintf(M1,"YOUR SCORE: %d",score);

outtextxy(100,100,M1);

music(3);

cleardevice();

entry(score);

}

void draw_car1(int u, int v)

{

   moveto(u, v);

   setcolor(ccar1);

   rectangle(u,v,u+r,v+2*r);

   rectangle(u+1,v+2,u+4,v+3);

   rectangle(u+r-4,v+2,u+r-1,v+3);

   line(u,v+12,u+r,v+12);

   line(u+2,v+20,u+r-2,v+20);

   line(u+2,v+30,u+r-2,v+30);

   line(u,v+35,u+r,v+35);

   line(u+2,v+20,u+2,v+30);

   line(u+r-2,v+20,u+r-2,v+30);

   line(u,v+12,u+2,v+20);

   line(u+r,v+12,u+r-2,v+20);

   line(u+2,v+30,u,v+35);

   line(u+r-2,v+30,u+r,v+35);

   line(u,v+r+5,u+2,v+r+5);

   line(u+r-2,v+r+5,u+r,v+r+5);

}

void draw_jeep(int c,int d)

{

moveto(c,d);

setcolor(cjeep);

rectangle(c,d,c+jr,d+2*jr);

rectangle(c+1,d+2,c+4,d+3);

rectangle(c+jr-4,d+2,c+jr-1,d+3);

line(c,d+17,c+jr,d+17);

rectangle(c+2,d+25,c+jr-2,d+47);

line(c,d+17,c+2,d+25);

line(c+jr,d+17,c+jr-2,d+25);

line(c,d+2*jr,c+2,d+47);

line(c+jr-2,d+47,c+jr,d+2*jr);

line(c,d+35,c+2,d+35);

line(c+jr-2,d+35,c+jr,d+35);

line(c+jr/2+1,d+47,c+jr/2+1,d+2*jr);

setfillstyle(1,cjeep);

bar(c+5,d+47,c+(jr/2)-2,d+50);

}

void draw_car(int x, int y)

{

   moveto(x, y);

   setcolor(0);

   setfillstyle(1,3);

   bar(x,y,x+r,y+2*r);

   rectangle(x+1,y+2,x+4,y+3);

   rectangle(x+r-4,y+2,x+r-1,y+3);

   line(x,y+12,x+r,y+12);

   line(x+2,y+20,x+r-2,y+20);

   line(x+2,y+30,x+r-2,y+30);

   line(x,y+35,x+r,y+35);

   line(x+2,y+20,x+2,y+30);

   line(x+r-2,y+20,x+r-2,y+30);

   line(x,y+12,x+2,y+20);

   line(x+r,y+12,x+r-2,y+20);

   line(x+2,y+30,x,y+35);

   line(x+r-2,y+30,x+r,y+35);

}

void reset()

{

free(car);

free(car1);

free(car2);

free(car3);

free(car4);

free(road);

free(truck);

free(jeep);

free(bus);

}

void art()

{

setcolor(3);

settextstyle(5,0,1);

outtextxy(210,130,"Racing");

setcolor(12);

settextstyle(10,0,8);

outtextxy(70,10,"MYGAME");

draw_car(260,220);

draw_car1(340,220);

draw_jeep(297,210);

setcolor(9);

settextstyle(0,0,1);

outtextxy(235,300,"WELLCOME TO MY GAME");

outtextxy(235,320,"  PRESS ANY KEY...");

setcolor(8);

settextstyle(0,0,2);

outtextxy(100,380,"CREATED BY...");

setcolor(3);

settextstyle(0,0,3);

outtextxy(150,420,"MD. MOHIUDDIN AHMED");

while(!kbhit()){ }

fflush(stdin);

if(getch()==0)

getch();

}

void mainscreen()

{

union REGS iii,ooo,im,om;

char *word,B4[100];

char ch;

int maxx=getmaxx(),maxy=getmaxy(),midx=maxx/2,midy=maxy/2;

int mbally=maxy-191;

int m5=0;

int chm=1;

masum6:

 setviewport(1,2,maxx,3,1);

 clearviewport();

 setviewport(0,0,maxx,maxy,1);

 setcolor(11);

 settextstyle(0,0,2);

 outtextxy(250,15,"SELECT:");

 settextstyle(0,HORIZ_DIR,2);

 while(1)

 {

 if(m5==0)

 {

  setviewport(1,170,maxx,maxy-100,1);

  clearviewport();

  setviewport(0,0,maxx,maxy,1);

  m5=1;

  }

  if(chm==1)

  {

  setcolor(11);

  settextstyle(0,HORIZ_DIR,2);

  outtextxy(midx-284,midy-20,"PLAY");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy,"HELP");

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  outtextxy(midx-284,midy+40,"CREDIT");

  outtextxy(midx-284,midy+60,"EXIT");

  }

  if(chm==2)

  {

  setcolor(11);

  outtextxy(midx-284,midy,"HELP");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy-20,"PLAY");

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  outtextxy(midx-284,midy+40,"CREDIT");

  outtextxy(midx-284,midy+60,"EXIT");

  }

  if(chm==3)

  {

  setcolor(11);

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy,"HELP");

  outtextxy(midx-284,midy-20,"PLAY");

  outtextxy(midx-284,midy+40,"CREDIT");

  outtextxy(midx-284,midy+60,"EXIT");

  }

  if(chm==4)

  {

  setcolor(11);

  outtextxy(midx-284,midy+40,"CREDIT");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy,"HELP");

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  outtextxy(midx-284,midy+60,"EXIT");

  outtextxy(midx-284,midy-20,"PLAY");

  }

  if(chm==5)

  {

  setcolor(11);

  outtextxy(midx-284,midy+60,"EXIT");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy,"HELP");

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  outtextxy(midx-284,midy+40,"CREDIT");

  outtextxy(midx-284,midy-20,"PLAY");

  }



  if(kbhit())

  {

   music(4);

   iii.h.ah=0;

   int86(22,&iii,&ooo);

   if(ooo.h.ah==72)

   {

   if(mbally==maxy-191)

   {

   setviewport(midx-305,midy-25,midx-305+14,midy+90,1);

   clearviewport();

   setviewport(0,0,maxx,maxy,1);

   mbally=maxy-191;

  setcolor(11);

  outtextxy(midx-284,midy-20,"PLAY");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy,"HELP");

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  outtextxy(midx-284,midy+40,"CREDIT");

  outtextxy(midx-284,midy+60,"EXIT");

  chm=1;

   }

   if(mbally==maxy-171)

   {

   setviewport(midx-305,midy-25,midx-305+14,midy+90,1);

   clearviewport();

   setviewport(0,0,maxx,maxy,1);

   mbally=maxy-191;

   setcolor(11);

  outtextxy(midx-284,midy-20,"PLAY");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy,"HELP");

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  outtextxy(midx-284,midy+40,"CREDIT");

  outtextxy(midx-284,midy+60,"EXIT");

  chm=1;

   }

   if(mbally==maxy-151)

   {

   setviewport(midx-305,midy-25,midx-305+14,midy+90,1);

   clearviewport();

   setviewport(0,0,maxx,maxy,1);

   mbally=maxy-171;

   setcolor(11);

  outtextxy(midx-284,midy,"HELP");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy-20,"PLAY");

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  outtextxy(midx-284,midy+40,"CREDIT");

  outtextxy(midx-284,midy+60,"EXIT");

  chm=2;

   }

   if(mbally==maxy-131)

   {

   setviewport(midx-305,midy-25,midx-305+14,midy+90,1);

   clearviewport();

   setviewport(0,0,maxx,maxy,1);

   mbally=maxy-151;

   setcolor(11);

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy,"HELP");

  outtextxy(midx-284,midy-20,"PLAY");

  outtextxy(midx-284,midy+40,"CREDIT");

  outtextxy(midx-284,midy+60,"EXIT");

  chm=3;

   }

     if(mbally==maxy-111)

   {

   setviewport(midx-305,midy-25,midx-305+14,midy+90,1);

   clearviewport();

   setviewport(0,0,maxx,maxy,1);

   mbally=maxy-131;

   setcolor(11);

  outtextxy(midx-284,midy+40,"CREDIT");

  setcolor(12);

  outtextxy(midx-290,midy-50,"SELECT ANY OF THE FOLLOWING:");

  setcolor(9);

  outtextxy(midx-284,midy,"HELP");

  outtextxy(midx-284,midy-20,"PLAY");

  outtextxy(midx-284,midy+20,"HIGH SCORE");

  //Sorry for  incomplete code ................please download the full cpp source file

  //from my google docs .........  link :

Download

Simple 2D Pinball Game using C/C++

Simple 2D Pinball Game with C\C++ :  Following , is a simple 2D Pinball game(like DX-Ball,Para etc.) , created , using the BGI graphics library of Turbo C/C++ 3.0.

While running this program , make sure using correct path for the BGI graphics driver .For example : if Turbo C/C++ is installed in the following location "C:\TC" , then the BGI graphics path should be "C:\\TC\\BGI" :

Screenshot :


C++ Code :

 #include"stdio.h"

#include"conio.h"

#include"ctype.h"

#include"process.h"

#include"alloc.h"

#include"dos.h"

#include"stdlib.h"

#include"graphics.h"

#define NULL 0

#define yes 1

#define no 0

char mmas[25];

int bri[15][20];

int s=1;

int score2=0;

char m;

int stage=1;

char mnmn[5]={'M','A','S','U','M'};

struct record{

int rscore;

char rname[30];

};

struct record record_book[10];

void cursor_on(void)

{

 union REGS mouse;

 mouse.x.ax=0;

 int86(0X33,&mouse,&mouse);

 }

 void cursor_off(void)

{

 union REGS mouse;

 mouse.x.ax=2;

 int86(0X33,&mouse,&mouse);

 }

 int leftb_pressed(void)

{

 union REGS r;

 r.x.ax=3;

 int86(0X33,&r,&r);

 return(r.x.bx&1);

 }

 void mouse_position(int *mousex,int *mousey)

{

 union REGS mouse;

 mouse.x.ax=3;

 int86(0X33,&mouse,&mouse);

 *mousex=mouse.x.cx;

 *mousey=mouse.x.dx;

}

void init_list(void)

{

int t;

for(t=0;t<10;t++)

record_book[t].rscore=0;

}

void rcd_info()

{

FILE *fp;

int t;

int i,k;

char M1[10],M2[30],M3[5];

if((fp=fopen("C:\\RCDBOOK.MYG","r"))==NULL)

{

fp=fopen("C:\\RCDBOOK.MYG","w");

for(i=500,t=0;t<10;i-=50,t++)

{

record_book[t].rscore=i;

for(k=0;k<5;k++)

record_book[t].rname[k]=mnmn[k];

}

fwrite(record_book,sizeof record_book,1,fp);

fclose(fp);

fp=fopen("C:\\RCDBOOK.MYG","r");

}

fread(record_book,sizeof record_book,1,fp);

setcolor(11);

settextstyle(0,0,2);

outtextxy(210,160,"Score");

outtextxy(80,160,"Name");

setcolor(11);

settextstyle(0,0,1);

for(t=0;t<10;t++)

{

sprintf(M3,"%d)",t+1);

if(t==9)

outtextxy(60,185+12*t,M3);

else

outtextxy(65,185+12*t,M3);

}

setcolor(10);

for(t=0;t<10;t++)

{

 if(record_book[t].rscore)

 {

  sprintf(M1,"%d",record_book[t].rscore);

  sprintf(M2,"%s",record_book[t].rname);

  if(record_book[t].rscore<100)

  outtextxy(235,185+12*t,M1);

  else if(record_book[t].rscore>=1000)

  outtextxy(225,185+12*t,M1);

  else if(record_book[t].rscore>=10000)

  outtextxy(220,185+12*t,M1);

  else

  outtextxy(230,185+12*t,M1);



  outtextxy(90,185+12*t,M2);

  }

 }

 fclose(fp);

}

void entry(int mscore)

{

FILE *fp;

int t,k;

int i,nnn=0;

char m1[10],m2[30];

cleardevice();

if((fp=fopen("C:\\RCDBOOK.MYG","r"))==NULL)

{

fp=fopen("C:\\RCDBOOK.MYG","w");

for(i=500,t=0;t<10;i-=50,t++)

{

record_book[t].rscore=i;

for(k=0;k<5;k++)

record_book[t].rname[k]=mnmn[k];

}

fwrite(record_book,sizeof(record_book),1,fp);

fclose(fp);

fp=fopen("C:\\RCDBOOK.MYG","r");

}

fread(record_book,sizeof(record_book),1,fp);

for(t=0;t<10;t++)

{

 if(mscore>record_book[t].rscore)

 {

  fclose(fp);

  fp=fopen("C:\\RCDBOOK.MYG","w");

  for(i=10-1;i>=t;i--)

  {

  record_book[i].rscore=record_book[i-1].rscore;

  for(k=0;k<30;k++)

  record_book[i].rname[k]=record_book[i-1].rname[k];

  }

  record_book[t].rscore=mscore;

  nnn=1;

  settextstyle(0,0,2);

  outtextxy(100,100," You have a high score.");

  outtextxy(100,120,"Enter your name:");

  gotoxy(30,11);

  scanf("%s",record_book[t].rname);

  fwrite(record_book,sizeof(record_book),1,fp);

  fclose(fp);

  fp=fopen("C:\\RCDBOOK.MYG","r");

  fread(record_book,sizeof record_book,1,fp);

  break;

  }



}

cleardevice();

if(nnn==1)

{

setcolor(9);

settextstyle(10,0,6);

outtextxy(60,1,"High Scores");

setcolor(3);

settextstyle(0,0,3);

outtextxy(82,130,"NAME");

outtextxy(362,130,"SCORE");

setcolor(10);

settextstyle(0,0,1);

  for(i=0;i<10;i++)

{

 if(record_book[i].rscore)

 {

  sprintf(m1,"%d",record_book[i].rscore);

  outtextxy(392,170+15*i,m1);

  sprintf(m2,"%s",record_book[i].rname);

  outtextxy(102,170+15*i,m2);

  }

 }

  while(!leftb_pressed())

  {

  setcolor(11);

  sprintf(m1,"%d",record_book[t].rscore);

  outtextxy(392,170+15*t,m1);

  sprintf(m2,"%s",record_book[t].rname);

  outtextxy(102,170+15*t,m2);

  delay(350);

  setcolor(0);

  sprintf(m1,"%d",record_book[t].rscore);

  outtextxy(392,170+15*t,m1);

  sprintf(m2,"%s",record_book[t].rname);

  outtextxy(102,170+15*t,m2);

  delay(350);

  }

  }

  fclose(fp);

}

drawbrick(int lx,int ly)

{

setcolor(s);

rectangle(lx,ly,lx+31,ly+9);

rectangle(lx+2,ly+2,lx+31-2,ly+9-2);

setfillstyle(1,s+1);

floodfill(lx+1,ly+1,s);

setfillstyle(s,9);

bar(lx+6,ly+4,lx+31-6,ly+9-4);

setfillstyle(11,3);

return(lx,ly);

}

music(int type)

{

float octave[7]={130.81,146.83,164.81,174.61,196,220,246.94};

int n,i;

switch(type)

{

 case 1:

 for(i=0;i<7;i++)

 {

  sound(octave[i]*8);

  delay(30);

  }

 nosound();

 break;

 case 2:

 for(i=0;i<15;i++)

 {

  n=random(7);

  sound(octave[n]*4);

  delay(100);

  }

 nosound();

 break;

 case 3:

 while(!leftb_pressed())

 {

  n=random(7);

  sound(octave[n]*4);

  delay(100);

  }

 nosound();

 while(leftb_pressed());

 break;

 case 4:

 for(i=4;i>=0;i--)

 {

  sound(octave[i]*4);

  delay(15);

  }

  nosound();

  break;

 case 5:

 sound(octave[6]*2);

 delay(50);

 nosound();

 }

 return(type);

 }

 bricks()

{

int i,j,lx=0,ly=0;

for(i=0;i<5;i++)

{

 for(j=0;j<20;j++)

 {

  drawbrick(lx,ly);

  lx=lx+32;

  delay(20);

  }

 lx=0;

 ly=ly+10;

 }

 return 0;

}

erasebrick(int b,int l)

{

setfillstyle(1,0);

bar(b*32,l*10,(b*32)+31,(l*10)+9);

return(b,l);

}

art()

{ void *q;

  int k,maxu,maxv,u=25,v=25,du=1,dv=1,area1,maxx=getmaxx(),maxy=getmaxy(),midx=maxx/2,midy=maxy/2;;

  setfillstyle(SOLID_FILL,RED);

  circle(50,50,25);

  floodfill(50,50,WHITE);

  area1=imagesize(u,v,75,75);

  q=malloc(area1);

  getimage(u,v,75,75,q);

  maxu=getmaxx();

  maxv=getmaxy();

  setcolor(getmaxcolor());

  setfillstyle(SOLID_FILL,getmaxcolor());

  setcolor(3);

  for(k=0;k<=10;k++)

  {

  rectangle(k,k,maxu-k,maxv-k);

  }

  setcolor(9);

  rectangle(0,0,maxu,maxv);

  setcolor(3);

  outtextxy(midx-60,midy-120,"CONVENER:");

  setcolor(12);

  settextstyle(0,HORIZ_DIR,2);

  outtextxy((maxx/2)-145,(maxy/2)-100,"S.M. SHAMIM HASAN");

  setcolor(6);

  settextstyle(0,HORIZ_DIR,1);

  outtextxy((maxx/2)-90,(maxy/2)-10,"WELLCOME TO MY GAME");

  outtextxy((maxx/2)-110,(maxy/2)+10,"PRESS ANY KEY TO CONTINUE...");

  setcolor(9);

  settextstyle(10,HORIZ_DIR,1);

  outtextxy((maxx/2)-280,(maxy/2)-80,"PATUAKHALI POLYTECHNIC INSTITUTE");

  settextstyle(0,HORIZ_DIR,1);

  setcolor(3);

  outtextxy((maxx/2)-60,(maxy/2)+50,"CREATED BY...");

  setcolor(11);

  settextstyle(0,HORIZ_DIR,2);

  outtextxy((maxx/2)-170,(maxy/2)+70,"MD. MOHIUDDIN AHMED.");

  setcolor(9);

  settextstyle(0,HORIZ_DIR,1);

  outtextxy((maxx/2)-90,(maxy/2)+100,"COMPUTER TECHNOLOGY");

  setcolor(9);

  setfillstyle(1,0);

 while(!leftb_pressed())

 {

 putimage(u,v,q,XOR_PUT);

 u=u+(du*5);

 v=v+(dv*2);

 putimage(u,v,q,XOR_PUT);

 if(u>maxu-60||u<15)

 {

 music(5);

 du=-du;

 }

 if(v>maxv-55||v<7)

 {

 music(5);

 dv=-dv;

 }

 delay(15);

 }

  while(leftb_pressed());

  free(q);

  cleardevice();

  return 0;

  }

int mainscreen()

{

int num;

char *word;

FILE *fp1,*fp2;

char ch;

void *mouse;

int mus0=0,mus1=0,mus2=0,mus3=0,mus4=0,area,maxx=getmaxx(),maxy=getmaxy(),midx=maxx/2,midy=maxy/2;

int mballx=midx-298,mbally=maxy-191;

int ff[12][40]={0,0,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,0};

int i,j,lx=0,ly=30;

setcolor(14);

circle(10,10,7);

setfillstyle(1,12);

floodfill(10,10,14);

area=imagesize(3,3,17,17);

mouse=malloc(area);

if(mouse==NULL)

{

printf("Insufficient memory space.\n");

getch();

exit(1);

}

getimage(3,3,17,17,mouse);

cleardevice();

setcolor(3);

rectangle(0,0,maxx,maxy);

rectangle(2,2,maxx-2,maxy-2);

setcolor(12);

rectangle(7,4,maxx-7,20);

setcolor(11);

outtextxy(midx-105,midy-165,"Use the mouse to continue...");

setcolor(1);

for(i=0;i<12;i++)

{

 for(j=0;j<40;j++)

 {

  if(ff[i][j])

  rectangle(lx,ly,lx+17,ly+9);

  lx=lx+17;

  delay(5);

  }

 lx=0;

 ly=ly+10;

 }

 setfillstyle(3,3);

 bar(22,112,622,108);

 setcolor(3);

 settextstyle(0,HORIZ_DIR,2);

 outtextxy(midx-235,midy-30,"Win this game by scoring 7500.");

 setcolor(9);

 settextstyle(0,HORIZ_DIR,1);

 outtextxy(midx-300,midy+18,"Programmer:");

 outtextxy(midx-300,midy+40,"MD. Mohiuddin ahmed.");

 outtextxy(midx-300,midy+55,"Computer Technology.");

 outtextxy(midx-300,midy+70,"Patuakhali Polytechnic Institute.");

 outtextxy(midx-300,midy+100,"COM-105553");

 setcolor(3);

 line(0,maxy-10,maxx,maxy-10);

 setfillstyle(1,12);

 setcolor(12);

 rectangle(midx-25,maxy-7-12,midx+25,maxy-12);

 floodfill(midx,maxy-1-12,12);

 setcolor(11);

 circle(320,320,12);

 setfillstyle(1,3);

 floodfill(320,320,11);

 music(3);

 masum6:

 setviewport(midx-155,midy-168,midx+115,midy-156,1);

 clearviewport();

 setviewport(0,0,maxx,maxy,1);

 setcolor(11);

 outtextxy(midx-30,midy-165,"SELECT:");

 settextstyle(0,HORIZ_DIR,2);

 setcolor(12);

 setviewport(3,125-12,maxx-3,maxy-3,1);

 clearviewport();

 setviewport(0,0,maxx,maxy,1);

 outtextxy(midx-290,midy-40,"Select any of the following:");

 setcolor(9);

 outtextxy(midx-284,midy-20,"Play");

 outtextxy(midx-284,midy,"Help");

 outtextxy(midx-284,midy+20,"High Score");

 outtextxy(midx-284,midy+40,"Credit");

 outtextxy(midx-284,midy+60,"Exit");

 cursor_on();

 while(1)

 {

 mouse_position(&mballx,&mbally);

 if(mballx>=maxx-14)

 mballx=maxx-14;

 if(mbally>=maxy-14)

 mbally=maxy-14;

 putimage(mballx,mbally,mouse,XOR_PUT);

 if(mballx>=midx-290&&mbally>=midy-22&&mballx<=midx-213&&mbally<=midy-5)

 {

 if(mus0==0)

 {

 sound(400);

 delay(30);

 nosound();

 mus0=1;

 }

 setcolor(11);

 outtextxy(midx-284,midy-20,"Play");

 if(leftb_pressed())

 goto masum5;

 }

 else

 {

 mus0=0;

 setcolor(9);

 outtextxy(midx-284,midy-20,"Play");

 }

 if(mballx>=midx-290&&mbally>=midy-2&&mballx<=midx-213&&mbally<=midy+15)

 {

 if(mus1==0)

 {

 sound(400);

 delay(30);

 nosound();

 mus1=1;

 }

 setcolor(11);

 outtextxy(midx-284,midy,"Help");

 if(leftb_pressed())

 break;

 }

 else

 {

 mus1=0;

 setcolor(9);

 outtextxy(midx-284,midy,"Help");

 }

  if(mballx>=midx-290&&mbally>=midy+18&&mballx<=midx-123&&mbally<=midy+35)

 {

 if(mus2==0)

 {

 sound(400);

 delay(30);

 nosound();

 mus2=1;

 }

 setcolor(11);

 outtextxy(midx-284,midy+20,"High Score");

 if(leftb_pressed())

 break;

 }

 else



------------------------------------------------

//sorry for incomplete code. Please download full code from  following link…

 Download

Simple Calendar in C/C++


Simple Calendar in C\C++ : The following is a simple calendar created using BGI graphics library of Turbo C/C++ 3.0 :

While running this program , make sure you have used correct path for BGI graphics driver . Example : if Turbo C/C++ is installed at "C:\TC" , BGI graphics path would be "C:\\TC\\BGI" :

Screenshot :





C++ Code :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<dos.h>
int gm,gd,scan,ascii;
void main()
{
initgraph(&gm,&gd,"F:\\TC\\BGI");
union REGS ii,oo;
char masum[20],*month[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
int m,y,leapyears,row,col,x,i,firstday,thisyrdays;
long int totaldays;
char str1[5],str2[3];
setcolor(3);
outtextxy(25,75,"Which month do you want to see?");
setcolor(6);
settextstyle(0,0,2);
outtextxy(210,35,"WELCOME TO...");
setcolor(10);
settextstyle(7,0,6);
outtextxy(10,195,"MY");
setcolor(12);
settextstyle(10,0,3);
outtextxy(50,245,"DISPLAY CALENDER SERVICE");
setcolor(11);
settextstyle(7,0,5);
outtextxy(420,295,"PROGRAM");
setcolor(8);
settextstyle(0,0,2);
outtextxy(10,360,"CREATED BY...");
setcolor(9);
settextstyle(0,0,4);
outtextxy(10,400,"MD. MOHIUDDIN AHMED");
setcolor(11);
settextstyle(0,0,1);
rectangle(0,0,getmaxx(),getmaxy());
rectangle(3,3,getmaxx()-3,getmaxy()-3);
line(3,getmaxy()-30,getmaxx()-3,getmaxy()-30);
line(3,getmaxy()-33,getmaxx()-3,getmaxy()-33);
setfillstyle(9,8);
bar(30,100,400,180);
bar(183,getmaxy()-6,425,getmaxy()-21);
setfillstyle(1,3);
bar(20,90,390,170);
bar(180,getmaxy()-9,420,getmaxy()-24);
setcolor(0);
outtextxy(195,getmaxy()-20,"My Display Calender Service");
MM:
setcolor(0);
outtextxy(30,100,"Enter Month(1-12):");
gotoxy(24,7);
scanf("%d",&m);
if(m<1||m>12)
{
setviewport(180,96,370,111,1);
clearviewport();
setviewport(0,0,getmaxx(),getmaxy(),1);
goto MM;
}
outtextxy(30,132,"Enter Year:");
gotoxy(18,9);
scanf("%d",&y);
setcolor(11);
while(1)
{
cleardevice();
setcolor(3);
rectangle(0,0,getmaxx(),getmaxy());
rectangle(3,3,getmaxx()-3,getmaxy()-3);
line(3,getmaxy()-30,getmaxx()-3,getmaxy()-30);
line(3,getmaxy()-33,getmaxx()-3,getmaxy()-33);
setfillstyle(9,8);
bar(183,getmaxy()-6,425,getmaxy()-21);
setfillstyle(1,3);
bar(180,getmaxy()-9,420,getmaxy()-24);
setcolor(0);
settextstyle(0,0,1);
outtextxy(195,getmaxy()-20,"My Display Calender Service");
setcolor(11);
thisyrdays=0;
leapyears=(y-1)/4-(y-1)/100+(y-1)/400;
if(y%400==0||y%100!=0&&y%4==0)
days[1]=29;
else
days[1]=28;
totaldays=leapyears+(y-1)*365L;
for(i=0;i<=m-2;i++)
thisyrdays=thisyrdays=thisyrdays+days[i];
firstday=(int)((totaldays+thisyrdays)%7);
settextstyle(10,0,4);
setcolor(12);
sprintf(masum,"%s",month[m-1]);
outtextxy(75,30,masum);
itoa(y,str1,10);
outtextxy(400,30,str1);
setcolor(9);
settextstyle(0,0,3);
outtextxy(20,113,"Mon");
outtextxy(20,143,"Tue");
outtextxy(20,173,"Wed");
outtextxy(20,203,"Thu");
outtextxy(20,233,"Fri");
outtextxy(20,263,"Sat");
outtextxy(20,293,"Sun");
setcolor(3);
settextstyle(0,0,2);
col=120+firstday*30;
row=150;
for(x=1;x<=days[m-1];x++)
{
itoa(x,str2,10);
outtextxy(row,col,str2);
col=col+30;
if(y==1752&&m==9&&x==2)
x=13;
if(col>300)
{
row=row+100;
col=120;
}
if(row>550&&col==120)
row=150;
}
settextstyle(5,0,1);
outtextxy(138,5,"Change month and year using arrow keys");
settextstyle(0,0,1);
outtextxy(3,330,"---------------------------------------------------------------------------------------------------");
settextstyle(2,0,4);
outtextxy(5,332,"---------------------------------------------------------------------------------------------------------");
setcolor(11);
settextstyle(0,0,1);
outtextxy(20,350,"Press Up arrow for the next year.");
outtextxy(20,370,"Press Dn arrow for the previous year.");
outtextxy(20,390,"Press Rt arrow for the next month.");
outtextxy(20,410,"Press Lt arrow for the previous month.");
outtextxy(20,430,"Press Esc to exit.....");
settextstyle(0,0,2);
while(!kbhit());
ii.h.ah=0;
int86(22,&ii,&oo);
scan=oo.h.ah;
ascii=oo.h.al;
switch(scan)
{
 case 77:
 if(m==12)
 {
 y=y+1;
 m=1;
 }
 else
 m=m+1;
 break;
 case 75:
 if(m==1)
 {
 y=y-1;
 m=12;
 if(y<=0)
 y=1;
 }
 else
 m=m-1;
 break;
 case 72:
 y++;
 break;
 case 80:
 y--;
 if(y<=0)
 y=1;
 break;
 case 1:
 cleardevice();
 goto MMM;

}
}
MMM:
closegraph();
restorecrtmode();
}