c++游戏

https://blog.csdn.net/studyhard520/article/details/122585349

include<stdio.h>

include<ctime>

include<time.h> //suiji

include<windows.h> //SLEEP函数

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

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

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

int main()
{

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

OrdinaryAct();
return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}
break;

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

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

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

贪吃蛇

/贪吃蛇*/
/2012-11-20*/

include <iostream>

include <cstdio>

include <cstdlib>

include <ctime>

include <conio.h>

include <cmath>

include <windows.h>

using namespace std;

/ 光标定位 /
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;

void locate(int x,int y)
{

coord.X=y;
coord.Y=x;
SetConsoleCursorPosition(hout,coord);

};

/ 隐藏光标 /
void hide()
{

CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(hout, &cursor_info);

}

/ 生成随机数 /
double random(double start, double end)
{

return start+(end-start)*rand()/(RAND_MAX + 1.0);

}

/ 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 /
int m,n;

struct node
{

int x,y;

}snake[1000];

int snake_length,dir;
node food;
int direct4={{-1,0},{1,0},{0,-1},{0,1}};

/ 输出墙 /
void print_wall()
{

cout << " ";
for (int i=1;i<=n;i++)
    cout << "-";
cout << endl;
for (int j=0;j<=m-1;j++)
{
    cout << "|";
    for (int i=1;i<=n;i++) cout << " ";
    cout << "|" << endl;
}
cout << " ";
for (int i=1;i<=n;i++)
    cout << "-";

}

/ 首次输出蛇,其中snake[0]代表头 /
void print_snake()
{

locate(snake[0].x,snake[0].y);
cout << "@";
for (int i=1;i<=snake_length-1;i++)
{
    locate(snake[i].x,snake[i].y);
    cout << "*";
}

}

/ 判断是否撞墙或者自撞 /
bool is_correct()
{

if (snake[0].x==0 || snake[0].y==0 || snake[0].x==m+1 || snake[0].y==n+1) return false;
for (int i=1;i<=snake_length-1;i++)
{
    if (snake[0].x==snake[i].x && snake[0].y==snake[i].y) return false;
}
return true;

}

/ 随机生成并输出食物位置 /
bool print_food()
{

srand((unsigned)time(0));
bool e;
while (1)
{
    e=true;
    int i=(int) random(0,m)+1,j=(int) random(0,n)+1;
    food.x=i;food.y=j;
    for (int k=0;k<=snake_length-1;k++)
    {
        if (snake[k].x==food.x && snake[k].y==food.y)
        {
            e=false;break;
        }
    }
    if (e) break;
}
locate(food.x,food.y);
cout << "$";
return true;

}

/ 蛇的前进 /
bool go_ahead()
{

node temp;
bool e=false;
temp=snake[snake_length-1];
for (int i=snake_length-1;i>=1;i--)
    snake[i]=snake[i-1];
snake[0].x+=direct[dir][0];
snake[0].y+=direct[dir][1];
locate(snake[1].x,snake[1].y);
cout << "*";
/*** 吃到了食物 ***/
if (snake[0].x==food.x && snake[0].y==food.y)
{
    snake_length++;
    e=true;
    snake[snake_length-1]=temp;
}
/*** 输出此时蛇状态 ***/
if (!e)
{
    locate(temp.x,temp.y);
    cout << " ";
}
else
    print_food();
locate(snake[0].x,snake[0].y);
cout << "@";
/*** 如果自撞 ***/
if (!is_correct())
{
    system("cls");
    cout << "You lose!" << endl << "Length: " << snake_length << endl;
    return false;
}
return true;

}

/ 主函数 /
int main()
{

cout << "--------------------贪吃蛇---------------------" << endl;
cout << "请注意窗口大小,以免发生错位.建议将窗口调为最大." << endl;
cout << "先选择难度.请在1-10中输入1个数,1最简单,10则最难" << endl;
cout << "然后进入游戏画面,以方向键控制方向.祝你游戏愉快!" << endl;
cout << "-----------------------------------------------" << endl;
m=25;
n=40; 
if (m<10 || n<10 || m>25 || n>40)
{
    cout << "ERROR" << endl;
    system("pause");
    return 0;
}
int hard;
cin >> hard;
if (hard<=0 || hard>100)
{
    cout << "ERROR" << endl;
    system("pause");
    return 0;
}
/*** 数据全部初始化,包括蛇长,位置,方向 ***/
snake_length=5;
clock_t a,b;
char ch;
double hard_len;
for (int i=0;i<=4;i++)
{
    snake[i].x=1;
    snake[i].y=5-i;
}
dir=3;
/*** 输出初始地图,蛇与食物 ***/
system("cls");
hide();
print_wall();
print_food();
print_snake();
locate(m+2,0);
cout << "Now length: ";
/*** 开始游戏 ***/
while (1)
{
    /*** 难度随长度增加而提高 ***/
    hard_len=(double)snake_length/(double) (m*n);
    /*** 调节时间,单位是ms ***/
    a=clock();
    while (1)
    {
        b=clock();
        if (b-a>=(int)(400-30*hard)*(1-sqrt(hard_len))) break;
    }
    /*** 接受键盘输入的上下左右,并以此改变方向 ***/
    if (kbhit())
    {
        ch=getch();
        if (ch==-32)
        {
            ch=getch();
            switch(ch)
            {
            case 72:
                if (dir==2 || dir==3)
                    dir=0;
                break;
            case 80:
                if (dir==2 || dir==3)
                    dir=1;
                break;
            case 75:
                if (dir==0 || dir==1)
                    dir=2;
                break;
            case 77:
                if (dir==0 || dir==1)
                    dir=3;
                break;
            }
        }
    }
    /*** 前进 ***/
    if (!go_ahead()) break;
    /*** 在最后输出此时长度 ***/
    locate(m+2,12);
    cout << snake_length;
}
system("pause");
return 0;

}

2048

include<iostream>

include<vector>

include<ctime>

include<cstdlib>

using namespace std;
class Game_2048
{
public:

Game_2048();
~Game_2048();
void introduction();
bool judgeOver();                            //判断游戏是否结束
void reSize();
void printBoard();                          //打印函数
void getRand();                              //随机在棋盘上生成2,4;
void slide();                                //滑动

private:

int m=4, n=4;
char op;                                   //用户操作
vector< vector<int> >     board;                                //棋盘
vector<int>  row;
bool judgeInsert(int x,int y);
bool judgeSlide();                               //判断是否能滑动,(未写完)
void copyBoard(vector< vector<int> > &newBoard,vector< vector<int> > &board);
void inputOp();
char getOp();                            //返回操作符

bool judgeLeftSlide(bool mark=true);
void leftSlide();                            //左滑动

bool judgeRightSlide(bool mark = true);
void rightSlide();

bool judgeUpSlide(bool mark = true);
void upSlide();

bool judgeDownSlide(bool mark = true);
void downSlide();

void reStart();
void enlarge();                             //将值扩大二倍

};

int main()
{

Game_2048 NB;
NB.introduction();
NB.getRand();
NB.printBoard();
while (!NB.judgeOver())
{
    NB.slide();
    NB.getRand();
    NB.printBoard();
} 
cout << "游戏结束!!!\n";
system("pause");
return 0;

}

void Game_2048::introduction()
{

cout << "这是一个2048游戏,规则如下:\n";
cout << "上划:W;\n下滑:S;\n左划:A;\n右划:D;\n退出:Q;\n重新开始:R;\n请输入下次操作,\n";

}

void Game_2048::slide()
{

inputOp();
switch (getOp())
{
case 'a':
case 'A':
    if (judgeLeftSlide())
        do
            leftSlide();
        while (judgeLeftSlide(false));
    else
    {
        cout << "无法左滑动,请重试!!!\n";
        slide();
    }
    break;
case 'd':
case 'D':
    if (judgeRightSlide())
        do
            rightSlide();
    while (judgeRightSlide(false));        
    else
    {
        cout << "无法右滑动,请重试!!!\n";
        slide();
    }
    break;
case 'w':
case 'W':
    if(judgeUpSlide())
        do
        upSlide();
    while (judgeUpSlide(false));
    else
    {
        cout << "无法上滑动,请重试!!!\n";
        slide();
    }
    break;
case 's':
case 'S':
    if(judgeDownSlide())
        do    
            downSlide();
        while (judgeDownSlide(false));
    else
    {
        cout << "无法下滑动,请重试!!!\n";
        slide();
    }
    break;
case 'p':
case 'P':
    enlarge();
    break;
case 'q':
case 'Q':
    exit(0);
    break;
case 'r':
case 'R':
    reStart();
    break;
default:
    cout << "输入错误,作为惩罚,随机生成一个数!\n";
    break;
}

}

void Game_2048::reStart()
{

for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {
        board[i][j] = 0;
    }

}

void Game_2048::enlarge()
{

for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
    {
        board[i][j] *= 2;
    }

}

char Game_2048::getOp()
{

return op;

}

void Game_2048::upSlide()
{

for (int j = 0; j < n; j++)
    for (int i = m - 1; i > 0; i--) {                              //n-1!!
        if (board[i][j] != 0 && board[i - 1][j] == 0)            //移动
        {
                board[i - 1][j] = board[i][j];
                board[i][j] = 0;
        }
    }
for (int j = 0; j < n; j++)
    for (int i = m - 1; i > 0; i--) {
        if (board[i][j] != 0 && board[i-1][j] == board[i][j])  //覆盖
        {
            board[i-1][j] += board[i][j];
            board[i][j] = 0;
        }
    }

}

bool Game_2048::judgeUpSlide(bool mark)
{

if (mark)
{
    for (int i = 0; i < m; i++)
        for (int j = n - 1; j > 0; j--)
        {
            if (board[i][j] == 0)
            return true;
        }
}
for (int j = 0; j < n; j++)
    for (int i = m - 1; i > 0; i--) {                              //n-1!!
        if (board[i][j] != 0 && board[i - 1][j] == 0)            //移动
            return true;
        if (board[i][j] != 0 && board[i - 1][j] == board[i][j])  //覆盖
            return true;
    }
return false;

}

bool Game_2048::judgeDownSlide(bool mark)
{

if (mark) {
    for (int i = 0; i < m; i++)
        for (int j = n - 1; j > 0; j--)
            {
                if (board[i][j] == 0)
                    return true;
            }
    
}
for (int j = 0; j < n; j++)
            for (int i = 0; i < m - 1; i++) {                              //n-1!!
                if (board[i][j] != 0 && board[i + 1][j] == 0)            //移动
                    return true;
                if (board[i][j] != 0 && board[i + 1][j] == board[i][j])  //覆盖
                    return true;

            }
return false;

}

void Game_2048::downSlide()
{

for (int j = 0; j < n; j++)
    for (int i = 0; i < m - 1; i++) {                         
        if (board[i][j] != 0 && board[i + 1][j] == 0)            //移动
        {
                board[i + 1][j] = board[i][j];
                board[i][j] = 0;
        }
    }
for (int j = 0; j < n; j++)
    for (int i = 0; i < m - 1; i++) {
        if (board[i][j] != 0 && board[i + 1][j] == board[i][j])  //覆盖
        {
            board[i + 1][j] += board[i][j];
            board[i][j] = 0;
        }
    }

}

void Game_2048::rightSlide()
{

for (int i = 0; i < m; i++)
    for (int j = 0; j < n - 1; j++) {                              //n-1!!
        if (board[i][j] != 0 && board[i][j + 1] == 0)            //移动
        {
                board[i][j + 1] = board[i][j];
                board[i][j] = 0;
        }
    }
for (int i = 0; i < m; i++)
    for (int j = 0; j < n - 1; j++) {
        if (board[i][j] != 0 && board[i][j + 1] == board[i][j])  //覆盖
        {
            board[i][j + 1] += board[i][j];
            board[i][j] = 0;
        }
    }

}

bool Game_2048::judgeRightSlide(bool mark )
{

if (mark) {
    for (int i = 0; i < m; i++)
        for (int j = n - 1; j > 0; j--)
        {
            if (board[i][j] == 0)
                return true;
        }
}
for (int i = 0; i < m; i++)
    for (int j = 0; j < n - 1; j++) {                              //n-1!!
        if (board[i][j] != 0 && board[i][j + 1] == board[i][j])  //覆盖
            return true;
        if (board[i][j] != 0 && board[i][j + 1] == 0)
            return true;
    }
return false;

}

void Game_2048::leftSlide()
{

for (int i = 0; i < m; i++)
    for (int j = 1; j < n; j++) {                              //n-1!!
        if (board[i][j] != 0 && board[i][j - 1] == 0)            //移动
        {
                board[i][j - 1] = board[i][j];
                board[i][j] = 0;
        }
    }
for (int i = 0; i < m; i++)
    for (int j = 1; j < n; j++) {
        if (board[i][j] != 0 && board[i][j - 1] == board[i][j])  //覆盖
        {
            board[i][j - 1] += board[i][j];
            board[i][j] = 0;
        }
    }

}

bool Game_2048::judgeLeftSlide(bool mark)
{

if (mark) {
    for (int i = 0; i < m; i++)
            for (int j = n - 1; j > 0; j--)
            {
                if (board[i][j] == 0)
                    return true;
            }
}
for (int i = 0; i < m; i++)
    for (int j = n - 1; j > 0; j--) {                              //n-1!!
        if (board[i][j] != 0 && board[i][j - 1] == 0)            //移动
            return true;
        if (board[i][j] != 0 && board[i][j - 1] == board[i][j])  //覆盖
            return true;
    }
return false;

}

bool Game_2048::judgeOver()
{

if (op == 'q' || op == 'Q')
    return true;
for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {
        if (board[i][j] == 2048)
        {
            printBoard();
            cout << "有数字达到了2048,恭喜!!!\n";
            return true;
        }    
    }

for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {
         if (board[i][j] == 0)
            return false;
    }
if (judgeSlide())
    return false;
else
{
    cout << "无法再滑动\n";
    return true;
}
    

}

bool Game_2048::judgeSlide()
{

vector< vector<int> >     copyBoard;                                //棋盘
vector<int>  copyRow;
for (int i = 0; i < n; i++) {
    copyRow.push_back(0);
}
for (int i = 0; i < m; i++) {
    copyBoard.push_back(copyRow);
}
copyBoard = board;
upSlide();
downSlide();
leftSlide();
rightSlide();
for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
    {
        if (board[i][j] == 0) {
            board = copyBoard;
            return true;
        }    
    }
return false;

}

void Game_2048::copyBoard(vector< vector<int> >& newBoard, vector< vector<int> >& board)
{

for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
        newBoard[i][j] = board[i][j];

}

bool Game_2048::judgeInsert(int x,int y)
{

if (board[x][y] == 0)
    return true;
else
    return false;

}
void Game_2048::getRand()
{

srand(time(0));
int x, y,val;
do
{
    x = rand() % m;
    y = rand() % n;
} while (!judgeInsert(x,y));
val = (rand() % 2 + 1)*2;
board[x][y] = val;

}

void Game_2048::inputOp()
{

cin >> op;

}
void Game_2048::reSize()
{

cout << "请输入棋盘大小m*n\n";
cin >> m >> n;
Game_2048();

}

Game_2048::~Game_2048()
{
}

Game_2048::Game_2048()
{

for (int i = 0; i < n; i++){
    row.push_back(0);
}
for (int i = 0; i < m; i++){
    board.push_back(row);
}

}
void Game_2048::printBoard()
{

cout << "\n--------------\n";
for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
        cout << board[i][j];
        if (j < n-1)
        {
            cout << "—";
        }
        if (j == n-1 && i < m-1)
        {
            cout << endl;
            int count = 0;
            while (count++ < n-1)
            {
                cout << "|  ";
            }
            cout << "|" << endl;
        }
    }
}

cout << "\n--------------\n" ;

}
五子棋

include<iostream>

include<time.h>

include<stdlib.h>

using namespace std;

const int N = 15;//棋盘大小
const char ChessBoardFlag = ' ';
const char flag1 = 'O';
const char flag2 = 'X';

typedef struct Coordinate
{

int x;
int y;

}ChessCoordi;

class FiveChess
{
public:

FiveChess()
{
    InitChessBoard();
}

void Play()
{
    ChessCoordi Pos1;
    ChessCoordi Pos2;
    while (1){
        int mode = ChoseMode();
        while (1){
            if (mode == 1){//玩家VS电脑
                static size_t count = 1;
                PalyerGo(Pos1, 1, flag1);
                if (count++ >= 9 && GetWiner(Pos1, 1, flag1))
                    break;
                ComputerGo(Pos2, flag2);
                if (count++ >= 10 && GetWiner(Pos2, 0, flag2))
                   break;
            }
            else if (mode == 2){//玩家VS玩家
                static size_t count = 1;
                PalyerGo(Pos1, 1, flag1);
                if (count++ >= 9 && GetWiner(Pos1,1, flag1))
                    break;
                PalyerGo(Pos2,2 ,flag2);
                if (count++ >= 10 && GetWiner(Pos2,2, flag2))
                    break;
            }
        }
        cout << "再来一局 y or no" << endl;
        char chose = 'y';
        cin >> chose;
        if (chose == 'n')
            break;
    }
}

void PrintChessBoard()
{
    system("cls");
    for (size_t i = 0; i < N + 1; ++i)
    {
        for (size_t j = 0; j < N + 1; ++j)
        {
            if (i == 0){
                if (j != 0)
                    printf("%d   ", j);
                else if (j == 0)
                    printf("  ");
            }
            else if (j == 0){
                if (i != 0)
                    printf("%2d", i);
            }
            else{
                printf("%c  |", ChessBoard[i][j]);
            }

        }
        cout << endl;
        cout << "  ";
        for (size_t i = 1; i < N + 1; ++i){
            cout << "---+";
        }
        cout << endl;
    }
}

void InitChessBoard()
{
    for (size_t i = 0; i < N + 1; ++i){
        for (size_t j = 0; j < N + 1; ++j){
            ChessBoard[i][j] = ChessBoardFlag;
        }
    }
}

protected:

int ChoseMode()
{
    system("cls");
    InitChessBoard();
    cout << "请选择 1.玩家VS电脑 2.玩家VS玩家 3.退出" << endl;
    int chose = 0;
    cin >> chose;
    while (1){
        if (chose == 1)
            return chose;
        else if (chose == 2)
            return chose;
        else if (chose == 3)
            exit(1);
        else
            cout << "对不起 您的输入有误。。" << endl;
    }
}

void PalyerGo(ChessCoordi& Pos, int player, char flag)
{
    PrintChessBoard();
    int x = 0;
    int y = 0;
    while (1){
        cout << "请玩家" << player << "下一步棋" << endl;
        cin >> Pos.x >> Pos.y;
        if (JudgePos(Pos))
            break;
        else
            cout << "玩家输入错误 ! 请重新输入" << endl;
    }
    ChessBoard[Pos.x][Pos.y] = flag;
}

void ComputerGo(ChessCoordi& Pos, char flag)
{
    PrintChessBoard();
    int x = 0;
    int y = 0;
    while (1){
        x = rand() % N + 1;
        srand((unsigned int)time(NULL));
        y = rand() % N + 1;
        srand((unsigned int)time(NULL));//这种方式下生成的x,y一定在棋盘上
        if (ChessBoard[x][y] == ChessBoardFlag)
            break;
    }
    Pos.x = x;
    Pos.y = y;
    ChessBoard[Pos.x][Pos.y] = flag;
}

int GetVictory(ChessCoordi Pos, char flag)//判断是否有赢家
{
    int begin = 0;//在检查对角线时 作为行坐标的开始 结束
    int end = 0;
    //检查行是否连续5个子
    int beginl = 0;//在检查对角线时 作为列坐标的开始 结束
    int endl = 0;
    (Pos.y - 4) > 0 ? begin = Pos.y - 4 : begin = 1;
    (Pos.y + 4) < N ? end = Pos.y + 4 : end = N;
    for (size_t i = Pos.x, j = begin; j + 4 <= end; ++j)
    {
        if (flag == ChessBoard[i][j] && flag == ChessBoard[i][j + 1] && \
            flag == ChessBoard[i][j + 2] && flag == ChessBoard[i][j + 3] && \
            flag == ChessBoard[i][j + 4])
            return 1;
    }
    //检查列是否连续5个子
    (Pos.x - 4) > 0 ? begin = Pos.x - 4 : begin = 1;
    (Pos.x + 4) > N ? end = Pos.x + 4 : end = N;
    for (size_t j = Pos.y, i = begin; i + 4 <= end; ++i)
    {
        if (flag == ChessBoard[i][j] && flag == ChessBoard[i + 1][j] && \
            flag == ChessBoard[i + 2][j] && flag == ChessBoard[i + 3][j] && \
            flag == ChessBoard[i + 4][j])
            return 1;
    }

    int len = 0;
    //检查主对角线是否满五个子
    (Pos.x > Pos.y) ? len = Pos.y - 1 : len = Pos.x - 1;
    if (len > 4)//找落子点到上 左两边的垂直距离较短的地方 如果其大于4 取4 不大于4 取其本身
        len = 4;
    begin = Pos.x - len;//向上 左移动适当距离找可能的五连子的起始位置
    beginl = Pos.y - len;
    (Pos.x > Pos.y) ? len = N - Pos.x : len = N - Pos.y;
    if (len > 4)
        len = 4;
    end = Pos.x + len;//向下 右移动适当距离找可能的五连子的终止位置
    endl = Pos.y + len;
    for (size_t i = begin, j = beginl; i + 4 <= end && j + 4 <= endl; ++i, ++j)
    {
        if (flag == ChessBoard[i][j] && flag == ChessBoard[i + 1][j + 1] && \
            flag == ChessBoard[i + 2][j + 2] && flag == ChessBoard[i + 3][j + 3] && \
            flag == ChessBoard[i + 4][j + 4])
            return 1;
    }
    //检查副对角线是否满五个子
    (Pos.x - 1 > N - Pos.y) ? len = N - Pos.y : Pos.x - 1;
    if (len > 4)//找落子点到右 下两边的垂直距离较短的地方 如果其大于4 取4 不大于4 取其本身
        len = 4;
    begin = Pos.x - len;//向上 右移动适当距离找可能的五连子的起始位置
    beginl = Pos.y + len;
    (N - Pos.x > Pos.y - 1) ? len = Pos.y - 1 : len = N - Pos.x;
    end = Pos.x + len;//向下 左移动适当距离找可能的五连子的最终位置
    endl = Pos.y - len;
    for (size_t i = begin, j = beginl; i + 4 <= end && j - 4 >= endl; ++i, ++j)
    {
        if (flag == ChessBoard[i][j] && flag == ChessBoard[i + 1][j - 1] && \
            flag == ChessBoard[i + 2][j - 2] && flag == ChessBoard[i + 3][j - 3] && \
            flag == ChessBoard[i + 4][j - 4])
            return 1;
    }
    //检查棋盘是否已满
    for (size_t i = 1; i < N + 1; ++i){
        for (size_t j = 1; j < N + 1; ++j){
            if (ChessBoard[i][j] == ChessBoardFlag)
                return 0;//表示棋盘没满
        }
    }
    //和棋
    return -1;
}

bool GetWiner(ChessCoordi& Pos, int player, char flag)//判断是谁赢了
{
    int n = 0;
    n = GetVictory(Pos, flag);
    PrintChessBoard();
    if (1 == n){
        if (0 == player)
            cout << "玩家1获胜" << endl;
        else if (1 == player)
            cout << "玩家1获胜" << endl;
        else
            cout << "电脑获胜"<<endl;
        return true;
    }
    else if (-1 == n){
        cout << "和棋" << endl;
        return true;
    }
    else{
        //还未分出胜负
        return false;
    }

}

bool JudgePos(const ChessCoordi& Pos)
{
    if (Pos.x < N + 1 && Pos.x > 0 && Pos.y < N + 1 && Pos.x > 0\
        && ChessBoard[Pos.x][Pos.y] == ChessBoardFlag)
        return true;

    return false;
}

private:

char ChessBoard[N + 1][N + 1];

};

//#include"FiveChess.cpp"
int main()
{

//char a[] = "exit";
//for (size_t i = 0; i < sizeof(a) / sizeof(char);++i)
//printf(":%d", a[i]);
FiveChess a;
a.InitChessBoard();
a.PrintChessBoard();
a.Play();
system("pause");
return 0;

}

飞机大战

include<iostream>

include<windows.h>

include<conio.h>

include<time.h>

include<string>

using namespace std;

/=============== all the structures ===============/

typedef struct Frame
{

COORD position[2];
int flag;

}Frame;

/=============== all the functions ===============/

void SetPos(COORD a)// set cursor
{

HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);

}

void SetPos(int i, int j)// set cursor
{

COORD pos={i, j};
SetPos(pos);

}

void HideCursor()
{

CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}

//把第y行,[x1, x2) 之间的坐标填充为 ch
void drawRow(int y, int x1, int x2, char ch)
{

SetPos(x1,y);
for(int i = 0; i <= (x2-x1); i++)
    cout<<ch;

}

//在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawRow(COORD a, COORD b, char ch)
{

if(a.Y == b.Y)
    drawRow(a.Y, a.X, b.X, ch);
else
{
    SetPos(0, 25);
    cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";
    system("pause");
}

}

//把第x列,[y1, y2] 之间的坐标填充为 ch
void drawCol(int x, int y1, int y2, char ch)
{

int y=y1;
while(y!=y2+1)
{
    SetPos(x, y);
    cout<<ch;
    y++;
}

}

//在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawCol(COORD a, COORD b, char ch)
{

if(a.X == b.X)
    drawCol(a.X, a.Y, b.Y, ch);
else
{
    SetPos(0, 25);
    cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";
    system("pause");
}

}

//左上角坐标、右下角坐标、用row填充行、用col填充列
void drawFrame(COORD a, COORD b, char row, char col)
{

drawRow(a.Y, a.X+1, b.X-1, row);
drawRow(b.Y, a.X+1, b.X-1, row);
drawCol(a.X, a.Y+1, b.Y-1, col);
drawCol(b.X, a.Y+1, b.Y-1, col);

}

void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{

COORD a={x1, y1};
COORD b={x2, y2};
drawFrame(a, b, row, col);

}

void drawFrame(Frame frame, char row, char col)
{

COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);

}

void drawPlaying()
{

drawFrame(0, 0, 48, 24, '=', '|');//    draw map frame;
drawFrame(49, 0, 79, 4, '-', '|');//        draw output frame
drawFrame(49, 4, 79, 9, '-', '|');//        draw score frame
drawFrame(49, 9, 79, 20, '-', '|');//    draw operate frame
drawFrame(49, 20, 79, 24, '-', '|');//    draw other message frame
SetPos(52, 6);
cout<<"得分:";
SetPos(52, 7);
cout<<"称号:";
SetPos(52,10);
cout<<"操作方式:";
SetPos(52,12);
cout<<"  a,s,d,w 控制战机移动。";
SetPos(52,14);
cout<<"  p 暂停游戏。";
SetPos(52,16);
cout<<"  e 退出游戏。";

}

//在[a, b)之间产生一个随机整数
int random(int a, int b)
{

int c=(rand() % (a-b))+ a;
return c;

}

//在两个坐标包括的矩形框内随机产生一个坐标
COORD random(COORD a, COORD b)
{

int x=random(a.X, b.X);
int y=random(a.Y, b.Y);
COORD c={x, y};
return c;

}

bool judgeCoordInFrame(Frame frame, COORD spot)
{

if(spot.X>=frame.position[0].X)
    if(spot.X<=frame.position[1].X)
        if(spot.Y>=frame.position[0].Y)
            if(spot.Y<=frame.position[0].Y)
                return true;
return false;

}

void printCoord(COORD a)
{

cout    <<"( "<<a.X<<" , "<<a.Y<<" )";

}

void printFrameCoord(Frame a)
{

printCoord(a.position[0]);
cout    <<" - ";
printCoord(a.position[1]);

}

int drawMenu()
{

SetPos(30, 1);
cout<<"P l a n e  W a r";
drawRow(3, 0, 79, '-');
drawRow(5, 0, 79, '-');
SetPos(28, 4);
cout<<"w 和 s 选择, k 确定";
SetPos(15, 11);
cout<<"1. 简单的敌人";
SetPos(15, 13);
cout<<"2. 冷酷的敌人";
drawRow(20, 0, 79, '-');
drawRow(22, 0, 79, '-');
SetPos(47, 11);
cout<<"简单的敌人:";
SetPos(51, 13);
cout<<"简单敌人有着较慢的移动速度。";
SetPos(24, 21);
cout<<"制作:LJF神犇";
int j=11;
SetPos(12, j);
cout<<">>";

//drawFrame(45, 9, 79, 17, '=', '|');

while(1)
{    if( _kbhit() )
    {    
        char x=_getch();
        switch (x)
        {
        case 'w' :
                {    
                    if( j == 13)
                    {
                        SetPos(12, j);
                        cout<<" ";
                        j = 11;
                        SetPos(12, j);
                        cout<<">>";
                        SetPos(51, 13);
                        cout<<"            ";
                        SetPos(47, 11);
                        cout<<"简单的敌人:";
                        SetPos(51, 13);
                        cout<<"简单敌人有着较慢的移动速度,容易对付。";
                    }
                    break;
                }
        case 's' :
                {    
                    if( j == 11 )
                    {
                        SetPos(12, j);
                        cout<<" ";        
                        j = 13;
                        SetPos(12, j);
                        cout<<">>";
                        SetPos(51, 13);
                        cout<<"              ";
                        SetPos(47, 11);
                        cout<<"冷酷的敌人:";
                        SetPos(51, 13);
                        cout<<"冷酷的敌人移动速度较快,难对付哟!!!";
                    }
                    break;
                }
        case 'k' :
                {    
                    if (j == 8)    return 1;
                    else return 2;
                }
        }
    }
}

}

/*
DWORD WINAPI MusicFun(LPVOID lpParamte)
{

//DWORD OBJ;
sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC);
return 0;

}
*/

/================== the Game Class ==================/

class Game
{
public:

COORD position[10];
COORD bullet[10];
Frame enemy[8];
int score;
int rank;
int rankf;
string title;
int flag_rank;

Game ();

//初始化所有
void initPlane();
void initBullet();
void initEnemy();

//初始化其中一个
//void initThisBullet( COORD );
//void initThisEnemy( Frame );

void planeMove(char);
void bulletMove();
void enemyMove();

//填充所有
void drawPlane();
void drawPlaneToNull();
void drawBullet();
void drawBulletToNull();
void drawEnemy();
void drawEnemyToNull();

//填充其中一个
void drawThisBulletToNull( COORD );
void drawThisEnemyToNull( Frame );

void Pause();
void Playing();
void judgePlane();
void judgeEnemy();

void Shoot();

void GameOver();
void printScore();

};

Game::Game()
{

initPlane();
initBullet();
initEnemy();
score = 0;
rank = 25;
rankf = 0;
flag_rank = 0;

}

void Game::initPlane()
{

COORD centren={39, 22};
position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
position[1].X=centren.X-2;    
position[2].X=position[6].X=centren.X-1;
position[3].X=position[8].X=centren.X+1;
position[4].X=centren.X+2;
for(int i=0; i<=4; i++)
    position[i].Y=centren.Y;
for(int i=6; i<=8; i++)
    position[i].Y=centren.Y+1;
position[5].Y=centren.Y-1;
position[9].Y=centren.Y-2;

}

void Game::drawPlane()
{

for(int i=0; i<9; i++)
{
    SetPos(position[i]);
    if(i!=5)
        cout<<"O";
    else if(i==5)
        cout<<"|";        
}

}

void Game::drawPlaneToNull()
{

for(int i=0; i<9; i++)
{
    SetPos(position[i]);
    cout<<" ";
}    

}

void Game::initBullet()
{

for(int i=0; i<10; i++)
    bullet[i].Y = 30;

}

void Game::drawBullet()
{

for(int i=0; i<10; i++)
{
    if( bullet[i].Y != 30)
    {
        SetPos(bullet[i]);
        cout<<"^";    
    }
}

}

void Game::drawBulletToNull()
{

for(int i=0; i<10; i++)
    if( bullet[i].Y != 30 )
        {
            COORD pos={bullet[i].X, bullet[i].Y+1};
            SetPos(pos);
            cout<<" ";
        }    

}

void Game::initEnemy()
{

COORD a={1, 1};
COORD b={45, 15};
for(int i=0; i<8; i++)
{
    enemy[i].position[0] = random(a, b);
    enemy[i].position[1].X = enemy[i].position[0].X + 3;
    enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}

}

void Game::drawEnemy()
{

for(int i=0; i<8; i++)
    drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');

}

void Game::drawEnemyToNull()
{

for(int i=0; i<8; i++)
{
    drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
}        

}

void Game::Pause()
{

SetPos(61,2);
cout<<"               ";
SetPos(61,2);
cout<<"暂停中...";
char c=_getch();
while(c!='p')
    c=_getch();
SetPos(61,2);
cout<<"         ";

}

void Game::planeMove(char x)
{

if(x == 'a')
    if(position[1].X != 1)
        for(int i=0; i<=9; i++)
            position[i].X -= 2;
            
if(x == 's')
    if(position[7].Y != 23)
        for(int i=0; i<=9; i++)
            position[i].Y += 1;

if(x == 'd')
    if(position[4].X != 47)
        for(int i=0; i<=9; i++)
            position[i].X += 2;

if(x == 'w')
    if(position[5].Y != 3)
        for(int i=0; i<=9; i++)
            position[i].Y -= 1;

}

void Game::bulletMove()
{

for(int i=0; i<10; i++)
{
    if( bullet[i].Y != 30)
    {
        bullet[i].Y -= 1;
        if( bullet[i].Y == 1 )
        {
            COORD pos={bullet[i].X, bullet[i].Y+1};
            drawThisBulletToNull( pos );
            bullet[i].Y=30;
        }
            
    }
}

}

void Game::enemyMove()
{

for(int i=0; i<8; i++)
{
    for(int j=0; j<2; j++)
        enemy[i].position[j].Y++;

    if(24 == enemy[i].position[1].Y)
    {
        COORD a={1, 1};
        COORD b={45, 3};
        enemy[i].position[0] = random(a, b);
        enemy[i].position[1].X = enemy[i].position[0].X + 3;
        enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
    }
}

}

void Game::judgePlane()
{

for(int i = 0; i < 8; i++)
    for(int j=0; j<9; j++)
        if(judgeCoordInFrame(enemy[i], position[j]))
        {
            SetPos(62, 1);
            cout<<"坠毁";
            drawFrame(enemy[i], '+', '+');
            Sleep(1000);
            GameOver();
            break;
        }

}

void Game::drawThisBulletToNull( COORD c)
{

SetPos(c);
cout<<" ";

}

void Game::drawThisEnemyToNull( Frame f )
{

drawFrame(f, ' ', ' ');

}

void Game::judgeEnemy()
{

for(int i = 0; i < 8; i++)
    for(int j = 0; j < 10; j++)
        if( judgeCoordInFrame(enemy[i], bullet[j]) )
        {
            score += 5;
            drawThisEnemyToNull( enemy[i] );
            COORD a={1, 1};
            COORD b={45, 3};
            enemy[i].position[0] = random(a, b);
            enemy[i].position[1].X = enemy[i].position[0].X + 3;
            enemy[i].position[1].Y = enemy[i].position[0].Y + 2;                    
            drawThisBulletToNull( bullet[j] );
            bullet[j].Y = 30;
        }

}

void Game::Shoot()
{

for(int i=0; i<10; i++)
    if(bullet[i].Y == 30)
    {
        bullet[i].X = position[5].X;
        bullet[i].Y = position[5].Y-1;
        break;
    }

}

void Game::printScore()
{

if(score == 120 && flag_rank == 0)
{
    rank -= 3;
    flag_rank = 1;
}

else if( score == 360 && flag_rank == 1)
{
    rank -= 5;
    flag_rank = 2;
}
else if( score == 480 && flag_rank == 2)
{
    rank -= 5;
    flag_rank = 3;
}
int x=rank/5;
SetPos(60, 6);
cout<<score;

if( rank!=rankf )
{
    SetPos(60, 7);
    if( x == 5)
        title="初级飞行员";
    else if( x == 4)
        title="中级飞行员";
    else if( x == 3)
        title="高级飞行员";
    else if( x == 2 )
        title="王牌飞行员";
    cout<<title;
}
rankf = rank;

}

void Game::Playing()
{

//HANDLE MFUN;
//MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL); 

drawEnemy();
drawPlane();

int flag_bullet = 0;
int flag_enemy = 0;

while(1)
{
    Sleep(8);
    if(_kbhit())
    {
        char x = _getch();
        if ('a' == x || 's' == x || 'd' == x || 'w' == x)
        {
            drawPlaneToNull();
            planeMove(x);
            drawPlane();
            judgePlane();
        }            
        else if ('p' == x)
            Pause();
        else if( 'k' == x)
            Shoot();
        else if( 'e' == x)
        {
            //CloseHandle(MFUN);
            GameOver();
            break;
        }
            
    }
    /* 处理子弹 */
    if( 0 == flag_bullet )
    {
        bulletMove();
        drawBulletToNull();
        drawBullet();
        judgeEnemy();
    }            
    flag_bullet++;
    if( 5 == flag_bullet )
        flag_bullet = 0;

    /* 处理敌人 */
    if( 0 == flag_enemy )
    {
        drawEnemyToNull();
        enemyMove();            
        drawEnemy();
        judgePlane();
    }
    flag_enemy++;
    if( flag_enemy >= rank )
        flag_enemy = 0;

    /* 输出得分 */
    printScore();
}

}

void Game::GameOver()
{

system("cls");                
COORD p1={28,9};
COORD p2={53,15};
drawFrame(p1, p2, '=', '|');
SetPos(36,12);
string str="Game Over!";
for(int i=0; i<str.size(); i++)
{
    Sleep(80);
    cout<<str[i];
}
Sleep(1000);
system("cls");
drawFrame(p1, p2, '=', '|');
SetPos(31, 11);
cout<<"击落敌机:"<<score/5<<" 架";
SetPos(31, 12);
cout<<"得  分:"<<score;
SetPos(31, 13);
cout<<"获得称号:"<<title;
SetPos(30, 16);
Sleep(1000);
cout<<"继续? 是(y)| 否(n)制作:最牛的LJF";

as:

char x=_getch();
if (x == 'n')
    exit(0);
else if (x == 'y')
{
    system("cls");
    Game game;
    int a = drawMenu();
    if(a == 2)
        game.rank = 20;
    system("cls");
    drawPlaying();
    game.Playing();
}
else goto as;

}

/================== the main function ==================/
int main()
{

//游戏准备
srand((int)time(0));    //随机种子
HideCursor();    //隐藏光标

Game game;
int a = drawMenu();
if(a == 2)
    game.rank = 20;
system("cls");
drawPlaying();
game.Playing();

}

发表新评论