pi

· · 个人记录

pi

欢迎来到 pi,一个帮助你记忆圆周率的小程序!

目前支持存档功能,进度不会丢失。

#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#define SetColor(x) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x)
using namespace std;
string a="3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949198336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989";
int mode=0,best,bestd,chkpt=1;
int pos,sc,delsc;
int m,s,t,tmpt;
int page=1;
int hp,combo,cb;
ifstream inf;
ofstream outf;
bool wr[10001];
bool mute;
//do的频率为261.6HZ,re的频率为293.6HZ,mi的频率为329.6HZ,fa的频率为349.2HZ,sol的频率为392HZ,la的频率为440HZ,si的频率为493.8HZ。
int pitch[]={659,262,294,330,349,392,440,494,523,587};
inline int bigrand()
{
    return rand()*32768+rand();
}
inline int randrange(int l,int r)
{
    return bigrand()%(r-l+1)+l;
}
void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hidecursor()
{
    CONSOLE_CURSOR_INFO cursor;
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleCursorInfo(handle,&cursor);
    cursor.bVisible=FALSE;
    SetConsoleCursorInfo(handle,&cursor);
}
void showcursor()
{
    CONSOLE_CURSOR_INFO cursor;
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleCursorInfo(handle,&cursor);
    cursor.bVisible=TRUE;
    SetConsoleCursorInfo(handle,&cursor);
}
void disableselect()
{
    HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(hStdin,&mode);
    mode&=~ENABLE_QUICK_EDIT_MODE;
    SetConsoleMode(hStdin,mode);
}
void setcolor(string col="white")
{
    if(col=="red") SetColor(12);
    else if(col=="green") SetColor(10);
    else if(col=="blue") SetColor(1);
    else if(col=="yellow") SetColor(14);
    else if(col=="purple") SetColor(13);
    else if(col=="cyan") SetColor(11);
    else if(col=="white") SetColor(15);
    else if(col=="grey") SetColor(8);
    else if(col=="brown") SetColor(6);
    else if(col=="lblue") SetColor(3);
    else SetColor(0);
}
struct GapCounter
{
    int range;
    queue<int> q;
    GapCounter(int range=0):range(range){}
    void editrange(int _range){range=_range;}
    void clear(){while(q.size()) q.pop();}
    void add(int val)
    {
        while(q.size() && val-q.front()>range) q.pop();
        q.push(val);
    }
    double operator ()(int range)//number of counts per range
    {
        if(q.empty()) return 0.0;
        if(q.size()==1) return 1.0;
        return 1.0*(q.size()-1)/(q.back()-q.front())*range;
    }
};
GapCounter kps;
int getpress()//10='q'
{
    if(_kbhit())
    {
        fflush(stdin);
        char key=_getch();
        if(key>='0' && key<='9') return key-'0';
        if(tolower(key)=='q') return 10;
    }
    return -1;
}
pair<int,int> getco(int pos)
{
    pos%=50;
    return make_pair(pos/10+1,pos%10+1);
}
void overview()
{
    system("cls");
    printf("    pi    < 0001-0050 >\n");
    printf("-----------------------\n");
    printf("| 3.1 4 1 5 9 2 6 5 3 |\n");
    printf("| 5 8 9 7 9 3 2 3 8 4 |\n");
    printf("| 6 2 6 4 3 3 8 3 2 7 |\n");
    printf("| 9 5 0 2 8 8 4 1 9 7 |\n");
    printf("| 1 6 9 3 9 9 3 7 5 1 |\n");
    printf("-----------------------\n");
    printf("Best:%04d    Play:Space\n",bestd);
    printf(mode ? "Mode:score          [M]\n" : "Mode:checkpoint     [M]\n");
    printf(mode ? "Best score:%06d\n" : "Checkpoint:page %02d\n",mode ? best : chkpt);
    printf(mute ? "Sound:Disabled      [S]\n" : "Sound:Enabled       [S]\n");
    gotoxy(0,0);
}
void drawmap(int page,int num=0)
{
    setcolor();
    system("cls");
    printf(mode ? "score %06d time %02d:%02d\n" : "digit %04d   time %02d:%02d\n",mode ? sc : pos,m,s);
    printf("-----------------------\n");
    printf("| . . . . . . . . . . |\n");
    printf("| . . . . . . . . . . |\n");
    printf("| . . . . . . . . . . |\n");
    printf("| . . . . . . . . . . |\n");
    printf("| . . . . . . . . . . |\n");
    printf("-----------------------\n");
    printf("health ");
    for(int i=1;i<=5;i++) printf(i<=hp ? "★" : "  ");printf("  %4d\n",cb);
    printf("50 combo = 1 star\n");
    printf("quit [Q]\n");
    printf("Keys Per Sec : %.5lf\n",kps(1000));
    int cnt=0;
    for(int i=(page-1)*50;i<page*50;i++)
    {
        cnt++;
        if(wr[i]) setcolor("red");
        else setcolor();
        int x=getco(i).first,y=getco(i).second;
        gotoxy(y*2,x+1);
        if(cnt<=num) printf("%c ",a[i]);
        else printf(". ");
    }
    gotoxy(0,0);
}
void printpage(int page)
{
    gotoxy(12,0);
    printf("%04d-%04d",(page-1)*50+1,page*50);
    for(int i=(page-1)*50;i<page*50;i++)
    {
        if(wr[i]) setcolor("red");
        else setcolor();
        int x=getco(i).first,y=getco(i).second;
        gotoxy(y*2,x+1);
        printf("%d",a[i]-'0');
        setcolor();
        if(i==0) printf(".");
        else printf(" ");
    }
    gotoxy(0,0);
    setcolor();
}
void read()
{
    inf.open("C:\\ProgramData\\SilvefishGames\\pi\\pi.txt");
    inf>>mode>>best>>bestd>>page>>chkpt;
    long long tmp1=best+bestd+page+101,tmp2=1000,tmp3=1,tmp4;
    for(int i=0;i<1000;i++)
    {
        inf>>wr[i];
        tmp2+=wr[i];
    }
    inf>>tmp4;
    for(int i=1;i<=tmp2;i++)
    {
        tmp3=tmp3*tmp1%1000000007;
    }
    if(tmp3!=tmp4)
    {
        best=bestd=0;
        page=1;
        memset(wr,false,sizeof(wr));
    }
    inf>>mute;
    inf.close();
}
void update()
{
    long long tmp1=best+bestd+page+101,tmp2=1000,tmp3=1;
    outf.open("C:\\ProgramData\\SilvefishGames\\pi\\pi.txt");
    outf<<" "<<mode<<" "<<best<<" "<<bestd<<" "<<page<<" "<<chkpt<<endl;
    for(int i=0;i<1000;i++)
    {
        tmp2+=wr[i];
        outf<<wr[i]<<" ";
    }
    for(int i=1;i<=tmp2;i++)
    {
        tmp3=tmp3*tmp1%1000000007;
    }
    outf<<tmp3<<" ";
    outf<<mute<<" ";
    outf.close();
}
void test();
int main()
{
//  test();
    hidecursor();
    disableselect();
    setcolor("white");
    system("mkdir C:\\ProgramData\\SilvefishGames\\pi");
    //↑72 ↓80 ←75 →77
    read();
    system("mode con lines=13 cols=24");
    system("cls");
    while(1)
    {
        overview();
        printpage(page);
        while(1)
        {
            int key=_getch();
            fflush(stdin);
            if(key==0 || key==224)
            {
                key=_getch();
                if(key==75)
                {
                    if(page>1)
                    {
                        page--;
                        printpage(page);
                    }
                }
                if(key==77)
                {
                    if(page<20)
                    {
                        page++;
                        printpage(page);
                    }
                }
            }
            else
            {
                if(key==' ') break;
                if(tolower(key)=='m')
                {
                    mode=1-mode;
                    overview();
                    printpage(page);
                }
                if(tolower(key)=='s')
                {
                    mute=!mute;
                    overview();
                    printpage(page);
                }
            }
        }
        pos=0;
        hp=5;
        tmpt=time(0);
        t=clock();
        m=10;s=0;
        combo=cb=0;
        kps.clear();
        kps.editrange(10000);
        if(mode==1) sc=0,delsc=-1;
        else
        {
            if(chkpt>1)
            {
                cb=(chkpt-1)*50-1;
                pos=(chkpt-1)*50-1;
                drawmap(pos/50+1,49);
            }
        }
        while(1)
        {
            if(pos%50==0) drawmap(pos/50+1);
            int k;
            setcolor();
            while(1)
            {
                if(time(0)>tmpt)
                {
                    s--;
                    if(s==-1) m--,s=59;
                    gotoxy(18,0);
                    printf("%02d:%02d",m,s);
                    gotoxy(0,0);
                    tmpt=time(0);
                }
                k=getpress();
                if(k>=0) break;
            }
            if(k==10) break;
            if(!mute) if(0<=k && k<=9) Beep(pitch[k]*2,100);
            if(k==a[pos]-'0')
            {
                combo++;
                cb++;
                int x=getco(pos).first,y=getco(pos).second;
                gotoxy(y*2,x+1);
                printf("%d",a[pos]-'0');
                if(pos==0) printf(".");
                wr[pos]=false;
                if(combo==50)
                {
                    hp=min(hp+1,5);
                    combo=0;
                    gotoxy(7,8);for(int i=1;i<=hp;i++) printf("★");
                }
            }
            else
            {
                combo=cb=0;
                hp--;
                wr[pos]=true;
                if(hp==0) break;
                int x=getco(pos).first,y=getco(pos).second;
                gotoxy(y*2,x+1);
                setcolor("brown");
                printf("%d",a[pos]-'0');
                setcolor("white");
                gotoxy(7,8);printf("          ");
                gotoxy(7,8);for(int i=1;i<=hp;i++) printf("★");
            }
            kps.add(clock());
            pos++;
            int t2=clock()-t;delsc+=2-(t2-100)/200;t+=t2;
            delsc=max(delsc,1);sc+=delsc;sc=min(sc,999999);
            gotoxy(6,0);printf(mode ? "%06d" : "%04d",mode ? sc : pos);
            gotoxy(19,8);printf("%4d",cb);
            gotoxy(15,11);printf("%.5lf ",kps(1000));
            gotoxy(0,0);
            if(pos>=1000) break;
            if(m==0 && s==0) break;
        }
        if(mode==1)
        {
            best=max(best,sc);
        }
        else
        {
            chkpt=pos/50;
            chkpt=max(chkpt,1);
        }
        bestd=max(bestd,pos);
        page=min(pos/50+1,20);
        update();
        system("cls");
        printf("Game Over!\n");
        printf("Digit : %d\n",pos);
        if(mode==1) printf("Score : %d\n",sc);
        printf("Key Per Sec : %.5lf\n",kps(1000));
        printf("Spacebar to continue\n");
        while(_getch()!=' ');
    }
}
void test()
{
    int key;
    GapCounter a(10000);
    while(1)
    {
        key=_getch();
        if(key==0 || key==224) continue;
        a.add(clock());
        printf("key per second : %.5lf\n",a(1000));
    }
}
/**/