炉石传说201609-3CSP(Java)

在这里插入图片描述

 import java.util.Scanner;

class Role {
	 int health;
	 int attack;
	public Role() {}
	public void setNumber(int health,int attack) {
		this.health=health;
		this.attack=attack;
	}
	
	public void changeHealth(int attacknum) {
		this.health-=attacknum;
	}
	
	public int getHealth() {
		return health;
	}
	
	public int getAttack() {
		return attack;
	}
}

 class Player {
	
	 Role roles[]=new Role[8];
	
	 void init() {
		Role hero=new Role();
		hero.setNumber(30, 0);
		roles[0]=hero;
	}
	
	 public void summon(int postion,int health,int attack) {
		Role temp=new Role();
		temp.setNumber(health, attack);
		for(int i=roles.length-1;i>=postion;i--) {
			if(roles[i]==null) {
				continue;
			}
			roles[i+1]=roles[i];
		}
		roles[postion]=temp;
	}
	
	 public boolean checkHeroIsDeadth() {
		if(roles[0].health<=0) {
			return true;
		}
		return false;
	}
	
	public void updateRetinuePostion() {
		for(int i=1;i<roles.length;i++) {
			if(roles[i]==null) {
				continue;
			}
			if(roles[i].health<=0) {
				for(int j=i;j+1<roles.length;j++) {
					roles[j]=roles[j+1];
					if(roles[j]==null) {
						break;
					}
				}
			}
	    }
    }
	
	public void attack(int attacker,int defender,Player another) {
		int attacknum=another.roles[defender].getAttack();
		roles[attacker].changeHealth(attacknum);
		attacknum=roles[attacker].getAttack();
		another.roles[defender].changeHealth(attacknum);
	}
	
	public void retinueNumHealth() {
		int num=0;
		int[] hvalue=new int[8];
		for(int i=1;i<roles.length;i++) {
			if(roles[i]!=null&&roles[i].health>0) {
				hvalue[num]=roles[i].health;
				num++;
			}
		}
		
		System.out.print(num+ " ");
		
		for(int i=0;i<num;i++) {
			if(i!=num-1) {
				System.out.print(hvalue[i]+ " ");
			}
			else {
				System.out.print(hvalue[i]);
			}
		}
		System.out.println();
	}
}

public class Main {
	
	static int people=0;
	
	public static int getPeople() {
		people=(people+1)%2;
		return people;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		Player A=new Player();
		A.init();
		
		Player B=new Player();
		B.init();
		
		Player game[]=new Player[2];
		game[0]=A;
		game[1]=B;
		int result=0;
		sc.nextLine();
		boolean flag=false;
		for(int i=0;i<n;i++) {
			String action=new String();
			action=sc.nextLine();
			String temp[]=action.split("\\s+");
			
			if(temp[0].equals("end")) {
				people=getPeople();
				continue;
			}

			if(temp[0].equals("summon")) {
				int postion=Integer.valueOf(temp[1]);
				int health=Integer.valueOf(temp[3]);
				int attack=Integer.valueOf(temp[2]);
				game[people].summon(postion, health, attack);
				continue;
			}
			if(temp[0].equals("attack")) {
				int attacker=Integer.valueOf(temp[1]);
				int defender=Integer.valueOf(temp[2]);
				
				game[people].attack(attacker, defender, game[(people+1)%2]);
				if(attacker==0||defender==0) {
					if(game[0].checkHeroIsDeadth()) {
						result=-1;
						flag=true;
						break;
					}
					if(game[1].checkHeroIsDeadth()) {
						result=1;
						flag=true;
						break;
					}
				}
				game[0].updateRetinuePostion();
				game[1].updateRetinuePostion();
			}
		}
		if(flag==false) {
			result=0;
		}

		System.out.println(result);
		System.out.println(game[0].roles[0].health);
		game[0].retinueNumHealth();
		System.out.println(game[1].roles[0].health);
		game[1].retinueNumHealth();
		sc.close();
	}
}

在这里插入代码片

在这里插入图片描述

实在抱歉测试只有90分,还没有找到问题,有小伙伴找到的话,欢迎评论区指教!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值