问题标题: 2048小游戏(纯手打)

1
1
凌夏
凌夏
初级守护
初级守护
#include<bits/stdc++.h>
#include<cstdio>
#include<time.h>
#include<windows.h> 
using namespace std;
int a[10][10];
string s;
void leftmove(){
    int cnt=20;
    while(cnt--){
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                if(a[i][j]==0 && a[i][j+1]!=0){
                    swap(a[i][j],a[i][j+1]);
                }
            }
        }
    }
    cnt=20;
    while(cnt--){
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                if(a[i][j]==a[i][j+1] && a[i][j]!=0){
                    a[i][j]=a[i][j]*2;
                    a[i][j+1]=0;
                }
            }
        }
    }
    cnt=20;
    while(cnt--){
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                if(a[i][j]==0 && a[i][j+1]!=0){
                    swap(a[i][j],a[i][j+1]);
                }
            }
        }
    }  
    while(1){
        int t1=rand()%5+1;
        Sleep(1);
        int t2=rand()%5+1;
        Sleep(1);
        if(!a[t1][t2]){
            a[t1][t2]=rand()%2+1;
            Sleep(1);
            break;
        }
    }
}
void rightmove(){
    int cnt=20;
    while(cnt--){
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                if(a[i][j]==0 && a[i][j-1]!=0){
                    swap(a[i][j],a[i][j-1]);
                }
            }
        }
    }
    cnt=20;
    while(cnt--){
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                if(a[i][j]==a[i][j-1] && a[i][j]!=0){
                    a[i][j]=a[i][j]*2;
                    a[i][j-1]=0;
                }
            }
        }
    }
    cnt=20;
    while(cnt--){
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                if(a[i][j]==0 && a[i][j-1]!=0){
                    swap(a[i][j],a[i][j-1]);
                }
            }
        }
    }
    while(1){
        int t1=rand()%5+1;
        Sleep(1);
        int t2=rand()%5+1;
        Sleep(1);
        if(!a[t1][t2]){
            a[t1][t2]=rand()%2+1;
            Sleep(1);
            break;
        }
    }
}
void upmove(){
    int cnt=20;
    while(cnt--){
        for(int j=1;j<=5;j++){
            for(int i=1;i<=5;i++){
                if(a[i][j]==0 && a[i+1][j]!=0){
                    swap(a[i][j],a[i+1][j]);
                }
            }
        }
    }
    cnt=20;
    while(cnt--){
        for(int j=1;j<=5;j++){
            for(int i=1;i<=5;i++){
                if(a[i][j]==a[i+1][j] && a[i][j]!=0){
                    a[i][j]=a[i][j]*2;
                    a[i+1][j]=0;
                }
            }
        }
    }
    cnt=20;
    while(cnt--){
        for(int j=1;j<=5;j++){
            for(int i=1;i<=5;i++){
                if(a[i][j]==0 && a[i+1][j]!=0){
                    swap(a[i][j],a[i+1][j]);
                }
            }
        }
    }
    while(1){
        int t1=rand()%5+1;
        Sleep(1);
        int t2=rand()%5+1;
        Sleep(1);
        if(!a[t1][t2]){
            a[t1][t2]=rand()%2+1;
            Sleep(1);
            break;
        }
    }
}
void downmove(){
    int cnt=20;
    while(cnt--){
        for(int j=1;j<=5;j++){
            for(int i=1;i<=5;i++){
                if(a[i][j]==0 && a[i-1][j]!=0){
                    swap(a[i][j],a[i-1][j]);
                }
            }
        }
    }
    cnt=20;
    while(cnt--){
        for(int j=1;j<=5;j++){
            for(int i=1;i<=5;i++){
                if(a[i][j]==a[i-1][j] && a[i][j]!=0){
                    a[i][j]=a[i][j]*2;
                    a[i-1][j]=0;
                }
            }
        }
    }
    cnt=20;
    while(cnt--){
        for(int j=1;j<=5;j++){
            for(int i=1;i<=5;i++){
                if(a[i][j]==0 && a[i-1][j]!=0){
                    swap(a[i][j],a[i-1][j]);
                }
            }
        }
    }
    while(1){
        int t1=rand()%5+1;
        Sleep(1);
        int t2=rand()%5+1;
        Sleep(1);
        if(!a[t1][t2]){
            a[t1][t2]=rand()%2+1;
            Sleep(1);
            break;
        }
    }
}
int main(){
    //for(int i=1;i<=5;i++){
    //  for(int j=1;j<=5;j++){
    //      cin>>a[i][j]; 
    //  }
    //}
    cout<<"2048小游戏,请用w,a,s,d控制\n输入start开始";
    string start;
    cin>>start;
    while(1){
        for(int i=1;i<=60;i++) cout<<endl;
        for(int i=1;i<=5;i++){
            for(int j=1;j<=5;j++){
                cout<<setw(5)<<a[i][j];
            }
            cout<<endl;
        }
        cin>>s;
        if(s=="a") leftmove();
        if(s=="d") rightmove();
        if(s=="w") upmove(); 
        if(s=="s") downmove(); 
    }
    return 0;
}

 


0
0
0
0
石峻帆
石峻帆
新手光能
新手光能

这个用system("cls")清屏吧

其实也可以不闪的,就这样:

#include<bits/stdc++.h>
#include<windows.h>//头文件
void gotoxy(int x, int y)//覆盖清屏 ,从指定行列覆盖
{
    COORD pos = {x,y};
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOut, pos);
    return ;
}

每次循环开始调用gotoxy(0,0);

隐藏光标:
HANDLE han = GetStdHandle(-11);
void hide(){
    CONSOLE_CURSOR_INFO cursor;
    cursor.bVisible = 0;
    cursor.dwSize = 1;
    SetConsoleCursorInfo(han,&cursor);

}

只需要在程序开始时调用一次就行了

0
0
0
0
0
我要回答