[JAVA] I would like to know how to improve redundant code!

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 (players [2] .getName () + "defeated!"); break; } else if (players[2].getHp() > 0 && players[1].getHp() <= 0) { System.out.println (players [1] .getName () + "has been killed ..."); break; } else if (players[2].getHp() <= 0 && players[1].getHp() <= 0) { System.out.println (players [2] .getName () + players [1] .getName () + "has fallen ..."); 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); players [2] .setHp (players [2] .getHp () --realDamage1); // Enemy damage calculation System.out.print (players [1] .getName () + "" + players [1] .skill.getName () + "!"); System.out.println (players [2] .getName () + "to" + realDamage1 + "damaged!"); System.out.print (players [2] .getName () + "remaining health" + players [2] .getHp () + "is!");

//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 (players [1] .getName () + "MP increased" + addMp1 + "increased!"); }

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); players [1] .setHp (players [1] .getHp () --realDamage2); // Enemy damage calculation System.out.print (players [2] .getName () + "" + players [2] .skill.getName () + "!"); System.out.println (players [1] .getName () + "to" + realDamage2 + "damaged!"); System.out.print (players [1] .getName () + "remaining health" + players [1] .getHp () + "is!");

//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 (players [2] .getName () + "MP increased" + addMp2 + "increased!"); } } /////////////////////////////////////////

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); players [2] .setHp (players [2] .getHp () --realmDamage1); // Enemy damage calculation System.out.print (players [1] .getName () + "" + players [1] .skill.getName () + "!"); System.out.println (players [2] .getName () + "to" + realmDamage1 + "damaged!"); System.out.print (players [2] .getName () + "remaining health" + players [2] .getHp () + "is!");

//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 (players [1] .getName () + "MP increased" + addMp1 + "increased!"); }

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); players [1] .setHp (players [1] .getHp () --realmDamage2); // Enemy damage calculation System.out.print (players [2] .getName () + "" + players [2] .skill.getName () + "!"); System.out.println (players [1] .getName () + "to" + realmDamage2 + "damaged!"); System.out.print (players [1] .getName () + "remaining health" + players [1] .getHp () + "is!");

//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 (players [2] .getName () + "MP increased" + addMp2 + "increased!"); } } ///////////////////////////////////////////

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 Red Adventurer, choose a hero"); int c1; do { c1 = new Scanner(System.in).nextInt(); if (c1 < 0 || c1 >= 7) { System.out.println ("You can't choose that number"); } } while (c1 < 0 || c1 >= 7); switch (c1) { case 1: { players[1] = new Paladin(); System.out.println (players [1] .getName () + "selected! \ N"); break; } case 2: { players[1] = new LichKing(); System.out.println (players [1] .getName () + "selected! \ N"); break; } } System.out.println ("\ n Blue Adventurer, choose a hero"); int c2; do { c2 = new Scanner(System.in).nextInt(); if (c2 < 0 || c2 >= 7) { System.out.println ("You can't choose that number"); } } while (c2 < 0 || c2 >= 7); switch (c2) { case 1: { players[2] = new Paladin(); System.out.println (players [2] .getName () + "selected! \ N"); break; } case 2: { players[2] = new LichKing(); System.out.println (players [2] .getName () + "selected! \ N"); break; } } }

//技選択

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

// Put a value in list. Ascending order at this stage for(int i = 0 ; i < 3 ; i++) { list.add(i); } // Shuffle and change the order Collections.shuffle(list);

	Skill skillBox = new Skill();

System.out.println ("\ n Red Adventurer," + players [1] .getName () + "The skill will be decided!");

	System.out.println();

System.out.println ("The first trick"); if (players [1] .getName (). equals ("paladin")) { switch (list.get(0)) { case 0: { skills1[1] = skillBox.new silverSlash(); System.out.println ("Silver slash!"); break; } case 1: { skills1[1] = skillBox.new ikari(); System.out.println ("Angry hammer!"); break; } case 2: { skills1[1] = skillBox.new goldSlash(); System.out.println ("Gold slash!"); break; } }

} else if (players [1] .getName (). equals ("rich king")) { switch (list.get(0)) { case 0: { skills1[1] = skillBox.new frostMourne(); System.out.println ("Frostmorn!"); break; } case 1: { skills1[1] = skillBox.new huhai(); System.out.println ("Death and Corruption!"); break; } case 2: { skills1[1] = skillBox.new deathcoil(); System.out.println ("Spiral of Death!"); break; } } }

System.out.println ("The second trick"); if (players [1] .getName (). equals ("paladin")) { switch (list.get(1)) { case 0: { skills1[2] = skillBox.new silverSlash(); System.out.println ("Silver slash!"); break; } case 1:{ skills1[2] = skillBox.new ikari(); System.out.println ("Angry hammer!"); break; } case 2: { skills1[1] = skillBox.new goldSlash(); System.out.println ("Gold slash!"); break; } } } else if (players [1] .getName (). equals ("rich king")) { switch (list.get(1)) { case 0: { skills1[2] = skillBox.new frostMourne(); System.out.println ("Frostmorn!"); break; } case 1: { skills1[2] = skillBox.new huhai(); System.out.println ("Death and Corruption!"); break; } case 2: { skills1[1] = skillBox.new deathcoil(); System.out.println ("Spiral of Death!"); break; } } }

	Collections.shuffle(list);

System.out.println ("\ n Blue Adventurer," + players [2] .getName () + "The skill will be decided!");

	System.out.println();

System.out.println ("The first trick"); if (players [2] .getName (). equals ("paladin")) { switch (list.get(0)) { case 0: { skills2[1] = skillBox.new silverSlash(); System.out.println ("Silver slash!"); break; } case 1:{ skills2[1] = skillBox.new ikari(); System.out.println ("Angry hammer!"); break; } case 2: { skills1[1] = skillBox.new goldSlash(); System.out.println ("Gold slash!"); break; } } } else if (players [2] .getName (). equals ("rich king")) { switch (list.get(0)) { case 0: { skills2[1] = skillBox.new frostMourne(); System.out.println ("Frostmorn!"); break; } case 1: { skills2[1] = skillBox.new huhai(); System.out.println ("Death and Corruption!"); break; } case 2: { skills1[1] = skillBox.new deathcoil(); System.out.println ("Spiral of Death!"); break; } } } System.out.println ("The second trick"); if (players [2] .getName (). equals ("paladin")) { switch (list.get(1)) { case 0: { skills2[2] = skillBox.new silverSlash(); System.out.println ("Silver slash!"); break; } case 1:{ skills2[2] = skillBox.new ikari(); System.out.println ("Angry hammer!"); break; } case 2: { skills1[1] = skillBox.new goldSlash(); System.out.println ("Gold slash!"); break; } } } else if (players [2] .getName (). equals ("rich king")) { switch (list.get(1)) { case 0: { skills2[2] = skillBox.new frostMourne(); System.out.println ("Frostmorn!"); break; } case 1: { skills2[2] = skillBox.new huhai(); System.out.println ("Death and Corruption!"); break; } case 2: { skills1[1] = skillBox.new deathcoil(); System.out.println ("Spiral of Death!"); 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 Red Adventurer, activate the 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 ("Not enough MP!"); } } while (skills1[c1].getType() == 0 || (skills1[c1].getType() == 2 && players[1].getMp() < skills1[c1].getmCost()));

System.out.println ("\ n Blue Adventurer, activate the 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 ("Not enough 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 ("Angry Hammer"); setmDamage(100); setmCost(300); setType(2); } }

public class goldSlash extends Skill {
	goldSlash() {

setName ("gold slash"); setDamage(100); setType(1); } }

public class frostMourne extends Skill {
	frostMourne() {

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

public class deathcoil extends Skill {
	deathcoil() {

setName ("Spiral of Death"); setDamage(150); setType(1); } } }

Recommended Posts

I would like to know how to improve redundant code!
[Rails] I don't know how to use the model ...
I tested how to use Ruby's test / unit and rock-paper-scissors code.
[RSpec] How to write test code
I tried migrating Processing to VS Code
Don't know how to build task'credentials: edit'
How to call Swift 5.3 code from Objective-C
I tried to use Selenium like JQuery
How to count UTF-8 code points fast
How to color code console output in Eclipse
How to write an if statement to improve readability-java
The code I used to connect Rails 3 to PostgreSQL 10
How to write code that thinks object-oriented Ruby
How to implement a like feature in Rails
How to write test code with Basic authentication