1.打砖块游戏
#include "graphics.h"
#include "stdio.h"
#include "conio.h" /*所需的头文件*/
int on; /*声明具有开关作用的全局变量*/
static int score; /*声明静态的记分器变量*/
/* 定义开始界面函数*/
int open()
{
setviewport(100,100,500,380,1); /*设置图形窗口区域*/
setcolor(4); /*设置作图色*/
rectangle(0,0,399,279); /*以矩形填充所设的图形窗口区域*/
setfillstyle(SOLID_FILL,7); /*设置填充方式*/
floodfill(50,50,4); /*设置填充范围*/
setcolor(8);
settextstyle(0,0,9); /*文本字体设置*/
outtextxy(90,80,"BALL"); /*输出文本内容*/
settextstyle(0,0,1);
outtextxy(110,180,"version 1.0");
outtextxy(110,190,"made by ddt");
setcolor(128);
settextstyle(0,0,1);
outtextxy(120,240,"Press any key to continue......");
}
/*定义退出界面函数*/
int quitwindow()
{
char s[100]; /*声明用于存放字符串的数组*/
setviewport(100,150,540,420,1);
setcolor(YELLOW);
rectangle(0,0,439,279);
setfillstyle(SOLID_FILL,7);
floodfill(50,50,14);
setcolor(12);
settextstyle(0,0,8);
outtextxy(120,80,"End");
settextstyle(0,0,2);
outtextxy(120,200,"quit? Y/N");
sprintf(s,"Your score is:%d",score);/*格式化输出记分器的值*/
outtextxy(120,180,s);
on=1; /*初始化开关变量*/
}
/*主函数*/
main()
{
int gdriver,gmode;
gdriver=DETECT; /*设置图形适配器*/
gmode=VGA; /*设置图形模式*/
registerbgidriver(EGAVGA_driver); /*建立独立图形运行程序*/
initgraph(&gdriver,&gmode,""); /*图形系统初试化*/
setbkcolor(14);
open(); /*调用开始界面函数*/
getch(); /*暂停*/
while(1) /*此大循环体控制游戏的反复重新进行*/
{
int driver,mode,l=320,t=400,r,a,b,dl=5,n,x=200,y=400,r1=10,dx=-2,dy=-2;/*初始化小球相关参数*/
int left[100],top[100],right[100],bottom[100],i,j,k,off=1,m,num[100][100];/*方砖阵列相关参数*/
static int pp;
static int phrase; /*一系列起开关作用的变量*/
int oop=15;
pp=1;
score=0;
driver=DETECT;
mode=VGA;
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
setbkcolor(10);
cleardevice(); /*图形状态下清屏*/
clearviewport(); /*清除现行图形窗口内容*/
b=t+6;
r=l+60;
setcolor(1);
rectangle(0,0,639,479);
setcolor(4);
rectangle(l,t,r,b);
setfillstyle(SOLID_FILL,1);
floodfill(l+2,t+2,4);
for(i=0,k=0;i<=6;i++) /*此循环绘制方砖阵列*/
{
top[i]=k;
bottom[i]=top[i]+20;
k=k+21;
oop--;
for(j=0,m=0;j<=7;j++)
{
left[j]=m;
right[j]=left[j]+80;
m=m+81;
setcolor(4);
rectangle(left[j],top[i],right[j],bottom[i]);
setfillstyle(SOLID_FILL,j+oop);
floodfill(left[j]+1,top[i]+1,4);
num[i][j]=pp++;
}
}
while(1) /*此循环控制整个动画*/
{
while(!kbhit())
{
x=x+dx; /*小球运动的圆心变量控制*/
y=y+dy;
if(x+r1>r||x+r1
if((x-r1<=r||x+r1<=r)&&x+r1>=l)
{
if(y
if(y+r1>=t&&phrase==1)
{dy=-dy;y=t-1-r1;}
}
if(off==0)
continue;
for(i=0;i<=6;i++) /*此循环用于判断、控制方砖阵列的撞击、擦除*/
for(j=0;j<=7;j++)
{
if((x+r1<=right[j]&&x+r1>=left[j])||(x-r1<=right[j]&&x-r1>=left[j]))
{
if(( y-r1>top[i]&&y-r1<=bottom[i])||(y+r1>=top[i]&&y+r1<=bottom[i] ))
{
if(num[i][j]==0)
{continue; }
setcolor(10);
rectangle(left[j],top[i],right[j],bottom[i]);
setfillstyle(SOLID_FILL,10);
floodfill(left[j]+1,top[i]+1,10);
dy=-dy;
num[i][j]=0;
score=score+10;
printf("%d\b\b\b",score);
}
}
if((y+r1>=top[i]&&y+r1<=bottom[i])||(y-r1>=top[i]&&y-r1<=bottom[i]))
{
if((x+r1>=left[j]&&x+r1
{
if(num[i][j]==0)
{ continue;}
setcolor(10);
rectangle(left[j],top[i],right[j],bottom[i]);
setfillstyle(SOLID_FILL,10);
floodfill(left[j]+1,top[i]+1,10);
dx=-dx;
num[i][j]=0;
score=score+10;
printf("%d\b\b\b",score);
}
}
}
if(x+r1>639) /*控制小球的弹射范围*/
{dx=-dx;x=638-r1;}
if(x<=r1)
{dx=-dx;x=r1+1;}
if(y+r1>=479)
{off=0;quitwindow();break;}
if(y<=r1)
{dy=-dy;y=r1+1;}
if(score==560)
{off=0;quitwindow();break;}
setcolor(6);
circle(x,y,r1);
setfillstyle(SOLID_FILL,14);
floodfill(x,y,6);
delay(1000);
setcolor(10);
circle(x,y,r1);
setfillstyle(SOLID_FILL,10);
floodfill(x,y,10);
}
a=getch();
setcolor(10);
rectangle(l,t,r,b);
setfillstyle(SOLID_FILL,10);
floodfill(l+2,t+2,10);
if(a==77&&l<=565) /*键盘控制设定*/
{dl=20;l=l+dl;}
if(a==75&&l>=15)
{dl=-20;l=l+dl;}
if(a=='y'&&on==1)
break;
if(a=='n'&&on==1)
break;
if(a==27)
{quitwindow();off=0;}
r=l+60;
setcolor(4);
rectangle(l,t,r,b);
setfillstyle(SOLID_FILL,1);
floodfill(l+5,t+5,4);
delay(100);
}
if(a=='y'&&on==1) /*是否退出游戏*/
{break;}
if(a=='n'&&on==1)
{ continue;}
}
closegraph();
}
2.狼人杀
#include
#include
#include
#include
#include
using namespace std;
struct IDname
{undefined
int geshu;
string NAME;
};
IDname jue_se[100];
struct ID
{undefined
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()
{undefined
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)
{undefined
switch (nn)
{undefined
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)
{undefined
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
{undefined
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)
{undefined
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()
{undefined
int cm = 0;
int sz = 0;
for (int i = 1; i <= n; i++)
{undefined
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++)
{undefined
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)
{undefined
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++)
{undefined
cout << player[i].num << "号位 ";
}
cout << endl;
for (int i = 1; i <= 6; i++)
{undefined
if (player[i].life == 1)
{undefined
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
{undefined
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++)
{undefined
if (player[i].know == 0)
cout << "未知 ";
else if (player[i].know == 1)
{undefined
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++)
{undefined
if (i < 10)
cout << player[i].num << "号位 ";
else
cout << player[i].num << "号位 ";
}
cout << endl;
for (int i = 7; i <= n; i++)
{undefined
if (player[i].life == 1)
{undefined
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
{undefined
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++)
{undefined
if (player[i].know == 0)
cout << "未知 ";
else if (player[i].know == 1)
{undefined
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)
{undefined
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)
{undefined
cout << "请问你要守护谁?" << endl << "输入:";
cin >> x;
while (x == shou || x<1 || x>n || player[x].life == 0)
{undefined
cout << "输入错误,请重新输入" << endl << "输入:";
cin >> x;
}
shou = x;
}
else if (player[shouwei].life == 1)
{undefined
cout << "请问你要守护谁?" << endl;
Sleep(rand() % 98);
srand(time(0));
x = rand() % n + 1;
while (x == shou || player[x].life == 0)
{undefined
Sleep(rand() % 98);
srand(time(0));
x = rand() % n + 1;
}
shou = x;
}
else
{undefined
cout << "请问你要守护谁?" << endl;
Sleep(3000);
shou = -1;
}
Sleep(3000);
system("cls");
print(hhh, hhhh);
cout << "守~卫~请~闭~眼~~~" << endl;
}
struct tou
{undefined
int xxx;
int num;
int toupiaoquan;
};
tou TOU[13];
bool cmp(tou x, tou y)
{undefined
if (x.xxx == y.xxx)
return x.num < y.num;
return x.xxx > y.xxx;
}
bool cmp1(tou x, tou y)
{undefined
return x.num < y.num;
}
void toupiao(int ddd, int nnn)
{undefined
//--------1--------
int x;
Sleep(2000);
system("cls");
print(ddd, nnn);
cout << "现在大家请投票";
for (int i = 1; i <= 3; i++)
{undefined
cout << ".";
Sleep(500);
}
cout << endl;
for (int i = 1; i <= n; i++)
{undefined
TOU[i].num = i;
TOU[i].toupiaoquan = 1;
TOU[i].xxx = 0;
}
for (int i = 1; i <= n; i++)
{undefined
if (player[i].life == 1)
{undefined
Sleep(3000);
if (i == MY)
{undefined
cout << "请投票...(0弃权)" << endl;
cin >> x;
while (player[x].life == 0 && x != 0)
{undefined
cin >> x;
}
if (x == 0)
cout << MY << "号玩家弃权" << endl;
else
cout << MY << "号玩家投给了" << x << "号玩家" << endl;
}
else
{undefined
srand(time(0));
if (player[i].name == "狼人 ")
{undefined
x = rand() % (n + 1);
while (x != 0 && (player[x].life == 0 || player[x].name == "狼人 " || x == i))
{undefined
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 == "预言家 ")
{undefined
x = rand() % (n + 1);
while (x != 0 && (player[x].life == 0 || player[x].name != "狼人 " || x == i))
{undefined
Sleep(rand() % 98);
srand(time(0));
x = rand() % (n + 1);
}
if (x == 0)
cout << i << "号玩家弃权" << endl;
else
cout << i << "号玩家投给了" << x << "号玩家" << endl;
}
else
{undefined
x = rand() % (n + 1);
while (x != 0 && (player[x].life == 0 || x == i))
{undefined
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)
{undefined
cout << "投票结束," << TOU[1].num << "号投票出局" << endl;
player[TOU[1].num].life = 0;
player[TOU[1].num].how = 2;
Sleep(3000);
return;
}
else
{undefined
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++)
{undefined
if (TOU[i].xxx == TOU[1].xxx)
{undefined
TOU[i].toupiaoquan = 0;
cout << "," << TOU[i].num << "号";
}
else
break;
}
if (i == n + 1)
{undefined
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++)
{undefined
cout << ".";
Sleep(500);
}
cout << endl;
for (int i = 1; i <= n; i++)
{undefined
if (player[i].life == 1 && TOU[i].toupiaoquan == 1)
{undefined
Sleep(3000);
if (i == MY)
{undefined
cout << "请投票...(0弃权)" << endl;
cin >> x;
while ((player[x].life == 0 || TOU[x].toupiaoquan == 1) && x != 0)
{undefined
cin >> x;
}
if (x == 0)
cout << MY << "号玩家弃权" << endl;
else
cout << MY << "号玩家投给了" << x << "号玩家" << endl;
}
else
{undefined
srand(time(0));
if (player[i].name == "狼人 ")
{undefined
x = rand() % (n + 1);
while (x != 0 && (player[x].life == 0 || player[x].name == "狼人 " || x == i || TOU[x].toupiaoquan == 1))
{undefined
srand(time(0));
x = rand() % (n + 1);
}
if (x == 0)
cout << i << "号玩家弃权" << endl;
else
cout << i << "号玩家投给了" << x << "号玩家" << endl;
}
else if (player[i].name == "预言家 ")
{undefined
x = rand() % (n + 1);
while (x != 0 && (player[x].life == 0 || player[x].name != "狼人 " || x == i || TOU[x].toupiaoquan == 1))
{undefined
srand(time(0));
x = rand() % (n + 1);
}
if (x == 0)
cout << i << "号玩家弃权" << endl;
else
cout << i << "号玩家投给了" << x << "号玩家" << endl;
}
else
{undefined
x = rand() % (n + 1);
while (x != 0 && (player[x].life == 0 || x == i || TOU[x].toupiaoquan == 1))
{undefined
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)
{undefined
cout << "投票结束," << TOU[1].num << "号投票出局" << endl;
player[TOU[1].num].life = 0;
player[TOU[1].num].how = 2;
Sleep(3000);
return;
}
else
{undefined
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++)
{undefined
if (TOU[i].xxx == TOU[1].xxx)
{undefined
TOU[i].toupiaoquan = 0;
cout << "," << TOU[i].num << "号";
}
else
break;
}
if (i == n + 1)
{undefined
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++)
{undefined
cout << ".";
Sleep(500);
}
cout << endl;
for (int i = 1; i <= n; i++)
{undefined
if (player[i].life == 1 && TOU[i].toupiaoquan == 1)
{undefined
Sleep(3000);
if (i == MY)
{undefined
cout << "请投票...(0弃权)" << endl;
cin >> x;
while ((player[x].life == 0 || TOU[x].toupiaoquan == 1) && x != 0)
{undefined
cin >> x;
}
if (x == 0)
cout << MY << "号玩家弃权" << endl;
else
cout << MY << "号玩家投给了" << x << "号玩家" << endl;
}
else
{undefined
srand(time(0));
if (player[i].name == "狼人 ")
{undefined
x = rand() % (n + 1);
while (x != 0 && (player[x].life == 0 || player[x].name == "狼人 " || x == i || TOU[x].toupiaoquan == 1))
{undefined
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 == "猎人 ")
{undefined
x = rand() % (n + 1);
while (x != 0 && (player[x].life == 0 || player[x].name != "狼人 " || x == i || TOU[x].toupiaoquan == 1))
{undefined
srand(time(0));
x = rand() % (n + 1);
}
if (x == 0)
cout << i << "号玩家弃权" << endl;
else
cout << i << "号玩家投给了" << x << "号玩家" << endl;
}
else
{undefined
x = rand() % (n + 1);
while (x != 0 && (player[x].life == 0 || x == i || TOU[x].toupiaoquan == 1))
{undefined
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)
{undefined
cout << "投票结束," << TOU[1].num << "号投票出局" << endl;
player[TOU[1].num].life = 0;
player[TOU[1].num].how = 2;
}
else
{undefined
cout << "投票结束,无人出局" << endl;
}
Sleep(5000);
}
bool game_over()
{undefined
int pingmin = 0;
int langren = 0;
int shenzhi = 0;
for (int i = 1; i <= n; i++)
{undefined
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()
{undefined
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 == "狼人 ")
{undefined
Sleep(1000);
cout << "你的同伴有:";
for (int i = 1; i <= n; i++)
{undefined
if (i == MY)
continue;
if (player[i].name == "狼人 ")
{undefined
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
{undefined
Sleep(4000);
system("cls");
print(1, 1);
cout << "请问你们要杀谁?" << endl;
do
{undefined
Sleep(rand() % 18);
srand(time(0));
int x = rand() % n + 1;
if (player[x].name != "狼人 " && player[x].life == 1)
{undefined
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)
{undefined
Sleep(1000);
if (jieyao == 1)
{undefined
cout << "今晚" << kill1 << "号玩家被杀" << endl;
Sleep(500);
cout << "请问你要救吗???" << endl << "A. 救 B.不救" << endl << "输入:";
cin >> a;
if (a == 'A')
{undefined
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
{undefined
if (shou == kill1)
kill1 = 0;
Sleep(2000);
system("cls");
print(1, 1);
cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
cin >> a;
if (a == 'A')
{undefined
cout << "请问你要毒谁???" << endl << "输入:";
cin >> kill2;
while (player[kill2].life != 1)
{undefined
cout << "输入错误,请重新输入:" << endl;
cin >> kill2;
}
duyao = 0;
}
}
}
else
{undefined
if (shou == kill1)
kill1 = 0;
Sleep(2000);
system("cls");
print(1, 1);
cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
cin >> a;
if (a == 'A')
{undefined
cout << "请问你要毒谁???" << endl << "输入:";
cin >> kill2;
while (player[kill2].life != 1)
{undefined
cout << "输入错误,请重新输入:" << endl;
cin >> kill2;
}
duyao = 0;
}
}
}
else
{undefined
bool b = 0;
cout << "请问你是否要用解药???" << endl;
int FFF = 0, kkkk;
for (int i = 1; i <= n; i++)
{undefined
if (player[i].life == 1 && player[i].name == "村民 ")
FFF++;
if (player[i].name == "女巫 ")
kkkk = i;
}
if (jieyao == 1 && player[kkkk].life == 1)
{undefined
if (FFF == 1)
{undefined
if (shou == kill1)
jieyao = 1;
else
jieyao = 0;
kill1 = 0;
b = 1;
}
else
for (int i = 1; i <= n; i++)
{undefined
if (player[i].name == "女巫 " && kill1 == i)
{undefined
kill1 = 0;
if (shou == kill1)
jieyao = 1;
else
jieyao = 0;
b = 1;
break;
}
else if (player[i].name == "预言家 " && kill1 == i)
{undefined
kill1 = 0;
if (shou == kill1)
jieyao = 1;
else
jieyao = 0;
b = 1;
break;
}
}
}
Sleep(3000);
if (b == 0 && duyao == 1 && player[kkkk].life == 1)
{undefined
system("cls");
print(1, 1);
cout << "请问你是否要用毒药???" << endl;
srand(time(0));
int x = rand() % 2;
Sleep(1500);
cout << "请问你要毒谁???" << endl;
if (x == 1)
{undefined
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
{undefined
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)
{undefined
Sleep(3000);
system("cls");
print(1, 1);
cout << "预~言~家~请~睁~眼~~~" << endl;
if (player[MY].name == "预言家 ")
{undefined
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
{undefined
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)
{undefined
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)
{undefined
Sleep(3000);
cout << endl << "请问你们要杀谁:" << endl << "输入:";
cin >> kill1;
Sleep(1500);
system("cls");
print(hhh, hhhh);
cout << "今晚你们要杀的是" << kill1 << "号玩家" << endl;
}
else
{undefined
Sleep(4000);
system("cls");
print(hhh, hhhh);
cout << "请问你们要杀谁?" << endl;
do
{undefined
srand(time(0));
int x = rand() % n + 1;
if (player[x].name != "狼人 " && player[x].life == 1)
{undefined
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)
{undefined
Sleep(1000);
if (jieyao == 1)
{undefined
cout << "今晚" << kill1 << "号玩家被杀" << endl;
Sleep(500);
cout << "请问你要救吗???" << endl << "A. 救 B.不救" << endl << "输入:";
cin >> a;
if (a == 'A')
{undefined
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
{undefined
if (shou == kill1)
kill1 = 0;
Sleep(2000);
system("cls");
print(hhh, hhhh);
cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
cin >> a;
if (a == 'A')
{undefined
cout << "请问你要毒谁???" << endl << "输入:";
cin >> kill2;
while (player[kill2].life != 1)
{undefined
cout << "输入错误,请重新输入:" << endl;
cin >> kill2;
}
duyao = 0;
}
}
}
else if (duyao == 1)
{undefined
if (shou == kill1)
kill1 = 0;
Sleep(2000);
system("cls");
print(hhh, hhhh);
cout << "请问你要毒吗???" << endl << "A. 毒 B.不毒" << endl << "输入:";
cin >> a;
if (a == 'A')
{undefined
cout << "请问你要毒谁???" << endl << "输入:";
cin >> kill2;
while (player[kill2].life != 1)
{undefined
cout << "输入错误,请重新输入:" << endl;
cin >> kill2;
}
duyao = 0;
}
}
else
{undefined
Sleep(2000);
system("cls");
print(hhh, hhhh);
cout << "请问你要毒吗???" << endl;
}
}
else
{undefined
bool b = 0;
cout << "请问你是否要用解药???" << endl;
int FFF = 0, kkkk;
for (int i = 1; i <= n; i++)
{undefined
if (player[i].life == 1 && player[i].name == "村民 ")
FFF++;
if (player[i].name == "女巫 ")
kkkk = i;
}
if (jieyao == 1 && player[kkkk].life == 1)
{undefined
if (FFF == 1)
{undefined
if (shou == kill1)
jieyao = 1;
else
jieyao = 0;
kill1 = 0;
b = 1;
}
else
for (int i = 1; i <= n; i++)
{undefined
if (player[i].name == "女巫 " && kill1 == i)
{undefined
kill1 = 0;
if (shou == kill1)
jieyao = 1;
else
jieyao = 0;
b = 1;
break;
}
else if (player[i].name == "预言家 " && kill1 == i)
{undefined
kill1 = 0;
if (shou == kill1)
jieyao = 1;
else
jieyao = 0;
b = 1;
break;
}
}
}
Sleep(3000);
if (b == 0 && duyao == 1 && player[kkkk].life == 1)
{undefined
system("cls");
print(hhh, hhhh);
cout << "请问你是否要用毒药???" << endl;
srand(time(0));
int x = rand() % 2;
Sleep(1500);
cout << "请问你要毒谁???" << endl;
if (x == 1)
{undefined
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
{undefined
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)
{undefined
Sleep(3000);
system("cls");
print(hhh, hhhh);
cout << "预~言~家~请~睁~眼~~~" << endl;
if (player[MY].name == "预言家 " && player[MY].life == 1)
{undefined
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
{undefined
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()
{undefined
if (lr == 1)
return;
if (MY == lieren)
{undefined
cout << "请射杀一名玩家" << endl;
int x;
cin >> x;
while (player[x].life != 1)
{undefined
cout << "输入错误,请重新输入" << endl;
cin >> x;
}
Sleep(1000);
cout << lieren << "号猎人发动技能,开枪带走了" << x << "号" << endl;
player[x].life = 0;
player[x].how = 4;
}
else if (n >= 9)
{undefined
srand(time(0));
int x = rand() % n + 1;
while (player[x].life != 1)
{undefined
x = rand() % n + 1;
}
Sleep(1000);
cout << lieren << "号猎人发动技能,开枪带走了" << x << "号" << endl;
player[x].life = 0;
player[x].how = 4;
}
lr = 1;
}
void print1()
{undefined
cout << "天亮了,昨晚";
if (kill1 != 0 || kill2 != 0)
{undefined
cout << kill1 << "号";
if (kill2 != 0)
{undefined
cout << "," << kill2 << "号";
kill2 = 0;
}
cout << "被杀" << endl;
}
else
cout << "是平安夜" << endl;
}
int main()
{undefined
system("cls");
cout << " " << "狼人杀online" << endl;
cout << "请输入人数个数:" << endl;
scanf("%d", &n);
cout << "加载时间长,请耐心等待";
init1();
init2(n);
int k = 1;
do
{undefined
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++)
{undefined
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++)
{undefined
cout << ".";
Sleep(500);
}
night();
print1();
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
if (player[lieren].life == 0 && lr == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
toupiao(2, 0);
system("cls");
print(2, 0);
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
else if (player[lieren].life == 0 && lr == 0)
{undefined
panduanlieren();
}
cout << "即将进入夜晚";
for (int i = 1; i <= 6; i++)
{undefined
cout << ".";
Sleep(500);
}
night2(2, 1);
print1();
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
if (player[lieren].life == 0 && lr == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
toupiao(3, 0);
system("cls");
print(3, 0);
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
else if (player[lieren].life == 0 && lr == 0)
{undefined
panduanlieren();
}
cout << "即将进入夜晚";
for (int i = 1; i <= 6; i++)
{undefined
cout << ".";
Sleep(500);
}
night2(3, 1);
print1();
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
if (player[lieren].life == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
toupiao(4, 0);
system("cls");
print(4, 0);
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
else if (player[lieren].life == 0 && lr == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
cout << "即将进入夜晚";
for (int i = 1; i <= 6; i++)
{undefined
cout << ".";
Sleep(500);
}
night2(4, 1);
print1();
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
if (player[lieren].life == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
toupiao(5, 0);
system("cls");
print(5, 0);
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
else if (player[lieren].life == 0 && lr == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
cout << "即将进入夜晚";
for (int i = 1; i <= 6; i++)
{undefined
cout << ".";
Sleep(500);
}
night2(5, 1);
print1();
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
if (player[lieren].life == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
toupiao(6, 0);
system("cls");
print(6, 0);
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
else if (player[lieren].life == 0 && lr == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
cout << "即将进入夜晚";
for (int i = 1; i <= 6; i++)
{undefined
cout << ".";
Sleep(500);
}
night2(6, 1);
print1();
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
if (player[lieren].life == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
toupiao(7, 0);
system("cls");
print(7, 0);
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
else if (player[lieren].life == 0 && lr == 0)
{undefined
panduanlieren();
}
if (game_over())
{undefined
Sleep(1000);
system("cls");
cout << "游戏结束" << endl; printhhh();
return 0;
}
while (1)
system("pause");
return 0;
}
2.俄罗斯方块
代码来了!
#include
#include
#include
#include
#include
#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]={undefined
{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){undefined
COORD c;
c.X=x-1; c.Y=y-1;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void Gameover(){undefined
int i,j,flag=0;
for(j=1;j
flag=1;break;
}
}
if(flag==1){undefined
for(i=1;i
for(j=1;j
}puts("");
}
gotoxy(7,9);
printf("GAME OVER!");
gotoxy(1,SIZE_N+1);
exit(0);
}
}
void ShowMap(int id){undefined
int i,j;
gotoxy(1,1);
if(id!=-1){undefined
for(i=0;i
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 {undefined
mark=1;
for(i=0;i
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){undefined
gotoxy(30+2*shape[id][i+1],6+shape[id][i]); printf(" ");
}
gotoxy(30,6); printf("■");
for(i=0;i<6;i=i+2){undefined
gotoxy(30+2*shape[next][i+1],6+shape[next][i]); printf("■");
}
Sleep(Gamespeed);
}
void init(int id){undefined
int i,j;
memset(map,0,sizeof(map));
for(i=0;i
map[i][j]=-1;
}
cur_x=0; cur_y=5;
ShowMap(id);
}
int judge_in(int x,int y,int id){undefined
int i;
if(map[x][y]!=0)return 0;
for(i=0;i<6;i=i+2){undefined
if(map[ x+shape[id][i] ][ y+shape[id][i+1] ]!=0)return 0;
}return 1;
}
void fun_score(){undefined
int i,j,ii,jj;
for(i=1;i
for(j=1;j
}
if(flag==0){undefined
int k=3;
while(k--){undefined
gotoxy(2,i+1);
for(ii=1;ii
if(k%2==1)printf(" ");
else printf("■");
}
}Sleep(100);
}
for(ii=i;ii>1;ii--){undefined
for(jj=1;jj
ShowMap(-1);
score+=10;
if(score%100==0 && score!=0)Gamespeed-=50;
}
}
}
int main(){undefined
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){undefined
Here: mark=0;
if(set==0){undefined
id=next;
next=rand()%MOD; next=(next+MOD)%MOD;
cur_x=0;cur_y=5;
set=1;
}
while(!kbhit()){undefined
Gameover();
if(judge_in(cur_x+1,cur_y,id)==1) cur_x++;
else {undefined
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){undefined
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;
}
3.五子棋
按wsad移动,j选定
#include
#include
#include
#include
using namespace std;
int main()
{undefined
int player=1; //定义一个玩家,当1时为白方,2时为黑方
//................................棋盘1..............................................
int qiPan[19][19]; //1.声明一个19行19列的数组来存储棋盘 1
for(int h=0;h<19;h++) //对数组进行遍历,所有元素进行初始化(赋值),默认为0值
{undefined
for(int l=0;l<19;l++)
{undefined
qiPan[h][l]=0; //对当前元素进行初始化(赋值),默认为0值
}
}
//...............................棋盘2................................................
int qiPanTwo[19][19]; //声明一个19行19列的数组来存储棋盘 2
for(int h=0;h<19;h++) //对数组进行遍历,所有元素进行初始化(赋值),默认为0值
{undefined
for(int l=0;l<19;l++)
{undefined
qiPanTwo[h][l]=0; //对当前元素进行初始化(赋值),默认为0值
}
}
//...............................光标声明.............................................
int X=9; //2.声明并初始化光标的横纵坐标
int Y=9;
qiPan[9][9]=8;
//..............................显示...................................................
for(int h=0;h<19;h++)
{undefined
for(int l=0;l<19;l++)
{undefined
if(qiPan[h][l]==8)
{undefined
cout<<"╋";
}
else if(qiPanTwo[h][l]==0)
{undefined
cout<<"·";
}
else if(qiPanTwo[h][l]==1)
{undefined
cout<<"●";
}
else if(qiPanTwo[h][l]==2)
{undefined
cout<<"○";
}
}
cout<
//..............................输入控制...............................................
while(true)
{undefined
char xx=getch(); //控制台从键盘获得一个字符
switch(xx) //控制
{undefined
case 'w': //上
Y=Y-1; //使光标纵坐标-1
qiPan[Y][X]=8; //讲光标写入棋盘
qiPan[Y+1][X]=0; //使原本位置的值还原
break;
case 's': //下
Y=Y+1;
qiPan[Y][X]=8;
qiPan[Y-1][X]=0;
break;
case 'a': //左
X=X-1;
qiPan[Y][X]=8;
qiPan[Y][X+1]=0;
break;
case 'd': //右
X=X+1;
qiPan[Y][X]=8;
qiPan[Y][X-1]=0;
break;
case 'j': //落子
if(player==1) //如果当前玩家为白色方时
{undefined
qiPanTwo[Y][X]=1; //通过棋盘1中的光标位置更改棋盘2中对应元素的值为1,也就是白棋落子
player=2; //白棋落子后更换玩家
}
else if(player==2)
{undefined
qiPanTwo[Y][X]=2;
player=1;
}
break;
}
//................................清屏操作................................................
system("cls");
//...............................打印.....................................................
for(int h=0;h<19;h++)
{undefined
for(int l=0;l<19;l++)
{undefined
if(qiPan[h][l]==8)
{undefined
cout<<"╋";
}
else if(qiPanTwo[h][l]==0)
{undefined
cout<<"·";
}
else if(qiPanTwo[h][l]==1)
{undefined
cout<<"●";
}
else if(qiPanTwo[h][l]==2)
{undefined
cout<<"○";
}
}
cout<
//.........................游戏规则..............................................
int shu=1,heng=1,pie=1,na=1;//横竖撇捺
for(int i=1;i<=4;i++) //上
{undefined
if(qiPanTwo[Y+i][X]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
shu++;
else
break;
}
for(int i=1;i<=4;i++) //下
{undefined
if(qiPanTwo[Y-i][X]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
shu++;
else
break;
}
for(int i=1;i<=4;i++) //左
{undefined
if(qiPanTwo[Y][X-i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
heng++;
else
break;
}
for(int i=1;i<=4;i++) //右
{undefined
if(qiPanTwo[Y][X+i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
heng++;
else
break;
}
for(int i=1;i<=4;i++) //左下
{undefined
if(qiPanTwo[Y+i][X-i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
pie++;
else
break;
}
for(int i=1;i<=4;i++) //右上
{undefined
if(qiPanTwo[Y-i][X+i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
pie++;
else
break;
}
for(int i=1;i<=4;i++) //右下
{undefined
if(qiPanTwo[Y+i][X+i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
na++;
else
break;
}
for(int i=1;i<=4;i++) //左上
{undefined
if(qiPanTwo[Y-i][X-i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
na++;
else
break;
}
if(shu==5||heng==5||pie==5||na==5)
{undefined
if(qiPanTwo[Y][X]==1)
{undefined
cout<<"白子获胜!";
system("pause");
break;
}
else
{undefined
cout<<"黑子获胜!";
system("pause");
break;
}
}
}
return 0;
}
3.战争游戏
#include
#include
#include
#include
#include
#include
#include
#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 {undefined
int x;
int y;
};
queue
//打印位置
void position(int x,int y) {undefined
COORD pos={x,y};
HANDLE Out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out,pos);
}
//隐藏光标
void Hide() {undefined
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;
GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
CursorInfo.bVisible = false; //隐藏控制台光标
SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}
//初始化
void Beginning() {undefined
while(!dui.empty()) {undefined
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 map[i][j]=' ';
flag[i][j]=0;
slect[i][j]=0;
}
while(BoomTotalNum) {undefined
x=rand()%A;
y=rand()%B;
if(map[x][y]==' ') {undefined
map[x][y]='@';
BoomTotalNum--;
}
}
SetConsoleTextAttribute(handle_out, FORE_GREEN);
for(int i=0;i for(int j=0;j 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) {undefined
switch(map[xx][yy]) {undefined
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) {undefined
if(flag[xx][yy]==0) {undefined
if(mode%2==0) printf("");
else printf("");
}
else printf("");
}
else {undefined
if(flag[xx][yy]==0) printf("█");
else printf("");
}
break;
case '@' :
if(xx==floatx&&yy==floaty) {undefined
if(flag[xx][yy]==0) {undefined
if(mode%2==0) printf("");
else printf("");
}
else printf("");
}
else {undefined
if(flag[xx][yy]==0) printf("█");
else printf("");
}
break;
case 'x' : if(floatx==xx&&floaty==yy) printf(""); else printf(" ");break; //已经挖开的空白
}
}
//移动光标
void Move() {undefined
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) {undefined
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') {undefined
position(floaty*2,floatx);
printf(" ");
}
position(floaty*2,floatx);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
Lump(floatx,floaty); //更新新位置
}
//插旗和排雷模式切换
void Mode() {undefined
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) {undefined
SetConsoleTextAttribute(handle_out, FORE_BLUE);
printf("建设模式");
}
else {undefined
SetConsoleTextAttribute(handle_out, FORE_RED);
printf("挖掘模式");
}
}
//该点周围地雷数
int Boomnum(int xx,int yy) {undefined
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 if((xx+0>=0)&&(yy-1>=0)&&(map[xx][yy-1]=='@')) num++;
if((xx+0>=0)&&(yy+1 if((xx+1=0) &&(map[xx+1][yy-1]=='@')) num++;
if((xx+1=0) &&(map[xx+1][yy]=='@')) num++;
if((xx+1 return num;
}
//更新地图
void Open() {undefined
node c;
node d;
while(!dui.empty()) {undefined
dui.pop();
}
c.x=floatx;
c.y=floaty;
dui.push(c);
slect[c.x][c.y]=1;
while(!dui.empty()) {undefined
c=dui.front();
dui.pop();
if(Boomnum(c.x,c.y)!=0) {undefined
map[c.x][c.y]=(Boomnum(c.x,c.y)+48);
continue;
}
else {undefined
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)) {undefined
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)) {undefined
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 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)) {undefined
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 d.x=c.x-0;
d.y=c.y+1;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x+1=0)&&(map[c.x+1][c.y-1]==' ')&&(slect[c.x+1][c.y-1]==0)) {undefined
d.x=c.x+1;
d.y=c.y-1;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x+1=0)&&(map[c.x+1][c.y]==' ')&&(slect[c.x+1][c.y]==0)) {undefined
d.x=c.x+1;
d.y=c.y-0;
dui.push(d);
slect[d.x][d.y]=1;
}
if((c.x+1 d.x=c.x+1;
d.y=c.y+1;
dui.push(d);
slect[d.x][d.y]=1;
}
}
}
}
int main() {undefined
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) {undefined
if(kbhit()!=0) {undefined
spare=getch();
//按其他
if((spare!=(-32))&&(spare!=13)&&(spare!=' ')) continue;//跳过
//按Enter
if(spare==13) { //确认
//排雷
if(mode%2==0) {undefined
if(map[floatx][floaty]=='@'&&flag[floatx][floaty]==0) {undefined
break; //触雷
game=0;
}
if(flag[floatx][floaty]==1) continue; //有旗跳过
Open();
position(0,0);
SetConsoleTextAttribute(handle_out, FORE_GREEN);
for(int i=0;i for(int j=0;j printf("\n");
}
position(floaty*2,floatx);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
Lump(floatx,floaty);
}
//插拔旗
else {undefined
//不能插旗的地方
if(map[floatx][floaty]=='x'||(map[floatx][floaty]>'0'&&map[floatx][floaty]<'9'))
continue; //跳过
//插旗
if(flag[floatx][floaty]==0) {undefined
flagnum++;
flag[floatx][floaty]=1;
position(floaty*2,floatx);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
Lump(floatx,floaty);
}
//拔旗
else {undefined
flagnum--;
flag[floatx][floaty]=0;
position(floaty*2,floatx);
SetConsoleTextAttribute(handle_out, FORE_BLUE);
Lump(floatx,floaty);
}
}
}
//按空格
if(spare==' ') Mode(); //切换模式
//按方向键
if(spare==-32) {undefined
news=getch();
Move(); //移动光标
}
for(int i=0;i'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;
}