1.炸弹小游戏
#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;
}
}
2.笨鸟先飞
#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;
}
3.城市建设
#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;
}
4.吃豆人
#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);
}
}
5.弹跳闯关
#include <iostream>
#include <time.h>
#include <windows.h>
#include <conio.h>
using namespace std;
int main() {
//世界尺寸
const int ww = 100;
const int wh = 40;
//窗口尺寸
int cx = 0;
int cy = 0;
const int cw = 25;
const int ch = 20;
const int cs = cw * ch;
int map[cs];
int herox = 0;
int heroy = 38;
int zl = 1; //重力
int jp = 4; //跳跃力
int curjp = 0; //跳跃判定
int herofx = 6; //英雄方向
int herost = 5; //英雄状态
//子弹
int zdx = -1;
int zdy = -1;
int zdfx = 6;
int zdzt = -1;
int zdspeed = 2;
//地面
int dmx[3] = { 0, 29, 69 };
int dmy[3] = { 39, 39, 39 };
int dmw[3] = { 20, 30, 30 };
int dmh[3] = { 1, 1, 1 };
int dmlen = 3;
//平台
int ptx[5] = { 5, 34, 74, 5, 13 };
int pty[5] = { 8, 17, 30, 35, 35 };
int ptw[5] = { 20, 25, 25, 2, 2 };
int pth[5] = { 2, 2, 2, 4, 4 };
int ptlen = 5;
while (1)
{
//卷轴算法
cx = herox - cw / 2;
if (cx < 0)
{
cx = 0;
}
else if (cx + cw >= ww) {
cx = ww - cw;
}
cy = heroy - ch / 2;
if (cy < 0)cy = 0;
else if (cy + ch > wh)cy = wh - ch;
//清空画面
for (int i = 0; i < cs; i++)
{
map[i] = 0;
}
//标记地面
for (int i = 0; i < dmlen; i++)
{
int x = dmx[i] - cx;
int y = dmy[i] - cy;
int w = dmw[i];
int h = dmh[i];
if (x >= cw || x + w < 0 || y + h < 0 || y >= ch)continue;
if (x < 0)
{
w += x;
x = 0;
}
if (x + w >= cw)
{
w -= x + w - cw;
}
if (y < 0)
{
h += y;
y = 0;
}
if (y + h >= ch)
{
h -= h + y - ch;
}
int s = w * h;
int pos = x + y * cw;
for (int j = 0; j < s; j++)
{
map[pos] = 1;
pos++;
if (j % w == w - 1)
{
pos = pos + cw - w;
}
}
}
//放入平台
for (int i = 0; i < ptlen; i++)
{
int x = ptx[i] - cx;
int y = pty[i] - cy;
int w = ptw[i];
int h = pth[i];
if (x + w < 0 || x >= cw || y + h < 0 || y >= ch)continue;
if (x < 0) {
w += x;
x = 0;
}
if (x + w > cw)
{
w -= x + w - cw;
}
if (y < 0)
{
h += y;
y = 0;
}
if (y + h > ch)
{
h -= y + h - ch;
}
int pos = x + y * cw;
int s = w * h;
for (int j = 0; j < s; j++)
{
map[pos] = 1;
pos++;
if (j % w == w - 1)
{
pos = pos - w + cw;
}
}
}
//放入子弹
int pos = zdx - cx + (zdy - cy) * cw;
if (pos >= 0)
{
if (zdfx == 4)map[pos] = 4;
else map[pos] = 3;
}
//放入英雄
map[herox - cx + (heroy - cy)*cw] = 2;
//打印
system("cls");
for (int i = 0; i < cs; i++)
{
if (map[i] == 0)cout << " ";
else if (map[i] == 1)cout << "■";
else if (map[i] == 2)cout << "英";
else if (map[i] == 3) cout << "→";
else if (map[i] == 4) cout << "←";
if (i % cw == cw - 1) cout << endl;
}
//操作控制
if (GetAsyncKeyState('A'))
{
herox--;
herofx = 4;
for (int i = 0; i < ptlen; i++)
{
if (herox < ptx[i] || herox >= ptx[i] + ptw[i] || heroy < pty[i] ||
heroy >= pty[i] + pth[i])continue;
herox++;
break;
}
}
else if (GetAsyncKeyState('D'))
{
herox++;
herofx = 6;
for (int i = 0; i < ptlen; i++)
{
if (herox < ptx[i] || herox >= ptx[i] + ptw[i] || heroy < pty[i] ||
heroy >= pty[i] + pth[i])continue;
herox--;
break;
}
}
if (GetAsyncKeyState('J'))//跳跃
{
if (herost == 5)
{
herost = 8;
curjp = jp;
}
}
if (GetAsyncKeyState('K'))//发射
{
if (zdy == -1)
{
if (herofx == 4)
{
zdfx = 4;
zdx = herox - 1;
}
else if (herofx == 6)
{
zdfx = 6;
zdx = herox + 1;
}
zdy = heroy;
}
}
//跳跃判定
if (curjp)
{
for (int i = 0; i < curjp; i++)
{
heroy--;
for (int j = 0; j < ptlen; j++)//碰撞判定
{
if (herox < ptx[j] || herox >= ptx[j] + ptw[j] || heroy < pty[j] || heroy >= pty[j] + pth[j])
continue;
heroy++;
break;
}
}
curjp--;
}
else
{
herost = 8;
heroy += zl;
for (int i = 0; i < ptlen; i++)//碰撞判定
{
if (heroy < pty[i] || heroy >= pty[i] + pth[i] || herox < ptx[i] || herox >= ptx[i] + ptw[i])
continue;
heroy--;
herost = 5;
break;
}
if (herost == 8) {
for (int i = 0; i < dmlen; i++)//碰撞判定
{
if (heroy < dmy[i] || heroy >= dmy[i] + dmh[i] || herox < dmx[i] || herox >= dmx[i] + dmw[i])
continue;
heroy--;
herost = 5;
break;
}
}
}
//子弹运动
if (zdy != -1)
{
int fx = 1;
if (zdfx == 4)fx = -1;
for (int i = 0; i < zdspeed; i++)
{
zdx += fx;
if (zdx < cx || zdx >= cx + cw)
{
zdx = -1;
zdy = -1;
}
for (int j = 0; j < ptlen; j++)
{
if (zdy < pty[j] || zdy >= pty[j] + pth[j] || zdx < ptx[j] || zdx >= ptx[j] + ptw[j])
continue;
zdx = -1;
zdy = -1;
}
}
}
}
}
6.地下城小游戏
#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;
}
7.斗地主
#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);
cout<<"作者:麻辣火锅"<<endl;
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;
}
}