Code deduplication

Asked 2 years ago, Updated 2 years ago, 284 views

abstract class AirUnit extends Unit {
    private int power;

    public AirUnit(String name, int tribal, Point location, BaseStat baseStat, int power, double speed) {
        super(name, tribal, location, baseStat, speed);
        setPower(power);
    }
    // an aerial attack
    public void airAttack(AirUnit airTarget) {
        System.out.println(this.getName() + "Attack!");
        System.out.println(airTarget.getName() + " damaged " + power + "!");
        // I've lost the enemy's defense in my attack
        int remainDamage = power - airTarget.getBaseStat().getDef();
        if(remainDamage > 0) { 
            airTarget.getBaseStat().setDef(0); /
            remainDamage = airTarget.getHp() - remainDamage; 
            airTarget.setHp(remainDamage);
            if(remainDamage < 0) {
                airTarget.setHp(0);
            }
        } } else { 
            airTarget.getBaseStat().setDef(- remainDamage);
        }
        System.out.println(airTarget.getName() + HP /def: " + airTarget.getHp() + " / " + airTarget.getBaseStat());
    }
    //ground attack
    public void groundAttack(GroundUnit groundTarget) {
        System.out.println(this.getName() + "Attack!");
        System.out.println(groundTarget.getName() + " damaged " + power + "!");
        If(groundTarget.getTribal() == PROTOSS) { // If the target's race is Protoss
            int remainDamage = power - ((ShieldStat)groundTarget.getBaseStat()).getShield();
            If (remainDamage > 0) { // If my attack is stronger,
                ((ShieldStat)groundTarget.getBaseStat()).setShield(0); 
                remainDamage = remainDamage - groundTarget.getBaseStat().getDef(); 
                groundTarget.setHp(groundTarget.getHp() - (remainDamage >= 0 ? remainDamage : 0));
                System.out.println (HP: " + groundTarget.getHp()) in groundTarget.getName() + ");
            } } else { 
                ((ShieldStat)groundTarget.getBaseStat()).setShield(- remainDamage);
                System.out.println(groundTarget.getName() + " HP / Shield: " + groundTarget.getHp() + " /" + ((ShieldStat)groundTarget.getBaseStat()).getShield());
            }
        } else { // any other race other than protos
            System.out.println(this.getName() + "Attack!");
            System.out.println(groundTarget.getName() + " damaged " + power + "!");
            int remainDamage = power - groundTarget.getBaseStat().getDef();
            if(remainDamage > 0) { 
                groundTarget.getBaseStat().setDef(0); // enemy's defense 0
                remainDamage = groundTarget.getHp() - remainDamage; 
                groundTarget.setHp(remainDamage);
                if(remainDamage < 0) {
                    groundTarget.setHp(0);
                }
            } } else { 
                groundTarget.getBaseStat().setDef(- remainDamage);
            }
            System.out.println(groundTarget.getName() + " HP / def: " + groundTarget.getHp() + " / " + groundTarget.getBaseStat());
        }
    }

The air attack method has the same code as the else part of the ground attack (else other than Protoss), so would it be bad for code duplication??! I wonder how to get rid of duplicates like that!

method duplicate

2022-11-05 00:00

1 Answers

The simplest way to remove duplicate code is to subtract it as a method or function.

For the code you wrote, you can use the upcasting method to Unit that is the parent of GroundUnit and AirUnit. However, Unit must have getName(), getBaseStat(), getHp(), and setHp() methods.

//ground attack
public void groundAttack(GroundUnit groundTarget) {
    // Omitted
    If(groundTarget.getTribal() == PROTOSS) { // If the target's race is Protoss
        // Omitted
    } else { // any other race other than protos
        executeMe(groundTarget);
    }
}

private void executeMe(Unit target) {
    System.out.println(this.getName() + "Attack!");
    System.out.println(target.getName() + " damaged " + power + "!");
    int remainDamage = power - target.getBaseStat().getDef();
    if(remainDamage > 0) { 
        target.getBaseStat().setDef(0); // enemy's defense 0
        remainDamage = target.getHp() - remainDamage; 
        target.setHp(remainDamage);
        if(remainDamage < 0) {
            target.setHp(0);
        }
    } } else { 
        target.getBaseStat().setDef(- remainDamage);
    }
    system.out.println(target.getName() + " HP /def: " + target.getHp() + " / " + target.getBaseStat().getDef());
}


2022-11-05 00:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.