gamec++*03

· · 休闲·娱乐

#include<windows.h>
#include<iostream>
#include<bits/stdc++.h>
#include<conio.h>
#include<string>
#include<ctime>
#include<cstdlib>
#define ll long long
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)//判断这个键是否按下
#define KEY_UP(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 0 : 1)//判断这个键是否弹起
using namespace std;
int stat=0,ex,inp,onp=1,tp,lv;
ll p,bp=100,pt,h,rp=15000,lvb=15000;
int grid[4][4];
string sp="█ ";
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
void gotoXY(HANDLE hOut, int x, int y);
string disp[]={
    "   1  ","   2  ","   3  ","   5  ","   8  ","  13  ","  21  ","  34  ","  55  ","  89  ",
    "  144 ","  233 ","  377 ","  610 ","  987 "," 1597 "," 2584 "," 4181 "," 6765 "," 10987",
};
int cl[]={7,9,10,6,4,5,11,14,12,13,8,8,8,8,8,8,8,8,8,8};
void hidecursor()
{
    CONSOLE_CURSOR_INFO cursor;
    HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleCursorInfo(handle,&cursor);
    cursor.bVisible=FALSE;
    SetConsoleCursorInfo(handle,&cursor);
}
void disableselect()
{
    HANDLE hStdin=GetStdHandle(STD_INPUT_HANDLE);
    DWORD mode;
    GetConsoleMode(hStdin,&mode);
    mode&=~ENABLE_QUICK_EDIT_MODE;
    SetConsoleMode(hStdin,mode);
}
int mergeable(int x,int y){//是否可合成 
    if(x==-1||y==-1) return 0;
    if(!x&&!y) return 1;
    if(abs(x-y)==1) return 1;
    return 0;
}
int mergeresult(int x,int y){return max(x,y)+1;}//合成结果 
void color(int a){
    SetConsoleTextAttribute(hOut,a);
}
void empgrd(){cout<<"      "<<sp<<"      "<<sp<<"      "<<sp<<"      "<<sp<<endl;}
void stfgrd(int i){
    for(int j=0;j<=3;j++) cout<<((grid[i][j]==-1)?"      ":disp[grid[i][j]])<<sp;
    cout<<endl;
}
string fm(ll w,ll d){
    string res("0",d);
    ll v=w;
    for(int i=d-1;i>=0;i--){
        res[i]='0';
        res[i]+=v%10;
        v/=10;
    }
    return res;
}
void gotoXY(HANDLE hOut, int x, int y) {
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hOut, pos);
}
int over(){
    //横向检查 
    for(int i=0;i<4;++i)  
        for(int j=0;j<3;++j)  
            if(grid[i][j]==-1||mergeable(grid[i][j],grid[i][j+1])) return 0; 
    //纵向检查  
    for(int j=0;j<4;++j)  
        for(int i=0;i<3;++i)  
            if(grid[i][j]==-1||mergeable(grid[i][j],grid[i+1][j])) return 0;    
    //不符合上述两种状况,游戏结束  
    return 1;
}
int spawn(){//生成 
    int x=-1;  
    int y=-1;  
    int times=0;  
    int maxTimes=1024;  
    do{  
        x=rand()%4;  
        y=rand()%4;  
        ++times;  
    }while(grid[x][y]!=-1&&times<=maxTimes);  
    //说明格子已经满了  
    if(times >= maxTimes) return -1;
    if(rand()%36>=9){
        grid[x][y]=ex;
        return ex;
    }
    if(rand()%36>=12){
        grid[x][y]=ex+1;
        return ex+1;
    }
    grid[x][y]=ex+2;
    return ex+2;  
}
int input(){
    int fr=25;
    ll ti=clock();
    //读取上下左右四个方向键  
    int upArrow = 0;
    int downArrow = 0; 
    int leftArrow = 0;
    int rightArrow = 0;
    while(true){  
        upArrow = GetAsyncKeyState(VK_UP);  
        downArrow = GetAsyncKeyState(VK_DOWN);  
        leftArrow = GetAsyncKeyState(VK_LEFT);  
        rightArrow = GetAsyncKeyState(VK_RIGHT);  
        if(upArrow) return 1; 
        else if(downArrow) return 2;
        else if(leftArrow) return 3; 
        else if(rightArrow) return 4;
        ll cps=CLOCKS_PER_SEC;
        ll tfi=clock()-ti;
        if(tfi*1000/cps>=fr) return 0;
    }  
    return 0;  
}
void process(int di){ 
    ll cps=CLOCKS_PER_SEC;
    ll tfp=(clock()-tp)*1000/cps;
    ll ps=0;
    bp=sqrt(lvb)*(100+h/1500)/100;
    switch(di){  
    case 1:
        for(int h=1;h<=4;h++) for(int i=1;i<4;i++) for(int j=0;j<4;j++){
            if(grid[i][j]!=-1&&grid[i-1][j]==-1){
                grid[i-1][j]=grid[i][j];
                grid[i][j]=-1;
            }
            else if(mergeable(grid[i][j],grid[i-1][j])){
                ps+=(10+mergeresult(grid[i][j],grid[i-1][j]));
                grid[i-1][j]=mergeresult(grid[i][j],grid[i-1][j]);
                grid[i][j]=-1;
            }
        }
        break;  
    case 2:
        for(int h=1;h<=4;h++) for(int i=2;i>=0;i--) for(int j=0;j<4;j++){
            if(grid[i][j]!=-1&&grid[i+1][j]==-1){
                grid[i+1][j]=grid[i][j];
                grid[i][j]=-1;
            }
            else if(mergeable(grid[i][j],grid[i+1][j])){
                ps+=(10+mergeresult(grid[i][j],grid[i+1][j]));
                grid[i+1][j]=mergeresult(grid[i][j],grid[i+1][j]);
                grid[i][j]=-1;
            }
        } 
        break;  
    case 3:
        for(int h=1;h<=4;h++) for(int j=1;j<4;j++) for(int i=0;i<4;i++){
            if(grid[i][j]!=-1&&grid[i][j-1]==-1){
                grid[i][j-1]=grid[i][j];
                grid[i][j]=-1;
            }
            else if(mergeable(grid[i][j],grid[i][j-1])){
                ps+=(10+mergeresult(grid[i][j],grid[i][j-1]));
                grid[i][j-1]=mergeresult(grid[i][j],grid[i][j-1]);
                grid[i][j]=-1;
            }
        }
        break;  
    case 4:
        for(int h=1;h<=4;h++) for(int j=2;j>=0;j--) for(int i=0;i<4;i++){
            if(grid[i][j]!=-1&&grid[i][j+1]==-1){
                grid[i][j+1]=grid[i][j];
                grid[i][j]=-1;
            }
            else if(mergeable(grid[i][j],grid[i][j+1])){
                ps+=(10+mergeresult(grid[i][j],grid[i][j+1]));
                grid[i][j+1]=mergeresult(grid[i][j],grid[i][j+1]);
                grid[i][j]=-1;
            }
        }
        break;
    }
    if(!ps){if(h) h-=(h/80)+1;}
    else h+=(ps*100/15)*(10+10000/(tfp+40));
    p+=bp*ps;
    return;
}
void ini(){
    system("cls");
    for(int i=1;i<=32;i++) cout<<sp;
    cout<<endl;
    for(int j=1;j<=16;j++) cout<<sp;
    empgrd();
    for(int j=1;j<=16;j++) cout<<sp;
    stfgrd(0);
    for(int j=1;j<=16;j++) cout<<sp;
    empgrd();
    for(int j=1;j<=32;j++) cout<<sp;
    cout<<endl;
    for(int j=1;j<=16;j++) cout<<sp;
    empgrd();
    for(int j=1;j<=16;j++) cout<<sp;
    stfgrd(1);
    for(int j=1;j<=16;j++) cout<<sp;
    empgrd();
    for(int j=1;j<=32;j++) cout<<sp;
    cout<<endl;
    for(int j=1;j<=16;j++) cout<<sp;
    empgrd();
    for(int j=1;j<=16;j++) cout<<sp;
    stfgrd(2);
    for(int j=1;j<=16;j++) cout<<sp;
    empgrd();
    for(int j=1;j<=32;j++) cout<<sp;
    cout<<endl;
    for(int j=1;j<=16;j++) cout<<sp;
    empgrd();
    for(int j=1;j<=16;j++) cout<<sp;
    stfgrd(3);
    for(int j=1;j<=16;j++) cout<<sp;
    empgrd();
    for(int j=1;j<=32;j++) cout<<sp;
    cout<<endl;
}
void brkscr(int f){//暂停屏幕
    system("cls");
    for(int i=1;i<=7;i++){
        for(int j=1;j<=32;j++) cout<<sp;
        cout<<endl;
    }
    for(int j=1;j<=13;j++) cout<<sp;
    if(!f) cout<<" GAME PAUSED";
    else if(f==1) cout<<"FIB 987 GAME";
    else if(f==2) cout<<" GAME  OVER ";
    for(int j=1;j<=13;j++) cout<<sp;
    cout<<endl;
    for(int j=1;j<=13;j++) cout<<sp;
    if(f!=2) cout<<" PRESS ANY  ";
    else cout<<" "<<fm(p/1000000,3)<<","<<fm(p/1000,3)<<","<<fm(p,3);
    for(int j=1;j<=13;j++) cout<<sp;
    cout<<endl;
    for(int j=1;j<=13;j++) cout<<sp;
    if(f!=2) cout<<"KEY TO START";
    else cout<<"  "<<fm(h/100,5)<<"."<<fm(h/10,1)<<"m"<<"  ";
    for(int j=1;j<=13;j++) cout<<sp;
    cout<<endl;
    for(int i=1;i<=7;i++){
        for(int j=1;j<=32;j++) cout<<sp;
        cout<<endl;
    }
    system("pause");
}
int main(){
    hidecursor();
    disableselect();
    srand((unsigned int)(time(0)%501761+clock()%74751)); 
    brkscr(1);
    for(int i=0;i<4;i++) for(int j=0;j<4;j++) grid[i][j]=-1;
    stat=1;
    spawn();
    spawn();
    ll tz=clock();
    tp=tz;
    ini();
    while(1){
        //grid[1][1]=3;
        if(inp&&!onp&&spawn()==-1) break;
        onp=inp;
        gotoXY(hOut,6,1);
        cout<<"ALTITUDE  ";
        cout<<fm(h/100,5)<<"."<<fm(h/10,1)<<"m";
        cout<<sp<<sp<<sp<<sp;
        empgrd();
        gotoXY(hOut,32,2);
        stfgrd(0);
        cout<<sp<<sp<<sp<<"TIME  ";
        ll cps=CLOCKS_PER_SEC;
        ll tfs=clock()-tz;
        ll th=tfs/cps/3600,tm=tfs/cps/60%60,ts=tfs/cps%60,tms=tfs*1000/cps%1000;
        cout<<fm(th,2)<<":"<<fm(tm,2)<<"'"<<fm(ts,2)<<"\""<<fm(tms,3);
        cout<<sp<<sp<<sp<<sp;
        empgrd();
        gotoXY(hOut,6,5);
        cout<<fm(p/1000000,3)<<","<<fm(p/1000,3)<<","<<fm(p,3);
        cout<<"   Pts."<<sp<<sp<<sp<<sp;
        empgrd();
        gotoXY(hOut,32,6);
        stfgrd(1);
        cout<<sp<<sp<<sp;
        cout<<"STAGE"<<char(lv/10+'0')<<"-"<<lv%10<<sp<<"EXLV  "<<fm(ex,2)<<sp<<sp<<sp<<sp; 
        empgrd();
        gotoXY(hOut,6,9);
        cout<<"DELAY ";
        tfs=clock()-tp;
        th=tfs/cps/3600,tm=tfs/cps/60%60,ts=tfs/cps%60,tms=tfs*1000/cps%1000;
        cout<<fm(th,2)<<":"<<fm(tm,2)<<"'"<<fm(ts,2)<<"\""<<fm(tms,3);
        cout<<sp<<sp<<sp<<sp;
        empgrd();
        gotoXY(hOut,32,10);
        stfgrd(2);
        gotoXY(hOut,32,11);
        empgrd();
        for(int j=1;j<=32;j++) cout<<sp;
        cout<<endl;
        for(int j=1;j<=16;j++) cout<<sp;
        empgrd();
        for(int j=1;j<=16;j++) cout<<sp;
        stfgrd(3);
        for(int j=1;j<=16;j++) cout<<sp;
        empgrd();
        for(int j=1;j<=32;j++) cout<<sp;
        cout<<endl;
        inp=input();
        if(inp&&!onp){
            process(inp);
            tp=clock();
        }
        if(h) h-=(h/2500)+1;
        while(p>rp){
            lv++;
            if(!(lv%10)) lvb=lvb/10*15;
            color(cl[lv/10]);
            ini();
            rp+=lvb;
        }
        //p+=lvb/5;
        //system("pause");
        if(over()) break;
        //brkscr(0);
    }
    brkscr(2);
    return 0;
}