孔明棋

· · 个人记录

#include<cstdio>
#include<cstdlib>
#include<windows.h>
#include<conio.h>
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
int fang[4][2]={-1,0,1,0,0,-1,0,1};
//0011100
//0011100
//1111111
//1111111
//1111111
//0011100
//0011100
void play();
void Hide(){
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
int qi[7][7],ji;
bool can[4];
char haha;
void chu(){
    for(int i=0;i<7;i++){
        for(int j=0;j<7;j++){
            if((i>4||i<2)&&(j>4||j<2))    qi[i][j]=0;
            else    qi[i][j]=1;
        }
    }
    qi[3][3]=2;
}
bool check(){
    for(int i=0;i<7;i++){
        for(int j=0;j<7;j++){
            if(qi[i][j]==1){
                for(int k=0;k<4;k++){
                    if(qi[i+fang[k][0]][j+fang[k][1]]==1&&qi[i+fang[k][0]*2][j+fang[k][1]*2]==2)    return true;
                }
            }
        }
    }
    return false;
}
void col(int now){
    if(now==0)    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY);
    if(now==1)    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    if(now==2)    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_BLUE);
    if(now==3)    SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
}
void print(int q,int w,bool e){
    system("cls");
    for(int i=0;i<7;i++){
        for(int j=0;j<7;j++){
            col(0);
            if(!qi[i][j]){
                col(3);
                printf("#  ");
            }
            else{
                col((i+j)%2+1);
                if(qi[i][j]-2){
                    if(i==q&&j==w){
                        if(e){
                            col(3);
                            printf("■  ");
                        }
                        else{
                            col(3);
                            printf("■  ");
                        }
                    }
                    else    printf("■  ");
                }
                else{
                    if(e && i==q && j==w){
                        col(3);
                        printf("□  "); 
                    }
                    else{
                        col((i+j)%2+1);
                        printf("□  ");
                    }
                }
            }
        }
        printf("\n\n");
    }
} 
void play(){
    int q=3,w=2;
    print(q,w,true);
    while(check()){
        bool qw=false;
        while(!qw){
            haha=getch();
            if(haha=='q'){
                for(int i=0;i<4;i++)    can[i]=false;
                for(int i=0,qq,ww;i<4;i++){
                    qq=q+2*fang[i][0];
                    ww=w+2*fang[i][1];
                    if(qq>=0&&qq<7&&ww>=0&&ww<7&&qi[qq][ww]==2&&qi[qq-fang[i][0]][ww-fang[i][1]]==1){
                        qw=true;
                        can[i]=true;
                    }
                }
                if(qi[q][w]==2){
                    printf("Hey,it`s no chess there");
                    Sleep(2000);
                    qw=true;
                }
                if(!qw){
                    printf("No found a way to move");
                    Sleep(2000);
                }
                if(qi[q][w]==2) qw=false;

            }
            if(haha=='a')
            {
                w=(w+6)%7;
                while(!qi[q][w])    w=(w+1)%7;
            }
            if(haha=='d'){
                w=(w+1)%7;
                while(!qi[q][w])    w=(w+6)%7;
            }
            if(haha=='w'){
                q=(q+6)%7;
                while(!qi[q][w])    q=(q+1)%7;
            }
            if(haha=='s'){
                q=(q+1)%7;
                while(!qi[q][w])    q=(q+6)%7;
            }
            print(q,w,true);
        }
        printf("please choose\n");
        if(can[0])  printf("1:up\n");
        if(can[1])  printf("2:down\n");
        if(can[2])  printf("3:left\n");
        if(can[3])  printf("4:right\n");
        do{
            haha=getch();
        }while(!(haha>='1'&&haha<='4'&&can[haha-'0'-1]));
        int ee=q+2*fang[haha-'0'-1][0];
        int rr=w+2*fang[haha-'0'-1][1];
        qi[ee][rr]=1;
        qi[ee-fang[haha-'0'-1][0]][rr-fang[haha-'0'-1][1]]=2;
        qi[q][w]=2;
        q=ee;
        w=rr;
        print(q,w,true);
    }
    int tj=0;
    for(int i=0;i<7;i++){
        for(int j=0;j<7;j++){
            if(qi[i][j]==1) tj++;
        }
    }
    print(0,0,true);
    if(tj!=1)   printf("you lose!");
    else{
        if(qi[3][3]==1) printf("you win!");
        else    printf("you lose!");
    }
}
int main(){
    Hide();
    col(0);
    printf("Welcome to this play\n");
    printf("Your job is to move chess\n");
    printf("and finally left one chess in the middle\n");
    printf("you can move like this\n");
    col(3);
    printf("■");
    col(0);
    printf("■□  to  □□");
    col(3);
    printf("■\n");
    printf("WSAD to move Q to choose\n");
    col(0); 
    printf("Press any key to continue...");
    getch();
    chu();
    play();
    Sleep(5000);
    system("cls");
    main(); 
    return 0;
}

函数

HideCursor

SetConsoleTextAttribute