Unlock Pattern: Using C/C++ and graphics

Hey guys, its cool program which depicts android unlock pattern. Use mouse pointer to draw the pattern and then set the password. After password is set try to unlock the pattern. If you have drawn correct pattern you will be asked to set new pattern. Here's the screen shot of the output.


here's the program, enjoy coding...


/*
    Programmer : Ashok Kumar Shrestha (ak007)
    Program details : pgm to set the password and match it using mouse v2.0
*/

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<process.h>

union REGS i,o;
struct SREGS s;

initmouse()
{
    i.x.ax=0;
    int86 (0x33,&i,&o);
    return(o.x.ax);
}

showmouseptr()
{
    i.x.ax=1;
    int86 (0x33,&i,&o);
    return 0;
}

changecursor(int *shape)
{
    i.x.ax=9;
    i.x.bx=0;
    i.x.cx=0;
    i.x.dx=(unsigned)shape;
    segread(&s);
    s.es=s.ds;
    int86x (0x33,&i,&i,&s);
    return 0;
}

void hidemouseptr()
{
    i.x.ax=2;
    int86(0x33,&i,&o);
}

getmousepos(int *button,int *x,int *y)
{
    i.x.ax=3;
    int86(0x33,&i,&o);
    *button=o.x.bx;
    *x=o.x.cx;
    *y=o.x.dx;
    return 0;
}

restrictmouseptr(int x1,int y1,int x2,int y2)
{
    i.x.ax=7;
    i.x.cx=x1;
    i.x.dx=x2;
    int86(0x33,&i,&o);
    i.x.ax=8;
    i.x.cx=y1;
    i.x.dx=y2;
    int86(0x33,&i,&o);
    return 0;
}

void setmousepos(int x,int y)
{
i.x.ax=4;
i.x.cx=x;
i.x.dx=y;
int86(0x0033,&i,&o);
}

void main();

void display()
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"..\\bgi");
    int bth=75,nbox=3,rad=20;  //breadth of box
    int x=getmaxx()/2,y=getmaxy()/2;
    int x1=x-bth*nbox/2,y1=y-bth*nbox/2;
    rectangle(x-bth*nbox/2,y-bth*nbox/2,x+bth*nbox/2,y+bth*nbox/2);
    int i,j;
    setcolor(10);
    setfillstyle(1,2);
    //    int rad=20;//rad-radius of circle, nbox-no. of box

    for(i=0;i<nbox;i++)
    {
        for(j=0;j<nbox;j++)
        {
             //  rectangle(x1+i*bth,y1+j*bth,x1+(i+1)*bth,y1+(j+1)*bth);
               circle(x1+bth/2+i*bth,y1+bth/2+j*bth,rad);
               circle(x1+bth/2+i*bth,y1+bth/2+j*bth,rad-10);

        }
    }
    restrictmouseptr(x-bth*nbox/2,y-bth*nbox/2,x+bth*nbox/2,y+bth*nbox/2);

}

long double pw;

void getpw(int get)
{
    int bth=75,nbox=3,rad=20;  //breadth of box
    int x=getmaxx()/2,y=getmaxy()/2;
    int x1=x-bth*nbox/2,y1=y-bth*nbox/2;

    int button=-1,mx,my;
    setmousepos(x1+bth/2,y1+bth/2);
    int box_pos,row1,col1,box_pos1=15,count=1;
    long double num=0;
    while(!kbhit())
    {
        showmouseptr();
        getmousepos(&button,&mx,&my);
        gotoxy(1,1);
        if(button==1)
        {       button=-1;
            delay(150);
            int row=(mx-x1)/bth,col=(my-y1)/bth;
            if(rad>=abs(mx-x1-bth*row-bth/2)&&rad>=abs(my-y1-bth*col-bth/2))
            {
                box_pos=(mx-x1)/bth+1+(my-y1)/bth*nbox;

                if(abs(box_pos-box_pos1)>0&&(count==1||(abs(row-row1)<2&&abs(col-col1)<2)))
                {    num=num*10+box_pos;
                    count++;
                    box_pos1=box_pos;

                    hidemouseptr();
                    setcolor(9);
                    setfillstyle(1,1);
                    if(num>9)
                    {
                          line(x1+(row1+.5)*bth,y1+(col1+.5)*bth,x1+(row+.5)*bth,y1+(col+.5)*bth);
                    }
                    circle(x1+(row+.5)*bth,y1+(col+.5)*bth,rad-10);
                    floodfill(x1+(row+.5)*bth-3,y1+(col+.5)*bth-4,9);
                    showmouseptr();
                    if(box_pos1==box_pos)
                    {
                        row1=row,    col1=col;
                    }
                    }
                else if(abs(box_pos-box_pos1)==0)
                {
                    count++;
                }
            else
            {
                gotoxy(35,6);
                cout<<"Invalid box";
                box_pos=-1;
                hidemouseptr();
                setcolor(3);
                setfillstyle(1,4);
                circle(x1+(row+.5)*bth,y1+(col+.5)*bth,rad-10);
                floodfill(x1+(row+.5)*bth-3,y1+(col+.5)*bth-4,3);

                delay(400);
                setfillstyle(1,0);
                bar(x1,75,x1+nbox*bth,100);
                setcolor(2);
                setfillstyle(1,0);
                circle(x1+(row+.5)*bth,y1+(col+.5)*bth,rad-10);
                floodfill(x1+(row+.5)*bth-3,y1+(col+.5)*bth-4,2);
            }
            }
        }

        if(button==2)
        {       button=-1;
            delay(150);
            if(get==1)
            {
                pw=num;
                gotoxy(32,6);
                cout<<"Password is set.";
            }
            else
            {
            gotoxy(30,5);
            if(num==pw)
            {
                cout<<"   Valid Password";
                gotoxy(22,24);
                cout<<"Do youwant to creat new password (y/n)?";
                if(getche()=='y')
                {    pw=0;
                    main();
                }
                else
                    exit(0);
            }
            else
                cout<<"  Invalid  Password";
            }
            break;
        }
    }
}
void mainpage()
{
   textcolor(2);
   gotoxy(28,5);
   cprintf(" WELCOME  TO  ' UNLOCK ME '");
   gotoxy(16,8);
   textcolor(9);
   cprintf(" PROGRAMMER   :");
   gotoxy(31,8);
   textcolor(11);
   cprintf("    ASHOK  KUMAR  SHRESTHA (0 0 7)");
   gotoxy(30,11);
   textcolor(6);
   cprintf("  RULES :");
   gotoxy(16,13);
   textcolor(7);
   cprintf(" 1.FIRST YOU HAVE TO SET PASSWORD PATTERN.");
   gotoxy(16,15);
   cprintf(" 2.THEN TRY UNLOCKING IT.");
   gotoxy(16,17);
   cprintf(" 3.SET MOUSE CURSOR TO DESIRED CIRCLE AND PRESS LEFT BUTTON");
   gotoxy(16,19);
   cprintf(" 4.AFTER PATTERN HAS BEEN SET PRESS RIGHT BUTTON.");
   gotoxy(26,22);
   textcolor(9+BLINK);
   cprintf("   ENJOY ......... %c %c %c %c %c",1,1,1,1,1);
   getch();

}


void main()
{
    display();
    gotoxy(25,2);
    cout<<"Set PassWord Pattern using mouse.";
    getpw(1);
    gotoxy(22,25);
    cout<<"Do you want try matching password (y/n)? ";
    while(getche()=='y')
    {
        cleardevice();
        display();
        getpw(0);
        gotoxy(28,25);
        cout<<"Do you want try again (y/n)? ";
    }
}
//End of the program

Comments

Popular posts from this blog

Wizard Quiz: Privacy Policy

Sorting Hat: Privacy Policy

mTranslator