[JAVA] J'aimerais savoir comment améliorer le code redondant!

public class Battle { static Hero player1; static Hero player2; static Skill Skill11; static Skill Skill12; static Skill Skill13; static Skill Skill14; static Skill Skill21; static Skill Skill22; static Skill Skill23; static Skill Skill24;

public static void main(String[] args) {
	Hero[] players = {null,player1,player2};
	MakePlayers.chooseHero(players);

	Skill[] skills1 = {null,Skill11,Skill12,Skill13,Skill14};
	Skill[] skills2 = {null,Skill21,Skill22,Skill23,Skill24};
	MakePlayers.chooseSkill(players,skills1,skills2);

	for (;;) {

//技発動部 PlaySkill.playSkill(players,skills1,skills2);

//先攻後攻決定部 int ranSpd1 = new java.util.Random().nextInt(players[1].getSpd()); int conSpd1 = players[1].getSpd() / 2; int realSpd1 = ranSpd1 + conSpd1; int ranSpd2 = new java.util.Random().nextInt(players[2].getSpd()); int conSpd2 = players[2].getSpd() / 2; int realSpd2 = ranSpd2 + conSpd2;

		if (realSpd1 >= realSpd2) {

//第1計算部 //技タイプ判断課 if (players[1].skill.getType() == 1) { //通常攻撃 CalcDam.calcDam1(players,skills1,skills2);

			} else if (players[1].skill.getType() == 2 && players[1].getMp() >= players[1].skill.getmCost()) {
				CalcMdam.calcMdam1(players,skills1,skills2);
			}
		}

//第2計算部 if (players[2].skill.getType() == 1) { CalcDam.calcDam2(players,skills1,skills2);

		} else if (players[2].skill.getType() == 2) {
			CalcMdam.calcMdam2(players,skills1,skills2);
			
		}

		if (realSpd1 < realSpd2) {

//第1計算部 if (players[1].skill.getType() == 1) { //通常攻撃 CalcDam.calcDam1(players,skills1,skills2);

			} else if (players[1].skill.getType() == 2 && players[1].getMp() >= players[1].skill.getmCost()) {
				CalcMdam.calcMdam1(players,skills1,skills2);
			}
		}

//判断部

		if (players[2].getHp() <= 0 && players[1].getHp() > 0) {

System.out.println (joueurs [2] .getName () + "vaincu!"); break; } else if (players[2].getHp() > 0 && players[1].getHp() <= 0) { System.out.println (joueurs [1] .getName () + "a été tué ..."); break; } else if (players[2].getHp() <= 0 && players[1].getHp() <= 0) { System.out.println (joueurs [2] .getName () + joueurs [1] .getName () + "est tombé ..."); break; } } } } ////////////////////////////////////////////

public class CalcDam { static void calcDam1(Hero[] players, Skill[] skills1, Skill[] skills2) { //atk計算課 double dAtk1 = -players[1].getAtk(); double aExp1 = Math.exp(dAtk1 / 1200); double atkVal1 = 1 - aExp1; double realAtkVal1 = players[1].skill.getDamage() * (1 + atkVal1);

//def計算課 double ddef1 = -players[2].getDef(); double dExp1 = Math.exp(ddef1 / 1200); double defVal1 = 1 - dExp1; double realdefVal1 = realAtkVal1 * (1 - defVal1);

//ダメージ計算課 int realDamage1 = (int) Math.floor(realdefVal1); joueurs [2] .setHp (joueurs [2] .getHp () --realDamage1); // Calcul des dégâts de l'ennemi System.out.print (joueurs [1] .getName () + "" + joueurs [1] .skill.getName () + "!"); System.out.println (joueurs [2] .getName () + "à" + realDamage1 + "endommagé!"); System.out.print (joueurs [2] .getName () + "force physique restante" + joueurs [2] .getHp () + "est!");

//MP計算課 int ranAddMp1 = new java.util.Random().nextInt(players[1].getMgc()); int conAddMp1 = players[1].getMgc() / 2; int addMp1 = (int) Math.floor((ranAddMp1 + conAddMp1) / 4); players[1].setMp(players[1].getMp() + addMp1); System.out.println (joueurs [1] .getName () + "MP augmenté" + addMp1 + "augmenté!"); }

static void calcDam2(Hero[] players, Skill[] skills1, Skill[] skills2) {

//atk計算課 double dAtk2 = -players[2].getAtk(); double aExp2 = Math.exp(dAtk2 / 1200); double atkVal2 = 1 - aExp2; double realAtkVal2 = players[2].skill.getDamage() * (1 + atkVal2);

//def計算課 double ddef2 = -players[1].getDef(); double dExp2 = Math.exp(ddef2 / 1200); double defVal2 = 1 - dExp2; double realDefVal2 = realAtkVal2 * (1 - defVal2);

//ダメージ計算課 int realDamage2 = (int) Math.floor(realDefVal2); joueurs [1] .setHp (joueurs [1] .getHp () --realDamage2); // Calcul des dégâts de l'ennemi System.out.print (joueurs [2] .getName () + "" + joueurs [2] .skill.getName () + "!"); System.out.println (joueurs [1] .getName () + "à" + realDamage2 + "endommagé!"); System.out.print (joueurs [1] .getName () + "force physique restante" + joueurs [1] .getHp () + "est!");

//MP計算課 int ranAddMp2 = new java.util.Random().nextInt(players[2].getMgc()); int conAddMp2 = players[2].getMgc() / 2; int addMp2 = (int) ((ranAddMp2 + conAddMp2) / 4); players[2].setMp(players[2].getMp() + addMp2); System.out.println (joueurs [2] .getName () + "MP augmenté" + addMp2 + "augmenté!"); } } /////////////////////////////////////////

public class CalcMdam { static void calcMdam1(Hero[] players, Skill[] skills1, Skill[] skills2) { //魔術攻撃 //mp減少課 players[1].setMp(players[1].getMp() - players[1].skill.getmCost()); //matk計算課 double mAtk1 = -players[1].getMgc(); double mExp1 = Math.exp(mAtk1 / 1200); double matkVal1 = 1 - mExp1; double realmAtkVal1 = players[1].skill.getmDamage() * (1 + matkVal1);

//mdef計算課

	double mddef1 = -players[2].getMgc();
	double mdExp1 = Math.exp(mddef1 / 1200);
	double mdefVal1 = 1 - mdExp1;
	double realmdefVal1 = realmAtkVal1 * (1 - mdefVal1);

//ダメージ計算課 int realmDamage1 = (int) Math.floor(realmdefVal1); joueurs [2] .setHp (joueurs [2] .getHp () --realmDamage1); // Calcul des dégâts de l'ennemi System.out.print (joueurs [1] .getName () + "" + joueurs [1] .skill.getName () + "!"); System.out.println (joueurs [2] .getName () + "à" + realmDamage1 + "endommagé!"); System.out.print (joueurs [2] .getName () + "force physique restante" + joueurs [2] .getHp () + "est!");

//MP計算課 int ranAddMp1 = new java.util.Random().nextInt(players[1].getMgc()); int conAddMp1 = players[1].getMgc() / 2; int addMp1 = (int) Math.floor((ranAddMp1 + conAddMp1) / 4); players[1].setMp(players[1].getMp() + addMp1); System.out.println (joueurs [1] .getName () + "MP augmenté" + addMp1 + "augmenté!"); }

static void calcMdam2(Hero[] players, Skill[] skills1, Skill[] skills2) {

//魔術攻撃 //mp減少課 players[2].setMp(players[2].getMp() - players[2].skill.getmCost()); //matk計算課 double mAtk2 = -players[2].getMgc(); double mExp2 = Math.exp(mAtk2 / 1200); double matkVal2 = 1 - mExp2; double realmAtkVal2 = players[2].skill.getmDamage() * (1 + matkVal2);

//mdef計算課

	double mddef2 = -players[1].getMgc();
	double mdExp2 = Math.exp(mddef2 / 1200);
	double mdefVal2 = 1 - mdExp2;
	double realmdefVal2 = realmAtkVal2 * (1 - mdefVal2);

//ダメージ計算課 int realmDamage2 = (int) Math.floor(realmdefVal2); joueurs [1] .setHp (joueurs [1] .getHp () --realmDamage2); // Calcul des dégâts de l'ennemi System.out.print (joueurs [2] .getName () + "" + joueurs [2] .skill.getName () + "!"); System.out.println (joueurs [1] .getName () + "à" + realmDamage2 + "endommagé!"); System.out.print (joueurs [1] .getName () + "force physique restante" + joueurs [1] .getHp () + "est!");

//MP計算課 int ranAddMp2 = new java.util.Random().nextInt(players[2].getMgc()); int conAddMp2 = players[2].getMgc() / 2; int addMp2 = (int) Math.floor((ranAddMp2 + conAddMp2) / 4); players[2].setMp(players[2].getMp() + addMp2); System.out.println (joueurs [2] .getName () + "MP augmenté" + addMp2 + "augmenté!"); } } ///////////////////////////////////////////

public class Hero { private int id; private String name; private int hp; private int mp=0; private int atk; private int def; private int spd; private int mgc; Skill skill;

public int getId() {
	return id;
}

public void setId(int id) {
	this.id = id;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getHp() {
	return hp;
}

public void setHp(int hp) {
	this.hp = hp;
}

public int getMp() {
	return mp;
}

public void setMp(int mp) {
	this.mp = mp;
}

public int getAtk() {
	return atk;
}

public void setAtk(int atk) {
	this.atk = atk;
}

public int getDef() {
	return def;
}

public void setDef(int def) {
	this.def = def;
}

public int getSpd() {
	return spd;
}

public void setSpd(int spd) {
	this.spd = spd;
}

public int getMgc() {
	return mgc;
}

public void setMgc(int mgc) {
	this.mgc = mgc;
}

}

//////////////////////////////////////////////////

public class LichKing extends Hero{

LichKing(){
	setId(6);

setName ("Rich King"); setHp(1200); setAtk(600); setDef(600); setSpd(600); setMgc(600); } } ////////////////////////////////////////

import java.util.ArrayList; import java.util.Collections; import java.util.Scanner;

public class MakePlayers { static void chooseHero(Hero[] players) { //ヒーローリスト表示 Hero[] HeroList = new Hero[2]; HeroList[0] = new Paladin(); HeroList[1] = new LichKing(); for (int i = 0; i < HeroList.length; i++) { System.out.print(i + 1 + ":" + HeroList[i].getName()); } System.out.println();

//ヒーロー選択 System.out.println ("\ n Aventurier rouge, choisissez un héros"); int c1; do { c1 = new Scanner(System.in).nextInt(); if (c1 < 0 || c1 >= 7) { System.out.println ("Vous ne pouvez pas choisir ce nombre"); } } while (c1 < 0 || c1 >= 7); switch (c1) { case 1: { players[1] = new Paladin(); System.out.println (joueurs [1] .getName () + "a été sélectionné! \ N"); break; } case 2: { players[1] = new LichKing(); System.out.println (joueurs [1] .getName () + "a été sélectionné! \ N"); break; } } System.out.println ("\ n Aventurier bleu, choisissez un héros"); int c2; do { c2 = new Scanner(System.in).nextInt(); if (c2 < 0 || c2 >= 7) { System.out.println ("Vous ne pouvez pas choisir ce nombre"); } } while (c2 < 0 || c2 >= 7); switch (c2) { case 1: { players[2] = new Paladin(); System.out.println (joueurs [2] .getName () + "a été sélectionné! \ N"); break; } case 2: { players[2] = new LichKing(); System.out.println (joueurs [2] .getName () + "a été sélectionné! \ N"); break; } } }

//技選択

static void chooseSkill(Hero[] players, Skill[] skills1, Skill[] skills2) {
	
	ArrayList<Integer> list = new ArrayList<Integer>();

// Mettez une valeur dans la liste. Ordre croissant à ce stade for(int i = 0 ; i < 3 ; i++) { list.add(i); } // Mélanger et changer l'ordre Collections.shuffle(list);

	Skill skillBox = new Skill();

System.out.println ("\ n Aventurier rouge," + joueurs [1] .getName () + "La compétence sera décidée!");

	System.out.println();

System.out.println ("La première astuce"); if (joueurs [1] .getName (). equals ("Palladin")) { switch (list.get(0)) { case 0: { skills1[1] = skillBox.new silverSlash(); System.out.println ("Barre oblique argentée!"); break; } case 1: { skills1[1] = skillBox.new ikari(); System.out.println ("C'est un marteau en colère!"); break; } case 2: { skills1[1] = skillBox.new goldSlash(); System.out.println ("Barre oblique!"); break; } }

} else if (joueurs [1] .getName (). equals ("rich king")) { switch (list.get(0)) { case 0: { skills1[1] = skillBox.new frostMourne(); System.out.println ("Frost Morne!"); break; } case 1: { skills1[1] = skillBox.new huhai(); System.out.println ("Mort et corruption!"); break; } case 2: { skills1[1] = skillBox.new deathcoil(); System.out.println ("Spirale de la mort!"); break; } } }

System.out.println ("La deuxième astuce est"); if (joueurs [1] .getName (). equals ("Palladin")) { switch (list.get(1)) { case 0: { skills1[2] = skillBox.new silverSlash(); System.out.println ("Barre oblique argentée!"); break; } case 1:{ skills1[2] = skillBox.new ikari(); System.out.println ("C'est un marteau en colère!"); break; } case 2: { skills1[1] = skillBox.new goldSlash(); System.out.println ("Barre oblique!"); break; } } } else if (joueurs [1] .getName (). equals ("rich king")) { switch (list.get(1)) { case 0: { skills1[2] = skillBox.new frostMourne(); System.out.println ("Frost Morne!"); break; } case 1: { skills1[2] = skillBox.new huhai(); System.out.println ("Mort et corruption!"); break; } case 2: { skills1[1] = skillBox.new deathcoil(); System.out.println ("Spirale de la mort!"); break; } } }

	Collections.shuffle(list);

System.out.println ("\ n Blue Adventurer," + joueurs [2] .getName () + "La compétence sera décidée!");

	System.out.println();

System.out.println ("La première astuce"); if (joueurs [2] .getName (). equals ("Palladin")) { switch (list.get(0)) { case 0: { skills2[1] = skillBox.new silverSlash(); System.out.println ("Barre oblique argentée!"); break; } case 1:{ skills2[1] = skillBox.new ikari(); System.out.println ("C'est un marteau en colère!"); break; } case 2: { skills1[1] = skillBox.new goldSlash(); System.out.println ("Barre oblique!"); break; } } } else if (joueurs [2] .getName (). equals ("rich king")) { switch (list.get(0)) { case 0: { skills2[1] = skillBox.new frostMourne(); System.out.println ("Frost Morne!"); break; } case 1: { skills2[1] = skillBox.new huhai(); System.out.println ("Mort et corruption!"); break; } case 2: { skills1[1] = skillBox.new deathcoil(); System.out.println ("Spirale de la mort!"); break; } } } System.out.println ("La deuxième astuce est"); if (joueurs [2] .getName (). equals ("Palladin")) { switch (list.get(1)) { case 0: { skills2[2] = skillBox.new silverSlash(); System.out.println ("Barre oblique argentée!"); break; } case 1:{ skills2[2] = skillBox.new ikari(); System.out.println ("C'est un marteau en colère!"); break; } case 2: { skills1[1] = skillBox.new goldSlash(); System.out.println ("Barre oblique!"); break; } } } else if (joueurs [2] .getName (). equals ("rich king")) { switch (list.get(1)) { case 0: { skills2[2] = skillBox.new frostMourne(); System.out.println ("Frost Morne!"); break; } case 1: { skills2[2] = skillBox.new huhai(); System.out.println ("Mort et corruption!"); break; } case 2: { skills1[1] = skillBox.new deathcoil(); System.out.println ("Spirale de la mort!"); break; } } } } } //////////////////////////////////////////

public class Paladin extends Hero{

Paladin(){
	setId(1);

setName ("Paladin"); setHp(800); setAtk(300); setDef(300); setSpd(300); setMgc(300); } } /////////////////////////////////////////

import java.util.Scanner;

public class PlaySkill {

public static void playSkill(Hero[] players, Skill[] skills1, Skill[] skills2) {

System.out.println ("\ n Aventurier rouge, active la technique"); System.out.println("1:"+skills1[1].getName()+" 2:"+skills1[2].getName()); int c1; do { c1 = new Scanner(System.in).nextInt(); switch (c1) { case 1: { players[1].skill = skills1[1]; break; } case 2: { players[1].skill = skills1[2]; break; } } if (skills1[c1].getType() == 2 && players[1].getMp() < skills1[c1].getmCost()) { System.out.println ("Pas assez de MP!"); } } while (skills1[c1].getType() == 0 || (skills1[c1].getType() == 2 && players[1].getMp() < skills1[c1].getmCost()));

System.out.println ("\ n Blue Adventurer, active la technique"); System.out.println("1:"+skills2[1].getName()+" 2:"+skills2[2].getName()); int c2; do { c2 = new Scanner(System.in).nextInt(); switch (c2) { case 1: { players[2].skill = skills2[1]; break; } case 2: { players[2].skill = skills2[2]; break; } } if (skills2[c2].getType() == 2 && players[2].getMp() < skills2[c2].getmCost()) { System.out.println ("Pas assez de MP!"); } } while (skills2[c2].getType() == 0 || (skills2[c2].getType() == 2 && players[2].getMp() < skills2[c2].getmCost())); }

} ////////////////////////////////////////////////

public class Skill {

private String name;
private int damage;
private int mDamage;
private int mCost;
private int type;

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getDamage() {
	return damage;
}

public void setDamage(int damage) {
	this.damage = damage;
}

public int getmDamage() {
	return mDamage;
}

public void setmDamage(int mDamage) {
	this.mDamage = mDamage;
}

public int getmCost() {
	return mCost;
}

public void setmCost(int mCost) {
	this.mCost = mCost;
}

public int getType() {
	return type;
}

public void setType(int type) {
	this.type = type;
}


public class silverSlash extends Skill {
	silverSlash() {

setName ("Silver Slash"); setDamage(50); setType(1); } }

public class ikari extends Skill {
	ikari() {

setName ("Marteau en colère"); setmDamage(100); setmCost(300); setType(2); } }

public class goldSlash extends Skill {
	goldSlash() {

setName ("barre oblique"); setDamage(100); setType(1); } }

public class frostMourne extends Skill {
	frostMourne() {

setName ("Frost Morne"); setDamage(100); setType(1); } } public class huhai extends Skill { huhai() { setName ("mort et corruption"); setmDamage(500); setmCost(200); setType(2); } }

public class deathcoil extends Skill {
	deathcoil() {

setName ("Spirale de la mort"); setDamage(150); setType(1); } } }

Recommended Posts

J'aimerais savoir comment améliorer le code redondant!
[Rails] Je ne sais pas comment utiliser le modèle ...
J'ai testé comment utiliser le test / l'unité de Ruby et le code de Janken.
J'ai essayé de migrer le traitement vers VS Code
Comment appeler le code Swift 5.3 depuis Objective-C
J'ai essayé d'utiliser Selenium comme JQuery
Comment compter rapidement les points de code UTF-8
Comment colorer la sortie de la console de code dans Eclipse
Comment écrire une instruction if pour améliorer la lisibilité-java
Code utilisé pour connecter Rails 3 à PostgreSQL 10
Comment écrire du code qui pense Ruby orienté objet
Comment implémenter une fonctionnalité similaire dans Rails
Comment écrire du code de test avec la certification de base