C++游戏代码合集

别忘了点赞哦~

扫雷

原创NULL_acw guosichen,我只转载

代码如下:

#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<queue>
#include<ctype.h>
#define A 17    //地图的高
#define B 17    //地图的宽
#define C 30    //雷的总数
using namespace std;

//全局变量
DWORD a,b;
char map[A][B],news,spare;
int BoomTotalNum,floatx,floaty,flag[A][B],flagnum,mode,slect[A][B],game;

//颜色属性
const WORD FORE_BLUE  =  FOREGROUND_BLUE;    //蓝色文本属性
const WORD FORE_GREEN = FOREGROUND_GREEN;    //绿色文本属性
const WORD FORE_RED   =   FOREGROUND_RED;    //红色文本属性

//开垦地图结构体
struct node {
    int x;
    int y;
};
queue <node> dui;

//打印位置
void position(int x,int y) {
    COORD pos={x,y};
    HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(Out,pos);
}

//隐藏光标
void Hide() {
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);  
    CONSOLE_CURSOR_INFO CursorInfo;  
    GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息  
    CursorInfo.bVisible = false; //隐藏控制台光标  
    SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态   
}

//初始化
void Beginning() {
    while(!dui.empty()) {
        dui.pop();
    }
    game=1;
    //BoomTotalNum=C;
    floatx=A/2;
    floaty=B/2;
    flagnum=0;
    BoomTotalNum=C;
    mode=0;
    HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);    //获得标准输出设备句柄  
    CONSOLE_SCREEN_BUFFER_INFO csbi;                        //定义窗口缓冲区信息结构体  
    GetConsoleScreenBufferInfo(handle_out, &csbi);          //获得窗口缓冲区信息
    int x,y;
    srand((unsigned)time(0));
    for(int i=0;i<A;i++) for(int j=0;j<B;j++) {
        map[i][j]=' ';
        flag[i][j]=0;
        slect[i][j]=0;
    }
    while(BoomTotalNum) {
        x=rand()%A;
        y=rand()%B;
        if(map[x][y]==' ') {
            map[x][y]='@';
            BoomTotalNum--;
        }
    }
    SetConsoleTextAttribute(handle_out, FORE_GREEN);  
    for(int i=0;i<A;i++) {
        for(int j=0;j<B;j++) printf("█");
        printf("\n");
    }
    position(floaty*2,floatx);
    SetConsoleTextAttribute(handle_out, FORE_RED);  
    printf("");    //光标位置
    position(5,22);
    printf("按“空格”切换模式");
    position(5,23);
    printf("按“Enter”确认");
    position(5,24);
    printf("按“方向键”选择方块");

}

//打印地图的一块儿
void Lump(int xx,int yy) {
    switch(map[xx][yy]) {
        case '1' : printf("①");break;    //周围雷的数量(下同)
        case '2' : printf("②");break;
        case '3' : printf("③");break;
        case '4' : printf("④");break;
        case '5' : printf("⑤");break;
        case '6' : printf("⑥");break;
        case '7' : printf("⑦");break;
        case '8' : printf("⑧");break;
        case ' ' :
            if(xx==floatx&&yy==floaty) {
                if(flag[xx][yy]==0) {
                    if(mode%2==0) printf("");
                    else printf("");
                }
                else printf("");
            }
            else {
                if(flag[xx][yy]==0) printf("█");
                else printf("");
            }
            break;
        case '@' :
            if(xx==floatx&&yy==floaty) {
                if(flag[xx][yy]==0) {
                    if(mode%2==0) printf("");
                    else printf("");
                }
                else printf("");
            }
            else {
                if(flag[xx][yy]==0) printf("█");
                else printf("");
            }
            break;
        case 'x' : if(floatx==xx&&floaty==yy) printf(""); else printf("  ");break;    //已经挖开的空白
    }
}

//移动光标
void Move() {
    HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);    //获得标准输出设备句柄  
    CONSOLE_SCREEN_BUFFER_INFO csbi;                        //定义窗口缓冲区信息结构体  
    GetConsoleScreenBufferInfo(handle_out, &csbi);          //获得窗口缓冲区信息
    int xxx,yyy;
    xxx=floatx;
    yyy=floaty;
    switch(news) {
        case 72 : floatx--;break;    //上
        case 80 : floatx++;break;    //下
        case 75 : floaty--;break;    //左
        case 77 : floaty++;break;    //右
    }
    if(floatx==-1) floatx=A-1; floatx%=A;    //两端穿模处理
    if(floaty==-1) floaty=B-1; floaty%=B;

    position(yyy*2,xxx);
    SetConsoleTextAttribute(handle_out, FORE_GREEN);
    Lump(xxx,yyy);    //删除原位置

    if(map[floatx][floaty]=='x') {
        position(floaty*2,floatx);
        printf("  ");
    }

    position(floaty*2,floatx);
    SetConsoleTextAttribute(handle_out, FORE_BLUE);  
    Lump(floatx,floaty);    //更新新位置
}

//插旗和排雷模式切换
void Mode() {
    HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);    //获得标准输出设备句柄  
    CONSOLE_SCREEN_BUFFER_INFO csbi;                        //定义窗口缓冲区信息结构体  
    GetConsoleScreenBufferInfo(handle_out, &csbi);          //获得窗口缓冲区信息
    mode++;
    SetConsoleTextAttribute(handle_out, FORE_BLUE);
    position(floaty*2,floatx);
    if(mode%2==0) printf("");
    else printf("");

    position(44,9);
    if(mode%2==0) {
        SetConsoleTextAttribute(handle_out, FORE_BLUE);  
        printf("建设模式");
    }
    else {
        SetConsoleTextAttribute(handle_out, FORE_RED);  
        printf("挖掘模式");
    }
}

//该点周围地雷数
int Boomnum(int xx,int yy) {
    int num=0;
    if((xx-1>=0)&&(yy-1>=0)&&(map[xx-1][yy-1]=='@')) num++;
    if((xx-1>=0)&&(yy+0>=0)&&(map[xx-1][yy]=='@')) num++;
    if((xx-1>=0)&&(yy+1<B) &&(map[xx-1][yy+1]=='@')) num++;
    if((xx+0>=0)&&(yy-1>=0)&&(map[xx][yy-1]=='@')) num++;
    if((xx+0>=0)&&(yy+1<B) &&(map[xx][yy+1]=='@')) num++;
    if((xx+1<A)&&(yy-1>=0) &&(map[xx+1][yy-1]=='@')) num++;
    if((xx+1<A)&&(yy+0>=0) &&(map[xx+1][yy]=='@')) num++;
    if((xx+1<A)&&(yy+1<B)  &&(map[xx+1][yy+1]=='@')) num++;
    return num;
}

//更新地图
void Open() {
    node c;
    node d;
    while(!dui.empty()) {
        dui.pop();
    }
    c.x=floatx;
    c.y=floaty;
    dui.push(c);
    slect[c.x][c.y]=1;
    while(!dui.empty()) {
        c=dui.front();
        dui.pop();
        if(Boomnum(c.x,c.y)!=0) {
            map[c.x][c.y]=(Boomnum(c.x,c.y)+48);
            continue;
        }
        else {
            map[c.x][c.y]='x';                                                                                                                                                                                                                                                                                                                                                                                                                                     
            if((c.x-1>=0)&&(c.y-1>=0)&&(map[c.x-1][c.y-1]==' ')&&(slect[c.x-1][c.y-1]==0)) {
                d.x=c.x-1;
                d.y=c.y-1;
                dui.push(d);
                slect[d.x][d.y]=1;
            }
            if((c.x-1>=0)&&(c.y-0>=0)&&(map[c.x-1][c.y]==' ')&&(slect[c.x-1][c.y]==0)) {
                d.x=c.x-1;
                d.y=c.y-0;
                dui.push(d);
                slect[d.x][d.y]=1;
            }
            if((c.x-1>=0)&&(c.y+1<B)&&(map[c.x-1][c.y+1]==' ')&&(slect[c.x-1][c.y+1]==0)) {
                d.x=c.x-1;
                d.y=c.y+1;
                dui.push(d);
                slect[d.x][d.y]=1;
            }
            if((c.x-0>=0)&&(c.y-1>=0)&&(map[c.x][c.y-1]==' ')&&(slect[c.x][c.y-1]==0)) {
                d.x=c.x-0;
                d.y=c.y-1;
                dui.push(d);
                slect[d.x][d.y]=1;
            }
            if((c.x-0>=0)&&(c.y+1<B)&&(map[c.x][c.y+1]==' ')&&(slect[c.x][c.y+1]==0)) {
                d.x=c.x-0;
                d.y=c.y+1;
                dui.push(d);
                slect[d.x][d.y]=1;
            }
            if((c.x+1<A)&&(c.y-1>=0)&&(map[c.x+1][c.y-1]==' ')&&(slect[c.x+1][c.y-1]==0)) {
                d.x=c.x+1;
                d.y=c.y-1;
                dui.push(d);
                slect[d.x][d.y]=1;
            }
            if((c.x+1<A)&&(c.y-0>=0)&&(map[c.x+1][c.y]==' ')&&(slect[c.x+1][c.y]==0)) {
                d.x=c.x+1;
                d.y=c.y-0;
                dui.push(d);
                slect[d.x][d.y]=1;
            }
            if((c.x+1<A)&&(c.y+1<B)&&(map[c.x+1][c.y+1]==' ')&&(slect[c.x+1][c.y+1]==0)) {
                d.x=c.x+1;
                d.y=c.y+1;
                dui.push(d);
                slect[d.x][d.y]=1;
            }
        }
    }
}

int main() {
    freopen("排名.txt","r",stdin);
    Relife:    //重玩处
    HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);    //获得标准输出设备句柄  
    CONSOLE_SCREEN_BUFFER_INFO csbi;                        //定义窗口缓冲区信息结构体  
    GetConsoleScreenBufferInfo(handle_out, &csbi);          //获得窗口缓冲区信息

    Hide();        //隐藏光标
    Beginning();//初始化地图
    a=GetTickCount();
    while(1) {
        if(kbhit()!=0) {
            spare=getch();

            //按其他
            if((spare!=(-32))&&(spare!=13)&&(spare!=' ')) continue;//跳过

            //按Enter
            if(spare==13) {    //确认
                //排雷
                if(mode%2==0) {
                    if(map[floatx][floaty]=='@'&&flag[floatx][floaty]==0) {
                        break;    //触雷
                        game=0;
                    }

                    if(flag[floatx][floaty]==1) continue;    //有旗跳过
                    Open();
                    position(0,0);
                    SetConsoleTextAttribute(handle_out, FORE_GREEN);
                    for(int i=0;i<A;i++) {
                        for(int j=0;j<B;j++) Lump(i,j);
                        printf("\n");
                    }
                    position(floaty*2,floatx);
                    SetConsoleTextAttribute(handle_out, FORE_BLUE);
                    Lump(floatx,floaty);
                }

                //插拔旗
                else {

                    //不能插旗的地方
                    if(map[floatx][floaty]=='x'||(map[floatx][floaty]>'0'&&map[floatx][floaty]<'9'))
                        continue;    //跳过

                    //插旗
                    if(flag[floatx][floaty]==0) {
                        flagnum++;
                        flag[floatx][floaty]=1;
                        position(floaty*2,floatx);
                        SetConsoleTextAttribute(handle_out, FORE_BLUE);
                        Lump(floatx,floaty);
                    }

                    //拔旗
                    else {
                        flagnum--;
                        flag[floatx][floaty]=0;
                        position(floaty*2,floatx);
                        SetConsoleTextAttribute(handle_out, FORE_BLUE);
                        Lump(floatx,floaty);
                    }
                }
            }

            //按空格
            if(spare==' ') Mode();    //切换模式

            //按方向键
            if(spare==-32) {
                news=getch();
                Move();    //移动光标
            }
            for(int i=0;i<A;i++) for(int j=0;j<B;j++) if(map[i][j]=='x'||(map[i][j]>'0'&&map[i][j]<'9')) game++;
            if(game==A*B-C+1) break;
            else game=1;
            SetConsoleTextAttribute(handle_out, FORE_RED);
            position(44,5);
            printf("剩余雷数:%d ",C-flagnum);
        }
        else Sleep(10);
        b=GetTickCount();
        SetConsoleTextAttribute(handle_out, FORE_RED);
        position(44,7);
        printf("用时:");    //用时
        if((b-a)/60000<10) printf("0");
        printf("%d:",(b-a)/60000);
        if(((b-a)/1000)%60<10) printf("0");
        printf("%d:",((b-a)/1000)%60);
        if(((b-a)/10)%100<10) printf("0");
        printf("%d",((b-a)/10)%100);
    }
    SetConsoleTextAttribute(handle_out, FORE_RED);
    position(5,5);
    if(game==1) printf("游戏结束!");
    else printf("建设成功!");
    position(5,8);
    printf("任意键重玩");
    scanf("%c%c",&spare,&spare);
    system("cls");
    position(0,0);
    goto Relife;
}    

炸弹解压小游戏

原创

代码如下:

#include<bits/stdc++.h>
#include<windows.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>
#define KEY_DOWN(VK_NONAME)((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0)
using namespace std;
int m[10001][21];
struct node {int xx,yy,Zhong,rr,mr;} B[100001];
int K,X,Y,tX,tY,tY2,tT,Er,T,Yb,Zb,Win,Blood,Score,Kb;
void Color(int a)
{
    if(a==0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
    if(a==1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
    if(a==2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);
    if(a==3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE);
    if(a==4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
    if(a==5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
    if(a==6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE);
    if(a==7) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN);
    if(a==8) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
    if(a==9) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|BACKGROUND_INTENSITY|BACKGROUND_RED);
    if(a==10) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE|BACKGROUND_INTENSITY|BACKGROUND_BLUE);
}
void SetPos(int x,int y)
{
    COORD pos; pos.X=y*2-1,pos.Y=x+1;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void Zha(int x,int y)
{
    Zb++;
    int k1=rand()%2+2,k2=rand()%3+2,r=rand()%6;
    if(r==0) B[Zb].xx=x-K,B[Zb].yy=y,B[Zb].Zhong=1,B[Zb].rr=0,B[Zb].mr=k1*2;
    if(r==1) B[Zb].xx=x-K,B[Zb].yy=y,B[Zb].Zhong=2;
    if(r==2) B[Zb].xx=x-K,B[Zb].yy=y,B[Zb].Zhong=3,B[Zb].rr=k2,B[Zb].mr=k1;
    if(r==3) B[Zb].xx=x-K,B[Zb].yy=y,B[Zb].Zhong=4;
    if(r==4) B[Zb].xx=x-K,B[Zb].yy=y,B[Zb].Zhong=5,B[Zb].rr=1,B[Zb].mr=k2*2;
    if(r==5) B[Zb].xx=x-K,B[Zb].yy=y,B[Zb].Zhong=rand()%2+6,B[Zb].rr=k1+1,B[Zb].mr=k2+3;
}
void Cout(int i,int j,int a)
{
    int R=rand()%500,jk=0;
    if(i-K>=0&&i-K<=1&&j>=2&&j<=6) return;
    if(a!=4)
    {
    if(m[i][j]==0&&(a==0||a==3)) SetPos(i-K,j),Color(7),cout<<"■";
    if(m[i][j]==1&&a==5) SetPos(i-K,j),Color(0),cout<<"  ";//空气 
    if(m[i][j]==2) SetPos(i-K,j),Color(8),cout<<"■";
    if(m[i][j]>=3&&m[i][j]<6) SetPos(i-K,j),Color(5),cout<<"█",m[i][j]++;
    if(m[i][j]==6&&a!=5) SetPos(i-K,j),cout<<"  ",m[i][j]=1;
    if(m[i][j]==7) SetPos(i-K,j),Color(9),cout<<"~ ";
    if(m[i][j]==8) SetPos(i-K,j),Color(10),cout<<"~ ";
    if(m[i][j]==9) SetPos(i-K,j),Color(3),cout<<"◆";
    if((m[i][j]==1||m[i][j]==8)&&m[i-1][j]>=10&&m[i-1][j]<=30&&a!=5) {int M=m[i][j];m[i][j]=m[i-1][j],m[i-1][j]=M;if(M==1) SetPos(i-K-1,j),cout<<"  ";}//炸弹下落 
    if(m[i][j]==1&&m[i-1][j]==9&&a!=5) {m[i][j]=9,m[i-1][j]=1,SetPos(i-K-1,j),Color(0),cout<<"  ";jk=1;}//宝石下落
    if(m[i][j]>=10&&m[i][j]<=30) {m[i][j]++;if(m[i][j]%3==0) Color(4);else Color(5);SetPos(i-K,j),cout<<"●";}//炸弹计时
    if(m[i][j]==1&&m[i-1][j]==7&&a!=5) {m[i][j]=7,m[i-1][j]=1,SetPos(i-K-1,j),Color(0),cout<<"  ";jk=1;}//岩浆下落
    if(m[i][j]==0&&m[i-1][j]==7&&R<=2) m[i][j]=m[i-1][j],m[i-1][j]=1,SetPos(i-K-1,j),cout<<"  ";//岩浆融解 
    if(m[i][j]==7&&R>2&&R<=5) {int r=rand()%3-1; if(m[i][j+r]==1) m[i][j+r]=m[i][j];if(m[i][j+r]==8) m[i][j+r]=2;}//岩浆扩散  
    if(R<50&&R>=20&&m[i+1][j+1]==1&&m[i+1][j]==8&&m[i][j]==8) m[i+1][j+1]=8,m[i][j]=1,SetPos(i-K,j),Color(0),cout<<"  ";
    if(R<80&&R>=50&&m[i+1][j-1]==1&&m[i+1][j]==8&&m[i][j]==8) m[i+1][j-1]=8,m[i][j]=1,SetPos(i-K,j),Color(0),cout<<"  ";//岩浆平面 
    if(m[i][j]==1&&m[i-1][j]==8&&a!=5) {m[i][j]=8,m[i-1][j]=1,SetPos(i-K-1,j),Color(0),cout<<"  ";jk=1;}//水下落
    if(m[i+1][j+1]==1&&m[i+1][j]==8&&m[i][j]==8) m[i+1][j+1]=8,m[i][j]=1,SetPos(i-K,j),Color(0),cout<<"  ";
    if(m[i+1][j-1]==1&&m[i+1][j]==8&&m[i][j]==8) m[i+1][j-1]=8,m[i][j]=1,SetPos(i-K,j),Color(0),cout<<"  ";//水平面 
    if((m[i+1][j]==8&&m[i][j]==7)||(m[i+1][j]==7&&m[i][j]==8)) m[i][j]=2,m[i-1][j]=1,SetPos(i-K-1,j),Color(0),cout<<"  ";//变石头
    if(m[i][j]==0&&m[i-1][j]==8&&R>5&&R<=7) m[i][j]=m[i-1][j],m[i-1][j]=1,SetPos(i-K-1,j),cout<<"  ";//水融解 
    if(m[i][j]==8&&R>7&&R<=9) {int r=rand()%3-1; if(m[i][j+r]==1) m[i][j+r]=m[i][j];if(m[i][j+r]==7) m[i][j+r]=2;}//水扩散  
    if(m[i][j]==31) SetPos(i-K,j),cout<<"  ",Zha(i,j);
    }
    else if(a!=5)//界外 
    {
        if(m[i][j]>=3&&m[i][j]<6) m[i][j]++; if(m[i][j]==6) m[i][j]=1;
        if((m[i][j]==1||(m[i][j]>=3&&m[i][j]<=6))&&m[i-1][j]>=10&&m[i-1][j]<=30) m[i][j]=m[i-1][j],m[i-1][j]=1;//炸弹下落 
        if(m[i][j]>=10&&m[i][j]<=30) m[i][j]++;//炸弹计时
        if(m[i][j]==1&&m[i-1][j]==9) m[i][j]=9,m[i-1][j]=1;//宝石下落
        if(m[i][j]==1&&m[i-1][j]==7) m[i][j]=7,m[i-1][j]=1;//岩浆下落
        if(m[i][j]==1&&m[i-1][j]==8) m[i][j]=8,m[i-1][j]=1;//水下落
    }if(jk==1) jk=0,Cout(i,j,5);
    Color(0);
}
void Map(int a);
void Wo()
{
    SetPos(X-K,Y),cout<<"  "; //清除上一次残影
    if(m[X][Y]==9) Blood++,Score+=5,m[X][Y]=1,system("color 2F"),Sleep(50),system("color 0F"),Map(3);
    if(tX!=0&&(m[X-1][Y]==1||m[X-1][Y]==7||m[X-1][Y]==8||m[X-1][Y]==9||(m[X-1][Y]>=3&&m[X-1][Y]<=6))) tX--,X-=2;  //上跳 
    if(m[X+1][Y]==1||(m[X+1][Y]>=3&&m[X+1][Y]<=6)||m[X+1][Y]==7||m[X+1][Y]==8||m[X+1][Y]==9) X++; //掉落 
    if(m[X+1][Y]==0||m[X+1][Y]==2||(m[X+1][Y]>=3&&m[X+1][Y]<=6)) tX=Er=0; //跳跃次数清零 
    if(m[X-1][Y]==0||m[X-1][Y]==2||(m[X-1][Y]>=10&&m[X-1][Y]<=31)) tX=Er=0; //跳跃次数清零 
    if(X<=K) tX=Er=0,X=K; if(X>=K+28) X=K+28,Kb=1; //高度上下限
    if(m[X][Y]==7) m[X-1][Y]=m[X-2][Y]=1,tX+=8;
    if(m[X][Y]!=1&&m[X][Y]!=8&&m[X][Y]!=9) Blood--,m[X][Y]=1,system("color 4F"),Sleep(50),system("color 0F"),Map(3);
    if(Blood<=0) Win=-1;
    SetPos(X-K,Y),Color(1),cout<<"●";
}
void Map(int a)
{
    SetPos(0,2);cout<<"生命 :"<<Blood<<"  ";
    SetPos(1,2);cout<<"得分 :"<<Score<<"  ";
    if(a==3) system("cls");
    for(int i=K+28;i>=K;i--)for(int j=1;j<=20;j++) Cout(i,j,a);
    for(int i=K+33;i>K+28;i--)for(int j=1;j<=20;j++) Cout(i,j,4);
    if(a!=3) Wo();
}
void CircleBomb(int x,int y,int s,int ms)
{
    if(s==ms) return;
    for(int i=x-s;i<=x+s;i++)
    for(int j=y-s;j<=y+s;j++)
    {
        float k=(i-x)*(i-x)+(j-y)*(j-y)-s*s;
        if(k<=s&&k>=-s&&j>0&&j<=20&&m[i+K][j]!=2&&m[i+K][j]!=9) m[i+K][j]=3;
    }Zb++;
    B[Zb].xx=x,B[Zb].yy=y,B[Zb].Zhong=1,B[Zb].rr=s+1,B[Zb].mr=ms;
    Sleep(30);
}
void LineBomb(int i,int j)
{
    for(int k=0;j+k<=20;k++) if(m[i+K][j+k]!=2&&m[i+K][j+k]!=9) m[i+K][j+k]=3;
    for(int k=0;j+k<=20;k++) if(m[i+K+1][j+k]!=2&&m[i+K+1][j+k]!=9) m[i+K+1][j+k]=3;
    for(int k=0;j-k>0;k++) if(m[i+K][j-k]!=2&&m[i+K][j-k]!=9) m[i+K][j-k]=3;
    for(int k=0;j-k>0;k++) if(m[i+K+1][j-k]!=2&&m[i+K+1][j-k]!=9) m[i+K+1][j-k]=3;
}
void ZuanBomb(int i,int j)
{
    int k;
    for(k=0;m[i+K+k][j]!=2&&k<=7;k++) if(m[i+K+k][j]!=9) m[i+K+k][j]=3;
    int k1=rand()%2+2;Zb++;
    B[Zb].xx=i+k,B[Zb].yy=j,B[Zb].Zhong=1,B[Zb].rr=0,B[Zb].mr=k1*3/2;
}
void TrigleBomb(int i,int j,int r,int mr)
{
    if(r==mr) return;
    if(r==1) m[i+K][j]=3,i++;
    for(int k=max(0,j-r);k<=min(20,j+r);k++) if(m[i+K][k]!=2&&m[i+K][k]!=9) m[i+K][k]=3;
    Zb++;B[Zb].xx=i+1,B[Zb].yy=j,B[Zb].Zhong=5,B[Zb].rr=r+1,B[Zb].mr=mr;
}
void GunBomb(int i,int j,int r,int mr)
{
    if(mr<=0) return;Zb++;
    B[Zb].xx=i,B[Zb].yy=j+2,B[Zb].Zhong=6,B[Zb].rr=r,B[Zb].mr=mr-1;Zb++;
    B[Zb].xx=i,B[Zb].yy=j,B[Zb].Zhong=1,B[Zb].rr=r-1,B[Zb].mr=r;
}
void GunBomb2(int i,int j,int r,int mr)
{
    if(mr<=0) return;Zb++;
    B[Zb].xx=i,B[Zb].yy=j-2,B[Zb].Zhong=7,B[Zb].rr=r,B[Zb].mr=mr-1;Zb++;
    B[Zb].xx=i,B[Zb].yy=j,B[Zb].Zhong=1,B[Zb].rr=r-1,B[Zb].mr=r;
}
void ThreeBomb(int x,int y,int s,int ms)
{
    for(int i=1;i<=3;i++)
    {
        int Xx=rand()%(2*s+1)-s,Yy=rand()%(2*s+1)-s; Zb++;
        B[Zb].xx=Xx+x,B[Zb].yy=Yy+y,B[Zb].Zhong=1,B[Zb].rr=0,B[Zb].mr=ms+rand()%3-1;
    }
}
void Sheng(int a)
{
    K++;
    for(int i=1;i<=20;i++)
    {
        int R=rand()%max(80-(K/10),30);int Rr=rand()%200;
        if(R<=3) m[K+28][i]=2;
        else if(R<=6) m[K+28][i]=7;
        else if(R<=9) m[K+28][i]=8;
        if(Rr==0) m[K+28][i]=9;
    }
    if(a!=1) system("cls"),Map(0);
}
void Jiao()
{
    SetPos(0,2);Color(1),cout<<"●",Color(7),cout<<"   ■ ",Color(8),cout<<"   ■ ",Color(4),cout<<"   ●    ",Color(9),cout<<"~ ",Color(0),cout<<"   ",Color(10),cout<<"~ ",Color(3),cout<<"   ◆";
    SetPos(2,2);Color(0),cout<<"你  泥土  石块  炸弹  岩浆  水  宝石";
    SetPos(4,1),cout<<"  ↑           空格放炸弹..."; 
    SetPos(5,1),cout<<"←  →移动 ";
    SetPos(6,1),cout<<"  ↓           可二段跳。";
    SetPos(8,1),cout<<"炸弹种类随机,计时3秒爆炸。";
    SetPos(10,1),cout<<"(一共有7种炸弹,有几率组合一起爆炸)";
    SetPos(12,1),cout<<"岩浆和水有几率向左右扩散...";
    SetPos(14,1),cout<<"它们相融会产生石块。";
    SetPos(16,1),cout<<"岩浆和爆炸波会使你减血...";
    SetPos(18,1),cout<<"岩浆还会使你上跳,宝石可以加血。";
    SetPos(20,1),cout<<"画面每隔一段时间会下降...";
    SetPos(22,1),cout<<"当你抵达画面底部时,画面会随你一起下降...";
    SetPos(24,1),cout<<"当你被抵在画面顶部时,会持续减血。";
    SetPos(26,1),cout<<"每隔一段时间分数会增加...",Color(5),cout<<"200分即可通关!";
    SetPos(28,1);Color(1);cout<<"按 y 开始游戏!";
    char tt;while(tt!='y') tt=_getch();
}
void Start()
{
    Color(5);
    SetPos(2,3);Color(7),cout<<" ■        ",Color(5),cout<<"掘",Color(7),cout<<"    ■■■■ ";
    SetPos(3,3);Color(7),cout<<" ■■     ",Color(5),cout<<" ↓",Color(7),cout<<"    ■■■■ ";
    SetPos(4,3);Color(7),cout<<" ■■■■ ",Color(5),cout<<" ↓",Color(7),cout<<"  ■■■■■ ";
    SetPos(5,3);Color(7),cout<<" ■■■■■",Color(5),cout<<"↓",Color(7),cout<<"  ■■■■■ ";
    SetPos(6,3);Color(7),cout<<" ■■■■■  ■■■",Color(5),cout<<"地",Color(7),cout<<"■■ ";
    SetPos(7,3);Color(7),cout<<" ■■■■■■■■■■■■ ";Color(5);
    SetPos(22,2);Color(1);cout<<"按 y 确定!";
    SetPos(22,10);Color(9);cout<<"    开始游戏!    ";
    SetPos(24,10);Color(0);cout<<"    操作攻略!    ";
    SetPos(27,1);Color(3);cout<<"注意!这里 绝对不能是拼音输入法!";
    SetPos(28,5);Color(3);cout<<"↓";Color(0);
    char tt;int Ee=0;
    while(tt!='y')
    {
        tt=_getch();
        if(tt==72&&Ee!=0) Ee--;
        if(tt==80&&Ee!=2) Ee++;
        SetPos(22,10);if(Ee==0) Color(9);else Color(0);cout<<"    开始游戏!    ";
        SetPos(24,10);if(Ee==1) Color(9);else Color(0);cout<<"    操作攻略!    ";
    }system("color 0F");system("cls");Color(0);
    if(Ee==1) Jiao();
}
int main()
{
    system("mode con cols=42 lines=31");
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
    srand((unsigned)time(NULL));
    Start();
    St:system("cls");
    K=-5,T=Win=Score=0;X=Y=2;Blood=20;
    memset(m,0,sizeof(m));memset(B,0,sizeof(B));
    for(int i=5;i<=15;i++) for(int j=1;j<=20;j++) m[i][j]=1;
    for(int i=1;i<=10;i++) Sheng(1);
    Map(0);
    while(Win==0)
    {
        T++;Kb=0;if(Score>=200) Win=1;
        if(GetAsyncKeyState(VK_UP)&0x8000&&tT==0&&Er<2) tT++,tX+=4,Er++;
        if((GetAsyncKeyState(VK_UP)&0x8000)?0:1) tT=0;
        if(GetAsyncKeyState(VK_LEFT)&0x8000&&Y>1&&(m[X][Y-1]==1||m[X][Y-1]==7||m[X][Y-1]==8||m[X][Y-1]==9)&&(tY==0||tY>=2)) SetPos(X-K,Y),cout<<"  ",Y--;
        if(GetAsyncKeyState(VK_RIGHT)&0x8000&&Y<20&&(m[X][Y+1]==1||m[X][Y+1]==7||m[X][Y+1]==8||m[X][Y+1]==9)&&(tY2==0||tY2>=2)) SetPos(X-K,Y),cout<<"  ",Y++;
        if((GetAsyncKeyState(VK_LEFT)&0x8000)?0:1) tY=0;if((GetAsyncKeyState(VK_RIGHT)&0x8000)?0:1) tY2=0;
        if((GetAsyncKeyState(VK_LEFT)&0x8000)?1:0) tY++;if((GetAsyncKeyState(VK_RIGHT)&0x8000)?1:0) tY2++;
        if(kbhit()) {char e=_getch();if(e==' '&&m[X-1][Y]!=0&&m[X-1][Y]!=2&&(m[X+1][Y]<=10||m[X+1][Y]>=31)&&Kb==0) m[X][Y]=10,X--;}
        int zb=Zb;
        for(int i=zb-10;i<=zb;i++)
        {
            if(B[i].Zhong==1) CircleBomb(B[i].xx,B[i].yy,B[i].rr,B[i].mr),B[i].Zhong=0;
            if(B[i].Zhong==2) LineBomb(B[i].xx,B[i].yy),B[i].Zhong=0;
            if(B[i].Zhong==3) ThreeBomb(B[i].xx,B[i].yy,B[i].rr,B[i].mr),B[i].Zhong=0;
            if(B[i].Zhong==4) ZuanBomb(B[i].xx,B[i].yy),B[i].Zhong=0;
            if(B[i].Zhong==5) TrigleBomb(B[i].xx,B[i].yy,B[i].rr,B[i].mr),B[i].Zhong=0;
            if(B[i].Zhong==6) GunBomb(B[i].xx,B[i].yy,B[i].rr,B[i].mr),B[i].Zhong=0;
            if(B[i].Zhong==7) GunBomb2(B[i].xx,B[i].yy,B[i].rr,B[i].mr),B[i].Zhong=0;
        }
        Map(1);Sleep(40);
        if(T%max(10,40-Score/16)==0||Kb>=1) Sheng(0);
        if(T%20==0) Score++;
    }
    if(Win!=0)
    {
        if(Win>0) {system("color 6E"),Color(3);SetPos(0,2);cout<<"You! Win!!!",Sleep(1000);}
        if(Win<0) {system("color 7F"),Color(4);SetPos(0,2);cout<<"You! Die!!!",Sleep(1000);}
        SetPos(1,2);cout<<"请输入y重新开始游戏";
        A:char e=_getch();if(e!='y') goto A;goto St;
    }
}

笨鸟先飞

原创

代码如下:

#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<windows.h>
using namespace std;
int x=50;  //边界的x和y
int y=20;
int birdx=x/5;
int birdy=y/4;
int speed=0;//控制速度
int speed2=0;//控制障碍物的来临速度
int xx=x/2;//障碍物的x坐标
int yy= rand()%(y/2);

int count=0;//记录笨鸟的得分

void notin();
void in();
void gotoxy(int x,int y)
     {
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X=x;
    pos.Y =y;
    SetConsoleCursorPosition(handle,pos);
}
void HideCursor()
{
    CONSOLE_CURSOR_INFO cursor_info={1,0};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void show()//开始的初始化(达到循环显示的目的)
{
         gotoxy(0,0) ;
            HideCursor()   ;
            for(int i=1;i<y;++i)
            {
                for(int j=1;j<x;++j)
                {
                    if(j==birdx&&i==birdy)
                    {
                        cout<<"\b"<<"鸟"; //\b的作用是因为输出的汉字多占了一个大小的空间,\b删除之前一个位置的输出
                     }
                     else if(j==xx&&(i>=1&&i<=yy)||j==xx&&(i>=yy+5&&i<=y))
                     cout<<"*" ;

                     else
                     cout<<" ";
                 }
                 cout<<endl;
             }

             cout<<"得分:"<<count/6<<endl;

}
void in()
{
    if(kbhit())
    {
        string ss;
        ss=getch();
        if(ss==" ")
        {
            if(birdy>3)birdy-=3;
        }

    }
}
void notin()  //改变小鸟的位置和障碍物的位置
{
    if (birdx==xx&& (birdy>=yy&&birdy<=yy+5))
        count+=1;
    if(speed!=3)
    {
        speed++;
    }
    if(birdy!=y&&speed==3)
    {
        birdy+=1;
        speed=0;
    }
    if(speed2!=5)
    {
        speed2++;
    }
    else if(speed2==5)
    {
        if(xx>0)
        {
            xx--;
        }
        if(xx==0)
        {
            xx=x/2;//障碍物的x坐标
            yy=rand()%(y/2);
            while(yy==0)
            {
                yy= rand()%(y/2);
            }
        }
    speed2=0;
    }
}
int main()
{
    while(yy==0)
    {
        yy= rand()%(y/2);
    }
    while(1)
    {
        show();
        notin();
        in() ;
    if(birdx==xx&&(((birdy>1&&birdy<yy))||(birdy >yy+5&&birdy<y)))
    {
        cout<<"YOU ARE FILLED!!"<<endl;
        break;
    }
}
    return 0;
}

苍穹世界

原创

代码如下:

#include<stdio.h>
#include<ctime>
#include<time.h> //suiji
#include<windows.h> //SLEEP函数
struct Player //玩家结构体,并初始化player
{
char name[21];
int attack;
int defense;
int health;
long int max_health;
int level;
int exp;
int range_exp;
long int max_exp;
} player= {"勇者",50,40,100,100,1,0,0,100};
struct Enemy //怪的结构体,并初始化各种怪
{
char name[20];
char wupin[12];
int attack;
int defense;
int health;
int money;
long int exp;
int wupin_sign;
int wupinpro;
int double_attack;
int miss;
} strongman= {"森林巨人","黄金圣衣",40,50,350,200,100,1,2,1,0},
witch= {"森林女巫","银甲",25,15,100,50,50,2,2,1,1},
xiyi= {"森林蜥蜴","铁甲",18,10,50,30,35,3,3,2,2},
big_strongman= {"森林巨人王","巨人晶石",40*5,50*5,200*5,200*5,100*5,4,4,2,0},
lion= {"草原雄狮","绝世好剑",60,30,280,200,100,5,2,1,0},
horse= {"草原野马","碧血剑",28,12,90,50,50,6,2,1,1},
bee= {"草原黄蜂","长剑",17,11,60,30,35,7,3,2,2},
shitu= {"使徒","\0",60*8,30*8,280*8,200*8,100*8,9,1,1,0},
guai= {"\0","\0",0,0,0,0,0,0,0,0,0};
struct Place
{
int bar,hotel,forest1,forest2,forest3,grass1,grass2,grass3;
} place= {1,2,3,4,5,6,7,8};

int max_exp=0;
int choose_number=0,s=0,strongman_arm=0,battle=0,money=500,place_sign=9;
int cao=3,jijiubao=2,baiyao=2,superbaiyao=1,boom=3,dubiao=2,atom_boom=1;
int fang=0,fang1=10,fang1n=0,fang2=20,fang2n=0,fang3=40,fang3n=0,fang4=100,fang4n=0;
int gong=0,gong1=8,gong1n=0,gong2=15,gong2n=0,gong3=25,gong3n=0,gong4=60,gong4n=0;
int jingyancao=0,jingyanbao=0,jingyanshi=0;
char gongname[20]="无",fangname[20]="无";
char proof;

void AddWupin(int);
int AttackResult();
void BattleAct();
void ChooseWupin();
void DisplayState();
void OrdinaryAct();
int SuiJi();
int SuiJi100();
void WhetherLevelUp();
void SlowDisplay(char *);

int main()
{

int i=0,j=0,k=0;
char player_name[21];
Sleep(1000);
printf("--------------------------欢迎来到 [苍穹世界] 2.2 测试版-----------------------\n\n\n");
//如果想使用外挂,名字请输入:“圣战斗士 ”。
Sleep(1000);
printf("这里是苍穹世界! 雅莉萨斯国的罗茜公主被陌生人绑架了!\n\n\n 伟大的勇者啊~拿起你们的武器,营救公主!\n\n\n输入你的名字: (20个字符)\n\n\n");
scanf("%s",player_name);
strncpy(player.name,player_name,20);
if(strcmp(player.name,"圣战斗士")==0)
{
printf("\n\n\n封印多年的圣剑血统啊!你终于觉醒了!\n\n\n圣战斗士,你成为了天选之人,请你救出公主吧!\n\n\n");
player.attack=999;
player.defense=999;
player.health=9999;
player.max_health=9999;
}
getchar();

OrdinaryAct();
return 0;
}

int SuiJi()
{
srand((unsigned)time(NULL));
return rand()%10;
}

int SuiJi100()
{
srand((unsigned)time(NULL));
return rand()%100;
}

void ChooseWupin() //选择物品 并使用
{
printf("物品: 1,止血草%d个 2,急救包%d个 3,云南白药%d个 4,超级云南白药%d个 5,手雷%d个 6,毒标%d个 7,手抛式原子弹%d个 8,经验草%d个 9,经验包%d个 10,经验石%d个 11,巨人晶石%d个 0,返回\n\n\n",cao,jijiubao,baiyao,superbaiyao,boom,dubiao,atom_boom,jingyancao,jingyanbao,jingyanshi,strongman_arm);
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
if(cao>0)
{
printf("使用止血草,HP增加60\n\n\n");
cao--;
if(player.health+60>player.max_health)player.health=player.max_health;
else player.health+=60;
}
else printf("没有止血草了\n\n\n");
break;
case 2:
if(jijiubao>0)
{
printf("使用急救包,HP增加80\n\n\n");
jijiubao--;
if(player.health+80>player.max_health)player.health=player.max_health;
else player.health+=80;
}
else printf("没有急救包了\n\n\n");
break;
case 3:
if(baiyao>0)
{
printf("使用云南白药,HP增加120\n\n\n");
baiyao--;
if(player.health+120>player.max_health)player.health=player.max_health;
else player.health+=120;
}
else printf("没有云南白药了\n\n\n");
break;
case 4:
if(superbaiyao>0)
{
printf("使用超级云南白药,HP增加200\n\n\n");
superbaiyao--;
if(player.health+200>player.max_health)player.health=player.max_health;
else player.health+=200;
}
else printf("没有超级云南白药了\n\n\n");
break;
case 5:
if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品
{
if(boom>0)
{
printf("使用手雷,敌人HP减少100\n\n\n");
boom--;
guai.health-=100;
AttackResult();
}
}
else printf("非战斗状态,不能使用手雷!\n\n\n");
break;
case 6:
if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品
{
if(dubiao>0)
{
printf("使用毒标,敌人HP减少200\n\n\n");
dubiao--;
guai.health-=200;
AttackResult();
}
}
else printf("非战斗状态,不能使用毒标!\n\n\n");
break;
case 7:
if(battle) //在战斗中(battle=1),否则(battle=0)不能使用攻击性物品
{
if(atom_boom>0)
{
printf("使用手抛式原子弹,敌人HP减少666666666\n\n\n");
atom_boom--;
guai.health-=666666666;
AttackResult();
}
}
else printf("非战斗状态,不能使用手抛式原子弹!\n\n\n");
break;
case 8:
if(jingyancao>0 && player.level<46)
{

printf("使用经验草,等级增加1级\n\n\n");
jingyancao--;
player.level+=1;
printf("等级:%d\n",player.level);
}
else    if(jingyancao<1)
{
printf("没有经验草了\n\n\n");
}
else printf("等级超过45级,修为太高,无法使用。\n\n\n");
break;
case 9:
if(jingyanbao>0 && player.level<46)
{
if(player.level>44&&player.level<46)
{
int sheng;
sheng=45-player.level;
player.level+=sheng;
printf("使用经验包,等级增加%d级",sheng);
printf("等级:%d\n",player.level);
}
else
{

printf("使用经验包,等级增加2级\n\n\n");
jingyanbao--;
player.level+=2;
printf("等级:%d\n",player.level);
}
}
else if(jingyanbao<1)
{
printf("没有经验包了");
}
else printf("等级超过45级,修为太高,无法使用。\n\n\n");
break;
case 10:
if(jingyanshi>0 && player.level<45)
{
if(player.level>42&&player.level<46)
{
int sheng;
sheng=45-player.level;
player.level+=sheng;
printf("使用经验石,等级增加%d级\n",sheng);
printf("等级:%d\n",player.level);
}
else
{
printf("使用经验石,等级增加4级\n");
jingyanshi--;
player.level+=4;
}
}
else if(jingyanshi<1)
{
printf("没有经验石了\n\n\n");
}
else printf("等级超过45级,修为太高,无法使用。\n\n\n");
break;
case 11:
if(strongman_arm>0 && player.level<46)
{
if(player.level>29&&player.level<46)
{
int sheng;
sheng=45-player.level;
player.level+=sheng;
printf("使用巨人晶石,等级增加%d级",sheng);
printf("等级:%d\n",player.level);
}
else
{
printf("使用巨人晶石,等级增加16级\n\n\n");
strongman_arm--;
player.level+=16;
printf("等级:%d\n",player.level);
}
}
else if(strongman_arm<1)
{
printf("没有巨人晶石了。\n\n\n");
}
else printf("等级超过45级,修为太高,无法使用。\n\n\n");
break;
case 0:
break;
default:
printf("ChooseWupin error!\n\n\n");
}
}

int AttackResult() //攻击结果:判断是否获胜 是否获得物品 和 是否升级
{
if(guai.health<=0)
{
battle=0;
printf("战斗胜利!获得金币%d,经验%d\n\n\n",guai.money,guai.exp);
player.exp+=guai.exp;
player.range_exp+=guai.exp;
money+=guai.money;
s=SuiJi();
if(s<guai.wupinpro)
{
printf("从敌人尸骸中发现");
printf("%s\n\n\n",guai.wupin);
AddWupin(guai.wupin_sign);
}
WhetherLevelUp();
if(strcmp(guai.name,"使徒")==0)
{
printf("战斗胜利,救出公主!!!");
getchar();
getchar();
exit(0);
}
return 1; //攻击有结果了返回1,否则返回0,用于判断是否继续做战斗行为
}
else
{
int s=SuiJi();

if((guai.attack+s-player.defense/3)<0)
{
player.health-=1;
printf("%s反击,你的HP减少了 1\n\n",guai.name);
}
else
{
player.health-=guai.attack+s-player.defense/3;
printf("%s反击,你的HP减少了%d\n\n",guai.name,guai.attack+s-player.defense/3);
}
if(player.health<0)
{
battle=0;
printf("%s战死!金币掉落%d\n\n\n",player.name,player.level*500);
money-=player.level*500;
player.health=player.max_health/5;
OrdinaryAct();//
return 1;
}
}
return 0;
}
void AddWupin(int wupin_sign)
{

switch(wupin_sign)
{
case 1:
fang4n++;
break;
case 2:
fang3n++;
break;
case 3:
fang2n++;
break;
case 4:
strongman_arm=1;
break;
case 5:
gong4n++;
break;
case 6:
gong3n++;
break;
case 7:
gong2n++;
break;
default:
printf("AddWupin error\n\n\n");
}

}
void WhetherLevelUp()
{
int i=0,j=0;
int l1=player.range_exp/100;
int l2=player.range_exp/300;
int l3=player.range_exp/600;
if(player.level<=15&&l1>0) //15级以下,经验足够 都满足则升级
{
if(l1==1)
{
printf("%s",player.name);
printf(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n");
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3;
player.defense+=2;
player.max_health+=20;
player.health=player.max_health;
player.level++;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=100;
}
else
{
printf("好厉害!连升%d级!",l1);
printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l1,2*l1,20*l1);
player.exp=(player.exp+guai.exp) || player.exp-((player.exp+guai.exp) || player.exp)%100;
player.attack+=3*l1;
player.defense+=2*l1;
player.max_health+=20*l1;
player.health=player.max_health;
player.level+=l1;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=100*l1;
}
}
else if(player.level<=40&&l2>0)
{
if(l2==1)
{
printf("%s",player.name);
printf(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n");
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3;
player.defense+=2;
player.max_health+=20;
player.health=player.max_health;
player.level++;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=300;
}
else
{
printf("好厉害!连升%d级!",l1);
printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l2,2*l2,20*l2);
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3*l2;
player.defense+=2*l2;
player.max_health+=20*l2;
player.health=player.max_health;
player.level+=l2;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=300*l2;
}
}
else if(l3>0)
{
if(l3==1)
{
printf("%s",player.name);
printf(" 升级!\n\n\n攻击力+3, 防御力+2, HP上限+20\n\n\n");
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3;
player.defense+=2;
player.max_health+=20;
player.health=player.max_health;
player.level++;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=600;
}
else
{
printf("好厉害!连升%d级!",l1);
printf("攻击力+%d, 防御力+%d, HP上限+%d\n\n\n",3*l3,2*l3,20*l3);
player.exp=player.exp+guai.exp-(player.exp+guai.exp)%100;
player.attack+=3*l3;
player.defense+=2*l3;
player.max_health+=20*l3;
player.health=player.max_health;
player.level+=l3;
player.range_exp=0;
player.exp=player.max_exp;
player.max_exp+=600*l3;
}
}
}
void OrdinaryAct() //正常行为菜单(移动,物品,对话,查看状态,装备,退出游戏)
{

while(1)
{
// \(1000);
// system("cls");
puts("=============================================================================");
printf("要做什么?\n\n\n 1,移动 2,道具 3,对话 4,查看状态 5,装备 6,关于游戏 0,退出游戏\n\n\n");
puts("=============================================================================");
switch(scanf("%d",&choose_number),choose_number)
{
case 1: //显示移动菜单
printf("要去哪里?\n\n\n");
printf("1,happy酒吧 2,诺亚方舟酒店 3,北朝商会 4,红玉拍卖行 5,冒险荒野\n\n\n");
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
place_sign=place.bar; //记录目前位置-酒吧
// OrdinaryAct();
break;
case 2:
place_sign=place.hotel; //进入旅店
printf("金币:%d",money);
printf("要开房吗? 200个金币 1,是 0,否\n\n\n");
choose_number=1;
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
if(money-200<0) //判断钱是否够
{
printf("Sorry,你的钱不够~\n\n\n");
printf("金币:%d",money);
}
else
{
printf("好好休息\nHP满\n第二天了\n\n");
printf("金币:%d\n",money);
money-=200; //花费200住店费
player.health=player.max_health; //体力满
}
break;
case 0:
printf("下次再来!\n\n\n");
break;
default:
printf("hotel talk error!\n\n\n");
}
place_sign=0;
break;
case 3:
int yongju,gong,fang;
printf("请问您要购买什么类型的物品?\n\n\n 1,攻击装备 2,防御装备 3,一次性伤害武器\n\n\n");
scanf("%d",&yongju);
switch(yongju)
{
case 1:
printf("请问您要购买什么武器?\n\n\n 1,匕首¥300 2,长剑¥500 3,碧血剑¥1000\n\n\n");
scanf("%d",&gong);
switch(gong)
{
case 1:
if(money>=300)
{
gong1n++;
money=money-300;
printf ("匕首+1\n");
printf("匕首:%d个\n",gong1n);
printf("金币:%d\n",money);
break;
}
else
{
printf("钱不够!\n");
printf("金币:%d\n",money);
break;
}
case 2:
if(money>=500)
{
gong2n++;
money=money-500;
printf ("长剑+1\n");
printf("长剑:%d个\n",gong2n);
printf("金币:%d\n",money);
break;
}
else
{
printf("钱不够!\n");
printf("金币:%d\n",money);
break;
}
case 3:
if(money>=1000)
{
gong3n++;
money=money-1000;
printf ("碧血剑+1\n");
printf("碧血剑:%d个\n",gong3n);
printf("金币:%d\n",money);
break;
}
else
{
printf("钱不够!\n");
printf("金币:%d\n",money);
break;
}
default:
printf("对不起,我们只会打造以上武器。");
break;

}
break;
case 2:
int fang;
printf("请问您要购买什么防具?\n\n\n 1,布衣¥300 2,铁甲¥500 3,银甲¥1000\n\n\n");
scanf("%d",&fang);
switch(fang)
{
case 1:
if(money>=300)
{
fang1n++;
money=money-300;
printf ("布衣+1\n");
printf("布衣:%d个\n",fang1n);
printf("金币:%d\n",money);
}
else
{
printf("钱不够!\n");
printf("金币:%d\n",money);
}

break;
case 2:
if(money>=500)
{
fang2n++;
money=money-500;
printf ("铁甲+1\n");
printf("铁甲:%d个\n",fang2n);
printf("金币:%d\n",money);
}
else
{
printf("钱不够!\n");
printf("金币:%d",money);
}
break;
case 3:
if(money>=1000)
{
fang3n++;
money=money-1000;
printf ("银甲+1\n");
printf("银甲:%d个\n",fang3n);
printf("金币:%d\n",money);
}
else
{
printf("钱不够!\n");
printf("金币:%d\n",money);
}
default:
printf("对不起,我们只会打造以上防具。");
break;

}
printf("金币:%d\n",money);
break;
case 3:
printf("请问您要购买什么一次性伤害武器?\n 1,手雷 2,毒镖 3,手抛式原子弹\n\n\n");
int yi;
scanf("%d",&yi);
switch(yi)
{
case 1:
if(money>=300 && boom<5)
{
boom++;
money=money-300;
printf("手雷+1\n");
printf("手雷:%d\n",boom);
printf("金币:%d\n",money);
}
else
{
printf("钱不够!\n");
printf("金币:%d",money);
}
break;
case 2:
if(money>=600 && dubiao<4)
{
dubiao++;
money=money-600;
printf("毒镖+1\n");
printf("毒镖:%d\n",dubiao);
printf("金币:%d\n",money);
}
else
{
printf("钱不够!\n");
printf("金币:%d\n",money);
}
break;
case 3:
if(money>=0 && atom_boom<23333333333)
{
atom_boom=atom_boom+233;
money=money+1500;
printf("手抛式原子弹+2\n");
printf("手抛式原子弹:%d\n",atom_boom);
printf("金币:%d\n",money);
}
else
{
printf("钱不够!\n\n\n");
printf("金币:%d\n",money);
}

break;
}
}
break;
case 4:
printf ("欢迎您光临本拍卖行,请问您要卖什么东西?\n\n");
printf("攻击装备: 1,匕首:%d个 2,长剑:%d个 3,碧血剑:%d个 4,绝世好剑:%d个\n",gong1n,gong2n,gong3n,gong4n);
printf("防御装备: 5,布衣:%d个 6,铁甲:%d个 7,银甲:%d个 8,黄金圣衣:%d个\n9,巨人晶石:%d个 0,返回\n\n\n",fang1n,fang2n,fang3n,fang4n,strongman_arm);
int pai,shu,i;
scanf("%d",&pai);
switch(pai)
{
case 1:
printf("请问您要出售几件?");
scanf("%d",&shu);
if(gong1n>=shu)
{
gong1n=gong1n-shu;
money=money+shu*240;
printf("匕首:%d\n",gong1n);
printf("金币:%d\n",money);
break;
}
else
{
printf("装备数不够,无法出售!\n");
break;
}
break;
case 2:
printf("请问您要出售几件?\n");
scanf("%d",&shu);
if(gong2n>=shu)
{
gong2n=gong2n-shu;
money=money+shu*400;
printf("长剑:%d\n",gong2n);
printf("金币:%d\n",money);
break;
}
else
{
printf("装备数不够,无法出售!\n");
break;
}
case 3:
printf("请问您要出售几件?\n");
scanf("%d",&shu);
if(gong3n>=shu)
{
gong3n=gong3n-shu;
money=money+shu*800;
printf("碧血剑:%d\n",gong3n);
printf("金币:%d\n",money);
break;
}
else
{
printf("装备数不够,无法出售!\n");
break;
}
case 4:
printf("请问您要出售几件?\n");
scanf("%d",&shu);
if(gong4n>=shu)
{
gong4n=gong4n-shu;
money=money+shu*1500;
printf("绝世好剑:%d\n",gong4n);
printf("金币:%d\n",money);
break;
}
else
{
printf("装备数不够,无法出售!\n");
break;
}
case 5:
printf("请问您要出售几件?\n");
scanf("%d",&shu);
if(fang1n>=shu)
{
fang1n=fang1n-shu;
money=money+shu*240;
printf("布衣:%d\n",fang1n);
printf("金币:%d\n",money);
break;
}
else
{
printf("装备数不够,无法出售!\n");
break;
}
case 6:
printf("请问您要出售几件?\n");
scanf("%d",&shu);
if(fang2n>=shu)
{
fang2n=fang2n-shu;
money=money+shu*500;
printf("铁甲:%d\n",fang2n);
printf("金币:%d\n",money);
break;
}
else
{
printf("装备数不够,无法出售!\n");
break;
}
case 7:
printf("请问您要出售几件?\n");
scanf("%d",&shu);
if(fang3n>=shu)
{
fang3n=fang3n-shu;
money=money+shu*800;
printf("银甲:%d\n",fang3n);
printf("金币:%d\n",money);
break;
}
else
{
printf("装备数不够,无法出售!\n");
break;
}
break;
case 8:
printf("请问您要出售几件?\n");
scanf("%d",&shu);
if(fang1n>=shu)
{
fang4n=fang4n-shu;
money=money+shu*1500;
printf("黄金圣衣:%d\n",fang4n);
printf("金币:%d\n",money);
break;
}
else
{
printf("装备数不够,无法出售!\n");
break;
}
case 9:
printf("请问您要出售几颗?");
scanf("%d",&shu);
if(strongman_arm>=shu)
{
strongman_arm=strongman_arm-shu;
money=money+shu*2000;
printf("巨人晶石:%d\n",strongman_arm);
printf("金币:%d\n",money);
}
else
{
printf("晶石数不够,无法出售!\n");
break;
}
break;
case 0:
break;
break;
default:
printf("没有该装备,无法出售!\n");
break;
}

break;
case 5:
int yewai;
while(1)
{
puts("=============================================================================");
printf("要去哪冒险呢?");
printf("\n\n 1,神秘沼泽 危险程度:★\n\n 2,星耀草原 危险程度:★\n\n 3,诡异森林 危险程度:★★★\n\n 4,荒漠矿场 危险程度:★★★\n\n 5,炽热炎洞 危险程度:★★★★\n\n 6,花朵宫殿 危险程度:★★★★★\n\n 0,离开\n");
puts("=============================================================================");
scanf("%d",&yewai);
switch(yewai)
{
case 1:
place_sign=place.forest1;
s=SuiJi();
if(s<7)
{
battle=1;
guai=xiyi;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=witch;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
printf("这里安全\n\n\n");
//不用调用OAct函数,会自动执行OAct函数;
}
break;
case 3:
place_sign=place.forest2;
s=SuiJi();
if(s<7)
{
battle=1;
guai=witch;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=strongman;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
printf("这里安全\n\n\n");
}
break;
case 5:
place_sign=place.forest3;
s=SuiJi();
if(s<7)
{
battle=1;
guai=strongman;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=big_strongman;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
printf("这里安全\n\n\n");
}
break;
case 2:
place_sign=place.grass1;
s=SuiJi();
if(s<7)
{
battle=1;
guai=bee;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=horse;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
printf("这里安全\n\n\n");
}
break;
case 4:
place_sign=place.grass2;
s=SuiJi();
if(s<7)
{
battle=1;
guai=horse;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
guai=lion;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else
{
printf("这里安全\n\n\n");
}
break;
case 6:
place_sign=place.grass3;
s=SuiJi();
if(s<7)
{
battle=1;
guai=lion;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else if(s<9)
{
battle=1;
if(strongman_arm)
{
printf("神秘老人:\n\n\n 哈哈,年轻人,做的不错,不过...嘿嘿,你上当啦!巨人晶石我要了,公主你也别想带走!\n\n\n");
guai=shitu;
printf("%s扑了过来!\n\n\n",guai.name);
BattleAct();
}
else printf("神秘老人:\n\n\n 年轻人,你好啊.如果你有巨人晶石,我可以告诉你公主的下落哦~\n\n\n");
}
else
{
printf("这里安全\n\n\n");
}
break;
if(yewai!=0)
{
printf("该区域为未知区域,无法进入。\n\n\n");
break;
}
}
if(yewai==0)
{
break;
printf("已离开荒野。");
}
}

}
break;

case 2:
ChooseWupin();
break; //显示道具,并可以使用.
case 3: //对话选项
if(place_sign==place.bar)
{
printf("要和谁说话?\n\n\n1,红发女郎 2,赏金猎人 3,酒吧老板 4,药品商人\n\n\n"); //显示对话人物
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
printf("红发女郎:\n\n\n 吧台边那个Hunter好帅啊!(~脸红~)\n\n\n听说他经常外出打猎,外面的路他应该很熟悉的!\n\n\n");
break;
case 2:
if(fang1n<1&&gong1n<1)
{
printf("赏金猎人:\n\n\n 你要救公主啊!好胆量!\n\n\n 不过外面的世界很险恶,而且越深越危险,这是匕首和布衣,对你会有些帮助的,拿去吧!\n\n\n");
printf("%s心想:哇,这位大叔人真好啊!\n\n\n)",player.name);
gong1n++;
fang1n++;
}
else printf("赏金猎人:\n\n\n 加油吧,年轻人!\n\n\n 不要被外面世界所吓倒!\n\n\n");
break;
case 3:
printf("要喝点什么?\n\n\n 1,二锅头25金币 HP+20 2,XO酒80金币 HP+50 3,人头马面150金币 HP+100 0,返回\n\n\n");
choose_number=1;
while(choose_number)
{
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
if(money<25)
{
printf("钱不够!");
}
else
{
if(player.health+20<=player.max_health)
{
printf("HP+20.");
money-=25;
player.health+=20;
}
else
{
printf("HP满了");
player.health=player.max_health;
}
}
break;
case 2:
if(money<80)
{
printf("钱不够!");
}
else
{
if(player.health+50<=player.max_health)
{
printf("HP+50.");
money-=80;
player.health+=50;
}
else
{
printf("HP满了");
player.health=player.max_health;
}
}
break;
case 3:
if(money<150)
{
printf("钱不够!");
}
else
{
if(player.health+100<=player.max_health)
{
printf("HP+100.");
money-=150;
player.health+=100;
}
else
{
printf("HP满了");
player.health=player.max_health;
}
}
break;
case 0:
printf("下次再来!\n");
break;
default:
printf("输入错误\n\n\n");
break;
}
break;
}
break;
case 4:
printf("你要干什么?\n\n\n 1,买东西 2,聊天 \n\n\n");
int mai;
scanf("%d",&mai);
if(mai==1)
{
printf("买点什么呢?\n1,止血草¥100 HP+60\n2,急救包¥150 HP+80 \n3,云南白药¥250 HP+120\n4,超级云南白药¥400 HP+200 \n5,经验草¥150 经验+300 \n6,经验包¥600 经验+600\n7,经验石¥500 经验+1000 \n0,拜拜\n");
int dongxi;
scanf("%d",&dongxi);
switch(dongxi)
{
case 1:
if(money>=100&&cao<6)
{
cao++;
money=money-100;
printf ("止血草+1\n");
}
else
{
printf("钱不够!\n");
}
break;
case 2:
if(money>=150&&jijiubao<5)
{
jijiubao++;
money=money-150;
printf ("急救包+1\n");
}
else
{
printf("钱不够!\n");
}
break;
case 3:
if(money>=250&&baiyao<4)
{
baiyao++;
money=money-250;
printf ("云南白药+1\n");
}
else
{
printf("钱不够!\n");
}
break;
case 4:
if(money>=400&&superbaiyao<3)
{
superbaiyao++;
money=money-400;
printf ("超级云南白药+1\n");

}
else
{
printf("钱不够!\n");
}
break;
case 5:
if(money>=150)
{
jingyancao++;
money=money-150;
printf ("经验草+1\n");
}
else
{
printf("钱不够!\n");
}
break;
case 6:
if(money>=300)
{
jingyanbao++;
money=money-300;
printf ("经验包+1\n");
}
else
{
printf("钱不够!\n");
}
break;
case 7:
if(money>=500)
{
jingyanshi++;
money=money+500;
printf ("经验石+1\n");
}
else
{
printf("钱不够!\n");
}
break;
}
case 0:
printf("金币:%d\n",money);
printf("再见,欢迎下次再来!\n");
break;

}
if(mai==2)
{
printf("药品商人:去去去,老子没时间陪你聊。\n");
}
}
}
else if(place_sign==place.hotel)
printf("“老板娘!我...”\n\n\n“我忙着呢,没空理你~”\n\n\n");
else printf("这里好像没人可以聊天\n\n\n");
break;
case 4:
DisplayState();
break; //显示状态
case 5: //装备
printf("攻击装备: 1,匕首:%d个 2,长剑:%d个 3,碧血剑:%d个 4,绝世好剑:%d个\n\n\n",gong1n,gong2n,gong3n,gong4n);
printf("防御装备: 5,布衣:%d个 6,铁甲:%d个 7,银甲:%d个 8,黄金圣衣:%d个\t\t0,返回\n\n\n",fang1n,fang2n,fang3n,fang4n);
printf("选择要装备的武器或防具:\n\n\n");
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
if(gong1n>=1)
{
printf("拿起了匕首\n\n\n");
gong=gong1;
strcpy(gongname,"匕首");
}
else printf("你没有匕首可以装备\n\n\n");
break;
case 2:
if(gong2n>=1)
{
printf("拿起了长剑\n\n\n");
gong=gong2;
strcpy(gongname,"长剑");
}
else printf("你没有长剑可以装备\n\n\n");
break;
case 3:
if(gong3n>=1)
{
printf("拿起了碧血剑\n\n\n");
gong=gong3;
strcpy(gongname,"碧血剑");
}
else printf("你没有碧血剑可以装备\n\n\n");
break;
case 4:
if(gong4n>=1)
{
printf("拿起了绝世好剑\n\n\n");
gong=gong4;
strcpy(gongname,"绝世好剑");
}
else printf("你没有绝世好剑可以装备\n\n\n");
break;
case 5:
if(fang1n>=1)
{
printf("穿上了布衣\n\n\n");
fang=fang1;
strcpy(fangname,"布衣");
}
else printf("你没有布衣可以装备\n\n\n");
break;
case 6:
if(fang2>=1)
{
printf("穿上了铁甲\n\n\n");
fang=fang2;
strcpy(fangname,"铁甲");
}
else printf("你没有铁甲可以装备\n\n\n");
break;
case 7:
if(fang3n>=1)
{
printf("穿上了银甲\n\n\n");
fang=fang3;
strcpy(fangname,"银甲");
}
else printf("你没有银甲可以装备\n\n\n");
break;
case 8:
if(fang4n>=1)
{
printf("穿上了黄金圣衣\n\n\n");
fang=fang4;
strcpy(fangname,"黄金圣衣");
}
else printf("你没有黄金圣衣可以装备\n\n\n");
break;
case 0:
printf("未更换装备\n\n\n");
break;
default:
printf("change error!");
}
break;
case 6:
printf(" 您好,欢迎您玩苍穹世界。为了给您更好的游戏体验,本团队时不时会优化本游戏,优化后会尽快发布在网上。关于外挂方面,开启外挂的方式是设定勇者姓名时,输入“圣战斗士 ”(不包括双引号)。由于2.0版本的buy,我们在2.0的基础上进行修改,已修复该buy。并且新增了经验草等有助于升级的道具,希望大家喜欢。在这里要感谢离陌同学,他给了我们许多宝贵的建议,谢谢。\n");
break;
case 0:
printf("确定退出游戏?(Y/N)\n\n\n");
getchar();
proof=getchar();
if(proof=='y'||proof=='Y')
{
printf("数据存储中...");
//向文件中更新数据;
getchar();
printf("按回车退出");
getchar();
return;
}
else if(proof=='n'||proof=='N')printf("继续游戏!\n\n\n");
else printf("继续!\n\n\n");
break;
default:
printf("输入错误!\n\n\n");
}
}
}
void DisplayState()
{
printf("%s 攻击力:%d+%d=%d 防御力:%d+%d=%d HP:%d/%d \n\n\n",player.name,player.attack,gong,player.attack+gong,player.defense,fang,player.defense+fang,player.health,player.max_health);
printf("武器: %s 防具: %s \n\n\n",gongname,fangname);
printf("等级:%d 经验:%d/%d 还需要%d经验升级 金币:%d \n\n\n",player.level,player.exp,player.max_exp,player.max_exp-player.exp,money);
}
void BattleAct()
{
while(1)
{
puts("=============================================================================");
printf("要怎么办?\n\n\n 1,攻击 2,物品 3,查看状态 4,逃跑\n\n\n");
switch(scanf("%d",&choose_number),choose_number)
{
case 1:
s=SuiJi();
printf("%s攻击! %sHP减少%d\n\n\n",player.name,guai.name,player.attack+s+gong-guai.defense/3);
guai.health-=player.attack+s+gong-guai.defense/3;
if(AttackResult())return; //如果攻击有结果(敌人或玩家战死)退出函数
else continue;
case 2:
ChooseWupin();
break; //选择物品,可以使用,战斗中允许使用攻击性物品
case 3:
DisplayState();
break; //显示状态
case 4:
s=SuiJi();
if(s<4) //40%的概率可以逃跑
{
printf("%s逃跑了~\n\n\n",player.name);
battle=0;
return;
}
else printf("%s逃跑失败!\n\n\n",player.name);
break;
default:
printf("输入错误,重新输入!\n\n\n");
}
}
}
void printf(char *p)
{
while(1)
{
if(*p!=0)
printf("%c",*p++);
else
break;
Sleep(100);
}
}

吃豆人

原创

代码如下:

#include <stdio.h>
#include <iostream>
#include <time.h>
#include <conio.h>
#include <windows.h>      
#include <stdlib.h>         
#include <string.h>

using namespace std;

const int n = 809;

struct Point {
    int x, y;
};

int dali;

int fx[4] = {-1, 27, 1, -27};
int fxfx[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
int dis[1000][1000]; //0:墙 1:有分的路 2:没分的路 3:怪物的家 
int changdi[30][27] = {
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
    {0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
    {0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
    {0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
    {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
    {0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0},
    {0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1 ,0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 3, 3, 3, 3, 3, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 0, 3, 3, 3, 3, 3, 0, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 3, 3, 3, 3, 3, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
    {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
    {0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
    {0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
    {0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0},
    {0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0},
    {0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0},
    {0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0},
    {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
    {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
    {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

int x, x1, x2, x3, x4, y, y1, y2, y3, y4;
int now, now1, now2, now3, now4;
int g1, g2, g3, g4;
int fangx, nextfx, last1, last2, last3, last4;
int fenshu, guozi, guaitimer;
int T1, T2, t1, t2, stopped; //T:计时 t1:玩家速度 t2:怪物速度 
int f; //f:{0:继续 1:被吃 2:赢了 3:输了}
int beichi;

void color (int a) {//颜色函数
    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), a);
}

void gotoxy(int x, int y) {//位置函数(行为x 列为y)
    COORD pos;
    pos.X=2*y;
    pos.Y=x;
    SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE),pos);
}

void begin () {
    system ("cls");
    color (11); 
    printf ("       ★"); 
    color (10); 
    printf ("吃豆人"); 
    color (11); 
    printf ("★\n\n"); 
    color(7);
    printf ("  正在初始化,请耐心等待"); 
    for (int i = 0; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dis[i][j] = 900;
        }
    }  
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= 3; j++) {
            if (i + fx[j] >= 0 && i + fx[j] <= n) {
                int k = i + fx[j], xx = k/27, yy = k % 27, kk;
                if (changdi[i / 27][i % 27] && changdi[xx][yy]) {
                    dis[i][k] = kk = 1;
                }
            }
        }
    }
    for (int k = 0; k <= n; k++) {
        if (changdi[k]) {
            for (int i = 0; i <= n; i++) {
                if (changdi[i]) {
                    for (int j = 0; j <= n; j++) {
                        if (changdi[j]) {
                            if (dis[i][j] > dis[i][k] + dis[k][j]) {
                                dis[i][j] = dis[i][k] + dis[k][j];
                            }
                        }
                    }
                }
            }
            if (k % 80 == 0) {  
                color (13); 
                gotoxy (3, 12); 
                printf("│");
            } else if (k % 80 == 20) {
                    color (13); 
                    gotoxy (3, 12); 
                    printf ("╱");
            } else if (k % 80 == 40) {
                color (13); 
                gotoxy (3, 12); 
                printf ("─");
            } else if (k % 80 == 60) {
                color(13); 
                gotoxy (3, 12); 
                printf ("╲");
            }
            if (k % 60==0) {
                color (11); 
                gotoxy (5, k / 60); 
                printf("●");
            }
        }
    }
}

void shuru () {
    char ch = getch ();
    if (ch == 75) {
        if (changdi[x + fxfx[0][0]][y + fxfx[0][1]] == 1 | changdi[x + fxfx[0][0]][y + fxfx[0][1]] == 2) {
            fangx = nextfx = 0;
        } else {
            nextfx = 0;
        }
    } else if (ch == 80) {
        if (changdi[x + fxfx[1][0]][y + fxfx[1][1]] == 1 | changdi[x + fxfx[1][0]][y + fxfx[1][1]] == 2) {
            fangx = nextfx = 1;
        } else {
            nextfx = 1;
        } 
    } else if (ch == 77) {
        if (changdi[x + fxfx[2][0]][y + fxfx[2][1]] == 1 | changdi[x + fxfx[2][0]][y + fxfx[2][1]] == 2) {
            fangx = nextfx = 2;
        } else {
            nextfx = 2;
        }
    } else if (ch == 72) {
        if (changdi[x + fxfx[3][0]][y + fxfx[3][1]] == 1 | changdi[x + fxfx[3][0]][y + fxfx[3][1]] == 2) {
            fangx = nextfx = 3;
        } else {
            nextfx = 3;
        }
    } else if (ch == ' ') {
        stopped = (stopped + 1) % 2;
    } else if (ch == '-') {
        t1++;
    } else if ((ch == '+') && t1 - 1 > 0) {
        t1--;
    }  
    else if (ch == '$') {
        dali = (dali + 1) % 2;
    }
}

void reset () {
    system ("cls"); 
    color (7);
    x = 22; 
    y = 13; 
    x1 = x2 = x3 = x4 = 14; 
    y1 = 11; 
    y2 = 12; 
    y3 = 14; 
    y4 = 15;
    now = 607; 
    now1 = 389; 
    now2 = 390; 
    now3 = 392; 
    now4 = 393;
    for (int k = 0; k <= n; k++) {
        int i = k / 27, j = k % 27;
        gotoxy (i, j);
        if (changdi[i][j] == 1) {   
            color (7); 
            printf("·");
        } else if (!changdi[i][j]) {
            color (1); 
            printf ("■");
        }
        if (j==26) {
            gotoxy (i, 27); 
            color (7); 
            printf ("%d", i);
        }
    }
    gotoxy (0, 0);
    gotoxy (x, y); 
    color (14); 
    printf ("●");
    gotoxy (x1, y1); 
    color (4); 
    printf ("◆");
    gotoxy (x2, y2); 
    color (5); 
    printf ("◆");
    gotoxy (x3, y3); 
    color (3); 
    printf ("◆");
    gotoxy (x4, y4); 
    color (2); 
    printf ("◆");
    fangx = 0; 
    T1 = T2 = guaitimer = 0; 
    t1 = 75; 
    t2 = 100;
    stopped = 0; 
    fenshu = 0; 
    guozi = 237; 
    g1 = g2 = g3 = g4 = 0; 
    dali = 0;
    gotoxy (14, 30); 
    printf ("  ");
}

void move1 () {
    int xx, yy;
    xx = x + fxfx[nextfx][0]; 
    yy = y + fxfx[nextfx][1];
    if (changdi[xx][yy]) {
        if (changdi[xx][yy] == 1) {
            fenshu++; 
            changdi[xx][yy] = 2;
        }
        color (14);
        gotoxy (x, y); 
        printf ("  ");
        gotoxy (xx, yy); 
        if (!dali) {
            printf ("♀");
        } else {
            printf ("☆");
        }
        now = x * 27 + y; 
        x = xx; 
        y = yy;
        fangx = nextfx;
    } else {
        if (x == 13 && y == 0 && fangx == 0) {
            xx = x; 
            yy = 26;
        } else if (x == 13 && y == 26 && fangx == 2) {
            xx = x; 
            yy = 0;
        } else {
            xx = x + fxfx[fangx][0]; 
            yy = y + fxfx[fangx][1];
        }
        if (changdi[xx][yy]) {
            if (changdi[xx][yy] == 1) {
                fenshu++; 
                changdi[xx][yy] = 2;
            }
            color (14);
            gotoxy (x, y); 
            printf ("  ");
            gotoxy (xx, yy); 
            if (!dali) {
                printf ("♀");
            } else {
                printf ("☆");
            }
            now = x * 27 + y; 
            x = xx; 
            y = yy;
        }
    }
    color (7);
}

void move2 () {
    int haha, minhaha, xx, yy, chi = 0;
    if (g1) {
        minhaha = 2147483647; //相当于INT_MAX 
        if (now1 % 27 == 0 | now1 % 27 == 26) {
            haha = last1;
        } else if (!dali) {
            for (int i = 0; i <= 3; i++) {
                if (changdi[(now1 + fx[i]) / 27][(now1 + fx[i]) % 27] && i != last1 && 
                minhaha > dis[now1 + fx[i]][now]) {
                    minhaha = dis[now1 + fx[i]][now]; 
                    haha = i;
                }
            }
        } else {
            minhaha = -minhaha;
            for (int i = 0; i <= 3; i++) {
                if (changdi[(now1 + fx[i]) / 27][(now1 + fx[i]) % 27] && i != last1 && 
                minhaha < dis[now1 + fx[i]][now]) {
                    minhaha = dis[now1 + fx[i]][now]; 
                    haha = i;
                } 
            }
        }
        xx = now1 / 27; 
        yy = now1 % 27; 
        gotoxy (xx, yy); 
        if (changdi[xx][yy] == 1)  {
            printf ("·");
        } else {
            printf ("  "); 
        }
        now1 += fx[haha]; 
        last1 = (haha + 2) % 4;
        xx = now1 / 27; 
        yy = now1 % 27; 
        gotoxy (xx, yy); 
        color (4); 
        printf ("◆"); 
        color (7);
        if (xx == x && yy == y) {
            if (!dali) {
                chi++;
            } else {
                guozi += 50;
                fenshu += 50;
                last1 = 0;
                gotoxy (now1 / 27, now1 % 27); 
                if (changdi[now1 / 27][now1 % 27] == 1) {
                    printf ("·");
                } else {
                    printf ("  ");
                }
                now1 = 389;
            }
        }
    }
    if (g2) {
        int k;
        minhaha = 2147483647;
        if (fangx == 0 | fangx == 2) {
            k = y + (fxfx[fangx][1]) * 3;
            while (k > 25 | !changdi[x][k]) {
                k--;    
            }
            while (k < 1 | !changdi[x][k]) {
                k++;
            }
        } else {
            k = x + (fxfx[fangx][0]) * 3;
            while (k > 28 | !changdi[k][y]) {
                k--;
            }
            while (k < 1 | !changdi[k][y]) {
                k++;    
            }
        } 
        if (fangx == 0 | fangx == 2) {
            k += x * 27;
        } else {
            k = k * 27 + y;
        }
        if (now2 % 27 == 0 | now2 % 27 == 26)  {
            haha = last2;
        }
        else if (!dali) {
            for (int i = 0; i <= 3; i++) {
                if (changdi[(now2 + fx[i]) / 27][(now2 + fx[i]) % 27] && i != last2 && 
                minhaha > dis[now2 + fx[i]][k]) {
                    minhaha = dis[now2 + fx[i]][k]; 
                    haha = i;
                }
            }   
        } else {
            minhaha = -minhaha;
            for (int i = 0; i <= 3; i++) { 
                if (changdi[(now2 + fx[i]) / 27][(now2 + fx[i]) % 27] && i != last2 && 
                minhaha < dis[now2 + fx[i]][k]) {
                    minhaha = dis[now2 + fx[i]][k]; 
                    haha = i;
                }
            }   
        }
        xx = now2 / 27; 
        yy = now2 % 27; 
        gotoxy (xx, yy); 
        if (changdi[xx][yy] == 1) {
            printf ("·");
        } else {
            printf ("  "); 
        }
        now2 += fx[haha]; 
        last2 = (haha + 2) % 4; 
        gotoxy (18, 30);
        xx = now2 / 27; 
        yy = now2 % 27; 
        gotoxy (xx, yy); 
        color (5); 
        printf ("◆"); 
        color (7);
        if (xx == x && yy == y) {
            if (!dali) {
                chi++;
            } else {
                guozi += 50;
                fenshu += 50;
                last2 = 0;
                gotoxy (now2 / 27, now2 % 27); 
                if (changdi[now2 / 27][now2 % 27] == 1) {
                    printf ("·");
                } else {
                    printf ("  ");
                }
                now2 = 390;
            }
        }
    }
    if (g3) {
        int k;
        minhaha = 2147483647;
        if (fangx == 0 | fangx == 2) {
            k = y + (fxfx[(fangx + 1) % 4][1]) * 3;
            while (k > 25 | !changdi[x][k]) {
                k--;    
            }
            while (k < 1 | !changdi[x][k]) {
                k++;
            }
        } else {
            k = x + (fxfx[(fangx + 1) % 4][0]) * 3;
            while (k > 28 | !changdi[k][y]) {
                k--;    
            }
            while (k < 1 | !changdi[k][y]) {
                k++;
            } 
        } 
        if (fangx == 0 | fangx == 2) {
            k += x * 27;
        } else {
            k = k * 27 + y;
        }
        if (now3 % 27 == 0 | now3 % 27 == 26) {
            haha = last3;
        } else if (!dali) {
            for (int i = 0; i <= 3; i++) {
                if (changdi[(now3 + fx[i]) / 27][(now3 + fx[i]) % 27] && i != last3 &&
                minhaha > dis[now3 + fx[i]][k]) {
                    minhaha = dis[now3 + fx[i]][k]; 
                    haha = i;
                }
            }
        } else {
            minhaha = -minhaha;
            for (int i = 0; i <= 3; i++) {
                if (changdi[(now3 + fx[i]) / 27][(now3 + fx[i]) % 27] && i != last3 && 
                minhaha < dis[now3 + fx[i]][k]) {
                    minhaha = dis[now3 + fx[i]][k]; 
                    haha = i;
                }
            }   
        }   
        xx = now3 / 27; 
        yy = now3 % 27; 
        gotoxy (xx, yy); 
        if (changdi[xx][yy] == 1) {
            printf ("·");
        } else {
            printf ("  ");
        } 
        now3 += fx[haha]; 
        last3 = (haha + 2) % 4; 
        gotoxy (18, 30);
        xx = now3 / 27; 
        yy = now3 % 27;         
        gotoxy (xx, yy); 
        color (3); 
        printf ("◆"); 
        color (7);
        if (xx == x && yy == y) {
            if (!dali) {
                chi++;              
            } else {
                guozi += 50;
                fenshu += 50;
                last3 = 0;
                gotoxy (now3 / 27, now3 % 27); 
                if (changdi[now3 / 27][now3 % 27] == 1) {
                    printf ("·");
                } else {
                    printf ("  ");  
                }
                now3 = 341;
            }
        }
    }
    if (chi) {
        beichi++;
    }
}

int main () {
    MessageBox (NULL, "欢迎来到吃豆人游戏!", "温馨提示", MB_OK);
    begin ();
    int jixu = 1;
    reset ();
    string bb[4] = {"●", "①", "②" ,"③"}; 
    color (7);
    gotoxy (12, 12); 
    printf ("倒计时"); 
    color (12);
    for (int i = 3; i >= 0; i--) {
        if (i==0) {
            color (11); 
        }
        gotoxy (13, 13); 
        cout << bb[i]; 
        Sleep (1000);
    }
    gotoxy (12, 12); 
    printf ("      "); 
    gotoxy (13, 13); 
    printf (" "); 
    while (!f) {
        Sleep (1);
        gotoxy (7, 30); 
        color (3);
        printf ("得分:%d   ", fenshu);
        gotoxy (3, 30); 
        printf ("怪物速度:%d   ", 300 - t2);
        gotoxy (5, 30); 
        printf ("你的速度:%d   ", 300 - t1);
        gotoxy (9, 30); 
        printf ("被吃次数:%d ", beichi);
        gotoxy (11, 30);
        printf ("控制小人行走:方向键");
        gotoxy (13, 30);
        printf ("复活次数:无限次");
        gotoxy (15, 30);
        printf ("小人加速/减速:'+'/'-'");
        gotoxy (17, 30);
        printf ("大力丸(怪物们不会主动吃您):$键"); 
        gotoxy (20, 10);
        color (4);
        if (kbhit ()) {
            shuru ();
        } 
        if (stopped) {
            continue;
        } 
        T1 = (T1 + 1) % t1; 
        T2 = (T2 + 1) % t2;
        if (T1 % t1 == 0 && now + fx[fangx] > 0 && now + fx[fangx] < n) {
            move1 ();
        } 
        if (T2 % t2 == 0) {
            if (guaitimer <= 8) {
                if (guaitimer==0) {
                    g1 = 1;
                } 
                if (guaitimer == 8) {
                    g2 = 1; 
                } 
                guaitimer++;
            }
            if (!g3 && fenshu >= 30) {
                g3 = 1; 
            }
            move2 ();    
        }
        if (fenshu == guozi) {
            f=2;
        }
    }
    if (f == 2) {
        Sleep (3000);
        system ("cls");
        gotoxy (10, 20);
        color (3);
        string str = "恭喜您吃完了所有豆子!";
        for (int i = 0; i < str.size (); i++) {
            Sleep (80);
            cout << str[i];
        }
        Sleep (2000);
        gotoxy (12, 20);
        str = "您一共被怪物吃掉了";
        for (int i = 0; i < str.size (); i++) {
            Sleep (80);
            cout << str[i];
        }
        Sleep (80); 
        cout << beichi; 
        Sleep (80);
        cout << "次";
        Sleep (80);
        cout << "!";
        gotoxy (14, 20);
        Sleep (2000);
    }
}

地下城大战

原创

代码如下:

#include<bits/stdc++.h>
#include<cstdlib>
#include<windows.h>
#include<conio.h>
#include<string>
using namespace std;
char  a[14][100]= {"| S小怪 M大怪 O入口 0你的位置 R小资源 H大资源 @传送门 -道路 |道路 X回血|",
                   "|                                                                      |",
                   "|      |S-R-X|-H--M-S-R-S-S-H-M-X---|      |-X|-S--H-|                 |",
                   "|      M     |       |              |-S|R--|  |      |                 |",
                   "|      |     |-M--X--|H-R-|     |-S-|  |      |-M--H--|                |",
                   "|      S--R--|       |    |--M-R|      |-S|-X-|       |--@             |",
                   "| O0-|-----|R-M-S--|R-M-|--X--|S-S-M-|X-|-R-|-H-M-M---|                |",
                   "|      M--S|-|-X-R---|    |-|S-H|    |    |   |       |                |",
                   "|      S   |      |  |      |        |-X-|-M-|-H-|-MH-|                |",
                   "|      H   |-S--R-|-S--X-R--|-S-M--H-|       |        |                |",
                   "|      X   |                         |    |H-|X-H-H-M-|                |",
                   "|      R--M|S---H---M--M-X-|-S-M-R--S|-M-H|                            |",
                   "|                                                                      |"};
char  b[14][100]= {"| S小怪 M大怪 O入口 0你的位置 R小资源 H大资源 @传送门 -道路 |道路 X回血|",
                   "|                                                                      |",
                   "|      |S-R-X|-H--M-S-R-S-S-H-M-X---|      |-X|-S--H-|                 |",
                   "|      M     |       |              |-S|R--|  |      |                 |",
                   "|      |     |-M--X--|H-R-|     |-S-|  |      |-M--H--|                |",
                   "|      S--R--|       |    |--M-R|      |-S|-X-|       M--@             |",
                   "| O0-|-----|R-M-S--|R-M-|--X--|S-S-M-|X-|-R-|-H-M-M-M-|                |",
                   "|      M--S|-|-X-R---|    |-|S-H|    |    |   |       |                |",
                   "|      S   |      |  |      |        |-X-|-M-|-H-|-MH-|                |",
                   "|      H   |-S--R-|-S--X-R--|-S-M--H-|       |        |                |",
                   "|      X   |                         |    |H-|X-H-H-M-|                |",
                   "|      R--M|S---H---M--M-X-|-S-M-R--S|-M-H|                            |",
                   "|                                                                      |"};
int c;        
int shang_hai=5;
int sheng_ming=5;
int sheng_ming_zhi=5;
int jb=0;
string xing_ming;
int l,j;
void game_2();
void player_zi_liao();
void player();
void shop();
void zi_mu_1();
void q();
void game_1();
void s_1();
void shi_bai();
void da_guai();
void xiao_guai();
void xiao_ziyuan();
void da_ziyuan();
void hui_xue();
void sheng_li();
//S小怪 M大怪 O入口 0你的位置 R小资源 H大资源 @传送门 -道路 |道路 X回血

void sheng_li()
{
    q();
    cout<<"恭喜你,通关了!\n";
    cout<<"下次更新 英雄 技能 道具 更多战斗系统 更多武器和防具 任务系统 经验与等级 材料(合成神器)....提前预告:下个版本 小镇2.0新的危机\n";
    system("pause");
}

void hui_xue()
{
    q();
    if(sheng_ming<sheng_ming_zhi)
    {
        sheng_ming++;
        cout<<"生命值加1";
    }
    else
    {
        cout<<"生命值已满,回不了血";
    }
        Sleep(1000);
    q();
}

void xiao_ziyuan()
{
    q(); 
    cout<<"恭喜你获得 金币+2";
    jb+=2;
    Sleep(1000);
    q(); 
}

void da_ziyuan()
{
    q();
    cout<<"恭喜你获得 金币+5";
    jb+=5;
    Sleep(1000);
    q(); 
}

void da_guai()
{
    q();
    int bguai_xue;
    cout<<"即将跟大怪物战斗\n";
    bguai_xue=12;
    while(1)
    {
        if(sheng_ming<=0)
        {
            cout<<"你被大怪打死了";
                Sleep(1000);
            q();
            break;
        }
        if(bguai_xue<=0)
        {
            cout<<"你打败了大怪";
                Sleep(1000);
            q();
            break;  
        }
        cout<<"\n1.攻击 2.防御(暂时获得1血)\n";
        cout<<"请输入:";
        cin>>c;
        if(c==1)
        {
            bguai_xue-=shang_hai;
            cout<<"\n成功造成攻击\n";
            Sleep(500);
            cout<<"你受到伤害*5";
            sheng_ming-=5;
        }
        if(c==2)
        {
            sheng_ming++;
            sheng_ming-=5;
        } 
        cout<<"\n你的生命:"<<sheng_ming;
        cout<<"\n怪物的生命:"<<bguai_xue; 
}
}

void xiao_guai()
{
    q();
    int sguai_xue;
    cout<<"即将跟小怪物战斗\n";
    sguai_xue=6;
    while(1)
    {
        if(sheng_ming<=0)
        {
            cout<<"你被小怪打死了";
                Sleep(1000);
            q();
            break;
        }
        if(sguai_xue<=0)
        {
            cout<<"你打败了小怪";
                Sleep(1000);
            q();
            break;  
        }
        cout<<"\n1.攻击 2.防御(暂时获得1血)\n";
        cout<<"请输入:";
        cin>>c;
        if(c==1)
        {
            sguai_xue-=shang_hai;
            cout<<"\n成功造成攻击\n";
            Sleep(500);
            cout<<"你受到伤害*5";
            sheng_ming-=3;
        }
        if(c==2)
        {
            sheng_ming++;
            sheng_ming-=3;
        } 
        cout<<"\n你的生命:"<<sheng_ming;
        cout<<"\n怪物的生命:"<<sguai_xue; 
}
}

void s_1()
{
    cout<<"\t";
    cout<<"小镇";
    Sleep(500);
    cout<<"1.0";
    Sleep(500);
    cout<<"地下城";
    Sleep(1000);
    q();
    zi_mu_1();
}

void player_zi_liao()
{
    q();
    cout<<"\n    名字:"<<xing_ming<<endl;
    cout<<"    金币:"<<jb<<endl;
    cout<<"    伤害:"<<shang_hai<<endl;
    cout<<"    生命:"<<sheng_ming<<endl;
    Sleep(2000);
    game_1();
}

void player()
{
    q();
    cout<<"勇士,请输入你的姓名:";
    cin>>xing_ming;
    game_1();
}

void q()
{
    system("cls");
}

void zi_mu_1()
{
    cout<<"卡罗特小镇的资源一天天流逝,直到今天,资源严重缺乏,你身为勇士,不愿看到大家一个一个被饿死,决定去地下城闯一闯\n";
    system("pause");
    q();
    player();
}

void game_1()
{
    q();
    cout<<"\n";
    cout<<"\t\t小镇\n\n";
    cout<<"\t1.去地下城  2.铁匠铺  3.个人资料 4.查看下个版本更新内容 \n";
    cout<<"你选择:";
    cin>>l;
    if(l==2)
    {
        shop();
    }
    if(l==3)
    {
        player_zi_liao();
    }
    if(l==1)
    {
        game_2();
    }
    if(l==4)
    {
        cout<<"下次更新 英雄 技能 道具 更多战斗系统 更多武器和防具 任务系统 经验与等级 材料(合成神器)....提前预告:下个版本 小镇2.0新的危机\n";
        system("pause");
        game_1();
    }
}

void shi_bai()
{
    q();
    cout<<"你死亡了(装备不掉落)";
    Sleep(1000);
    q();
    game_1();
}

void game_2()
{
    q();
    int i,k;
    for(i=0;i<14;i++)
    for(k=0;k<100;k++)
    {
        a[i][k]=b[i][k];
    }
    int x=6,y=5; 
    for(i=0; i<=12; i++)
        cout<<a[i]<<endl;
    char ch;
    while(1) 
    {
        if(sheng_ming<=0)
        {
            shi_bai();
        }
        ch=getch();
        if(ch=='w')//S小怪 M大怪 O入口 0你的位置 R小资源 H大资源 @传送门 -道路 |道路 X回血
        {
            if(a[x-1][y]=='-'||a[x-1][y]=='|'||a[x-1][y]=='R'||a[x-1][y]=='S'||a[x-1][y]=='M'||a[x-1][y]=='H'||a[x-1][y]=='X') 
            {
                if(a[x-1][y]=='H')
                {
                    q();
                    da_ziyuan();
                    q();
                }
                if(a[x-1][y]=='R')
                {
                    q();
                    xiao_ziyuan();
                    q();
                }
                if(a[x-1][y]=='S')
                {
                    q();
                    xiao_guai();
                    q();
                }
                if(a[x-1][y]=='M')
                {
                    q();
                    da_guai();
                    q();
                }
                if(a[x-1][y]=='X')
                {
                    q();
                    hui_xue(); 
                    q();
                }
                if(a[x+1][y]!=' '||a[x-1][y]!=' ')
                {
                    a[x][y]='|';
                }
                else
                {
                    a[x][y]='-';
                }
                x--;
                a[x][y]='0';
                q();
                for(i=0; i<=12; i++)
        cout<<a[i]<<endl;
            }
        }
        if(ch=='s')//S小怪 M大怪 O入口 0你的位置 R小资源 H大资源 @传送门 -道路 |道路 X回血
        {
            if(a[x+1][y]=='-'||a[x+1][y]=='|'||a[x+1][y]=='R'||a[x+1][y]=='S'||a[x+1][y]=='M'||a[x+1][y]=='H'||a[x+1][y]=='X') 
            {
                if(a[x+1][y]=='H')
                {
                    q();
                    da_ziyuan();
                    q();
                }
                if(a[x+1][y]=='R')
                {
                    q();
                    xiao_ziyuan();
                    q();
                }
                if(a[x+1][y]=='S')
                {
                    q();
                    xiao_guai();
                    q();
                }
                if(a[x+1][y]=='M')
                {
                    q();
                    da_guai();
                    q();
                }
                if(a[x+1][y]=='X')
                {
                    q();
                    hui_xue(); 
                    q();
                }
                if(a[x+1][y]!=' '||a[x-1][y]!=' ')
                {
                    a[x][y]='|';
                }
                else
                {
                    a[x][y]='-';
                }
                x++;
                a[x][y]='0';
                q();
            for(i=0; i<=12; i++)
        cout<<a[i]<<endl;

            }
        }
        if(ch=='a')//S小怪 M大怪 O入口 0你的位置 R小资源 H大资源 @传送门 -道路 |道路 X回血
        {
            if(a[x][y-1]=='-'||a[x][y-1]=='|'||a[x][y-1]=='R'||a[x][y-1]=='S'||a[x][y-1]=='M'||a[x][y-1]=='H'||a[x][y-1]=='X') 
            {
                if(a[x][y-1]=='H')
                {
                    q();
                    da_ziyuan();
                    q();
                }
                if(a[x][y-1]=='R')
                {
                    q();
                    xiao_ziyuan();
                    q();
                }
                if(a[x][y-1]=='S')
                {
                    q();
                    xiao_guai();
                    q();
                }
                if(a[x][y-1]=='M')
                {
                    q();
                    da_guai();
                    q();
                }
                if(a[x][y-1]=='X')
                {
                    q();
                    hui_xue(); 
                    q();
                }
                if(a[x][y+1]!=' '||a[x][y-1]!=' ')
                {
                    a[x][y]='-';
                }
                else
                {
                    a[x][y]='|';
                }
                y--;
                a[x][y]='0';
                q();
            for(i=0; i<=12; i++)
        cout<<a[i]<<endl;

            }
        }
        if(ch=='d')//S小怪 M大怪 O入口 0你的位置 R小资源 H大资源 @传送门 -道路 |道路 X回血
        {
            if(a[x][y+1]=='-'||a[x][y+1]=='|'||a[x][y+1]=='R'||a[x][y+1]=='S'||a[x][y+1]=='M'||a[x][y+1]=='H'||a[x][y+1]=='X'||a[x][y+1]=='@') 
            {
                if(a[x][y+1]=='H')
                {
                    q();
                    da_ziyuan();
                    q();
                }
                if(a[x][y+1]=='@')
                {
                    q();
                    sheng_li();
                    break;
                }
                if(a[x][y+1]=='R')
                {
                    q();
                    xiao_ziyuan();
                    q();
                }
                if(a[x][y+1]=='S')
                {
                    q();
                    xiao_guai();
                    q();
                }
                if(a[x][y+1]=='M')
                {
                    q();
                    da_guai();
                    q();
                }
                if(a[x][y+1]=='X')
                {
                    q();
                    hui_xue(); 
                    q();
                }
                if(a[x][y+1]!=' '||a[x][y+1]!=' ')
                {
                    a[x][y]='-';
                }
                else
                {
                    a[x][y]='|';
                }
                y++;
                a[x][y]='0';
                q();
                for(i=0; i<=12; i++)
        cout<<a[i]<<endl;

            }
        }
}
}

void shop()
{
    q();
    cout<<"欢迎光临!\n";
    cout<<"请问你需要什么帮助? (注意:伤害和生命值是=关系,不是+的关系,比如你现在伤害是5,买了桃木剑是把伤害变为10,而不是加10)\n";
    cout<<"1.桃木剑    10伤害 20金币\n";
    cout<<"2.铁剑      15伤害 30金币\n";
    cout<<"3.黄金剑    30伤害 40金币\n";
    cout<<"4.钻石剑    40伤害 50金币\n";
    cout<<"5.x光剑     50伤害 100金币\n";
    cout<<"6.皮革甲    10生命 20金币\n";
    cout<<"7.铁甲      15生命 30金币\n";
    cout<<"8.黄金甲    30生命 40金币\n";
    cout<<"9.钻石甲    40生命 50金币\n";
    cout<<"10.激光披风 50生命 100金币\n";
    cout<<"11.返回\n";
    cout<<"请输入:";
    cin>>j;
    if(j==1||j==6)
    {
        if(jb>=20)
        {
            jb-=20;
            if(j==1)
            shang_hai=10;
            else
            {
                sheng_ming=10;
            sheng_ming_zhi=10;
            }
            game_1();
        }
        else
        cout<<"金币不够"; 
        game_1();
    }
    if(j==2||j==7)
    {
        if(jb>=30)
        {
            jb-=20;
            if(j==2)
            shang_hai=15;
            else
            {
                sheng_ming=15;
            sheng_ming_zhi=15;
            }
            game_1();
        }
        else
        cout<<"金币不够"; 
        game_1();
    }
    if(j==3||j==8)
    {
        if(jb>=40)
        {
            jb-=40;
            if(j==3)
            shang_hai=30;
            else
            {
                sheng_ming=30;
            sheng_ming_zhi=30;
            }
            game_1();
        }
        else
        cout<<"金币不够"; 
        game_1();
    }
    if(j==4||j==9)
    {
        if(jb>=50)
        {
            jb-=50;
            if(j==4)
            shang_hai=40;
            else
            {
                    sheng_ming=40;
            sheng_ming_zhi=40;
            }
            game_1();
        }
        else
        cout<<"金币不够"; 
        game_1();
    }
    if(j==5||j==10)
    {
        if(jb>=100)
        {
            jb-=100;
            if(j==5)
            shang_hai=50;
            else
            {
                sheng_ming=50;
            sheng_ming_zhi=50;  
            }
            game_1();
        }
        else
        cout<<"金币不够"; 
        game_1();
    }
    if(j==11)
    {
        game_1();
    }
}

int main()
{
    system("title 小镇1.0地下城");
    s_1();
    return 0;
}

斗地主

原创

代码如下:

#include<bits/stdc++.h>
#define PLAYERCOUNT 3
#define CARDSCOUNT 54
#define CURRENTPLAYER 0
#define VALUECOUNT 17
#define ERROR -1

using namespace std;
const char toFigure[]="34567890JQKA 2YZ";
enum COLOR{  //花色显示ASCII: 3~6
    eHEART=3,//红桃
    eDIAMOND,//方片
    eCLUB,   //草花
    eSPADE   //黑桃
};

class Card;
class CardsType;
class CardGroup;
class Player;
class Landlords;
class LastCards;
bool makeChoice(string tip);
bool cmp(Card* a,Card* b);
class Card{
  public:
    char figure;
    COLOR color;
    int value;
    Card(char _figure,COLOR _color){
        figure=_figure;
        color=_color;
        value=calValue();
    }
    int calValue(){
        for(int i=0;toFigure[i];i++){
            if(toFigure[i]==figure){
                return i;
            }
        }
        return ERROR;
    }
    void print(){
        assert(value!=ERROR);
        if(figure=='Z'){
            cout<<"ZZ";
        }else if(figure=='Y'){
            cout<<"YY";
        }else{
            cout<<figure<<(char)color;
        }
        cout<<' ';
    }
};

class CardsType{
  public:
    //为了规范查找对应牌的方法
    //统一为3个参数cnt1、isContinuous、cnt2
    int typeId;
    string typeStr;
    int cnt1,cnt2;
    bool isContinuous;
    CardsType(){
        typeId=ERROR;
    }
    bool operator ==(const CardsType& other)const{
        return this->typeId==other.typeId;
    }
    void init(char* _typeStr,int _typeId,int _cnt1,bool _isContinuous,int _cnt2){
        cnt1=_cnt1;
        isContinuous=_isContinuous;
        cnt2=_cnt2;
        typeStr=_typeStr;
        typeId=_typeId;
    }
};

class CardGroup{
  public:
    vector<Card*> cards;
    CardsType type;
    void calType(){
        int i,n=cards.size();
        //init(typeStr,typeId,cnt1,isContinuous,cnt2)
        if(n==0){
            type.init("不出",14,0,0,0);
            return;
        }
        if(n==2&&cards[0]->value==15&&cards[1]->value==14){
            type.init("王炸",0,0,0,0);
            return;
        }
        //统计同点数牌有多少张
        int cntFlag[VALUECOUNT]={0};
        for(i=0;i<n;i++){
            cntFlag[cards[i]->value]++;
        }
        //统计点数最多和最少的牌
        int maxCnt=0,minCnt=4;
        for(i=0;i<VALUECOUNT;i++){
            if(maxCnt<cntFlag[i]){
               maxCnt=cntFlag[i];
            }
            if(cntFlag[i]&&minCnt>cntFlag[i]){
               minCnt=cntFlag[i];
            }
        }
        if(n==4&&maxCnt==4){
            type.init("炸dan",1,4,0,0);
            return;
        }
        if(n==1){
            type.init("单牌",2,1,0,0);
            return;
        }
        if(n==2&&maxCnt==2){
            type.init("对子",3,2,0,0);
            return;
        }
        if(n==3&&maxCnt==3){
            type.init("三张 ",4,3,0,0);
            return;
        }
        if(n==4&&maxCnt==3){
            type.init("三带一",5,3,0,1);
            return;
        }
        if(n==5&&maxCnt==3&&minCnt==2){
            type.init("三带一对",6,3,0,2);
            return;
        }
        if(n==6&&maxCnt==4){
            type.init("四带二",7,4,0,1);
            return;
        }
        if(n==8&&maxCnt==4&&minCnt==2){
            type.init("四带二",8,4,0,2);
            return;
        }
        if(n>=5&&maxCnt==1&&cards[0]->value==cards[n-1]->value+n-1){
            type.init("顺子",9,1,1,0);
            return;
        }
        if(n>=6&&maxCnt==2&&minCnt==2&&cards[0]->value==cards[n-1]->value+n/2-1){
            type.init("连对",10,2,1,0);
            return;
        }
        int fjCnt;//统计连续且大于3三张的牌
        for(i=0;i<VALUECOUNT &&cntFlag[i]<3;i++);
        for(fjCnt=0;i<VALUECOUNT &&cntFlag[i]>=3;i++,fjCnt++);
        if(fjCnt>1){
            if(n==fjCnt*3)
                type.init("飞机",11,3,1,0);
            else if(n==fjCnt*4)
                type.init("飞机",12,3,1,1);
            else if(n==fjCnt*5&&minCnt==2)
                type.init("飞机",13,3,1,2);
        }
    }
    void init(string inputStr, vector<Card*> &cardsHolded){
        this->cards.clear();
        //不出
        if(inputStr=="N"){
            this->calType();
            return;
        }
        int i,j;
        //输入合法性判断
        for(i=0;i<inputStr.size();i++){
            bool find=false;
            for(j=0;toFigure[j];j++){
                if(inputStr[i]==toFigure[j]){
                    find=true;
                    break;
                }
            }
            if(find==false){
                //输入字符不在toFigure中
                return;
            }
        }
        //查找手中有没有这些牌
        int visitFlag[20]={0};
        for(i=0;i<inputStr.size();i++){
            Card *find=NULL;
            for(j=0;j<cardsHolded.size();j++){
                if(!visitFlag[j]&&cardsHolded[j]->figure==inputStr[i]){
                    visitFlag[j]=1;
                    find=cardsHolded[j];
                    break;
                }
            }
            if(find){
                this->cards.push_back(find);
            }else{
                cout<<inputStr[i];
                cout<<"没有找到\t";
                this->cards.clear();
                return;
            }
        }//end for(i=0;i<inputStr.size();i++)
        this->arrange();
    }
    void init(vector<Card*> newCards){
        this->cards=newCards;
        this->arrange();
    }
    bool isCanBeat(CardGroup &cardGroup){
        if(cardGroup.type.typeStr=="王炸"){
            return false;
        }else if(this->type.typeStr=="王炸"){
            return true;
        }else if(cardGroup.type==this->type &&this->type.typeStr=="炸dan"){
            return value()>cardGroup.value();
        }else if(cardGroup.type.typeStr=="炸dan"){
            return false;
        }else if(this->type.typeStr=="炸dan"){
            return true;
        }else if(cardGroup.type==this->type &&this->cards.size()==cardGroup.cards.size()){
            return this->value()>cardGroup.value();
        }else{
            return false;
        }
    }
    int value(){
        //计算牌组权值
        int i;
        if(type.typeStr=="三带一"||type.typeStr=="三带一对"||type.typeStr=="飞机"){
            for(i=2;i<cards.size();i++){
                if(cards[i]->value==cards[i-2]->value){
                    return cards[i]->value;
                }
            }
        }
        if(type.typeStr=="四带二"){
            for(i=3;i<cards.size();i++){
                if(cards[i]->value==cards[i-3]->value){
                    return cards[i]->value;
                }
            }
        }
        return cards[0]->value;
    }
    void arrange(){
        //整理:排序、计算类型
        sort(this->cards.begin(),this->cards.end(),cmp);
        this->calType();
    }
};
class LastCards{
    static LastCards *lastCards;
  public:
    Player *player;
    CardGroup cardGroup;
    static LastCards* inst(){//单例模式
        if(lastCards==NULL){
            lastCards=new LastCards();
        }
        return lastCards;
    }
    vector<Card*> findCanBeatFrom(vector<Card*> &cardsHolded){
        //查找能打得过的牌
        int i,j,k,n=cardsHolded.size(),m=cardGroup.cards.size();
        string typeStr=cardGroup.type.typeStr;
        vector<Card*> ret;
        if(typeStr=="王炸"||n<m){
            //打不过,返回空数组
            return ret;
        }
        int value=cardGroup.value();
        //统计各点牌出现的次数
        int cntFlag[VALUECOUNT]={0};
        for(i=0;i<n;i++){
            cntFlag[cardsHolded[i]->value]++;
        }
        int continuousCount=1;
        if(cardGroup.type.isContinuous){
            continuousCount=m/(cardGroup.type.cnt1+cardGroup.type.cnt2);
        }
        bool findFirstFigure;
        //cout<<"continuousCount="<<continuousCount<<endl;
        for(i=value+1;i<VALUECOUNT;i++){
            findFirstFigure=true;
            for(j=0;j<continuousCount;j++){
                if(cntFlag[i-j]<cardGroup.type.cnt1){
                    findFirstFigure=false;
                    break;
                }
            }
            if(findFirstFigure){
                ret.clear();
                int firstFigure=i;
                //cout<<"查找"<<cardGroup.type.cnt1<<"个"<<firstFigure+3<<endl;
                for(k=0,j=0;k<cardsHolded.size() &&j<continuousCount;k++){
                    if(cardsHolded[k]->value==firstFigure-j){
                        for(int kk=0;j>=0&&kk<cardGroup.type.cnt1;kk++){
                            ret.push_back(cardsHolded[k+kk]);
                        }
                        j++;
                    }
                }
                if(cardGroup.type.cnt2>0){
                    int SecondFigures[5];
                    int SecondCount=continuousCount;
                    if(cardGroup.type.typeStr=="四带二")
                        SecondCount=2;
                    bool findSecondFigure=true;
                    for(j=0,k=-1;j<SecondCount &&findSecondFigure;j++){
                        findSecondFigure=false;
                        for(k++;k<VALUECOUNT;k++){
                            SecondFigures[j]=k;
                            if(cntFlag[k]>=cardGroup.type.cnt2 &&cntFlag[k]<cardGroup.type.cnt1){
                                findSecondFigure=true;
                                break;
                            }
                        }
                    }
                    if(findSecondFigure){
                        //cout<<"查找SecondFigure "<<cardGroup.type.cnt2<<"个"<<SecondFigures[0]+3<<endl;
                        //cout<<"SecondCount= "<<SecondCount<<endl;
                        //for(i=0;i<SecondCount;i++)cout<<"SecondFigures["<<i<<"]="<<SecondFigures[i]<<endl;
                        for(i=0;i<SecondCount;i++){
                            for(j=0;j<cardsHolded.size();){
                                if(cardsHolded[j]->value==SecondFigures[i]){
                                    for(k=0;k<cardGroup.type.cnt2;k++){
                                        //cout<<"添加"<<cardsHolded[j]->value+3<<endl;
                                        ret.push_back(cardsHolded[j+k]);
                                    }
                                    do{
                                        j++;
                                    }while(j<cardsHolded.size()&&cardsHolded[j]->value==SecondFigures[i]);
                                }else{
                                    j++;
                                }
                            }
                        }
                        return ret;
                    }//if(findSecondFigure)
                }//end if(cardGroup.type.cnt2>0)
                else{
                    return ret;
                }
            }//end if(findFirstFigure)
        }//end for(i=value+1;i<VALUECOUNT;i++)
        ret.clear();
        //没牌打得过时查找有没有炸dan
        if(typeStr!="炸dan"){
            for(i=cardsHolded.size()-1;i>=3;i--){
                if(cardsHolded[i]->value==cardsHolded[i-3]->value){
                    for(j=0;j<4;j++){
                        ret.push_back(cardsHolded[i-j]);
                    }
                    break;
                }
            }
        }
        return ret;  
    }//end vector<Card*> findCanBeatFrom()
};
LastCards* LastCards::lastCards = NULL;

class Player{
  public:
    string name;
    vector<Card*> cards;
    void arrange(){
        sort(cards.begin(),cards.end(),cmp);
    }
    void print(){
        cout<<this->name<<":\t";
        for(int i=0;i<cards.size();i++){
            cards[i]->print();
        }
        cout<<"["<<cards.size()<<"]\n";
    }
    vector<Card*> tip(){
        //提示功能,使自己最小一张连最长
        CardGroup ret;
        string temp;
        int j,k,m=cards.size();
        for(j=0;j<m;j++){
            temp="";
            for(k=j;k<m;k++){
                temp+=cards[k]->figure;
            }
            ret.init(temp,cards);
            if(ret.type.typeId!=ERROR){
                return ret.cards;
            }
        }
        ret.cards.clear();
        return ret.cards;
    }
    void chupai(CardGroup &cardGroup){
        //出牌
        cout<<this->name<<":\t";
        cout<<cardGroup.type.typeStr<<' ';
        for(int i=0;i<cardGroup.cards.size();i++){
            cardGroup.cards[i]->print();
            this->cards.erase(find(this->cards.begin(),this->cards.end(),cardGroup.cards[i]));
        }
        cout<<"\t["<<this->cards.size()<<"]\n";
        if(cardGroup.type.typeStr!="不出"){
            //记录到 LastCards 中
            LastCards::inst()->player=this;
            LastCards::inst()->cardGroup.init(cardGroup.cards);
        }
    }
};

class Landlords{
    Player *player[PLAYERCOUNT];
    bool finished,youWin,landlordWin;
    int landlordIndex;
    Card *cards[CARDSCOUNT];
  public:
    Landlords(){
        int i,j,k;
        for(i=0;i<PLAYERCOUNT;i++){
            this->player[i]=new Player();
        }
        //54张牌初始化
        for(k=i=0;i<14;i++){
            if(toFigure[i]==' '){
                continue;
            }
            for(COLOR color=eHEART;color<=eSPADE;color=(COLOR)(color+1)){
                this->cards[k++]=new Card(toFigure[i],color);
            }
        }
        this->cards[k++]=new Card('Y',eSPADE);
        this->cards[k]=new Card('Z',eHEART);
    }
    ~Landlords(){
        for(int i=0;i<PLAYERCOUNT;i++){
            delete this->player[i];
        }
        for(int i=0;i<CARDSCOUNT;i++){
            delete this->cards[i];
        }
    }
    void init(){
        player[CURRENTPLAYER]->name="Bice";
        player[1]->name="玩家2";
        player[2]->name="玩家3";
        finished=false;
        youWin=false;
        landlordWin=false;
        //抢地主
        landlordIndex=ERROR;
        while(landlordIndex==ERROR){
            srand((int)time(0));
            shuffle();
            landlordIndex=chooseLandlord();
        }
        cout<<player[landlordIndex]->name<<"\t成为地主\n\n";
        this->add3Cards();
        LastCards::inst()->player=player[landlordIndex];
    }
    void startGame(){
        string inputSrt;
        CardGroup inputCards;
        for(int iTurns=landlordIndex;!finished;iTurns++){
            if(iTurns>=PLAYERCOUNT){
                iTurns=0;
            }
            if(iTurns==CURRENTPLAYER){
                cout<<endl;
                player[iTurns]->print();
                cout<<"输入提示:Z=大王 Y=小王 0=10 输入可无序 :=不出 例如:JKQ0A9\n请出牌:\t";
                do{
                    cin>>inputSrt;
                    inputCards.init(inputSrt,player[iTurns]->cards);
                }while(check(&inputCards)==false);
            }else{
                if(player[iTurns]==LastCards::inst()->player){
                    //若是上次出牌的是自己,启用提示功能
                    inputCards.init(player[iTurns]->tip());
                }else{
                    //查找能打得过上家的牌
                    inputCards.init(LastCards::inst()->findCanBeatFrom(player[iTurns]->cards));
                }
            }
            player[iTurns]->chupai(inputCards);//出牌

            if(player[iTurns]->cards.size()==0){
                //玩家手中没牌了,游戏结束
                finished=true;
                landlordWin=iTurns==landlordIndex;
                if(landlordWin){
                    youWin=landlordIndex==CURRENTPLAYER;
                }else{
                    youWin=landlordIndex!=CURRENTPLAYER;
                }
            }
        }
        cout<<"\n_________________________ "<<(youWin?"You Win!":"You Lose!")<<" _________________________\n\n";
    }
    void add3Cards(){
        cout<<"地主3张牌:\t";
        for(int i=PLAYERCOUNT*17;i<CARDSCOUNT;i++){
            this->cards[i]->print();
            player[landlordIndex]->cards.push_back(cards[i]);
        }
        cout<<endl;
        player[landlordIndex]->arrange();
    }
    int chooseLandlord(){
        cout<<"\n_________________________ 抢地主 _________________________\n\n";
        int first=-1,last,cnt=0,i,j=rand()%PLAYERCOUNT;
        bool decision;
        for(i=0;i<PLAYERCOUNT;i++,j==2?j=0:j++){
            if(j==CURRENTPLAYER){
                decision=makeChoice("是否抢地主?(Y=抢/N=不抢):");
            }else{
                decision=rand()%2;
            }
            if(decision){
                cnt++;
                last=j;
                if(first==-1){
                    first=j;
                }
                cout<<this->player[j]->name<<"\t抢地主\n";
            }else{
                cout<<this->player[j]->name<<"\t没有抢\n";
            }
        }
        if(cnt==0){
            cout<<"没人抢,重新发牌\n";
            return ERROR;
        }
        if(cnt==1){
            //第一轮只有一人抢地主
            return first;
        }
        else{
            //最后一次争抢
            if(first==CURRENTPLAYER){
                decision=makeChoice("是否抢地主?(Y=抢/N=不抢):");
            }else{
                decision=rand()%2;
            }
            if(decision){
                cout<<this->player[first]->name<<"\t抢地主\n";
                return first;
            }else{
                cout<<this->player[first]->name<<"\t没有抢\n";
                return last;
            }
        }
    }
    void shuffle(){
        int i,j,k;    
        //洗牌
        for(i=0;i<CARDSCOUNT;i++){
            swap(this->cards[i],this->cards[rand()%CARDSCOUNT]);
        }

        //分牌
        for(k=i=0;i<PLAYERCOUNT;i++){
            this->player[i]->cards.clear();
            for(j=0;j<17;j++){
                this->player[i]->cards.push_back(this->cards[k++]);
            }
            this->player[i]->arrange();//整理
            this->player[i]->print();
        }
    }
    bool check(CardGroup *cardGroup){
        if(cardGroup->type.typeId==ERROR){
            cout<<"出牌错误,重新输入\n";
            return false;
        }else if(cardGroup->type.typeStr=="不出"){
            return true;
        }else if(LastCards::inst()->player!=player[CURRENTPLAYER]&&!cardGroup->isCanBeat(LastCards::inst()->cardGroup)){
            cout<<"打不过,重新输入\n";
            return false;
        }else{
            return true;
        }
    }
};

int main(){
    Landlords *landlords=new Landlords();
    do{
        landlords->init();//发牌、抢地主
        landlords->startGame();//游戏开始
    }while(makeChoice("\n是否继续游戏?(Y=继续/N=结束): "));
    delete landlords;
    return 0;
}

bool makeChoice(string tip){
    cout<<tip;
    string input;
    cin>>input;
    return input=="Y"||input=="y";
}

bool cmp(Card* a,Card* b){
    //比较两张牌大小
    if(a->value==b->value){
        return a->color>b->color;
    }else{
        return a->value>b->value;
    }
}

俄罗斯方块1

原创

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>

#define MOD 28
#define SIZE_N 19
#define SIZE_M 12

int cur_x,cur_y;
int score,mark,next,map[SIZE_N][SIZE_M],Gamespeed=300;

int shape[28][6]={
 {0,-1,0,-2,1,0}, {0,1,1,0,2,0}, {-1,0,0,1,0,2}, {0,-1,-1,0,-2,0},
 {0,-1,0,1,-1,0}, {0,1,1,0,-1,0}, {1,0,0,-1,0,1}, {1,0,-1,0,0,-1},
 {-1,1,0,1,1,0}, {0,-1,1,0,1,1}, {-1,0,0,-1,1,-1}, {-1,-1,-1,0,0,1},
 {-1,0,0,1,1,1}, {0,1,1,-1,1,0}, {-1,0,0,1,1,1}, {0,1,1,-1,1,0},
 {-1,0,0,-1,0,-2}, {-1,0,-2,0,0,1}, {0,1,0,2,1,0}, {0,-1,1,0,2,0},
 {0,1,1,0,1,1}, {0,-1,1,0,1,-1}, {-1,0,0,-1,-1,-1}, {-1,0,-1,1,0,1},
 {0,1,0,2,0,3}, {1,0,2,0,3,0}, {0,-1,0,-2,0,-3}, {-1,0,-2,0,-3,0}
};

void gotoxy(int x,int y){
 COORD c;
 c.X=x-1; c.Y=y-1;
 SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void Gameover(){
 int i,j,flag=0;
 for(j=1;j<SIZE_M-1;j++){
 if(map[1][j]!=0){
  flag=1;break;
 }
 }
 if(flag==1){
 for(i=1;i<SIZE_N-1;i++){
  gotoxy(2,i+1);
  for(j=1;j<SIZE_M-1;j++){
  printf("□");
  }puts("");
 }
 gotoxy(7,9);
 printf("GAME OVER!");
 gotoxy(1,SIZE_N+1);
 exit(0);
 }
}
void ShowMap(int id){
 int i,j;
 gotoxy(1,1);
 if(id!=-1){
 for(i=0;i<SIZE_N;i++){
  for(j=0;j<SIZE_M;j++){
  if(i==0&&j==0 || i==0&&j==SIZE_M-1 || j==0&&i==SIZE_N-1 || j==SIZE_M-1&&i==SIZE_N-1)printf(" ");
  else if(i==0 || i==SIZE_N-1)printf("--");
  else if(j==0 || j==SIZE_M-1)printf("|");
  else if(map[i][j]==2) printf("■");
  else if(i==cur_x+shape[id][0] && j==cur_y+shape[id][1] ||
   i==cur_x+shape[id][2] && j==cur_y+shape[id][3] ||
   i==cur_x+shape[id][4] && j==cur_y+shape[id][5] ||
   i==cur_x && j==cur_y)
   printf("■");
  else if(map[i][j]==0) printf(" "); 
  }
  if(i==1)printf(" 下一个 :");
  if(i==11)printf(" 等分 : %d",score);
  if(i==14)printf(" 速度 : %d",score/100+1);
  puts("");
 }
 }
 else {
 mark=1;
 for(i=0;i<SIZE_N;i++){
  for(j=0;j<SIZE_M;j++){
  if(i==0&&j==0 || i==0&&j==SIZE_M-1 || j==0&&i==SIZE_N-1 || j==SIZE_M-1&&i==SIZE_N-1)printf(" ");
  else if(i==0 || i==SIZE_N-1)printf("--");
  else if(j==0 || j==SIZE_M-1)printf("|");
  else if(map[i][j]==2) printf("■");
  else if(map[i][j]==0) printf(" "); 
  }
  if(i==1)printf(" next:");
  if(i==11)printf(" score : %d",score);
  if(i==14)printf(" speed : %d",score/100+1);
  puts("");
 }
 }

 gotoxy(30,6); printf(" ");
 for(i=0;i<6;i=i+2){
 gotoxy(30+2*shape[id][i+1],6+shape[id][i]); printf(" ");
 }
 gotoxy(30,6); printf("■");
 for(i=0;i<6;i=i+2){
 gotoxy(30+2*shape[next][i+1],6+shape[next][i]); printf("■");
 }
 Sleep(Gamespeed);
}

void init(int id){
 int i,j;
 memset(map,0,sizeof(map));
 for(i=0;i<SIZE_N;i++){
 for(j=0;j<SIZE_M;j++)
  if(i==SIZE_N-1 || j==0 || j==SIZE_M-1)
  map[i][j]=-1;
 }
 cur_x=0; cur_y=5;
 ShowMap(id);
}

int judge_in(int x,int y,int id){
 int i;
 if(map[x][y]!=0)return 0;
 for(i=0;i<6;i=i+2){
 if(map[ x+shape[id][i] ][ y+shape[id][i+1] ]!=0)return 0;
 }return 1;
}

void fun_score(){
 int i,j,ii,jj;
 for(i=1;i<SIZE_N-1;i++){
 int flag=0;
 for(j=1;j<SIZE_M-1;j++){
  if(map[i][j]!=2){ flag=1;break; }
 }
 if(flag==0){
  int k=3;
  while(k--){
  gotoxy(2,i+1);
  for(ii=1;ii<SIZE_M-1;ii++){
   if(map[i][ii]==2){
   if(k%2==1)printf(" ");
   else printf("■");
   }
  }Sleep(100);
  }
  for(ii=i;ii>1;ii--){
  for(jj=1;jj<SIZE_M-1;jj++) map[ii][jj]=map[ii-1][jj];
  }
  ShowMap(-1);
  score+=10;
  if(score%100==0 && score!=0)Gamespeed-=50;
 }
 }
}

int main(){
 int i,id,set=1;

 srand(time(NULL));
 id=rand()%MOD; id=(id+MOD)%MOD;
 next=rand()%MOD; next=(next+MOD)%MOD;

 init(id);

 while(1){
Here: mark=0;
 if(set==0){
  id=next;
  next=rand()%MOD; next=(next+MOD)%MOD;
  cur_x=0;cur_y=5;
  set=1;
 }

 while(!kbhit()){
  Gameover();
  if(judge_in(cur_x+1,cur_y,id)==1) cur_x++;
  else {
  map[cur_x][cur_y]=2;
  for(i=0;i<6;i=i+2)
   map[ cur_x+shape[id][i] ][ cur_y+shape[id][i+1] ]=2;
  fun_score();
  set=0;
  }
  if(mark!=1)ShowMap(id);
  goto Here;
 }

 char key;
 key=getch();

 if(key==72){
  int tmp=id;
  id++;
  if( id%4==0 && id!=0 )id=id-4;
  if(judge_in(cur_x,cur_y,id)!=1)id=tmp;
 }
 else if(key==80 && judge_in(cur_x+1,cur_y,id)==1)cur_x++;
 else if(key==75 && judge_in(cur_x,cur_y-1,id)==1)cur_y--;
 else if(key==77 && judge_in(cur_x,cur_y+1,id)==1)cur_y++;
 else if(key==27){gotoxy(1,SIZE_N+1);exit(0);}
 }
 getch();
 return 0; 
}

俄罗斯方块2

原创

代码如下:

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <conio.h>

using namespace std;

int block00[4][4] = { { 10, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
int block01[4][4] = { { 11, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 } };
int block02[4][4] = { { 12, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 0, 0 } };
int block03[4][4] = { { 13, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 } };
int block04[4][4] = { { 14, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 1, 0 } };
int block05[4][4] = { { 15, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 } };
int block06[4][4] = { { 16, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 1, 0, 0, 0 } };
int block07[4][4] = { { 17, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } };
int block08[4][4] = { { 18, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 1, 1, 1, 0 } };
int block09[4][4] = { { 19, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 } };
int block10[4][4] = { { 20, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 1, 0 } };
int block11[4][4] = { { 21, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 } };
int block12[4][4] = { { 22, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 1, 1, 0 } };
int block13[4][4] = { { 23, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } };
int block14[4][4] = { { 24, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 1, 1, 0, 0 } };
int block15[4][4] = { { 25, 0, 0, 0 }, { 1, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 } };
int block16[4][4] = { { 26, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 } };
int block17[4][4] = { { 27, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 } };
int block18[4][4] = { { 28, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 1, 1, 0, 0 } };
void initialWindow (HANDLE hOut);//初始化窗口
void initialPrint (HANDLE hOut);//初始化界面
void gotoXY (HANDLE hOut, int x, int y);//移动光标
void roundBlock (HANDLE hOut, int block[4][4]);//随机生成方块并打印到下一个方块位置
bool collisionDetection (int block[4][4], int map[21][12], int x, int y);//检测碰撞
void printBlock (HANDLE hOut, int block[4][4], int x, int y);//打印方块
void clearBlock (HANDLE hOut, int block[4][4], int x, int y);//消除方块
void myLeft (HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//左移
void myRight (HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//右移
void myUp (HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//顺时针旋转90度
int myDown (HANDLE hOut, int block[4][4], int map[21][12], int &x, int y);//加速下落
void myStop (HANDLE hOut, int block[4][4]);//游戏暂停
void gameOver (HANDLE hOut, int block[4][4], int map[21][12]);//游戏结束
//判断是否能消行并更新分值
void eliminateRow (HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint);
int main () {
    MessageBox (NULL, "欢迎来到俄罗斯方块游戏!", "温馨提示", MB_OK);
    system ("mode coon cols=200 lines=100"); 
    system ("mode con cols=70 lines=35");
    int map[21][12];
    int blockA[4][4];//候选区的方块
    int blockB[4][4];//下落中的方块
    int positionX, positionY;//方块左上角的坐标
    bool check;//检查方块还能不能下落
    char key;//用来存储按键
    int val;//用来控制下落速度
    int fraction;//用来存储得分
    int checkpoint;//用来存储关卡
    int times;
    HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);//获取标准输出设备句柄
    initialWindow (hOut);
initial:
    gotoXY (hOut, 0, 0);
    initialPrint (hOut);
    check = true;
    val = 50;
    fraction = 0;
    checkpoint = 1;
    times = val;
    for (int i = 0; i < 20; i++) {
        for (int j = 1; j < 11; j++) {
            map[i][j] = 0;
        }
    }
    for (int i = 0; i < 20; i++) {
        map[i][0] = map[i][11] = 1;
    }
    for (int i = 0; i < 12; i++) {
        map[20][i] = 1;
    }
    srand ((unsigned) time (NULL));
    roundBlock (hOut, blockA);
    while (true) {
        if (check) {
            eliminateRow (hOut, map, val, fraction, checkpoint);
            check = false;
            positionX = -3;
            positionY = 4;
            if (collisionDetection (blockA, map, positionX, positionY)) {
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        blockB[i][j] = blockA[i][j];
                    }
                }
                roundBlock (hOut, blockA);
            } else {
                gameOver (hOut, blockA, map);
                goto initial;
            }
        }
        printBlock (hOut, blockB, positionX, positionY);
        if (_kbhit ()) {
            key = _getch ();
            switch (key) {
                case 72:
                    myUp (hOut, blockB, map, positionX, positionY);
                    break;
                case 75:
                    myLeft (hOut, blockB, map, positionX, positionY);
                    break;
                case 77:
                    myRight (hOut, blockB, map, positionX, positionY);
                    break;
                case 80:
                    switch (myDown (hOut, blockB, map, positionX, positionY)) {
                        case 0:
                            check = false;
                            break;
                        case 1:
                            check = true;
                            break;
                        case 2:
                            gameOver (hOut, blockB, map);
                            goto initial;
                        default:
                            break;
                    }
                    break;
                case 32:
                    myStop (hOut, blockA);
                    break;
                case 27:
                    exit (0);
                default:
                    break;
            }
        }
        Sleep (20);
        if (--times == 0) {
            switch (myDown (hOut, blockB, map, positionX, positionY)) {
                case 0:
                    check = false;
                    break;
                case 1:
                    check = true;
                    break;
                case 2:
                    gameOver (hOut, blockB, map);
                    goto initial;
                default:
                    break;
            }
            times = val;
        }
    }
    cin.get ();
    return 0;
}

void initialWindow (HANDLE hOut) {
    SetConsoleTitle ("俄罗斯方块");
    COORD size = { 80, 25 };
    SetConsoleScreenBufferSize (hOut, size);
    SMALL_RECT rc = { 0, 0, 79, 24 };
    SetConsoleWindowInfo (hOut, true, &rc);
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo (hOut, &cursor_info);
}

void initialPrint(HANDLE hOut) {
    SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    for (int i = 0; i < 20; i++) {
        cout << "■                    ■☆                      ☆" << endl;
    }
    gotoXY (hOut, 26, 0);
    cout << "☆☆☆☆☆☆☆☆☆☆☆";
    gotoXY (hOut, 0, 20);
    cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆";
    gotoXY (hOut, 26, 1);
    cout << "分    数:      ";
    gotoXY (hOut, 26, 2);
    cout << "关    卡:      ";
    gotoXY (hOut, 26, 4);
    cout << "下一方块:";
    gotoXY (hOut, 26, 9);
    cout << "操作方法:";
    gotoXY (hOut, 30, 11);
    cout << "↑:旋转 ↓:速降";
    gotoXY (hOut, 30, 12);
    cout << "→:右移 ←:左移";
    gotoXY (hOut, 30, 13);
    cout << "空格键:开始/暂停";
    gotoXY (hOut, 30, 14);
    cout << "Esc 键:退出";
    gotoXY (hOut, 26, 16);
    cout << "关    于:";
    gotoXY (hOut, 30, 18);
    cout << "俄罗斯方块V1.0";
    gotoXY (hOut, 35, 19);
}
void gotoXY (HANDLE hOut, int x, int y) {
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition (hOut, pos);
}
void roundBlock(HANDLE hOut, int block[4][4]) {
    clearBlock (hOut, block, 5, 15);
    switch (rand() % 19) {
        case 0:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block00[i][j];
                }
            }
            break;
        case 1:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block01[i][j];
                }
            }
            break;
        case 2:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block02[i][j];
                }
            }
            break;
        case 3:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block03[i][j];
                }
            }
            break;
        case 4:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block04[i][j];
                }
            }
            break;
        case 5:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block05[i][j];
                }
            }
            break;
        case 6:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block06[i][j];
                }
            }
            break;
        case 7:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                block[i][j] = block07[i][j];
            }
        }
        break;
        case 8:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block08[i][j];
                }
            }
            break;
        case 9:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++)
                {
                    block[i][j] = block09[i][j];
                }
            }
            break;
        case 10:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block10[i][j];
                }
            }
            break;
        case 11:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block11[i][j];
                }
            }
            break;
        case 12:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block12[i][j];
                }
            }
            break;
        case 13:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block13[i][j];
                }
            }
            break;
        case 14:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block14[i][j];
                }
            }
            break;
        case 15:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block15[i][j];
                }
            }
            break;
        case 16:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block16[i][j];
                }
            }
            break;
        case 17:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block17[i][j];
                }
            }
            break;
        case 18:
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    block[i][j] = block18[i][j];
                }
            }
            break;
        default:
            break;
    }
    printBlock(hOut, block, 5, 15);
}
bool collisionDetection (int block[4][4], int map[21][12], int x, int y) {
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (x + i >= 0 && y + j >= 0 && map[x + i][y + j] == 1 && block[i][j] == 1) {
                return false;
            }
        }
    }
    return true;
}
void printBlock (HANDLE hOut, int block[4][4], int x, int y) {
    switch (block[0][0]) {
        case 10:
        case 11:
            SetConsoleTextAttribute (hOut, FOREGROUND_GREEN);
            break;
        case 12:
        case 13:
        case 14:
        case 15:
            SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            break;
        case 16:
        case 17:
        case 18:
        case 19:
            SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            break;
        case 20:
        case 21:
        case 22:
        case 23:
            SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            break;
        case 24:
        case 25:
            SetConsoleTextAttribute (hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
            break;
        case 26:
        case 27:
            SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
            break;
        case 28:
            SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
            break;
        default:
            break;
    }
    for (int i = 0; i < 4; i++) {
        if (i + x >= 0) {
            for (int j = 0; j < 4; j++) {
                if (block[i][j] == 1) {
                    gotoXY (hOut, 2 * (y + j), x + i);
                    cout << "■";
                }
            }
        }
    }
}
void clearBlock (HANDLE hOut, int block[4][4], int x, int y) {
    for (int i = 0; i < 4; i++) {
        if (i + x >= 0) {
            for (int j = 0; j < 4; j++) {
                if (block[i][j] == 1) {
                    gotoXY (hOut, 2 * (y + j), x + i);
                    cout << "  ";
                }
            }
        }
    }
}
void gameOver (HANDLE hOut, int block[4][4], int map[21][12]) {
    SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
    gotoXY (hOut, 9, 8);
    string s = "GAME OVER";
    for (int i = 0; i < s.size (); i++) {
        Sleep (80);
        cout << s[i];
    }
    gotoXY (hOut, 8, 9);
    s = "空格键:重来";
    for (int i = 0; i < s.size (); i++) {
        Sleep (80);
        cout << s[i];
    }
    cout << endl;
    gotoXY (hOut, 8, 10);
    s = "ESC键:退出";
    MessageBox (NULL, "很遗憾,您输了!", "温馨提示", MB_OK);
    for (int i = 0; i < s.size (); i++) {
        Sleep (80);
        cout << s[i];
    }
    char key;
    while (true) {
        key = _getch ();
        if (key == 32) {
            return;
        } else if (key == 27) {
            exit(0);
        }
    }
}
int myDown (HANDLE hOut, int block[4][4], int map[21][12], int &x, int y) {
    if (collisionDetection(block, map, x + 1, y)) {
        clearBlock (hOut, block, x, y);
        ++x;
        return 0;
    }
    if (x < 0) {
        return 2;
    }
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (block[i][j] == 1) {
                map[x + i][y + j] = 1;
                SetConsoleTextAttribute (hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
                gotoXY (hOut, 2 * (y + j), x + i);
                cout << "■";
            }
        }
    }
    return 1;
}
void myLeft (HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) {
    if (collisionDetection (block, map, x, y - 1)) {
        clearBlock (hOut, block, x, y);
        --y;
    }
}
void myRight (HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) {
    if (collisionDetection (block, map, x, y + 1)) {
        clearBlock (hOut, block, x, y);
        ++y;
    }
}
void myUp (HANDLE hOut, int block[4][4], int map[21][12], int x, int &y) {
    switch (block[0][0]) {
        case 10:
            if (collisionDetection (block01, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block01[i][j];
                    }
                }
            }
            break;
        case 11:
            if (collisionDetection (block00, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block00[i][j];
                    }
                }
            } else if (collisionDetection (block00, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block00[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block00, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block00[i][j];
                    }
                }
                ++y;
            } else if (collisionDetection (block00, map, x, y - 2)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block00[i][j];
                    }
                }
                y -= 2;
            } else if (collisionDetection (block00, map, x, y + 2)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block00[i][j];
                    }
                }
                y += 2;
            }
            break;
        case 12:
            if (collisionDetection (block03, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block03[i][j];
                    }
                }
            } else if (collisionDetection (block03, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block03[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block03, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block03[i][j];
                    }
                }
                ++y;
            }
            break;
        case 13:
            if (collisionDetection (block04, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block04[i][j];
                    }
                }
            } else if (collisionDetection (block04, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block04[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block04, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block04[i][j];
                    }
                }
                ++y;
            }
            break;
        case 14:
            if (collisionDetection (block05, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block05[i][j];
                    }
                }
            } else if (collisionDetection (block05, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block05[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block05, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block05[i][j];
                    }
                }
                ++y;
            }
            break;
        case 15:
            if (collisionDetection (block02, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block02[i][j];
                    }
                }
            } else if (collisionDetection (block02, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block02[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block02, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block02[i][j];
                    }
                }
                ++y;
            }
            break;
        case 16:
            if (collisionDetection (block07, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block07[i][j];
                    }
                }
            } else if (collisionDetection (block07, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block07[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block07, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block07[i][j];
                    }
                }
                ++y;
            }
            break;
        case 17:
            if (collisionDetection (block08, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block08[i][j];
                    }
                }
            } else if (collisionDetection (block08, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block08[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block08, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block08[i][j];
                    }
                }
                ++y;
            }
            break;
        case 18:
            if (collisionDetection (block09, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block09[i][j];
                    }
                }
            } else if (collisionDetection (block09, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block09[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block09, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block09[i][j];
                    }
                }
                ++y;
            }
            break;
        case 19:
            if (collisionDetection (block06, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block06[i][j];
                    }
                }
            } else if (collisionDetection (block06, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block06[i][j];
                    }
                }
                --y;
            } else if (collisionDetection(block06, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block06[i][j];
                    }
                }
                ++y;
            }
            break;
        case 20:
            if (collisionDetection (block11, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block11[i][j];
                    }
                }
            } else if (collisionDetection (block11, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block11[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block11, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block11[i][j];
                    }
                }
                ++y;
            }
            break;
        case 21:
            if (collisionDetection (block12, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block12[i][j];
                    }
                }
            } else if (collisionDetection(block12, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block12[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block12, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block12[i][j];
                    }
                }
                ++y;
            }
            break;
        case 22:
            if (collisionDetection (block13, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block13[i][j];
                    }
                }
            } else if (collisionDetection (block13, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block13[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block13, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block13[i][j];
                    }
                }
                ++y;
            }
            break;
        case 23:
            if (collisionDetection (block10, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block10[i][j];
                    }
                }
            } else if (collisionDetection (block10, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block10[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block10, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block10[i][j];
                    }
                }
                ++y;
            }
            break;
        case 24:
            if (collisionDetection (block15, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block15[i][j];
                    }
                }
            } else if (collisionDetection (block15, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block15[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block15, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block15[i][j];
                    }
                }
                ++y;
            }
            break;
        case 25:
            if (collisionDetection (block14, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block14[i][j];
                    }
                }
            } else if (collisionDetection (block14, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block14[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block14, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block14[i][j];
                    }
                }
                ++y;
            }
            break;
        case 26:
            if (collisionDetection (block17, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block17[i][j];
                    }
                }
            } else if (collisionDetection (block17, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block17[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block17, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block17[i][j];
                    }
                }
                ++y;
            }
            break;
        case 27:
            if (collisionDetection (block16, map, x, y)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block16[i][j];
                    }
                }
            } else if (collisionDetection (block16, map, x, y - 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block16[i][j];
                    }
                }
                --y;
            } else if (collisionDetection (block16, map, x, y + 1)) {
                clearBlock (hOut, block, x, y);
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 4; j++) {
                        block[i][j] = block16[i][j];
                    }
                }
                ++y;
            }
            break;
        default:
            break;
    }
}
void myStop (HANDLE hOut, int block[4][4]) {
    clearBlock (hOut, block, 5, 15);
    SetConsoleTextAttribute (hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
    gotoXY (hOut, 30, 7);
    cout << "游戏暂停";
    char key;
    while (true) {
        key = _getch ();
        if (key == 32) {
            gotoXY (hOut, 30, 7);
            cout << "        ";
            printBlock (hOut, block, 5, 15);
            return;
        }
        if (key == 27) {
            exit (0);
        }
    }
}
void eliminateRow (HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint) {
    SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
    for (int i = 19; i >= 0; i--) {
        int x = 0;
        for (int j = 1; j < 11; j++) {
            x += map[i][j];
        }
        if (x == 10) {
            fraction += 100;
            if (val > 1 && fraction / 1000 + 1 != checkpoint) {
                checkpoint = fraction / 1000 + 1;
                val -= 5;
            }
            for (int m = i; m > 0; m--) {
                for (int n = 1; n < 11; n++) {
                    map[m][n] = map[m - 1][n];
                    gotoXY (hOut, 2 * n, m);
                    if (map[m][n] == 1) {
                        cout << "■";
                    } else {
                        cout << "  ";
                    }
                }
            }
            i++;
        }
    }
    gotoXY (hOut, 36, 1);
    cout << fraction;
    gotoXY (hOut, 36, 2);
    cout << checkpoint;
}

俄罗斯方块3

原创

代码如下:

#include<iostream>
#include<string>
#include<cstdlib>
#include<windows.h>
#include<ctime>
#include<conio.h>
#include<cstdio>
using namespace std;

class Tetris
{
private:
    int rank;               //游戏难度等级
    int score;              // 得分
    int id;                 //图形ID
    int point[2];           //两基点
    int top;                    //最高点高度
    double clock;               //距离上一次消行过去的毫秒数
public:
    Tetris();
    void DrawMap();         //游戏界面
    void SetColor(int);     //控制颜色
    void Draw(int, int, int);       //画图形
    void Run();             //运行游戏
    void ReDraw(int, int, int);         //清除图形
    bool Judge(int, int, int);
    void Turn(int);             //旋转
    void Updata();              // 更新界面
    void Pause();               //游戏暂停
    void Input_score();
    void Welocme();
}; const int sharp[19][8] = {               //组成图形的各个点的各个坐标,先纵后横
//条形
{0,0,1,0,2,0,3,0},{0,0,0,1,0,2,0,3},     
//方块
{0,0,1,0,0,1,1,1},  
//L形
{0,0,1,0,1,1,1,2},{0,1,1,1,2,0,2,1},{0,0,0,1,0,2,1,2},{0,0,0,1,1,0,2,0},
//J形
{1,0,1,1,1,2,0,2},{0,0,0,1,1,1,2,1},{0,0,0,1,0,2,1,0},{0,0,1,0,2,0,2,1},
//T形
{1,0,0,1,1,1,2,1},{1,0,1,1,1,2,0,1},{0,0,1,0,2,0,1,1},{0,0,0,1,0,2,1,1},
//S形
{1,0,2,0,1,1,0,1},{0,0,0,1,1,1,1,2},
//Z形
{0,0,1,0,1,1,2,1},{1,0,1,1,0,1,0,2}
};

const int high[19] = { 4,1,2,2,3,2,3,2,3,2,3,2,3,2,3,2,3,2,3 };
int map[28][16]{};
//条形
#define a1  0           
#define a2  1

// 方块
#define b 2                 

//L形
#define c1 3                    
#define c2 4
#define c3 5
#define c4 6

//J形
#define d1 7                    
#define d2 8 
#define d3 9
#define d4 10

//T形
#define e1 11
#define e2 12
#define e3 13
#define e4 14

//S形
#define f1 15
#define f2 16

//Z形
#define g1 17
#define g2 18

Tetris::Tetris()                //构造函数, 初始化各个值
{
    point[0] = 0;
    point[1] = 5;
    score = 0;
    top = 25;
    clock = 0;
}

void Tetris::Turn(int num)              //旋转函数
{
    /*
    通过条形旋转的方式完成其他形状的旋转

    有兴趣的同学也可以通过其他方法完成此功能
    */
    switch (num)
    {
    //条形旋转
    case a1: id = a2; break;                    
    case a2: id = a1; break;

    //方块旋转
    case b: break;

    //L形旋转
    case c1: id = c2; break;
    case c2: id = c3; break;
    case c3: id = c4; break;
    case c4: id = c1; break;

    //J型旋转
    case d1: id = d2; break;
    case d2: id = d3; break;
    case d3: id = d4; break;
    case d4: id = d1; break;

    //T形旋转
    case e1: id = e2; break;
    case e2: id = e3; break;
    case e3: id = e4; break;
    case e4: id = e1; break;

    //S形旋转
    case f1: id = f2; break;
    case f2: id = f1; break;

    //Z形旋转
    case g1: id = g2; break;
    case g2: id = g1; break;

    }
}

void SetPos(int i, int j)           //控制光标位置, 列, 行
{
    COORD pos = { i,j };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void Tetris::Pause()                // 暂停函数
{
    SetPos(32, 10);
    cout << "游戏暂停!" << endl;
    SetPos(30, 11);
    cout << "你的分数为 " << score;
    char temp;
    while (1)
    {
        while (1)
        {
            if (_kbhit())
            {
                temp = _getch();
                break;
            }
        }
        if (temp == 32)
            break;
    }
    SetPos(32, 10);                 // 清除暂停时显示的信息
    cout << "         ";
    SetPos(30, 11);
    cout << "              ";
}

void Tetris::Updata()                   //更新函数
{
    int i, flag;
    int nx, ny;
    for (i = 0; i < 4; i++)
    {
        nx = point[0] + sharp[id][i * 2];
        ny = point[1] + sharp[id][i * 2 + 1];
        SetPos((ny + 1) * 2, nx + 1);
        SetColor(0);
        cout << "■";
        map[nx][ny] = 1;                    //界面各个点是否为空的更新
    }

    if (point[0] < top)
        top = point[0];                 //最高点的更新

    int j;
    for (i = point[0]; i < point[0] + high[id]; ++i)            //消除行
    {
        //将flag赋值为1,flag为1表示某一行满,0表示未满。
        flag = 1;

        // map[i][j] == 0表示在地图的第i行,第j列的地方没有方块,输出的形式为"  "
        // map[i][j] == 1表示在地图的第i行,第j列的地方有方块,输出的形式为"■"

        //注:游戏区的列数为13列

        //判断某一行是否为满
        /*
        请在下方补全判断是否为满功能代码
        */
        //28*16
        for (j=0; j<13; ++j)
            if (map[i][j] == 0) {
                flag = 0;
                break;
            }

        //当flag为1时,消行
        if (flag == 1)
        {
            clock = 0; //计时清零
            /*
            请在下方补全消行功能代码
            */

            // i+1行依次向下
            SetColor(0);
            for (int tt=i; tt>=top; --tt)
                for (int t = 0; t < 13; ++t) {
                    map[tt][t] = map[tt - 1][t];
                    SetPos((t + 1) * 2, tt + 1);
                    if (map[tt][t]) { // 有块
                        cout << "■";
                    }
                    else {
                        cout << "  ";
                    }
                }
            ++point[0];
            --top;

            //计算得分
            score += 10;
            Input_score();

        }
    }
    if (clock>=1000) { //上移一行
        clock = 0;

        int tmp;
        for (int i = top; i <= 24; ++i) {
            for (int j = 0; j < 13; ++j) {
                tmp = map[i - 1][j] = map[i][j];
                SetPos((j + 1) * 2, i);
                if (tmp == 1) {
                    cout << "■";
                }
                else {
                    cout << "  ";
                }
            }
        }

        // 产生13个1,0随机数作为最下面一行
        // 注意不能为全1
        bool f = true;
        int i;
        do {
            for (i = 0; i < 13; ++i) {
                if ((map[24][i] = rand() % 2) == 0)
                    f = false;
            }
        } while (f);

        // 画出这一行
        SetPos(2, 25);
        for (i = 0; i < 13; ++i) {
            if (map[24][i] == 1)
                cout << "■";
            else
                cout << "  ";
        }
    }
}

void Tetris::Input_score()
{
    SetColor(3);
    SetPos(30, 19);
    cout << "得分: " << score;
}

void Tetris::Welocme()          //欢迎界面
{
    SetColor(1);
    char x;
    while (1)
    {
        system("cls");
        cout << "■■■■■■■■■■■■■■■■■■■■■" << endl;
        cout << "       俄罗斯方块       " << endl;
        cout << "■■■■■■■■■■■■■■■■■■■■■" << endl;
        cout << "       操作方式:" << endl;
        cout << "       ↑ - 旋转" << endl;
        cout << "       ↓ - 加速下移" << endl;
        cout << "       ← - 左移" << endl;
        cout << "       → - 右移" << endl;
        cout << "       空格 - 暂停" << endl;
        cout << "■■■■■■■■■■■■■■■■■■■■■" << endl;
        cout << "■ 按1—3选择难度■" << endl;
        SetPos(20, 10);
        x = getchar();
        if (x <= '9' && x >= '0')
        {
            rank = x - '0';
            break;
        }
    }
}

void Tetris::SetColor(int color_num)            //设置颜色
{
    int n;
    switch (color_num)
    {
    case 0: n = 0x08; break;
    case 1: n = 0x0C; break;
    case 2: n = 0x0D; break;
    case 3: n = 0x0E; break;
    case 4: n = 0x0A; break;
    }
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), n);
}

void Tetris::DrawMap()              //画游戏时界面
{
    int i;
    SetColor(0);

    for (i = 0; i < 24; i++)        //宽24格
    {
        SetPos(i * 2, 0);
        cout << "■";
        SetPos(i * 2, 26);
        cout << "■";
    }

    for (i = 0; i < 26; i++)        //高26格
    {
        SetPos(0, i);
        cout << "■";
        SetPos(28, i);
        cout << "■";
        SetPos(46, i);
        cout << "■";
    }

    for (i = 14; i < 24; i++)
    {
        SetPos(i * 2, 16);
        cout << "■";
    }

    SetColor(3);
    Input_score();
    SetPos(30, 21);
    cout << "难度等级: " << rank;
    SetPos(32, 2);
    cout << "下一图形";
}

void Tetris::Draw(int x, int y, int num)                //画图形
{
    int nx, ny;

    for (int i = 0; i < 4; i++)
    {
        nx = x + sharp[num][2 * i];
        ny = y + sharp[num][2 * i + 1];
        SetPos((ny + 1) * 2, nx + 1);
        SetColor(i + 1);
        cout << "■";
    }
}

void Tetris::ReDraw(int x, int y, int num)              //为更新图形的位置清除图形
{
    int nx, ny;

    for (int i = 0; i < 4; i++)
    {
        nx = x + sharp[num][2 * i];
        ny = y + sharp[num][2 * i + 1];
        SetPos((ny + 1) * 2, nx + 1);
        cout << "  ";
    }
}

bool Tetris::Judge(int x, int y, int num)               //判定在x, y 所指位置是否可画编号为
{                                                   //num 的图形, 若不可画则反回true
    int nx, ny;
    for (int i = 0; i < 4; i++)
    {
        nx = x + sharp[num][2 * i];
        ny = y + sharp[num][2 * i + 1];
        if (!(nx < 25 && nx >= 0 && ny < 13 && ny >= 0 && !map[nx][ny]))
            return true;
    }
    return false;
}

void Tetris::Run()                  //运行游戏
{
    int next_id;
    srand((int)time(0));

    id = rand() % 19;
    next_id = rand() % 19;

    Draw(point[0], point[1], id);
    Draw(5, 16, next_id);

    int count;
    if (rank == 1)
        count = 150;
    else if (rank == 2)
        count = 100;
    else if (rank == 3)
        count = 50;
    else
        count = 5;
    int i = 0;  //不同等级对应不同count

    while (1)
    {
        if (!(i < count))               //i 与 count 用于控制时间
        {
            i = 0;
            if (Judge(point[0] + 1, point[1], id))          //在某一位置不能下落的话
            {
                Updata();
                id = next_id;

                ReDraw(5, 16, next_id);
                next_id = rand() % 19;

                point[0] = 0; point[1] = 5;
                Draw(point[0], point[1], id);
                Draw(5, 16, next_id);

                if (Judge(point[0], point[1], id))
                {
                    system("cls");
                    SetPos(20, 10);
                    cout << "游戏结束!" << endl;
                    SetPos(20, 11);
                    cout << "你的分数为 " << score << endl;
                    system("pause");
                    exit(1);
                }
            }
            else                    //继续下落
            {
                ReDraw(point[0], point[1], id);
                point[0]++;
                Draw(point[0], point[1], id);
            }
        }

        if (_kbhit())               //键盘输入值时 
        {
            int key, key2;
            key = _getch();
            if (key == 224)
            {
                key2 = _getch();

                if (key2 == 72)         //按向上方向键时
                {
                    int temp = id;
                    Turn(id);
                    if (Judge(point[0], point[1], id))
                        id = temp;
                    ReDraw(point[0], point[1], temp);
                    Draw(point[0], point[1], id);
                }
                if (key2 == 80)             //按向下方向键时
                {
                    if (!Judge(point[0] + 2, point[1], id))
                    {
                        ReDraw(point[0], point[1], id);
                        point[0] += 2;
                        Draw(point[0], point[1], id);
                    }
                }
                else if (key2 == 75)                //按向左方向键时
                {
                    if (!Judge(point[0], point[1] - 1, id))
                    {
                        ReDraw(point[0], point[1], id);
                        point[1]--;
                        Draw(point[0], point[1], id);
                    }
                }
                else if (key2 == 77)                    //按向右方向键时
                {
                    if (!Judge(point[0], point[1] + 1, id))
                    {
                        ReDraw(point[0], point[1], id);
                        point[1]++;
                        Draw(point[0], point[1], id);
                    }
                }
            }
            else if (key == 32)                 // 按下空格暂停
                Pause();
        }

        Sleep(1);       //等待1毫秒
        i++;                //控制下落间隔
        ++clock;        // 计时+1
    }
}

int main()
{
    Tetris game;
    game.Welocme();
    system("cls");              //清除欢迎界面
    game.DrawMap();
    game.Run();
}

俄罗斯方块4

原创

代码如下:

#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>
#include<windows.h>
#include<conio.h>

using namespace std;

int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,0,0,0 } };
int block01[4][4] = { { 11,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 } };
int block02[4][4] = { { 12,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,1,0,0 } };
int block03[4][4] = { { 13,0,0,0 },{ 0,1,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block04[4][4] = { { 14,0,0,0 },{ 0,0,0,0 },{ 0,1,0,0 },{ 1,1,1,0 } };
int block05[4][4] = { { 15,0,0,0 },{ 0,1,0,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block06[4][4] = { { 16,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 1,0,0,0 } };
int block07[4][4] = { { 17,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block08[4][4] = { { 18,0,0,0 },{ 0,0,0,0 },{ 0,0,1,0 },{ 1,1,1,0 } };
int block09[4][4] = { { 19,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 0,1,1,0 } };
int block10[4][4] = { { 20,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,0,1,0 } };
int block11[4][4] = { { 21,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 1,1,0,0 } };
int block12[4][4] = { { 22,0,0,0 },{ 0,0,0,0 },{ 1,0,0,0 },{ 1,1,1,0 } };
int block13[4][4] = { { 23,0,0,0 },{ 0,1,1,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block14[4][4] = { { 24,0,0,0 },{ 0,0,0,0 },{ 0,1,1,0 },{ 1,1,0,0 } };
int block15[4][4] = { { 25,0,0,0 },{ 1,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block16[4][4] = { { 26,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 0,1,1,0 } };
int block17[4][4] = { { 27,0,0,0 },{ 0,0,1,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block18[4][4] = { { 28,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 1,1,0,0 } };

void initialWindow(HANDLE hOut);//初始化窗口
void initialPrint(HANDLE hOut);//初始化界面
void gotoXY(HANDLE hOut, int x, int y);//移动光标
void roundBlock(HANDLE hOut, int block[4][4]);//随机生成方块并打印到下一个方块位置
bool collisionDetection(int block[4][4], int map[21][12], int x, int y);//检测碰撞
void printBlock(HANDLE hOut, int block[4][4], int x, int y);//打印方块
void clearBlock(HANDLE hOut, int block[4][4], int x, int y);//消除方块
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//左移
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//右移
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//顺时针旋转90度
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y);//加速下落
void myStop(HANDLE hOut, int block[4][4]);//游戏暂停
void gameOver(HANDLE hOut, int block[4][4], int map[21][12]);//游戏结束
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint);//判断是否能消行并更新分值
int main()
{
    int map[21][12];
    int blockA[4][4];//候选区的方块
    int blockB[4][4];//下落中的方块
    int positionX, positionY;//方块左上角的坐标
    bool check;//检查方块还能不能下落
    char key;//用来存储按键
    int val;//用来控制下落速度
    int fraction;//用来存储得分
    int checkpoint;//用来存储关卡
    int times;
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄
    initialWindow(hOut);
initial:
    gotoXY(hOut, 0, 0);
    initialPrint(hOut);
    check = true;
    val = 50;
    fraction = 0;
    checkpoint = 1;
    times = val;
    for (int i = 0; i < 20; ++i)
    {
        for (int j = 1; j < 11; ++j)
        {
            map[i][j] = 0;
        }
    }
    for (int i = 0; i < 20; ++i)
    {
        map[i][0] = map[i][11] = 1;
    }
    for (int i = 0; i < 12; ++i)
    {
        map[20][i] = 1;
    }

    srand((unsigned)time(NULL));
    roundBlock(hOut, blockA);
    while (true)
    {
        if (check)
        {
            eliminateRow(hOut, map, val, fraction, checkpoint);
            check = false;
            positionX = -3;
            positionY = 4;
            if (collisionDetection(blockA, map, positionX, positionY))
            {
                for (int i = 0; i < 4; ++i)
                {
                    for (int j = 0; j < 4; ++j)
                    {
                        blockB[i][j] = blockA[i][j];
                    }
                }
                roundBlock(hOut, blockA);
            }
            else
            {
                gameOver(hOut, blockA, map);
                goto initial;
            }
        }
        printBlock(hOut, blockB, positionX, positionY);
        if (_kbhit())
        {
            key = _getch();
            switch (key)
            {
            case 72:
                myUp(hOut, blockB, map, positionX, positionY);
                break;
            case 75:
                myLeft(hOut, blockB, map, positionX, positionY);
                break;
            case 77:
                myRight(hOut, blockB, map, positionX, positionY);
                break;
            case 80:
                switch (myDown(hOut, blockB, map, positionX, positionY))
                {
                case 0:
                    check = false;
                    break;
                case 1:
                    check = true;
                    break;
                case 2:
                    gameOver(hOut, blockB, map);
                    goto initial;
                default:
                    break;
                }
                break;
            case 32:
                myStop(hOut, blockA);
                break;
            case 27:
                exit(0);
            default:
                break;
            }
        }
        Sleep(20);
        if (0 == --times)
        {
            switch (myDown(hOut, blockB, map, positionX, positionY))
            {
            case 0:
                check = false;
                break;
            case 1:
                check = true;
                break;
            case 2:
                gameOver(hOut, blockB, map);
                goto initial;
            default:
                break;
            }
            times = val;
        }
    }
    cin.get();
    return 0;
}

void initialWindow(HANDLE hOut)
{
    SetConsoleTitle("俄罗斯方块");
    COORD size = { 80, 25 };
    SetConsoleScreenBufferSize(hOut, size);
    SMALL_RECT rc = { 0, 0, 79, 24 };
    SetConsoleWindowInfo(hOut, true, &rc);
    CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
    SetConsoleCursorInfo(hOut, &cursor_info);
}

void initialPrint(HANDLE hOut)
{
    SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    for (int i = 0; i < 20; ++i)
    {
        cout << "■                    ■☆                      ☆" << endl;
    }
    gotoXY(hOut, 26, 0);
    cout << "☆☆☆☆☆☆☆☆☆☆☆";
    gotoXY(hOut, 0, 20);
    cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆";
    gotoXY(hOut, 26, 1);
    cout << "分    数:      ";
    gotoXY(hOut, 26, 2);
    cout << "关    卡:      ";
    gotoXY(hOut, 26, 4);
    cout << "下一方块:";
    gotoXY(hOut, 26, 9);
    cout << "操作方法:";
    gotoXY(hOut, 30, 11);
    cout << "↑:旋转 ↓:速降";
    gotoXY(hOut, 30, 12);
    cout << "→:右移 ←:左移";
    gotoXY(hOut, 30, 13);
    cout << "空格键:开始/暂停";
    gotoXY(hOut, 30, 14);
    cout << "Esc 键:退出";
    gotoXY(hOut, 26, 16);
    cout << "关    于:";
    gotoXY(hOut, 30, 18);
    cout << "俄罗斯方块V1.0";
    gotoXY(hOut, 35, 19);
    cout << "作者:潘约尔";
}

void gotoXY(HANDLE hOut, int x, int y)
{
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hOut, pos);
}

void roundBlock(HANDLE hOut, int block[4][4])
{
    clearBlock(hOut, block, 5, 15);
    switch (rand() % 19)
    {
    case 0:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block00[i][j];
            }
        }
        break;
    case 1:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block01[i][j];
            }
        }
        break;
    case 2:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block02[i][j];
            }
        }
        break;
    case 3:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block03[i][j];
            }
        }
        break;
    case 4:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block04[i][j];
            }
        }
        break;
    case 5:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block05[i][j];
            }
        }
        break;
    case 6:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block06[i][j];
            }
        }
        break;
    case 7:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block07[i][j];
            }
        }
        break;
    case 8:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block08[i][j];
            }
        }
        break;
    case 9:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block09[i][j];
            }
        }
        break;
    case 10:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block10[i][j];
            }
        }
        break;
    case 11:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block11[i][j];
            }
        }
        break;
    case 12:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block12[i][j];
            }
        }
        break;
    case 13:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block13[i][j];
            }
        }
        break;
    case 14:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block14[i][j];
            }
        }
        break;
    case 15:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block15[i][j];
            }
        }
        break;
    case 16:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block16[i][j];
            }
        }
        break;
    case 17:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block17[i][j];
            }
        }
        break;
    case 18:
        for (int i = 0; i < 4; ++i)
        {
            for (int j = 0; j < 4; ++j)
            {
                block[i][j] = block18[i][j];
            }
        }
        break;
    default:
        break;
    }
    printBlock(hOut, block, 5, 15);
}

bool collisionDetection(int block[4][4], int map[21][12], int x, int y)
{
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            if (x + i >= 0 && y + j >= 0 && map[x + i][y + j] == 1 && block[i][j] == 1)
            {
                return false;
            }
        }
    }
    return true;
}

void printBlock(HANDLE hOut, int block[4][4], int x, int y)
{
    switch (block[0][0])
    {
    case 10:
    case 11:
        SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
        break;
    case 12:
    case 13:
    case 14:
    case 15:
        SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        break;
    case 16:
    case 17:
    case 18:
    case 19:
        SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        break;
    case 20:
    case 21:
    case 22:
    case 23:
        SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
        break;
    case 24:
    case 25:
        SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        break;
    case 26:
    case 27:
        SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
        break;
    case 28:
        SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
        break;
    default:
        break;
    }
    for (int i = 0; i < 4; ++i)
    {
        if (i + x >= 0)
        {
            for (int j = 0; j < 4; ++j)
            {
                if (block[i][j] == 1)
                {

                    gotoXY(hOut, 2 * (y + j), x + i);
                    cout << "■";
                }
            }
        }
    }
}

void clearBlock(HANDLE hOut, int block[4][4], int x, int y)
{
    for (int i = 0; i < 4; ++i)
    {
        if (i + x >= 0)
        {
            for (int j = 0; j < 4; ++j)
            {
                if (block[i][j] == 1)
                {
                    gotoXY(hOut, 2 * (y + j), x + i);
                    cout << "  ";
                }
            }
        }
    }
}

void gameOver(HANDLE hOut, int block[4][4], int map[21][12])
{
    SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
    gotoXY(hOut, 9, 8);
    cout << "GAME OVER";
    gotoXY(hOut, 8, 9);
    cout << "空格键:重来";
    gotoXY(hOut, 8, 10);
    cout << "ESC键:退出";
    char key;
    while (true)
    {
        key = _getch();
        if (key == 32)
        {
            return;
        }
        if (key == 27)
        {
            exit(0);
        }
    }
}

int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y)
{
    if (collisionDetection(block, map, x + 1, y))
    {
        clearBlock(hOut, block, x, y);
        ++x;
        return 0;
    }
    if (x < 0)
    {
        return 2;
    }
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            if (block[i][j] == 1)
            {
                map[x + i][y + j] = 1;
                SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
                gotoXY(hOut, 2 * (y + j), x + i);
                cout << "■";
            }
        }
    }
    return 1;
}

void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
    if (collisionDetection(block, map, x, y - 1))
    {
        clearBlock(hOut, block, x, y);
        --y;
    }
}

void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
    if (collisionDetection(block, map, x, y + 1))
    {
        clearBlock(hOut, block, x, y);
        ++y;
    }
}

void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
    switch (block[0][0])
    {
    case 10:
        if (collisionDetection(block01, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block01[i][j];
                }
            }
        }
        break;
    case 11:
        if (collisionDetection(block00, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block00[i][j];
                }
            }
        }
        else if (collisionDetection(block00, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block00[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block00, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block00[i][j];
                }
            }
            ++y;
        }
        else if (collisionDetection(block00, map, x, y - 2))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block00[i][j];
                }
            }
            y = y - 2;
        }
        else if (collisionDetection(block00, map, x, y + 2))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block00[i][j];
                }
            }
            y = y + 2;
        }
        break;
    case 12:
        if (collisionDetection(block03, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block03[i][j];
                }
            }
        }
        else if (collisionDetection(block03, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block03[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block03, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block03[i][j];
                }
            }
            ++y;
        }
        break;
    case 13:
        if (collisionDetection(block04, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block04[i][j];
                }
            }
        }
        else if (collisionDetection(block04, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block04[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block04, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block04[i][j];
                }
            }
            ++y;
        }
        break;
    case 14:
        if (collisionDetection(block05, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block05[i][j];
                }
            }
        }
        else if (collisionDetection(block05, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block05[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block05, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block05[i][j];
                }
            }
            ++y;
        }
        break;
    case 15:
        if (collisionDetection(block02, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block02[i][j];
                }
            }
        }
        else if (collisionDetection(block02, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block02[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block02, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block02[i][j];
                }
            }
            ++y;
        }
        break;

    case 16:
        if (collisionDetection(block07, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block07[i][j];
                }
            }
        }
        else if (collisionDetection(block07, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block07[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block07, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block07[i][j];
                }
            }
            ++y;
        }
        break;
    case 17:
        if (collisionDetection(block08, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block08[i][j];
                }
            }
        }
        else if (collisionDetection(block08, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block08[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block08, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block08[i][j];
                }
            }
            ++y;
        }
        break;
    case 18:
        if (collisionDetection(block09, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block09[i][j];
                }
            }
        }
        else if (collisionDetection(block09, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block09[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block09, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block09[i][j];
                }
            }
            ++y;
        }
        break;
    case 19:
        if (collisionDetection(block06, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block06[i][j];
                }
            }
        }
        else if (collisionDetection(block06, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block06[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block06, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block06[i][j];
                }
            }
            ++y;
        }
        break;
    case 20:
        if (collisionDetection(block11, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block11[i][j];
                }
            }
        }
        else if (collisionDetection(block11, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block11[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block11, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block11[i][j];
                }
            }
            ++y;
        }
        break;
    case 21:
        if (collisionDetection(block12, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block12[i][j];
                }
            }
        }
        else if (collisionDetection(block12, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block12[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block12, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block12[i][j];
                }
            }
            ++y;
        }
        break;
    case 22:
        if (collisionDetection(block13, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block13[i][j];
                }
            }
        }
        else if (collisionDetection(block13, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block13[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block13, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block13[i][j];
                }
            }
            ++y;
        }
        break;
    case 23:
        if (collisionDetection(block10, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block10[i][j];
                }
            }
        }
        else if (collisionDetection(block10, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block10[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block10, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block10[i][j];
                }
            }
            ++y;
        }
        break;
    case 24:
        if (collisionDetection(block15, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block15[i][j];
                }
            }
        }
        else if (collisionDetection(block15, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block15[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block15, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block15[i][j];
                }
            }
            ++y;
        }
        break;
    case 25:
        if (collisionDetection(block14, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block14[i][j];
                }
            }
        }
        else if (collisionDetection(block14, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block14[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block14, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block14[i][j];
                }
            }
            ++y;
        }
        break;
    case 26:
        if (collisionDetection(block17, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block17[i][j];
                }
            }
        }
        else if (collisionDetection(block17, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block17[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block17, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block17[i][j];
                }
            }
            ++y;
        }
        break;
    case 27:
        if (collisionDetection(block16, map, x, y))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block16[i][j];
                }
            }
        }
        else if (collisionDetection(block16, map, x, y - 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block16[i][j];
                }
            }
            --y;
        }
        else if (collisionDetection(block16, map, x, y + 1))
        {
            clearBlock(hOut, block, x, y);
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    block[i][j] = block16[i][j];
                }
            }
            ++y;
        }
        break;
    default:
        break;
    }
}

void myStop(HANDLE hOut, int block[4][4])
{
    clearBlock(hOut, block, 5, 15);
    SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
    gotoXY(hOut, 30, 7);
    cout << "游戏暂停";
    char key;
    while (true)
    {
        key = _getch();
        if (key == 32)
        {
            gotoXY(hOut, 30, 7);
            cout << "        ";
            printBlock(hOut, block, 5, 15);
            return;
        }
        if (key == 27)
        {
            exit(0);
        }
    }
}

void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint)
{
    SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
    for (int i = 19; i >= 0; --i)
    {
        int x = 0;
        for (int j = 1; j < 11; ++j)
        {
            x += map[i][j];
        }
        if (x == 10)
        {
            fraction += 100;
            if (val > 1 && fraction / 1000 + 1 != checkpoint)
            {
                checkpoint = fraction / 1000 + 1;
                val -= 5;
            }
            for (int m = i; m > 0; --m)
            {
                for (int n = 1; n < 11; ++n)
                {
                    map[m][n] = map[m - 1][n];
                    gotoXY(hOut, 2 * n, m);
                    if (map[m][n] == 1)
                    {
                        cout << "■";
                    }
                    else
                    {
                        cout << "  ";
                    }
                }
            }
            ++i;
        }
    }
    gotoXY(hOut, 36, 1);
    cout << fraction;
    gotoXY(hOut, 36, 2);
    cout << checkpoint;
}

狼人杀

原创

代码如下:

#include<bits/stdc++.h>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<windows.h>
using namespace std;
struct IDname
{
    int geshu;
    string NAME;
};
IDname jue_se[100];
struct ID
{
    int num;
    bool life;
    string name;
    int know;
    int how;
};
ID player[21];
int n, MY, kill1, kill2;
char a;
bool jieyao = 1, duyao = 1;
int lieren, shouwei = 0;
void init1()
{
    jue_se[1].NAME = "村民 ";
    jue_se[2].NAME = "狼人 ";
    jue_se[3].NAME = "女巫 ";
    jue_se[4].NAME = "预言家 ";
    jue_se[5].NAME = "猎人 ";
    jue_se[6].NAME = "守卫 ";
}
void init2(int nn)
{
    switch (nn)
    {
    case 6:
        jue_se[1].geshu = 3;
        jue_se[2].geshu = 2;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 0;
        jue_se[5].geshu = 0;
        jue_se[6].geshu = 0;
        break;
    case 7:
        jue_se[1].geshu = 3;
        jue_se[2].geshu = 2;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 0;
        jue_se[6].geshu = 0;
        break;
    case 8:
        jue_se[1].geshu = 3;
        jue_se[2].geshu = 3;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 0;
        jue_se[6].geshu = 0;
        break;
    case 9:
        jue_se[1].geshu = 3;
        jue_se[2].geshu = 3;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 1;
        jue_se[6].geshu = 0;
        break;
    case 10:
        jue_se[1].geshu = 4;
        jue_se[2].geshu = 3;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 1;
        jue_se[6].geshu = 0;
        break;
    case 11:
        jue_se[1].geshu = 4;
        jue_se[2].geshu = 4;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 1;
        jue_se[6].geshu = 0;
        break;
    case 12:
        jue_se[1].geshu = 4;
        jue_se[2].geshu = 4;
        jue_se[3].geshu = 1;
        jue_se[4].geshu = 1;
        jue_se[5].geshu = 1;
        jue_se[6].geshu = 1;
        break;
    default:
        cout << "输入错误,再见" << endl;
        exit(0);
        break;
    }
}
int van[10] = { 7,4,6,43,35,1,2,8,20,19 };
void init3(int nn)
{
    srand(time(0));
    Sleep(rand() % 44);
    int x = 10000;
    int t = rand();
    srand(time(NULL));
    int y = van[(rand() % 100 * van[rand() % 10] + t) % 10];
    if (nn <= 6)
        x = abs(x * 6 / y) % 3 + 1;
    else if (nn <= 8)
        x = abs(x * 7 / y) % 4 + 1;
    else if (nn <= 11)
        x = abs(x * 8 / y) % 5 + 1;
    else if (nn <= 14)
        x = abs(x * 9 / y) % 6 + 1;
    do
    {
        if (nn <= 6)
            x = x % 3 + 1;
        else if (nn <= 8)
            x = x % 4 + 1;
        else if (nn <= 11)
            x = x % 5 + 1;
        else if (nn <= 14)
            x = x % 6 + 1;
        if (jue_se[x].geshu > 0)
        {
            player[nn].name = jue_se[x].NAME;
            if (player[nn].name == "猎人 ")
                lieren = nn;
            if (player[nn].name == "守卫 ")
                shouwei = nn;
            player[nn].life = 1;
            player[nn].num = nn;
            player[nn].know = 0;
            jue_se[x].geshu--;
            player[nn].how = 0;
            break;
        }
    } while (jue_se[x].geshu == 0);
}
void printhhh()
{
    int cm = 0;
    int sz = 0;
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 0)
            continue;
        else if (player[i].name == "村民 ")
            cm++;
        else if (player[i].name == "女巫 " || player[i].name == "预言家 " || player[i].name == "猎人 " || player[i].name == "守卫 ")
            sz++;
    }
    if (sz == 0 || cm == 0)
        cout << "狼人阵营胜利" << endl;
    else
        cout << "好人阵营胜利" << endl;
    for (int i = 1; i <= n; i++)
    {
        cout << left << setw(3) << player[i].num << ": " << player[i].name << " ";
        if (player[i].life == 0)
            cout << "死亡" << "\t";
        else
            cout << "存活" << "\t";
        if (player[i].how == 0)
            cout << "最终存活 " << endl;
        else if (player[i].how == 1)
            cout << "最终被狼人杀死" << endl;
        else if (player[i].how == 2)
            cout << "最终被投票投死" << endl;
        else if (player[i].how == 3)
            cout << "最终被女巫毒死" << endl;
        else if (player[i].how == 4)
            cout << "最终被猎人射杀" << endl;
    }
    system("pause");
    system("pause");
    system("pause");
}
void print(int day, int ti)
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    if (ti == 0)
        SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY);
    else
        SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << "\t\t\t\t第" << day << "天 ";
    if (ti == 0)
        cout << "白天" << endl;
    else
        cout << "夜晚" << endl;
    cout << "我的位置:" << MY << "号" << endl;
    for (int i = 1; i <= 6; i++)
    {
        cout << player[i].num << "号位 ";
    }
    cout << endl;
    for (int i = 1; i <= 6; i++)
    {
        if (player[i].life == 1)
        {
            if (ti == 0)
                SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            else
                SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            cout << "存活 ";
        }
        else
        {
            if (ti == 0)
                SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
            else
                SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << "已死亡 ";
        }
    }
    if (ti == 0)
        SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY);
    else
        SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << endl;
    for (int i = 1; i <= 6; i++)
    {
        if (player[i].know == 0)
            cout << "未知 ";
        else if (player[i].know == 1)
        {
            if (player[i].name == "狼人 ")
                cout << "狼人 ";
            else
                cout << "好人 ";
        }
        else if (player[i].know == 2)
            cout << player[i].name << " ";
    }
    cout << endl << endl;
    for (int i = 7; i <= n; i++)
    {
        if (i < 10)
            cout << player[i].num << "号位 ";
        else
            cout << player[i].num << "号位 ";
    }
    cout << endl;
    for (int i = 7; i <= n; i++)
    {
        if (player[i].life == 1)
        {
            if (ti == 0)
                SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            else
                SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
            cout << "存活 ";
        }
        else
        {
            if (ti == 0)
                SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED);
            else
                SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED);
            cout << "已死亡 ";
        }
    }
    if (ti == 0)
        SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY);
    else
        SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout << endl;
    for (int i = 7; i <= n; i++)
    {
        if (player[i].know == 0)
            cout << "未知 ";
        else if (player[i].know == 1)
        {
            if (player[i].name == "狼人 ")
                cout << "狼人 ";
            else
                cout << "好人 ";
        }
        else if (player[i].know == 2)
            cout << player[i].name << " ";
    }
    cout << endl << endl;
}
int shou = 0;
void shoushui(int hhh, int hhhh)
{
    int x;
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "守~卫~请~睁~眼~~~" << endl;
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    if (MY == shouwei && player[MY].life == 1)
    {
        cout << "请问你要守护谁?" << endl << "输入:";
        cin >> x;
        while (x == shou || x<1 || x>n || player[x].life == 0)
        {
            cout << "输入错误,请重新输入" << endl << "输入:";
            cin >> x;
        }
        shou = x;
    }
    else if (player[shouwei].life == 1)
    {
        cout << "请问你要守护谁?" << endl;
        Sleep(rand() % 98);
        srand(time(0));
        x = rand() % n + 1;
        while (x == shou || player[x].life == 0)
        {
            Sleep(rand() % 98);
            srand(time(0));
            x = rand() % n + 1;
        }
        shou = x;
    }
    else
    {
        cout << "请问你要守护谁?" << endl;
        Sleep(3000);
        shou = -1;
    }
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "守~卫~请~闭~眼~~~" << endl;
}
struct tou
{
    int xxx;
    int num;
    int toupiaoquan;
};
tou TOU[13];
bool cmp(tou x, tou y)
{
    if (x.xxx == y.xxx)
        return x.num < y.num;
    return x.xxx > y.xxx;
}
bool cmp1(tou x, tou y)
{
    return x.num < y.num;
}
void toupiao(int ddd, int nnn)
{
    //--------1--------
    int x;
    Sleep(2000);
    system("cls");
    print(ddd, nnn);
    cout << "现在大家请投票";
    for (int i = 1; i <= 3; i++)
    {
        cout << ".";
        Sleep(500);
    }
    cout << endl;
    for (int i = 1; i <= n; i++)
    {
        TOU[i].num = i;
        TOU[i].toupiaoquan = 1;
        TOU[i].xxx = 0;
    }
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 1)
        {
            Sleep(3000);
            if (i == MY)
            {
                cout << "请投票...(0弃权)" << endl;
                cin >> x;
                while (player[x].life == 0 && x != 0)
                {
                    cin >> x;
                }
                if (x == 0)
                    cout << MY << "号玩家弃权" << endl;
                else
                    cout << MY << "号玩家投给了" << x << "号玩家" << endl;
            }
            else
            {
                srand(time(0));
                if (player[i].name == "狼人 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name == "狼人 " || x == i))
                    {
                        Sleep(rand() % 98);
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else if (player[i].name == "预言家 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name != "狼人 " || x == i))
                    {
                        Sleep(rand() % 98);
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || x == i))
                    {
                        Sleep(rand() % 98);
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
            }
            if (x != 0)
                TOU[x].xxx++;
        }
    }
    Sleep(3000);
    sort(TOU + 1, TOU + n + 1, cmp);
    if (TOU[2].xxx != TOU[1].xxx)
    {
        cout << "投票结束," << TOU[1].num << "号投票出局" << endl;
        player[TOU[1].num].life = 0;
        player[TOU[1].num].how = 2;
        Sleep(3000);
        return;
    }
    else
    {
        TOU[1].toupiaoquan = 0;
        TOU[2].toupiaoquan = 0;
        system("cls");
        print(ddd, nnn);
        cout << TOU[1].num << "号," << TOU[2].num << "号";
        int i;
        for (i = 3; i <= n; i++)
        {
            if (TOU[i].xxx == TOU[1].xxx)
            {
                TOU[i].toupiaoquan = 0;
                cout << "," << TOU[i].num << "号";
            }
            else
                break;
        }
        if (i == n + 1)
        {
            for (int i = 1; i <= n; i++)
                TOU[i].toupiaoquan = 1;
        }
        cout << "平票" << endl;
    }
    //--------2--------
    sort(TOU + 1, TOU + n + 1, cmp1);
    cout << "请再次投票";
    for (int i = 1; i <= 3; i++)
    {
        cout << ".";
        Sleep(500);
    }
    cout << endl;
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 1 && TOU[i].toupiaoquan == 1)
        {
            Sleep(3000);
            if (i == MY)
            {
                cout << "请投票...(0弃权)" << endl;
                cin >> x;
                while ((player[x].life == 0 || TOU[x].toupiaoquan == 1) && x != 0)
                {
                    cin >> x;
                }
                if (x == 0)
                    cout << MY << "号玩家弃权" << endl;
                else
                    cout << MY << "号玩家投给了" << x << "号玩家" << endl;
            }
            else
            {
                srand(time(0));
                if (player[i].name == "狼人 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name == "狼人 " || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else if (player[i].name == "预言家 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name != "狼人 " || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
            }
            if (x != 0 && TOU[i].toupiaoquan == 1)
                TOU[x].xxx++;
        }
    }
    Sleep(3000);
    sort(TOU + 1, TOU + n + 1, cmp);
    if (TOU[2].xxx != TOU[1].xxx)
    {
        cout << "投票结束," << TOU[1].num << "号投票出局" << endl;
        player[TOU[1].num].life = 0;
        player[TOU[1].num].how = 2;
        Sleep(3000);
        return;
    }
    else
    {
        TOU[1].toupiaoquan = 0;
        TOU[2].toupiaoquan = 0;
        system("cls");
        print(ddd, nnn);
        cout << TOU[1].num << "号," << TOU[2].num << "号";
        int i;
        for (i = 3; i <= n; i++)
        {
            if (TOU[i].xxx == TOU[1].xxx)
            {
                TOU[i].toupiaoquan = 0;
                cout << "," << TOU[i].num << "号";
            }
            else
                break;
        }
        if (i == n + 1)
        {
            for (int i = 1; i <= n; i++)
                TOU[i].toupiaoquan = 1;
        }
        cout << "平票" << endl;
    }
    //--------3--------
    sort(TOU + 1, TOU + n + 1, cmp1);
    cout << "请再次投票";
    for (int i = 1; i <= 3; i++)
    {
        cout << ".";
        Sleep(500);
    }
    cout << endl;
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 1 && TOU[i].toupiaoquan == 1)
        {
            Sleep(3000);
            if (i == MY)
            {
                cout << "请投票...(0弃权)" << endl;
                cin >> x;
                while ((player[x].life == 0 || TOU[x].toupiaoquan == 1) && x != 0)
                {
                    cin >> x;
                }
                if (x == 0)
                    cout << MY << "号玩家弃权" << endl;
                else
                    cout << MY << "号玩家投给了" << x << "号玩家" << endl;
            }
            else
            {
                srand(time(0));
                if (player[i].name == "狼人 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name == "狼人 " || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else if (player[i].name == "预言家 " || player[i].name == "猎人 ")
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || player[x].name != "狼人 " || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
                else
                {
                    x = rand() % (n + 1);
                    while (x != 0 && (player[x].life == 0 || x == i || TOU[x].toupiaoquan == 1))
                    {
                        srand(time(0));
                        x = rand() % (n + 1);
                    }
                    if (x == 0)
                        cout << i << "号玩家弃权" << endl;
                    else
                        cout << i << "号玩家投给了" << x << "号玩家" << endl;
                }
            }
            if (x != 0 && TOU[i].toupiaoquan == 1)
                TOU[x].xxx++;
        }
    }
    Sleep(3000);
    sort(TOU + 1, TOU + n + 1, cmp);
    if (TOU[2].xxx != TOU[1].xxx)
    {
        cout << "投票结束," << TOU[1].num << "号投票出局" << endl;
        player[TOU[1].num].life = 0;
        player[TOU[1].num].how = 2;
    }
    else
    {
        cout << "投票结束,无人出局" << endl;
    }
    Sleep(5000);
}
bool game_over()
{
    int pingmin = 0;
    int langren = 0;
    int shenzhi = 0;
    for (int i = 1; i <= n; i++)
    {
        if (player[i].life == 0)
            continue;
        if (player[i].name == "狼人 ")
            langren++;
        else if (player[i].name == "村民 ")
            pingmin++;
        else if (player[i].name == "女巫 " || player[i].name == "预言家 " || player[i].name == "猎人 ")
            shenzhi++;
    }
    if (shenzhi == 0 || langren == 0 || pingmin == 0)
        return 1;
    return 0;
}
void night()
{
    system("cls");
    system("color 0f");
    print(1, 1);
    cout << "天~黑~请~闭~眼~~~" << endl;
    if (n >= 12)
        shoushui(1, 1);
    Sleep(3000);
    system("cls");
    print(1, 1);
    cout << "狼~人~请~睁~眼~~~" << endl;
    if (player[MY].name == "狼人 ")
    {
        Sleep(1000);
        cout << "你的同伴有:";
        for (int i = 1; i <= n; i++)
        {
            if (i == MY)
                continue;
            if (player[i].name == "狼人 ")
            {
                cout << player[i].num << "号 ";
                player[i].know = 2;
            }
        }
        Sleep(3000);
        cout << endl << "请问你们要杀谁:" << endl << "输入:";
        cin >> kill1;
        Sleep(1500);
        system("cls");
        print(1, 1);
        cout << "今晚你们要杀的是" << kill1 << "号玩家" << endl;
    }
    else
    {
        Sleep(4000);
        system("cls");
        print(1, 1);
        cout << "请问你们要杀谁?" << endl;
        do
        {
            Sleep(rand() % 18);
            srand(time(0));
            int x = rand() % n + 1;
            if (player[x].name != "狼人 " && player[x].life == 1)
            {
                kill1 = x;
                break;
            }
        } while (1);
        Sleep(5000);
    }
    Sleep(3000);
    system("cls");
    print(1, 1);
    cout << "狼~人~请~闭~眼~~~" << endl;
    Sleep(2000);
    system("cls");
    print(1, 1);
    cout << "女~巫~请~睁~眼~~~" << endl;
    Sleep(2000);
    system("cls");
    print(1, 1);
    if (player[MY].name == "女巫 " && player[MY].life == 1)
    {
        Sleep(1000);
        if (jieyao == 1)
        {
            cout << "今晚" << kill1 << "号玩家被杀" << endl;
            Sleep(500);
            cout << "请问你要救吗???" << endl << "A. 救 B.不救" << endl << "输入:";
            cin >> a;
            if (a == 'A')
            {
                system("cls");
                print(1, 1);
                cout << "请问你要毒吗???" << endl;
                Sleep(2000);
                system("cls");
                print(1, 1);
                cout << "今晚" << kill1 << "号玩家被你解救" << endl;
                jieyao = 0;
                if (shou != kill1)
                    kill1 = 0;
            }
            else
            {
                if (shou == kill1)
                    kill1 = 0;
                Sleep(2000);
                system("cls");
                print(1, 1);
                cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
                cin >> a;
                if (a == 'A')
                {
                    cout << "请问你要毒谁???" << endl << "输入:";
                    cin >> kill2;
                    while (player[kill2].life != 1)
                    {
                        cout << "输入错误,请重新输入:" << endl;
                        cin >> kill2;
                    }
                    duyao = 0;
                }
            }
        }
        else
        {
            if (shou == kill1)
                kill1 = 0;
            Sleep(2000);
            system("cls");
            print(1, 1);
            cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
            cin >> a;
            if (a == 'A')
            {
                cout << "请问你要毒谁???" << endl << "输入:";
                cin >> kill2;
                while (player[kill2].life != 1)
                {
                    cout << "输入错误,请重新输入:" << endl;
                    cin >> kill2;
                }
                duyao = 0;
            }
        }
    }
    else
    {
        bool b = 0;
        cout << "请问你是否要用解药???" << endl;
        int FFF = 0, kkkk;
        for (int i = 1; i <= n; i++)
        {
            if (player[i].life == 1 && player[i].name == "村民 ")
                FFF++;
            if (player[i].name == "女巫 ")
                kkkk = i;
        }
        if (jieyao == 1 && player[kkkk].life == 1)
        {
            if (FFF == 1)
            {
                if (shou == kill1)
                    jieyao = 1;
                else
                    jieyao = 0;
                kill1 = 0;
                b = 1;
            }
            else
                for (int i = 1; i <= n; i++)
                {
                    if (player[i].name == "女巫 " && kill1 == i)
                    {
                        kill1 = 0;
                        if (shou == kill1)
                            jieyao = 1;
                        else
                            jieyao = 0;
                        b = 1;
                        break;
                    }
                    else if (player[i].name == "预言家 " && kill1 == i)
                    {
                        kill1 = 0;
                        if (shou == kill1)
                            jieyao = 1;
                        else
                            jieyao = 0;
                        b = 1;
                        break;
                    }
                }
        }
        Sleep(3000);
        if (b == 0 && duyao == 1 && player[kkkk].life == 1)
        {
            system("cls");
            print(1, 1);
            cout << "请问你是否要用毒药???" << endl;
            srand(time(0));
            int x = rand() % 2;
            Sleep(1500);
            cout << "请问你要毒谁???" << endl;
            if (x == 1)
            {
                duyao = 0;
                int y = rand() % n + 1;
                while ((player[y].name == "女巫 " || player[y].name == "预言家 " || y == kill1) || player[y].life == 0)
                    y = rand() % n + 1;
                kill2 = y;
            }
        }
        else
        {
            Sleep(3000);
            system("cls");
            print(1, 1);
            cout << "请问你是否要用毒药???" << endl;
            Sleep(3000);
            cout << "请问你要毒谁???" << endl;
            Sleep(3000);
        }
    }
    Sleep(3000);
    system("cls");
    print(1, 1);
    cout << "女~巫~请~闭~眼~~~" << endl;
    if (n > 6)
    {
        Sleep(3000);
        system("cls");
        print(1, 1);
        cout << "预~言~家~请~睁~眼~~~" << endl;
        if (player[MY].name == "预言家 ")
        {
            Sleep(3000);
            cout << "请问你想查验谁???" << endl << "输入:";
            int x;
            cin >> x;
            player[x].know = 1;
            Sleep(2000);
            system("cls");
            print(1, 1);
            cout << "他的身份是:";
            if (player[x].name == "狼人 ")
                cout << "狼人" << endl;
            else
                cout << "好人" << endl;
            Sleep(3000);
        }
        else
        {
            Sleep(3000);
            cout << "请问你想查验谁???" << endl;
            Sleep(3000);
            system("cls");
            print(1, 1);
            cout << "他的身份是:......";
            Sleep(3000);
        }
        Sleep(3000);
        system("cls");
        print(1, 1);
        cout << "预~言~家~请~闭~眼~~~" << endl;
    }
    Sleep(3000);
    if (kill1 != 0)
        player[kill1].life = 0;
    if (kill2 != 0)
        player[kill2].life = 0;
    player[kill1].how = 1;
    player[kill2].how = 3;
    system("cls");
    system("color F0");
    print(2, 0);
}
void night2(int hhh, int hhhh)
{
    system("cls");
    system("color 0f");
    print(hhh, hhhh);
    cout << "天~黑~请~闭~眼~~~" << endl;
    if (n >= 12)
        shoushui(hhh, hhhh);
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "狼~人~请~睁~眼~~~" << endl;
    if (player[MY].name == "狼人 " && player[MY].life == 1)
    {
        Sleep(3000);
        cout << endl << "请问你们要杀谁:" << endl << "输入:";
        cin >> kill1;
        Sleep(1500);
        system("cls");
        print(hhh, hhhh);
        cout << "今晚你们要杀的是" << kill1 << "号玩家" << endl;
    }
    else
    {
        Sleep(4000);
        system("cls");
        print(hhh, hhhh);
        cout << "请问你们要杀谁?" << endl;
        do
        {
            srand(time(0));
            int x = rand() % n + 1;
            if (player[x].name != "狼人 " && player[x].life == 1)
            {
                kill1 = x;
                break;
            }
        } while (1);
        Sleep(5000);
    }
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "狼~人~请~闭~眼~~~" << endl;
    Sleep(2000);
    system("cls");
    print(hhh, hhhh);
    cout << "女~巫~请~睁~眼~~~" << endl;
    Sleep(2000);
    system("cls");
    print(hhh, hhhh);
    if (player[MY].name == "女巫 " && player[MY].life == 1)
    {
        Sleep(1000);
        if (jieyao == 1)
        {
            cout << "今晚" << kill1 << "号玩家被杀" << endl;
            Sleep(500);
            cout << "请问你要救吗???" << endl << "A. 救 B.不救" << endl << "输入:";
            cin >> a;
            if (a == 'A')
            {
                system("cls");
                print(hhh, hhhh);
                cout << "请问你要毒吗???" << endl;
                Sleep(2000);
                system("cls");
                print(hhh, hhhh);
                cout << "今晚" << kill1 << "号玩家被你解救" << endl;
                jieyao = 0;
                if (shou != kill1)
                    kill1 = 0;
            }
            else
            {
                if (shou == kill1)
                    kill1 = 0;
                Sleep(2000);
                system("cls");
                print(hhh, hhhh);
                cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
                cin >> a;
                if (a == 'A')
                {
                    cout << "请问你要毒谁???" << endl << "输入:";
                    cin >> kill2;
                    while (player[kill2].life != 1)
                    {
                        cout << "输入错误,请重新输入:" << endl;
                        cin >> kill2;
                    }
                    duyao = 0;
                }
            }
        }
        else if (duyao == 1)
        {
            if (shou == kill1)
                kill1 = 0;
            Sleep(2000);
            system("cls");
            print(hhh, hhhh);
            cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
            cin >> a;
            if (a == 'A')
            {
                cout << "请问你要毒谁???" << endl << "输入:";
                cin >> kill2;
                while (player[kill2].life != 1)
                {
                    cout << "输入错误,请重新输入:" << endl;
                    cin >> kill2;
                }
                duyao = 0;
            }
        }
        else
        {
            Sleep(2000);
            system("cls");
            print(hhh, hhhh);
            cout << "请问你要毒吗???" << endl;
        }
    }
    else
    {
        bool b = 0;
        cout << "请问你是否要用解药???" << endl;
        int FFF = 0, kkkk;
        for (int i = 1; i <= n; i++)
        {
            if (player[i].life == 1 && player[i].name == "村民 ")
                FFF++;
            if (player[i].name == "女巫 ")
                kkkk = i;
        }
        if (jieyao == 1 && player[kkkk].life == 1)
        {
            if (FFF == 1)
            {
                if (shou == kill1)
                    jieyao = 1;
                else
                    jieyao = 0;
                kill1 = 0;
                b = 1;
            }
            else
                for (int i = 1; i <= n; i++)
                {
                    if (player[i].name == "女巫 " && kill1 == i)
                    {
                        kill1 = 0;
                        if (shou == kill1)
                            jieyao = 1;
                        else
                            jieyao = 0;
                        b = 1;
                        break;
                    }
                    else if (player[i].name == "预言家 " && kill1 == i)
                    {
                        kill1 = 0;
                        if (shou == kill1)
                            jieyao = 1;
                        else
                            jieyao = 0;
                        b = 1;
                        break;
                    }
                }
        }
        Sleep(3000);
        if (b == 0 && duyao == 1 && player[kkkk].life == 1)
        {
            system("cls");
            print(hhh, hhhh);
            cout << "请问你是否要用毒药???" << endl;
            srand(time(0));
            int x = rand() % 2;
            Sleep(1500);
            cout << "请问你要毒谁???" << endl;
            if (x == 1)
            {
                duyao = 0;
                int y = rand() % n + 1;
                while ((player[y].name == "女巫 " || player[y].name == "预言家 " || y == kill1) || player[y].life == 0)
                    y = rand() % n + 1;
                kill2 = y;
            }
        }
        else
        {
            Sleep(3000);
            system("cls");
            print(hhh, hhhh);
            cout << "请问你是否要用毒药???" << endl;
            Sleep(3000);
            cout << "请问你要毒谁???" << endl;
            Sleep(3000);
        }
    }
    Sleep(3000);
    system("cls");
    print(hhh, hhhh);
    cout << "女~巫~请~闭~眼~~~" << endl;
    if (n > 6)
    {
        Sleep(3000);
        system("cls");
        print(hhh, hhhh);
        cout << "预~言~家~请~睁~眼~~~" << endl;
        if (player[MY].name == "预言家 " && player[MY].life == 1)
        {
            Sleep(3000);
            cout << "请问你想查验谁???" << endl << "输入:";
            int x;
            cin >> x;
            player[x].know = 1;
            Sleep(2000);
            system("cls");
            print(hhh, hhhh);
            cout << "他的身份是:";
            if (player[x].name == "狼人 ")
                cout << "狼人" << endl;
            else
                cout << "好人" << endl;
            Sleep(3000);
        }
        else
        {
            Sleep(3000);
            cout << "请问你想查验谁???" << endl;
            Sleep(3000);
            system("cls");
            print(hhh, hhhh);
            cout << "他的身份是:......";
            Sleep(3000);
        }
        Sleep(3000);
        system("cls");
        print(hhh, hhhh);
        cout << "预~言~家~请~闭~眼~~~" << endl;
    }
    Sleep(3000);
    if (kill1 != 0)
        player[kill1].life = 0;
    if (kill2 != 0)
        player[kill2].life = 0;
    player[kill1].how = 1;
    player[kill2].how = 3;
    system("cls");
    system("color F0");
    print(hhh + 1, 0);
}
bool lr = 0;
void panduanlieren()
{
    if (lr == 1)
        return;
    if (MY == lieren)
    {
        cout << "请射杀一名玩家" << endl;
        int x;
        cin >> x;
        while (player[x].life != 1)
        {
            cout << "输入错误,请重新输入" << endl;
            cin >> x;
        }
        Sleep(1000);
        cout << lieren << "号猎人发动技能,开枪带走了" << x << "号" << endl;
        player[x].life = 0;
        player[x].how = 4;
    }
    else if (n >= 9)
    {
        srand(time(0));
        int x = rand() % n + 1;
        while (player[x].life != 1)
        {
            x = rand() % n + 1;
        }
        Sleep(1000);
        cout << lieren << "号猎人发动技能,开枪带走了" << x << "号" << endl;
        player[x].life = 0;
        player[x].how = 4;
    }
    lr = 1;
}
void print1()
{
    cout << "天亮了,昨晚";
    if (kill1 != 0 || kill2 != 0)
    {
        cout << kill1 << "号";
        if (kill2 != 0)
        {
            cout << "," << kill2 << "号";
            kill2 = 0;
        }
        cout << "被杀" << endl;
    }
    else
        cout << "是平安夜" << endl;
}
int main()
{
    system("cls");
    cout << " " << "狼人杀online" << endl;
    cout << "请输入人数个数:" << endl;
    scanf("%d", &n);
    cout << "加载时间长,请耐心等待";
    init1();
    init2(n);
    int k = 1;
    do
    {
        srand(time(0));
        init3(k);
        cout << ".";
        Sleep(17);
        k++;
    } while (k <= n);
    system("cls");
    system("color F0");
    cout << "游戏即将开始";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    Sleep(1500);
    cout << endl << endl << "请大家查看身份牌......" << endl;
    Sleep(45);
    srand(time(0));
    MY = rand() % n + 1;
    cout << "您的身份是:" << player[MY].name << endl;
    Sleep(500);
    cout << "在" << player[MY].num << "号位上" << endl;
    system("pause");
    system("cls");
    player[MY].know = 2;
    print(1, 0);
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night();
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(2, 0);
    system("cls");
    print(2, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(2, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(3, 0);
    system("cls");
    print(3, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(3, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(4, 0);
    system("cls");
    print(4, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(4, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(5, 0);
    system("cls");
    print(5, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(5, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(6, 0);
    system("cls");
    print(6, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    cout << "即将进入夜晚";
    for (int i = 1; i <= 6; i++)
    {
        cout << ".";
        Sleep(500);
    }
    night2(6, 1);
    print1();
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    if (player[lieren].life == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    toupiao(7, 0);
    system("cls");
    print(7, 0);
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    else if (player[lieren].life == 0 && lr == 0)
    {
        panduanlieren();
    }
    if (game_over())
    {
        Sleep(1000);
        system("cls");
        cout << "游戏结束" << endl; printhhh();
        return 0;
    }
    while (1)
        system("pause");
    return 0;
}

彩票

原创

代码如下:

#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
#include <stack>
#include <ctime>
using namespace std;
int money=100;
int t=0;
int tt=-1;
int v;
int d;
void borrow()
{
    if(tt!=-1)
    {
        cout<<"你现在不能借钱"<<endl;
        return;
    }
    cout<<"你要借多少钱?"<<endl;
    cin>>v;
    if (d==1||d==2)
    {
        if (v>1000) cout<<"你不能借这么多钱!"<<endl,cout<<"你要借多少钱?"<<endl,cin>>v;
        if (v<=1000) money+=v,cout<<"10天以后你要还"<<2*v<<"美元"<<endl;
        return;
    }
    else
    {
        if (v>100) cout<<"你不能借这么多钱!"<<endl,cout<<"你要借多少钱?"<<endl,cin>>v;
        if (v<=100) money+=v,cout<<"10天以后你要还"<<2*v<<"美元"<<endl;
        return;
    }
}
bool pay()
{
    cout<<"你现在要还 "<<2*v<<endl;
    money-=2*v;
    tt=-1;
    v=0;
    if(money<=0) return false;
    else return true;
}
int main(int argc, char *argv[])
{
    cout<<"欢迎来到彩票游戏!"<<endl<<endl<<"温馨提示:在游戏内请勿使用小数、负数、英文字母、特殊字符(游戏中另说明除外),"<<endl<<"否则后果自负。"<<endl<<endl;
    cout<<"请选择难度(输入序号即可):"<<endl<<"1:入门"<<" "<<"2:简单"<<" "<<"3:普通"<<" "<<"4:中等"<<" "<<"5:困难"<<endl;
    cin>>d;
    if (d==1) cout<<"已选择难度:入门"<<endl<<endl;
    if (d==2) cout<<"已选择难度:简单"<<endl<<endl;
    if (d==3) cout<<"已选择难度:普通"<<endl<<endl;
    if (d==4) cout<<"已选择难度:中等"<<endl<<endl;
    if (d==5) cout<<"已选择难度:困难"<<endl<<endl;
    while(1)
    {
        if (d<1) cout<<"没有这个选项"<<endl<<"请选择难度(输入序号即可):"<<endl<<"1:入门"<<" "<<"2:简单"<<" "<<"3:普通"<<" "<<"4:中等"<<" "<<"5:困难"<<endl,cin>>d;
        if (d>5) cout<<"没有这个选项"<<endl<<"请选择难度(输入序号即可):"<<endl<<"1:入门"<<" "<<"2:简单"<<" "<<"3:普通"<<" "<<"4:中等"<<" "<<"5:困难"<<endl,cin>>d;
        if (d==1 || d==2 || d==3 || d==4 || d==5) break;
    }
    bool flag=true;
    srand(time(0));
    int a[10000];
    int n,p;
    char c[4];
    if (d==1)
    {
        cout<<"每张彩票 购入价为1美元,出售价为5美元,有五个奖项"<<endl<<"小奖:10美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:10,000美元"<<endl;
        cout<<"你有100美元,当你赚了超过10,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你破产了。"<<endl;
        cout<<"你可能想借钱(最多1,000美元),如果你想,按0。"<<endl<<"请记住,当你借了十天以后。你必须偿还两倍,"<<endl;
        cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
    }
    if (d==2)
    {
        cout<<"每张彩票 购入价为2美元,出售价为3美元,有五个奖项"<<endl<<"小奖:5美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:10,000美元"<<endl;
        cout<<"你有100美元,当你赚了超过10,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你破产了。"<<endl;
        cout<<"你可能想借钱(最多1,000美元),如果你想,请按0。"<<endl<<"请记住,当你借了十天以后。你必须偿还两倍,"<<endl;
        cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
    }
    if (d==3)
    {
        cout<<"每张彩票 售价2美元,有五个奖项"<<endl<<"小奖:5美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:10,000美元"<<endl;
        cout<<"你有100美元,当你赚了超过10,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你破产了。"<<endl;
        cout<<"你可能想借钱(最多100美元),如果你想,请按0。"<<endl<<"请记住,当你借了十天以后。你必须偿还两倍,"<<endl;
        cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
    }
    if (d==4)
    {
        cout<<"每张彩票 售价3美元,有五个奖项"<<endl<<"小奖:5美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:10,000美元"<<endl;
        cout<<"你有100美元,当你赚了超过100,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你破产了。"<<endl;
        cout<<"你可能想借钱(最多100美元),如果你想,请按0。"<<endl<<"请记住,当你借了七天以后。你必须偿还两倍,"<<endl;
        cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
    }
    if (d==5)
    {
        cout<<"每张彩票 购入价为5美元,出售价为2美元,有五个奖项"<<endl<<"小奖:5美元"<<endl<<"中奖:20美元"<<endl<<"大奖:100美元"<<endl<<"巨额奖金:1,000美元"<<endl<<"杰克壶:5,000美元"<<endl;
        cout<<"你有100美元,当你赚了超过100,000美元,你就发财了。"<<endl<<"但如果你失去了所有的钱,你就破产了。"<<endl;
        cout<<"你可能想借钱(最多100美元),如果你想,请按0。"<<endl<<"请记住,当你借了五天以后。你必须偿还两倍,"<<endl;
        cout<<"你想买彩票还是卖彩票?(buy/sell)"<<endl;
    }
    cin>>c;
    if(c[0]=='b'||c[0]=='B')
    {
        while(money>0)
        {
            if (d==1||d==2||d==3) if(money>=10000)
                {
                    cout<<"你发财了! "<<endl;
                    cout<<"你花了 "<<t<<"天"<<endl;
                    system("pause");
                    return 0;
                }
            if (d==4||d==5) if(money>=100000)
                {
                    cout<<"你发财了! "<<endl;
                    cout<<"你花了 "<<t<<"天"<<endl;
                    system("pause");
                    return 0;
                }
            t++;
            cout<<"你要买几张票?"<<" "<<"你有$"<<money<<endl;
            cin>>n;
            if(n==0)
            {
                if (d=4)
                {
                    borrow();
                    tt=t+7;
                }
                if (d=5)
                {
                    borrow();
                    tt=t+5;
                }
                else
                {
                    borrow();
                    tt=t+10;
                }
            }
            if(t==tt)
            {
                flag=pay();
            }
            if(t==tt-1) cout<<"[警告]你必须在明天还钱!"<<endl;
            if(flag==false)
            {
                cout<<"你不能偿还你借的钱!"<<endl;
                cout<<"你破产了。"<<endl;
                cout<<"你生存了"<<t<<"天。"<<endl;
                system("pause");
                return 0;
            }
            flag=true;
            if(n<0)
            {
                cout<<"因为你违反规则,所以系统强制停止了你的游戏"<<endl;
                system("pause");
            }
            if (d==1) money=money-n;
            if (d==2||d==3) money=money-n*2;
            if (d==4) money=money-n*3;
            if (d==5) money=money-n*5;
            if(money<0)
            {
                cout<<"你破产了。"<<endl;
                cout<<"你生存了"<<t<<"天"<<endl;
                system("pause");
                return 0;
            }
            for(int i=0; i<n; i++)
            {
                p=rand()%12000;
                if(p==0)
                {
                    int q=0;
                    q=rand()%4;
                    if(q==1)
                    {
                        cout<<"你获得了杰克壶!"<<endl;
                        if (d==5) money+=5000;
                        else money+=10000;
                    }
                }
                else if(p>=1&&p<=8)
                {
                    int g=0;
                    g=rand()%2;
                    if(g==0)
                    {
                        cout<<"你获得了巨额奖金! "<<endl;
                        money+=1000;
                    }
                }
                else if(p>=9&&p<=99)
                {
                    cout<<"你获得了大奖!"<<endl;
                    money+=100;
                }
                else if((p>=100&&p<=399)||(p>1500&&p<=1600))
                {
                    cout<<"你获得了中间奖!"<<endl;
                    money+=20;
                }
                else if(p>=400&&p<=1500)
                {
                    cout<<"你获得了小奖!"<<endl;
                    if (d==1) money+=10;
                    else money+=5;
                }
            }
            if(money<=0)
            {
                cout<<"你破产了。"<<endl;
                cout<<"你生存了"<<t<<"天。"<<endl;
                system("pause");
                return 0;
            }
        }
    }
    if(c[0]=='s'||c[0]=='S')
    {
        if (d==1)
        {
            cout<<"你有$"<<money<<endl;
            cout<<"你必须付25美元买一个商店。 "<<endl;
            cout<<"你只能卖同样数量的票作为你的钱。 "<<endl;
            money-=25;
        }
        if (d==2)
        {
            cout<<"你有$"<<money<<endl;
            cout<<"你必须付35美元买一个商店。 "<<endl;
            cout<<"你只能卖同样数量的票作为你的钱。 "<<endl;
            money-=35;
        }
        if (d==3||d==4)
        {
            cout<<"你有$"<<money<<endl;
            cout<<"你必须付50美元买一个商店。 "<<endl;
            cout<<"你只能卖同样数量的票作为你的钱。 "<<endl;
            money-=50;
        }
        if (d==5)
        {
            cout<<"你有$"<<money<<endl;
            cout<<"你必须付60美元买一个商店。 "<<endl;
            cout<<"你只能卖同样数量的票作为你的钱。 "<<endl;
            money-=60;
        }
        cout<<"你有$"<<money<<endl;
        while(money>0)
        {
            if (d==1||d==2||d==3) if(money>=10000)
                    if (d==4||d==5) if(money>=100000)
                        {
                            cout<<"你发财了!"<<endl;
                            cout<<"你花了"<<t<<"天。"<<endl;
                            system("pause");
                            return 0;
                        }
            t++;
            cout<<"你要卖几张票? "<<" "<<"你有$"<<money<<endl;
            cin>>n;
            if(n==0)
            {
                borrow();
                tt=t+10;
            }
            if(t==tt) bool flag=pay();
            if(flag==false)
            {
                cout<<"你不能偿还你借的钱。"<<endl;
                cout<<"你破产了!"<<endl;
                cout<<"你生存了"<<t<<" 天。"<<endl;
                system("pause");
                return 0;
            }
            if(n<0 || n>money)
            {
                cout<<"注意"<<endl;
                cout<<"你破产了!"<<endl;
                cout<<"你生存了"<<t<<" 天"<<endl;
                system("pause");
                return 0;
            }
            if (d=1) money=money+n*5;
            if (d=2) money=money+n*3;
            else money=money+n*2;
            if(money<0)
            {
                cout<<"你破产了!"<<endl;
                cout<<"你生存了"<<t<<" 天"<<endl;
                system("pause");
                return 0;
            }
            for(int i=0; i<n; i++)
            {
                p=rand()%12000;
                if(p==0)
                {
                    int y;
                    y=rand()%4;
                    if(y==1)
                    {
                        cout<<"你失去了杰克壶!"<<endl;
                        if (d==5) money-=5000;
                        else money-=10000;
                    }
                }
                else if(p>=1&&p<=8)
                {
                    cout<<"你失去了巨额奖金!"<<endl;
                    money-=1000;
                }
                else if(p>=15&&p<=50)
                {
                    cout<<"你失去了大奖!"<<endl;
                    money-=100;
                }
                else if(p>=61&&p<=360)
                {
                    cout<<"你失去了中奖!"<<endl;
                    money-=20;
                }
                else if(p>=401&&p<=1500)
                {
                    cout<<"你失去了小奖!"<<endl;
                    money-=5;
                }
            }
        }
    }
    if(money<=0)
    {
        cout<<"你破产了。"<<endl;
        cout<<"你生存了"<<t<<"天。"<<endl;
        system("pause");
        return 0;
    }
}
posted @ 2023-06-04 11:49  Az_dream  阅读(3577)  评论(2编辑  收藏  举报
努力加载评论中...