最更新了一下优优自走棋:
1:使玩家在最左边是往左走是可以重新输入
2:修复了玩家进入重生点和家时不会显示的bug
3:让玩家都变成了紫色,便于区分
4:修复了【巫师】技能【冲刺】的错误
话不多说,直接上代码
//上72 下80 左75 右77
#include<bits/stdc++.h>//输入输出用的
#include<ctime>//计算时间用的 colok()
#include<conio.h>//_getch()用的
#include<fstream>//存档用的
#include<windows.h>//SLeep等函数
using namespace std;
const int jss=4,hs=19,wj1csx=1,wj1csy=4,wj2csx=5,wj2csy=4,XXx=3,XXy=4;
int wj1x=3,wj1y=4,wj1jx,wj1jy,wj1xl,wj1Mxl,wj1js/*角色*/,wj1jn=0/*技能*/,wj1kh=0/*刻痕*/;
int wj2x=3,wj2y=3,wj2jx,wj2jy,wj2xl,wj2Mxl,wj2js/*角色*/,wj2jn=0/*技能*/,wj2kh=0/*刻痕*/;
int xxs=0;
string xx[100000];
struct js{
string name;
int xl;//血量
int gj;//攻击
int jh;//技能消耗
string jnm;//技能名
}js[jss+1]={
{"无",1,0,0},
{"剑客",2,1,3,"剑雨"},
{"巫师",1,1,3,"冲刺"},
{"肉盾",3,1,2,"回血"},
{"游侠",2,1,3,"暴击"},
};
void yx();
void Hyx(char s,bool as);
void out();
void zk1(string s,char t,string cd);//1 中间 0 前面
void zk0(string s,char t,string cd);
void color(int c);
void xjs();
void yx();
void jn(int x);
void check();
void xxd(bool as);
int main(){
system("mode con cols=100 lines=30");
system("cls");
color(15);
yx();
return 0;
}
void yx(){
xjs();
bool as=1;
while(1){
system("cls");
if(as)
cout<<"轮到玩家1走了(wsad移动";
else
cout<<"轮到玩家2走了(↑↓←→移动";
cout<<" 空格使用技能)\n";
out();
bool jx=1;
while(jx){
char s=_getch();
if(as)while(s!=' '&&s!='w'&&s!='a'&&s!='s'&&s!='d')s=_getch();
else while(s!=' '&&s!=72&&s!=75&&s!=80&&s!=77)s=_getch();
if(s==' '){
if(as){
if(wj1jn>=js[wj1js].jh){
wj1jn-=js[wj1js].jh;
jn(1);
jx=0;
}
else{
jx=1;
cout<<"技能点不够,无法使用技能\n请重新输入\n";
s=_getch();
while(s!='w'&&s!='a'&&s!='s'&&s!='d')s=_getch();
}
}
else{
if(wj2jn>=js[wj2js].jh){
wj2jn-=js[wj2js].jh;
jn(2);
jx=0;
}
else{
jx=1;
cout<<"技能点不够,无法使用技能\n请重新输入\n";
s=_getch();
while(s!=72&&s!=80&&s!=75&&s!=77)s=_getch();
}
}
}
if(!jx)
{
check();
as=!as;
continue;
}
jx=0;
if(as==1){
if(wj1x==1&&s=='w')jx=1;
if(wj1x==5&&s=='s')jx=1;
if(wj1y==1&&s=='a')jx=1;
if(wj1y==7&&s=='d')jx=1;
}
if(as==0){
if(wj2x==1&&s==72)jx=1;
if(wj2x==5&&s==80)jx=1;
if(wj2y==1&&s==75)jx=1;
if(wj2y==7&&s==77)jx=1;
}
Hyx(s,as);
check();
if(wj1kh>=3){
system("cls");
color(12);
cout<<"玩家二胜利!!!\n";
cout<<"玩家二胜利!!!\n";
cout<<"玩家二胜利!!!\n";
color(15);
system("pause");
return;
}
if(wj2kh>=3){
system("cls");
color(12);
cout<<"玩家一胜利!!!\n";
cout<<"玩家一胜利!!!\n";
cout<<"玩家一胜利!!!\n";
color(15);
system("pause");
return;
}
}
as=!as;
}
}
void Hyx(char s,bool as){
if(as){
if(s=='w'&&wj1x-1>=1){
if(wj1x-1==wj2x&&wj1y==wj2y&&wj2x==5&&wj2y==7)return;
if(wj1x-1==wj2x&&wj1y==wj2y){
wj2xl--;
}
else
wj1x--,xxd(1);
}
if(s=='s'&&wj1x+1<=5){
if(wj1x+1==wj2x&&wj1y==wj2y&&wj2x==5&&wj2y==7)return;
if(wj1x+1==wj2x&&wj1y==wj2y){
wj2xl--;
}
else
wj1x++,xxd(1);
}
if(s=='a'&&wj1y-1>=1){
if(wj1y-1==wj2y&&wj1x==wj2x&&wj2x==5&&wj2y==7)return;
if(wj1y-1==wj2y&&wj1x==wj2x){
wj2xl--;
}
else
wj1y--,xxd(1);
}
if(s=='d'&&wj1y+1<=7){
if(wj1y+1==wj2y&&wj1x==wj2x&&wj2x==5&&wj2y==7)return;
if(wj1y+1==wj2y&&wj1x==wj2x){
wj2xl--;
}
else
wj1y++,xxd(1);
}
}
else{
if(s==72&&wj2x-1>=1){
if(wj2x-1==wj1x&&wj2y==wj1y&&wj1x==1&&wj1y==1)return;
if(wj2x-1==wj1x&&wj2y==wj1y){
wj1xl--;
}
else
wj2x--,xxd(0);
}
if(s==80&&wj2x+1<=5){
if(wj2x+1==wj1x&&wj2y==wj1y&&wj1x==1&&wj1y==1)return;
if(wj2x+1==wj1x&&wj2y==wj1y){
wj1xl--;
}
else
wj2x++,xxd(0);
}
if(s==75&&wj2y-1>=1){
if(wj2y-1==wj1y&&wj2x==wj1x&&wj1x==1&&wj1y==1)return;
if(wj2y-1==wj1y&&wj2x==wj1x){
wj1xl--;
}
else
wj2y--,xxd(0);
}
if(s==77&&wj2y+1<=7){
if(wj2y+1==wj1y&&wj2x==wj1x&&wj1x==1&&wj1y==1)return;
if(wj2y+1==wj1y&&wj2x==wj1x){
wj1xl--;
}
else
wj2y++,xxd(0);
}
}
}
void jn(int x){
if(x==1){
switch(wj1js){
case 1:{
if(wj2x==wj1x-1&&wj2y==wj1y-1)wj2xl--;
if(wj2x==wj1x-1&&wj2y==wj1y)wj2xl--;
if(wj2x==wj1x-1&&wj2y==wj1y+1)wj2xl--;
if(wj2x==wj1x&&wj2y==wj1y-1)wj2xl--;
if(wj2x==wj1x&&wj2y==wj1y+1)wj2xl--;
if(wj2x==wj1x+1&&wj2y==wj1y-1)wj2xl--;
if(wj2x==wj1x+1&&wj2y==wj1y+1)wj2xl--;
xxs++;
xx[xxs]="【剑客】发动了技能【剑雨】";
break;
}
case 2:{
cout<<"请【巫师】输入冲刺的方向(wasd上下左右 冲刺三格)\n";
char s=_getch();
while(s!='w'&&s!='a'&&s!='s'&&s!='d')s=_getch();
if(s=='w'){
for(int i=1;i<=3;i++){
if(wj1x-1<=0||wj1x-1==wj2x&&wj1y==wj2y)break;
wj1x--;
}
}
if(s=='a'){
for(int i=1;i<=3;i++){
if(wj1y-1<=0||wj1y-1==wj2y&&wj1x==wj2x)break;
wj1y--;
}
}
if(s=='s'){
for(int i=1;i<=3;i++){
if(wj1x+1>=6||wj1x+1==wj2x&&wj1y==wj2y)break;
wj1x++;
}
}
if(s=='w'){
for(int i=1;i<=3&&wj1y+1<=7;i++){
if(wj1y+1>=8||wj1y+1==wj2y&&wj1x==wj2x)break;
wj1x--;
}
}
xxd(x==1);
xxs++;
xx[xxs]="【巫师】发动了技能【冲刺】";
break;
}
case 3:{
wj1xl++;
if(wj1xl>wj1Mxl)wj1xl=wj1Mxl;
xxs++;
xx[xxs]="【肉盾】发动了技能【回血】";
break;
}
case 4:{
if(wj1x+1==wj2x&&wj1y==wj2y||wj1y-1==wj2y&&wj1x==wj2x||wj1x-1==wj2x&&wj1y==wj2y||wj1y+1==wj2y&&wj1x==wj2x){
cout<<"您对玩家2造成了暴击,他扣了三滴血";
wj2xl-=3;
xxs++;
xx[xxs]="【游侠】发动了技能【暴击】";
}
else{
cout<<"您身边并未发现玩家,技能失效";
wj1jn+=js[4].jh;
}
break;
}
}
}
else{
switch(wj2js){
case 1:{
if(wj1x==wj2x-1&&wj1y==wj2y-1)wj1xl--;
if(wj1x==wj2x-1&&wj1y==wj2y)wj1xl--;
if(wj1x==wj2x-1&&wj1y==wj2y+1)wj1xl--;
if(wj1x==wj2x&&wj1y==wj2y-1)wj1xl--;
if(wj1x==wj2x&&wj1y==wj2y+1)wj1xl--;
if(wj1x==wj2x+1&&wj1y==wj2y-1)wj1xl--;
if(wj1x==wj2x+1&&wj1y==wj2y+1)wj1xl--;
xxs++;
xx[xxs]="【剑客】发动了技能【剑雨】";
break;
}
case 2:{
cout<<"请【巫师】输入冲刺的方向(↑↓←→上下左右 冲刺三格)\n";
char s=_getch();
while(s!=72&&s!=75&&s!=80&&s!=77)s=_getch();
if(s==72){
for(int i=1;i<=3;i++){
if(wj2x-1<=0||wj2x-1==wj1x&&wj2y==wj1y)break;
wj2x--;
}
}
if(s==75){
for(int i=1;i<=3;i++){
if(wj2y-1<=0||wj2y-1==wj1y&&wj2x==wj1x)break;
wj2y--;
}
}
if(s==77){
for(int i=1;i<=3;i++){
if(wj2y+1>=6||wj2y+1==wj1y&&wj2x==wj1x)break;
wj2y++;
}
}
if(s==80){
for(int i=1;i<=3&&wj1y+1<=7;i++){
if(wj2x+1>=8||wj2x+1==wj1x&&wj2y==wj1y)break;
wj2x++;
}
}
xxd(x==1);
xxs++;
xx[xxs]="【巫师】发动了技能【冲刺】";
break;
}
case 3:{
wj2xl++;
if(wj2xl>wj2Mxl)wj2xl=wj2Mxl;
xxs++;
xx[xxs]="【肉盾】发动了技能【回血】";
break;
}
case 4:{
if(wj2x+1==wj1x&&wj2y==wj1y||wj2y-1==wj1y&&wj2x==wj1x||wj2x-1==wj1x&&wj2y==wj1y||wj2y+1==wj1y&&wj2x==wj1x){
cout<<"您对玩家1造成了暴击,他扣了三滴血";
wj1xl-=3;
xxs++;
xx[xxs]="【游侠】发动了技能【暴击】";
}
else{
cout<<"您身边并未发现玩家,技能失效";
wj2jn+=js[4].jh;
}
break;
}
}
}
return;
}
void xxd(bool as){
if(wj1x==3&&wj1y==4&&as){
cout<<"玩家1到了星星点\n请玩家1选择一项:\n1:回家 2:加一格技能点";
char s1=_getch();
while(s1!='1'&&s1!='2')s1=_getch();
if(s1=='1')wj1x=1,wj1y=1;
if(s1=='2')wj1jn++;
}
if(wj2x==3&&wj2y==4&&as==0){
cout<<"玩家2到了星星点\n请玩家1选择一项:\n1:回家 2:加一格技能点";
char s1=_getch();
while(s1!='1'&&s1!='2')s1=_getch();
if(s1=='1')wj2x=5,wj2y=7;
if(s1=='2')wj2jn++;
}
return;
}
void check(){
if(wj1xl<=0){
wj1x=1;
wj1y=4;
xxs++;
xx[xxs]="玩家1被打死了!!!";
wj1xl=wj1Mxl;
}
if(wj1xl>wj1Mxl)
wj1xl=wj1Mxl;
if(wj2xl<=0){
wj2x=5;
wj2y=4;
xxs++;
xx[xxs]="玩家2被打死了!!!";
wj2xl=wj2Mxl;
}
if(wj2xl>wj2Mxl)
wj2xl=wj2Mxl;
if(wj1x==5&&wj1y==7){
wj2kh++;
xxs++;
string s="玩家2被偷了",k="";
k+=char(wj2kh+'0');
k+="次";
s+=k;
xx[xxs]=s;
wj1x=1,wj1y=1;
}
if(wj2x==1&&wj2y==1){
wj1kh++;
xxs++;
string s="玩家1被偷了",k="";
k+=char(wj1kh+'0');
k+="次";
s+=k;
xx[xxs]=s;
wj2x=5,wj2y=7;
}
return;
}
void zk1(string s,char t,string cd){
int n=cd.size(),n1=s.size();
int tot=n1-n;
tot/=2;
for(int i=1;i<=tot;i++)
cout<<t;
cout<<s;
for(int i=1;i<=tot;i++)
cout<<t;
return;
}
void zk0(string s,char t,string cd){
cout<<s;
for(int i=s.size();i<cd.size();i++)
cout<<t;
return;
}
void color(int c)//颜色
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
return;
}
void xjs(){
system("cls");
color(12);
zk1("轮到玩家1选角色了",' '," ");
cout<<"\n\n\n";
color(15);
for(int i=1;i<=jss;i++)
{
cout<<i<<":"<<js[i].name<<" ";
}
cout<<"\n\n请输入需要选择的角色编号\n";
char s;
s=_getch();
while(s<'0'||s>char(jss+'0'))s=_getch();
wj1js=s-'0';
cout<<"选择成功!!!选择的是";
color(12);
cout<<js[wj1js].name<<"\n";
color(15);
wj1x=1,wj1y=1;
wj1Mxl=js[wj1js].xl;
wj1xl=js[wj1js].xl;
system("pause");
system("cls");
color(12);
zk1("轮到玩家2选角色了",' '," ");
cout<<"\n\n\n";
color(15);
for(int i=1;i<=jss;i++)
{
cout<<i<<":"<<js[i].name<<" ";
}
cout<<"\n\n请输入需要选择的角色编号\n";
s=_getch();
while(s<'0'||s>char(jss+'0')||s==wj1js+'0')
{
if(s==wj1js+'0'){
system("cls");
cout<<"\n不能选择和玩家1一样的角色";
system("pause");
system("cls");
color(12);
zk1("轮到玩家2选角色了",' '," ");
cout<<"\n\n\n";
color(15);
for(int i=1;i<=jss;i++)
{
cout<<i<<":"<<js[i].name<<" ";
}
cout<<"\n\n请输入需要选择的角色编号\n";
}
s=_getch();
}
wj2js=s-'0';
cout<<"选择成功!!!选择的是";
color(12);
cout<<js[wj2js].name<<"\n";
color(15);
system("pause");
wj2x=5,wj2y=7;
wj2Mxl=js[wj2js].xl;
wj2xl=js[wj2js].xl;
return;
}
void out(){
int xxt=xxs;
cout<<"-------------------------------------------------- --------------------------------\n";
for(int i=1;i<=5;i++){
for(int j=1;j<=7;j++){
cout<<"|";
if(i==1&&j==1){
cout<<"xl:"<<wj1xl<<" ";
}
else
if(i==5&&j==7){
cout<<"xl:"<<wj2xl<<" ";
}
else
if((i==1||i==5)&&j==4){
cout<<"重 ";
}
else
if(i==3&&j==4){
cout<<"☆☆☆";
}
else
cout<<" ";
}
cout<<"| |";
if(xxt>0)
zk0(xx[xxt],' '," ");
else
cout<<" ";
cout<<"|\n";
xxt--;
for(int j=1;j<=7;j++){
cout<<"|";
if(i==1&&j==1){
if(wj1x==1&&wj1y==1)color(13);
cout<<" A ";
color(15);
}
else
if(i==5&&j==7){
if(wj2x==5&&wj2y==7)color(13);
cout<<" B ";
color(15);
}
else
if((i==1||i==5)&&j==4){
bool as=1;
if(wj1x==1&&wj1y==4&&i==1&&j==4){
cout<<"生 ";
color(13);
cout<<"A ";
color(15);
as=0;
}
if(wj2x==1&&wj2y==4&&i==1&&j==4){
cout<<"生 ";
color(13);
cout<<"B ";
color(15);
as=0;
}
if(wj2x==5&&wj2y==4&&i==5&&j==4){
cout<<"生 ";
color(13);
cout<<"B ";
color(15);
as=0;
}
if(wj1x==5&&wj1y==4&&i==5&&j==4){
cout<<"生 ";
color(13);
cout<<"A ";
color(15);
as=0;
}
if(as)
cout<<"生 ";
}
else
if(i==3&&j==4){
if(wj1x==3&&wj1y==4)
{
cout<<"☆ ";
color(13);
cout<<"A";
color(15);
cout<<"☆";
}
else
if(wj2x==3&&wj2y==4)
{
cout<<"☆ ";
color(13);
cout<<"B";
color(15);
cout<<"☆";
}
else
cout<<"☆☆☆";
}
else
if(wj1x==i&&wj1y==j){
color(13);
cout<<" A ";
color(15);
}
else
if(wj2x==i&&wj2y==j){
color(13);
cout<<" B ";
color(15);
}
else
cout<<" ";
}
cout<<"| |";
if(xxt>0)
zk0(xx[xxt],' '," ");
else
cout<<" ";
cout<<"|\n";
xxt--;
for(int j=1;j<=7;j++){
cout<<"|";
if(i==1&&j==1){
cout<<"Mxl:"<<wj1Mxl<<" ";
}
else
if(i==5&&j==7){
cout<<"Mxl:"<<wj2Mxl<<" ";
}
else
if(i==1&&j==4){
cout<<"点A ";
}
else
if(i==5&&j==4){
cout<<"点B ";
}
else
if(i==3&&j==4){
cout<<"☆☆☆";
}
else
cout<<" ";
}
cout<<"| |";
if(xxt>0)
zk0(xx[xxt],' '," ");
else
cout<<" ";
cout<<"|\n";
xxt--;
if(i==5){
cout<<"-------------------------------------------------- --------------------------------\n";
continue;
}
cout<<"|------------------------------------------------| |";
if(xxt>0)
zk0(xx[xxt],' '," ");
else
cout<<" ";
cout<<"|\n";
xxt--;
if(xxt<0)xxt=0;
}
return;
}