TIL

[스타크래프트 게임 출력] 2일차

oceanflow 2025. 1. 15. 20:34

Unit을 부모 클래스, Terran,Zerg,Protoss를 자식클래스로 만들어 Unit을 상속받은 형태로 코드를 구현하였다.

  • Terran은 Unit의 attack, move와 같음
  • Zerg는 move 시 +1,-1이 아닌, +2,-2 로 진행이 됨
  • Protoss는 Zerg 객체를 Attack 시 damage가 x2가 됨
  • move, attack은 추상화시켜 Override 하였다.
package com.example;

import java.util.Random;

public abstract class Unit {

    private String name;
    private String description;
    protected String direction;
    private int speed;
    private int hp;
    private int damage;
    protected int position;

    public Unit(String _name, int _speed, int _hp, int _damage, int _position, String _description) {
        this.name = _name;
        this.speed = _speed;
        this.hp = _hp;
        this.damage = _damage;
        this.position = _position;
        this.description = _description;
    }

    public String getName() {
        return this.name;
    }

    public int getHp() {
        return this.hp;
    }

    public int getDamage() {
        return this.damage;
    }

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

    public int setPosition() {
        Random random = new Random();
        int[] directions  = {1, -1};
        return directions[random.nextInt(2)];
    }

    public abstract void move();

    public void currentPosition() {
        System.out.println(this.getName() + "의 위치는 현재 " + position + "입니다.");
    }

    public void printUnitInfo() {
        System.out.println(this.name + "\t 체력 : " + this.hp + "\t 공격력 : " + this.damage + "\t 설명 : " + this.description);
    }

    public abstract void attack(Unit target);
}
package com.example;

import java.util.Random;

public class Terran extends Unit{

    public Terran(String _name, int _speed, int _hp, int _damage, int _position, String _description) {
        super(_name, _speed, _hp, _damage, _position, _description);
    }

    @Override
    public void move() {
        Random random = new Random();
        int randomInt = random.nextInt(20 + 1);

        for ( int i = 0; i < randomInt; i++) {
            position += setPosition();
            if (setPosition() == -1)
                direction = "오른쪽";
            else if (setPosition() == 1)
                direction = "왼쪽";

            System.out.println(this.getName() + "이(가) " + direction + "으로 이동했습니다.");
        }
    }

    @Override
    public void attack(Unit target) {

        System.out.println(this.getName() + "이가" + target.getName() + "을 공격합니다!");

        if (target.getHp() <= 0) {
            System.out.println("공격 불가능한 대상입니다.");
            return;
        }

        int newHp = target.getHp() - this.getDamage();
        target.setHp(newHp);

        if (newHp <= 0) {
            System.out.println(target.getName() + "이(가) 사망했습니다.");
        } else {
            System.out.println(target.getName() + "의 남은 체력 " + newHp);
        }

    }
}
package com.example;

import java.util.Random;

public class Zerg extends Unit {

    public Zerg(String _name, int _speed, int _hp, int _damage, int _position, String _description) {
        super(_name, _speed, _hp, _damage, _position, _description);
    }

    @Override
    public int setPosition() {
        Random random = new Random();
        int[] directions  = {2, -2};
        return directions[random.nextInt(2)];
    }

    @Override
    public void move() {
        Random random = new Random();
        int randomInt = random.nextInt(20 + 1);

        for ( int i = 0; i < randomInt; i++) {
            position += setPosition();
            if (setPosition() == -2)
                direction = "오른쪽";
            else if (setPosition() == 2)
                direction = "왼쪽";

            System.out.println(this.getName() + "이(가) " + direction + "으로 이동했습니다.");
        }
    }

    @Override
    public void attack(Unit target) {

        System.out.println(this.getName() + "이가" + target.getName() + "을 공격합니다!");

        if (target.getHp() <= 0) {
            System.out.println("공격 불가능한 대상입니다.");
            return;
        }

        int newHp = target.getHp() - this.getDamage();
        target.setHp(newHp);

        if (newHp <= 0) {
            System.out.println(target.getName() + "이(가) 사망했습니다.");
        } else {
            System.out.println(target.getName() + "의 남은 체력 " + newHp);
        }

    }

}
package com.example;

import java.util.Random;

public class Protoss extends Unit {

    public Protoss(String _name, int _speed, int _hp, int _damage, int _position, String _description) {
        super(_name, _speed, _hp, _damage, _position, _description);
    }

    @Override
    public void move() {
        Random random = new Random();
        int randomInt = random.nextInt(20 + 1);

        for ( int i = 0; i < randomInt; i++) {
            position += setPosition();
            if (setPosition() == -1)
                direction = "오른쪽";
            else if (setPosition() == 1)
                direction = "왼쪽";

            System.out.println(this.getName() + "이(가) " + direction + "으로 이동했습니다.");
        }
    }

    @Override
    public void attack(Unit target) {

        System.out.println(this.getName() + "이가" + target.getName() + "을 공격합니다!");

        if (target.getHp() <= 0) {
            System.out.println("공격 불가능한 대상입니다.");
            return;
        }
        int damage = this.getDamage();
        if (target.getName().equals("Zealot")) {
            damage *= 2;
        }

        int newHp = target.getHp() - damage;
        target.setHp(newHp);


        if (newHp <= 0) {
            System.out.println(target.getName() + "이(가) 사망했습니다.");
        } else {
            System.out.println(target.getName() + "의 남은 체력 " + newHp);
        }

    }

}
package com.example;

import java.util.Random;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {

        Terran Marine = new Terran("Marine", 5, 20, 8, 0, "Terran Unit");
        Protoss Zealot = new Protoss("Zealot", 10, 30, 8, 0, "Protoss Unit");
        Zerg Zergling = new Zerg("Zergling", 15, 15, 5, 0, "Zerg Unit");

        Marine.printUnitInfo();
        Marine.currentPosition();
        Marine.move();
        Marine.currentPosition();

        Marine.attack(Zealot);
        Zealot.attack(Zergling);
        Marine.attack(Zergling);
        Zealot.attack(Zergling);
        Zealot.attack(Marine);

    }
}

 

추상화를 하기 위해서는 메서드 뿐만 아니라 클래스에도 abstract를 적용시켜야 한다는 것을 알았다.

또한 오버라이드를 사용하며 오버라이드를 어떻게 적용시킬지에 관해 잘 알 수 있었다.

그리고 부모 클래스의 생성자를 호출하기 위해서는 super를 사용해야 한다는 것을 배웠다.