当前位置: 代码迷 >> 综合 >> csp ccf 201609-3 第三题炉石传说java 100
  详细解决方案

csp ccf 201609-3 第三题炉石传说java 100

热度:80   发布时间:2023-11-21 12:29:27.0

整个题目还是挺简单的,以下是java代码!

可以直接提交

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;//题目炉石传说
public class Main{public static void main(String[] args) throws Exception {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();//操作数量scanner.nextLine();int playercount  = 2;//控制玩家的回合分辨先手和后手玩家Game game = new Game();for(int i = 0; i < n; i++) {String command = scanner.nextLine();if(command.equals("end")) {//结束回合playercount ++;} else if(command.contains("summon")) {//召唤随从//summon <position> <attack> <health>String[] strs = command.split(" ");Follower follower = new Follower(Integer.parseInt(strs[3]),Integer.parseInt(strs[2]) );game.players[playercount%2].summonFollower(Integer.parseInt(strs[1]), follower);} else {//攻击// attack <attacker> <defender>:String[] strs = command.split(" ");int a = playercount%2;int b = a==0?1:0;game.players[a].attack(Integer.parseInt(strs[1]), Integer.parseInt(strs[2]), game.players[b]);}}game.outputResult();}//玩家类public static class Player{public int heroBlood = 30;public int code ;public Game game;public Map<Integer,Follower> followers = new HashMap<>();public Player(int code,Game game){this.code = code;this.game = game;}public void summonFollower(int place, Follower follower) {Follower fo = followers.get(place);//如果当前位置已经被占用 那么需要把右边的位置都加1if(fo != null) {int a = followers.size();for(int i =a; i >=place; i--) {//从后往前修改Follower fol = followers.get(i);//if(fol!=null) {followers.remove(i);followers.put(i+1, fol);//}}}followers.put(place, follower);}//攻击public void attack(int myFollower,int enemyFoller,Player enemy) {Follower my = followers.get(myFollower);if(enemyFoller == 0) {//如果攻击的是英雄enemy.heroBlood -= my.attack;//判断游戏是否结束boolean isGameOver = enemy.isGameOver();if(isGameOver) {game.endCode = this.code;}} else { //如果攻击的是小兵Follower enem = enemy.followers.get(enemyFoller);my.blood = my.blood - enem.attack;enem.blood = enem.blood - my.attack;updateBattleFiledAfterAttack(myFollower);enemy.updateBattleFiledAfterAttack(enemyFoller);}}//如果攻击后随从死亡 需要更新战场public void updateBattleFiledAfterAttack(int place) {Follower f = followers.get(place);if(f.blood <= 0) {followers.remove(place);if(followers.get(place+1)==null) {//如果当前死亡的是最后一个不需要更新序号return ;} else if(place==1 && followers.size() >=1){//如果死的是第一个且随从中不止一个for(int i = 2; i <= followers.size()+1; i++) {Follower follower = followers.get(i);followers.remove(i);followers.put(i-1, follower);}} else {for(int i = place-1; i >=1;i-- ) {Follower ff = followers.get(i);if(ff != null) {followers.remove(i);followers.put(i+1, ff);}}}}}public boolean isGameOver() {return heroBlood <=0? true:false;}}//随从类public static class Follower{public int blood = 0;public int attack = 0;public Follower(int blood, int attack) {this.blood = blood;this.attack = attack;}}//游戏类public static class Game{public Player[] players = new Player[2];public int endCode = 0;//1表示先手玩家赢了 -1表示后手 0表示还没游戏结束public Game() {players[0] = new Player(1,this);players[1] = new Player(-1,this);}public void outputResult() {System.out.println(endCode);//输出先手的情况System.out.println(players[0].heroBlood);System.out.print(players[0].followers.size());for(int i = 1;i<=7;i++) {Follower follower = players[0].followers.get(i);if(follower == null) {break;} else {System.out.print(" "+follower.blood);}}System.out.println();//输出后手System.out.println(players[1].heroBlood);System.out.print(players[1].followers.size());for(int i = 1;i<=7;i++) {Follower follower = players[1].followers.get(i);if(follower == null) {break;} else {System.out.print(" "+follower.blood);}}}}
}