TIL

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

oceanflow 2025. 1. 14. 23:57

클래스와 필드, 메서드에 대해서 더 익숙해지기 위해 만들게 되었다.

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) {

        Unit Marine = new Unit("Marine", 5, 20, 8, 0, "Terran Unit");
        Unit Zealot = new Unit("Zealot", 10, 30, 8, 0, "Protoss Unit");
        Unit Zergling = new Unit("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);

    }
}
package com.example;

import java.util.Random;

public class Unit {

    private String name;
    private String description;
    private String direction;
    private int speed;
    private int hp;
    private int damage;
    private 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, 0, -1};
        return directions[random.nextInt(3)];
    }

    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 = "왼쪽으로 이동했습니다.";
            else if (setPosition() == 0)
                direction = "이동하지 않았습니다.";

            System.out.println(this.getName() + "이(가) " + direction);
        }
    }

    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 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);
        }

    }
}

 

처음에는 setPsiton 부분을

public int setPosition() {
Random random = new Random();
int direction = random.nextInt(1 - (-1) + 1);
return direction;
}

이렇게 구현했었다. 그 이유는 구글링을 하였을 때 nextInt(Max - Min + Min)으로 난수의 범위를 지정할 수 있다고 해서 구현 했었다.

하지만

1 - ( -1 ) = 2

2 + 1 = 3

따라서 random.nextInt(3)이 되어 0, 1, 2 중 하나를 반환하게 된다.

 

원래의 의도는 -1, 0, 1 중 하나를 무작위로 선택하는 것이 목표였기에

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

 

이렇게 바꿔주게 되었다.

앞으로는 더 많이, 그리고 자세히 찾아보고 코드에 적용시켜야 되겠다는 교훈을 얻었다.

 

그리고 처음에는 필드를 타입별로 묶어서 선언하였는데 그렇게 하면 명시성이 좋지 않다는 이야기를 들어 수정하였다.