模仿 神仙道 九宫格模式写的 战斗算法,分为3个类,武将类代码如下:
package duizhan; public class WuJiangBean { private int gongji;//武将攻击力 private int fangyu;//武将防御力 private int hp;//武将HP private String name;//武将名称 private boolean isHandelNow=true;//true 攻击方,false 被打方 private boolean isDead=false;//true 为死亡,false 未死亡 //武将属性设置 (A队) public WuJiangBean A_WuJiang(String name,int fangyu,int gongji,int hp){ //设置武将选中位置的属性值 WuJiangBean WuJiangBean=new WuJiangBean(); WuJiangBean.setFangyu(fangyu); WuJiangBean.setGongji(gongji); WuJiangBean.setHp(hp); WuJiangBean.setName("A队的: "+name); return WuJiangBean; } //BOSS属性设置 (B队) public WuJiangBean B_WuJiang(String name,int fangyu,int gongji,int hp){ //设置武将选中位置的属性值 WuJiangBean WuJiangBean=new WuJiangBean(); WuJiangBean.setFangyu(fangyu); WuJiangBean.setGongji(gongji); WuJiangBean.setHp(hp); WuJiangBean.setName("B队的: "+name); return WuJiangBean; } public int getGongji() { return gongji; } public void setGongji(int gongji) { this.gongji = gongji; } public int getFangyu() { return fangyu; } public void setFangyu(int fangyu) { this.fangyu = fangyu; } public int getHp() { return hp; } public void setHp(int hp) { this.hp = hp; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isHandelNow() { return isHandelNow; } public void setHandelNow(boolean isHandelNow) { this.isHandelNow = isHandelNow; } public boolean isDead() { return isDead; } public void setDead(boolean isDead) { this.isDead = isDead; } }
下一个类战斗具体算法类,代码如下:
package duizhan; import java.util.Random; public class ZhanDou { int count=0; //打架过程代码 public void zhandou(WuJiangBean a[][],WuJiangBean b[][]){ boolean startFlag=true;//开始进入战斗模式 boolean teamChangeFlag=true;//true为A出手,false为B出手 while(startFlag){//由于未知循环次数,while 循环直至 A或B队伍武将阵亡 //全部出手过了,全部设置成未出手 boolean ishandl=true; for (int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(a[i][j] != null && a[i][j].isHandelNow() != true){ ishandl=false; } if(b[i][j] != null && b[i][j].isHandelNow()!=true){ ishandl=false; } } } if(ishandl){//所有人初始化未出手 for(int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ if(a[i][j]!=null){ a[i][j].setHandelNow(true); } if(b[i][j]!=null){ b[i][j].setHandelNow(true); } } } } int row=0;//行变量 WuJiangBean beiDa=null;//被攻击 WuJiangBean jinGong=null;//进攻者 if(teamChangeFlag){//true为A攻击,false为B攻击 for (int i = 0; i <= 2; i++) { for (int j = 2; j >= 0; j--) { if(a[j][i]!=null && a[j][i].isHandelNow() && a[j][i].getHp()>0){ jinGong=a[j][i]; row=j;//判断是第几行对战 } } } boolean currentBoolean=false; for (int i = row; i < 3 && i > -1;){ for (int j = 2; j < 3 && j > -1; j--) { if(b[i][j] != null && b[i][j].getHp()>0){ beiDa=b[i][j]; currentBoolean=true; } } if(currentBoolean){//说明对应行存在武将 break; } if(row!=0){ i--; }else{ i++; } if(i==-1){ i=2; } } }else{//b队转为进攻方 a队改为被打方 int rowB=0; //boolean flag=false; for (int i = 2; i >= 0; i--) { for (int j = 2; j >= 0; j--) { if(b[j][i]!=null && b[j][i].isHandelNow() && b[j][i].getHp()>0){ jinGong=b[j][i]; rowB=j;//判断是第几行对战 } } } boolean isCunZai=false; for (int i = rowB; i < 3 && i > -1;){ for (int j = 0; j < 3 && j > -1; j++) { if(a[i][j] != null && a[i][j].getHp()>0){ beiDa=a[i][j]; isCunZai=true; } } if(isCunZai){ break; } if(rowB!=0){ i--; }else{ i++; } if(i==-1){ i=2; } } } this.shuChu(jinGong, beiDa); if(teamChangeFlag){//交换攻击对象 teamChangeFlag=false; }else{ teamChangeFlag=true; this.setInitValue(a,b);//判断是否进行一次对打结束 } boolean a_DeadFlag = this.isDeadNot(a); if(a_DeadFlag){ System.out.println("A队输"); break; } boolean b_DeadFlag = this.isDeadNot(b); if(b_DeadFlag){ System.out.println("B队输"); break; } } } //判断武将队伍是否全部死亡 public boolean isDeadNot(WuJiangBean dead[][]){ int noDeadCount=0;//武将活着的人数 boolean deadFlag=false; for (int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ if(dead[i][j] != null ){ if(dead[i][j].isDead() == false){//武将活着 noDeadCount = noDeadCount+1;//统计活着的武将人数 } } } } if(noDeadCount > 0){//部分武将还活着 deadFlag=false;//武将未死 }else{ deadFlag=true;//武将全部阵亡 } return deadFlag; } //初始对打标志 public void setInitValue(WuJiangBean a[][],WuJiangBean b[][]){ boolean a_Flag=false; boolean b_Flag=false; for (int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++){ if(a[i][j] != null ){//非空 未死亡的对象 if(a[i][j].isDead() == false){//武将还活着的情况下 if(a[i][j].isHandelNow() == false){ a_Flag=true; }else{ a_Flag = false; } } } if(b[i][j] != null){ if(b[i][j].isDead() == false){//武将还活着的情况下 if( b[i][j].isHandelNow() == false ){ b_Flag=true; }else{ b_Flag = false; } } } } } if(a_Flag ==true && b_Flag == true){// 所有对打进行一遍后,初始对打标志 count+=1; System.out.println("已经打完了 "+count+ "回合"); for(int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ if(a[i][j]!=null && a[i][j].isHandelNow() == false){ a[i][j].setHandelNow(true); } if(b[i][j]!=null && b[i][j].isHandelNow() == false){ b[i][j].setHandelNow(true); } } } } } public void shuChu(WuJiangBean jinGong,WuJiangBean beiDa){ int hp=0;//掉血数 int leftHp=0;//剩余Hp数 Random r=new Random(); if(jinGong != null && beiDa != null && jinGong.getGongji()-beiDa.getFangyu()>0){ hp=jinGong.getGongji()-beiDa.getFangyu()+r.nextInt(20)+1;//掉血数 leftHp=beiDa.getHp()-hp;//当前剩余血数 System.out.println(jinGong.getName()+" 攻击 "+beiDa.getName()+" "+beiDa.getName()+" 损失"+hp+"点体力"); //jinGong.setIshander(false); }else{//(如果攻击力-防御力为负值时公式变为:0+(1到20)). hp=r.nextInt(20)+1; if(beiDa != null && jinGong != null){ leftHp=beiDa.getHp()-hp;//当前剩余血数 System.out.println(jinGong.getName()+" 攻击 "+beiDa.getName()+" "+beiDa.getName()+" 损失"+hp+"点体力"); } } if(beiDa != null && jinGong != null){ if(leftHp<=0){ System.out.println(beiDa.getName()+" 死亡"); beiDa.setHp(0); beiDa.setDead(true);//死亡 }else{ beiDa.setHp(leftHp); beiDa.setDead(false);//未死亡 } jinGong.setHandelNow(false); } } } 下一个为将武将存放在二维数组中,这样就可以随意摆放武将的位置,代码如下:
package duizhan; public class MainZhandou { public static void main(String[] args) { //A队B队初始化 定义二维数组 WuJiangBean[][] a = new WuJiangBean[3][3];//A 队武将 WuJiangBean[][] b = new WuJiangBean[3][3];// B 队武将 WuJiangBean WuJiangBean=new WuJiangBean(); //设置武将在布阵中的位置及属性 A队 a[0][2]=WuJiangBean.A_WuJiang("上官文静",280,380,300);//设置武将属性 (名字,防御,攻击,hp) a[1][0]=WuJiangBean.A_WuJiang("李四",200,60,320); a[2][2]=WuJiangBean.A_WuJiang("张麻子",200,70,320); //a[1][0]=WuJiangBean.A_WuJiang("李四",200,50,350); //a[2][1]=WuJiangBean.A_WuJiang("王五",200,65,380); //设置BOSS位置及属性 B 队 b[0][1]=WuJiangBean.B_WuJiang("闭月",300,180,400); // b[0][2]=WuJiangBean.B_WuJiang("羞花",460,105,500); b[1][1]=WuJiangBean.B_WuJiang("冷月",420,262,300); b[1][2]=WuJiangBean.B_WuJiang("蛇仙",430,100,300); // b[2][2]=WuJiangBean.B_WuJiang("小灵",520,380,300); new ZhanDou().zhandou(a, b);//战斗方法 } }
代码难免不足,只求分享!