ccf炉石传说

来源:互联网 发布:淘宝联盟微信打不开 编辑:程序博客网 时间:2024/04/29 17:22

刷了几题ccf之后发现他的第三题并没有很难,没有用上算法,只是题目很复杂,实现起来用的结构比较麻烦而已

炉石传说的代码是看了别人的再自己写的,奈何思路完全被套住了所以写出的代码几乎一致,会再更新自己思路的代码

package CCF;import java.util.ArrayList;import java.util.List;import java.util.Scanner;class role{public int attack;public int health;role(int a,int b){attack=a;health=b;}}public class Game {public static List<role> p1=new ArrayList<role>();    //存储先手玩家的英雄和随从public static List<role> p2=new ArrayList<role>();    //存储后手玩家的英雄和随从//两个分别指向p1和p2的引用,在进行轮换攻击的时候操作的是引用,因此P1始终是先手,P2始终是后手static List<role> fA;                          static List<role> sA; public static void main(String args[]){Scanner scan=new Scanner(System.in);//将两个引用指向两个链表fA=p1;sA=p2;//创建两个英雄role hero1=new role(0,30);role hero2=new role(0,30);//将两个英雄存入两个链表中p1.add(hero1);p2.add(hero2);String []cmd;String line;int n=Integer.parseInt(scan.nextLine());for(int i=0;i<n;i++){line=scan.nextLine()+" ";cmd=line.split(" ");//添加随从操作if(cmd.length==4){operate(cmd[0],Integer.parseInt(cmd[1]),Integer.parseInt(cmd[2]),Integer.parseInt(cmd[3]));//攻击操作}else if(cmd.length==3){operate(cmd[0],Integer.parseInt(cmd[1]),Integer.parseInt(cmd[2]),0);//end操作}else{operate(cmd[0],0,0,0);}}//做完一系列操作之后判断哪方胜利if(p1.get(0).health<=0){System.out.println(-1);}else{if(p2.get(0).health>0){System.out.println(0);}else{System.out.println(1);}}//在判断哪方胜利后,需要遍历并输出双方英雄和随从的生命值fheal(p1);fheal(p2);}public static void operate(String ope,int a,int b,int c){//对于summon操作,a相当于随从添加位置,b是攻击值attack,c是生命值healthif(ope.equals("summon")){fA.add(a,new role(b,c));//对于attack操作,a是己方位置,b是地方位置}else if(ope.equals("attack")){fA.get(a).health-=sA.get(b).attack;sA.get(b).health-=fA.get(a).attack;//攻击操作完成之后,要立刻检查随从是否死掉并移除生命值为负的随从remove();}else{//在这个地方交换攻击的双方List<role> temp;temp=fA;fA=sA;sA=temp;}}public static void fheal(List<role> p){System.out.println(p.get(0).health);System.out.print(p.size()-1+" ");if(p.size()>1){for(int i=1;i<p.size();i++){System.out.print(p.get(i).health+" ");}System.out.println();}else{System.out.println();}}//找到并移除生命值小于等于0的随从public static void remove(){for(int i=1;i<p1.size();i++){if(p1.get(i).health<=0){p1.remove(i);}}for(int i=1;i<p2.size();i++){if(p2.get(i).health<=0){p2.remove(i);}}}}

思路什么都很详细了

ccf前三题其实都不难,基本功就行,还是要多加练习