#include <graphics.h>
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int x, gasit=0,n, nr,nr_ales;
short int nr_pc;

void outintxy();
void menu();
void despre_joc();
void joc();
void nr_incercari();
void numar_ales();
void clearall();

int nrales[4];
int incercari[11];

int corect (int x)
{
    int  i, vx[10];
    for(i=0;i<=9;i++) vx[i]=0;
    while(x!=0)
    {
        vx[x%10]++;
        x=x/10;
    }
    for(i=0;i<=9;i++)
        if (vx[i]>1)
            return 0;
    return 1;
}

int exista (int a, int b)
{
while (a!=0)
    {
    if(a%10==b) return 1;
    a=a/10;
    }
return 0;
}

int apar(int x,int  y)
{ int ap=0;
    while(y!=0)
        {
            ap=ap+exista(x,y%10);
            y=y/10;
        }
return ap;
}


int pelocullor(int x,int y)
{
    int peloc=0;
    while (x!=0)
    {
        if(x%10==y%10) peloc++;
        x=x/10;
        y=y/10;
    }
    return peloc;
}
void menu()
{
    cleardevice();
    setfillstyle(SOLID_FILL,COLOR(255,153,0));
    setbkcolor(COLOR(0,204,51));
    floodfill (getmaxx()/2,getmaxy()/2,COLOR(153,255,51));
    setcolor(COLOR(153,255,51));
    setfillstyle(SOLID_FILL,COLOR(0,204,51));
    bar(100,100,1250,570);
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,20);
    outtextxy(350,150,"Goobix");
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,15);
    outtextxy(350,300,"Despre joc");
    rectangle(349,299,550,333);
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,15);
    outtextxy(350,350,"Start");
    rectangle(349,349,432,382);
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,15);
    outtextxy(350,500,"Click stanga pe butonul dorit");
    int apasat=0,x,y;
    while(!apasat)
    {
        if(ismouseclick(WM_LBUTTONDOWN))
            getmouseclick(WM_LBUTTONDOWN,x,y);
        if(x>349&&x<550&&y>299&&y<333)
            despre_joc();
        if(x>349&&x<432&&y>349&&y<382)
            joc();
    }
}
void despre_joc()
{
    cleardevice();
    floodfill (getmaxx()/2,getmaxy()/2,COLOR(153,255,51));
    setfillstyle(SOLID_FILL,COLOR(0,204,51));
    setcolor(COLOR(255,153,0));
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,20);
    outtextxy(350,150,"Despre joc: ");
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,14);
    outtextxy(300,300,"Ideea jocului: ");
    settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
    outtextxy(200,340,"Calculatorul alege la intamplare un nr. de 4 cifre. Alege nr de incercari de care crezi ca ai nevoie(de la 1-9). Dupa fiecare incercare\n");
    outtextxy(190,370,"calculatorul va afisa nr. de cifre pe care le-ai ghicit si nr. de cifre care se afla pe pozitia corespunzatoare.\n");
    outtextxy(190,600,"Distractie placuta. SUCCES!");
    outtextxy(190,630,"Apasati ENTER pentru a reveni la meniu");
    while(getch()!='\n')
        getch();
    menu();

}
void joc()
{
    int aparitii, peloc,y,i,gasit=0;
    cleardevice();
    floodfill (getmaxx()/2,getmaxy()/2,COLOR(153,255,51));
    setfillstyle(SOLID_FILL,COLOR(0,204,51));
    setcolor(COLOR(255,153,0));
    settextjustify(CENTER_TEXT,CENTER_TEXT);
    bgiout<<"Alegeti nr de incercari: ";
    outstreamxy(getmaxx()/2,getmaxy()/2);
    outintxy();
    srand(time(NULL));
    do
    {
        nr_pc = rand()%10000*(rand()%10)%10000;
    }
    while(nr_pc<1000);
    for(n=1;n<=nr && gasit==0;n++)
    {
        nr_incercari();
        numar_ales();
        incercari[n]=nr_ales;
        aparitii=apar(nr_pc,nr_ales);
        peloc=pelocullor(nr_pc,nr_ales);
        if(aparitii==4 && peloc==4)
        {
            gasit=1;
            nr_incercari();
        }
    }
    if(!gasit)
    {
        bgiout<<"Ai pierdut!";
        outstreamxy(getmaxx()/4*3,getmaxy()/2);
        delay(5000);
        settextjustify(LEFT_TEXT,TOP_TEXT);
        clearall();
        menu();
    }
        else
        {
            bgiout<<"Bravo! Ai castigat!";
            outstreamxy(getmaxx()/4*3,getmaxy()/2);
            delay(5000);
            settextjustify(LEFT_TEXT,TOP_TEXT);
            clearall();
            menu();
        }
}

int main()
{

    initwindow(1366,768);
    menu();
}
void outintxy()
{

    char a;
    a = (getch());
    nr=a-'0';
    bgiout<<a;
    outstreamxy(getmaxx()/2+100,getmaxy()/2);

}
void numar_ales()
{
    int i;
    nr_ales=0;
    for(i=0;i<4;i++)
    {
        nrales[i]=getch()-'0';
    }
    for(i=0;i<4;i++)
    {
        nr_ales=(nr_ales+nrales[i])*10;
    }
    nr_ales/=10;
}
void nr_incercari()
{
    int i;
    cleardevice();
    bgiout<<"Nr. Incercari :"<<nr;
    outstreamxy(150,50);
    bgiout<<"Incercarea numarul:"<<n;
    outstreamxy(200,100);
    for(i=1;i<=nr;i++)
        if(incercari[i])
        {
            bgiout<<incercari[i]<<" "<<apar(nr_pc,incercari[i])<<" "<<pelocullor(nr_pc,incercari[i]);
            outstreamxy(480,50*i);
        }

}
void clearall()
{
    int i;
    for(i=0;i<11;i++)
        incercari[i]=0;
    nr=0;
    n=0;
}
