ETC/프로젝트

<JAVA>Text-based RPG Ver 0.1

지과쌤 2019. 5. 27.
반응형

영문->한글로 번역한 기본적인 베이스.

 

 

 

Game class

public final class Game {

    private final Player player = Player.newInstance();

    public void play() throws IOException {
        System.out.println(player + " " + player.getDescription());
        Dungeon.newInstance().startQuest(player);
    }

    public static void main(String[] args) throws IOException {
        Game game = new Game();
        game.play();
    }

}

Battle class

public final class Battle {

    public Battle(Player player, Monster monster) throws IOException {
        System.out.println("삐슝빠슝! " + monster + " 를(을) 마주쳤다! : " + monster.getDescription() + "\n");
        System.out.println( monster + " 과의 싸움을 시작합니다.  (" + player.getStatus() + " / "
                + monster.getStatus() + ")");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while (player.isAlive() && monster.isAlive()) {
            System.out.print("공격은 (a), 포션을 마시려면 (h) 을 눌러주세요. ");
            String action = in.readLine();
            if (action.equals("h")) {
                player.heal();
            } else {
                monster.defend(player);
            }
            if (monster.isAlive()) {
                player.defend(monster);
            }
        }
    }

}

Dungeon class

public final class Dungeon {

    private final Map<Integer, Map<Integer, Room>> map = new HashMap<Integer, Map<Integer, Room>>();
    private Room currentRoom;
    private int currentX = 0;
    private int currentY = 0;

    private Dungeon() {
    }

    private void putRoom(int x, int y, Room room) {
        if (!map.containsKey(x)) {
            map.put(x, new HashMap<Integer, Room>());
        }
        map.get(x).put(y, room);
    }

    private Room getRoom(int x, int y) {
        return map.get(x).get(y);
    }

    private boolean roomExists(int x, int y) {
        if (!map.containsKey(x)) {
            return false;
        }
        return map.get(x).containsKey(y);
    }

    private boolean isComplete() {
        return currentRoom.isBossRoom() && currentRoom.isComplete();
    }

    public void movePlayer(Player player) throws IOException {
        boolean northPossible = roomExists(currentX, currentY + 1);
        boolean southPossible = roomExists(currentX, currentY - 1);
        boolean eastPossible = roomExists(currentX + 1, currentY);
        boolean westPossible = roomExists(currentX - 1, currentY);
        System.out.println("");
        System.out.println("더이상 본 세계로 갈 방법은 없다!");
        System.out.println("");
        System.out.print("현재 갈 수 있는 방향은 다음과 같다.:");
        if (northPossible) {
            System.out.print(" 위 (n)");
        }
        if (eastPossible) {
            System.out.print(" 오른쪽 (e)");
        }
        if (southPossible) {
            System.out.print(" 아래 (s)");
        }
        if (westPossible) {
            System.out.print(" 왼쪽 (w)");
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String direction = in.readLine();
        if (direction.equals("n") && northPossible) {
            currentY++;
        } else if (direction.equals("s") && southPossible) {
            currentY--;
        } else if (direction.equals("e") && eastPossible) {
            currentX++;
        } else if (direction.equals("w") && westPossible) {
            currentX--;
        }
        currentRoom = getRoom(currentX, currentY);
        currentRoom.enter(player);
    }

    public void startQuest(Player player) throws IOException {
        while (player.isAlive() && !isComplete()) {
            movePlayer(player);
        }
        if (player.isAlive()) {
            System.out.println("축하합니다");
        } else {
            System.out.println("YOU DIED");
        }
    }

    public static Dungeon newInstance() {
        Dungeon dungeon = new Dungeon();
        dungeon.putRoom(0, 0, Room.newRegularInstance());
        dungeon.putRoom(-1, 1, Room.newRegularInstance());
        dungeon.putRoom(0, 1, Room.newRegularInstance());
        dungeon.putRoom(1, 1, Room.newRegularInstance());
        dungeon.putRoom(-1, 2, Room.newRegularInstance());
        dungeon.putRoom(1, 2, Room.newRegularInstance());
        dungeon.putRoom(-1, 3, Room.newRegularInstance());
        dungeon.putRoom(0, 3, Room.newRegularInstance());
        dungeon.putRoom(1, 3, Room.newRegularInstance());
        dungeon.putRoom(0, 4, Room.newBossInstance());
        dungeon.currentRoom = dungeon.getRoom(0, 0);
        return dungeon;
    }

}

Monster class

public final class Monster {

    private final String name;
    private final String description;
    private int hitPoints;
    private final int minDamage;
    private final int maxDamage;
    private final static Random random = new Random();
    private final static Set<Integer> monstersSeen = new HashSet<Integer>();
    private final static int NUM_MONSTERS = 3;

    public static Monster newRandomInstance() {
        if (monstersSeen.size() == NUM_MONSTERS) {
            monstersSeen.clear();
        }
        int i;
        do {
            i = random.nextInt(NUM_MONSTERS);
        } while (monstersSeen.contains(i));
        monstersSeen.add(i);

        if (i == 0) {
            return new Monster("하피", "날개달린 인간", 40, 8, 12);
        } else if (i == 1) {
            return new Monster("가고일", "온몸이 돌로 된 크리처", 26, 4, 6);
        } else {
            return new Monster("고블린", "난쟁이 고블린", 18, 1, 2);
        }
    }

    public static Monster newBossInstance() {
        return new Monster("드래곤", "화염을 내뿜는 드래곤", 60, 10, 20);
    }

    private Monster(String name, String description, int hitPoints, int minDamage, int maxDamage) {
        this.name = name;					//이름
        this.description = description;		//설명
        this.hitPoints = hitPoints;			//hp
        this.minDamage = minDamage;			//데미지 최솟값
        this.maxDamage = maxDamage;			//데미지 최대값

    }

    @Override
    public String toString() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public String getStatus() {
        return "몬스터 hp : " + hitPoints;
    }

    public int attack() {
        return random.nextInt(maxDamage - minDamage + 1) + minDamage;
    }

    public void defend(Player player) {
        int attackStrength = player.attack();
        hitPoints = (hitPoints > attackStrength) ? hitPoints - attackStrength : 0;
        System.out.printf("  %s 가 %s 에게 %d 의 데미지를 주었습니다! (%s)\n", player, name, attackStrength,
                getStatus());
        if (hitPoints == 0) {
            System.out.println("  " + player + " 가 마침내 " + name
                    + " 를 무찔렀습니다.");
        }
    }

    public boolean isAlive() {
        return hitPoints > 0;
    }

}

Player class

public final class Player {

    private final String name;
    private final String description;
    private final int maxHitPoints;
    private int hitPoints;
    private int numPotions;
    private final int minDamage;
    private final int maxDamage;
    private final Random random = new Random();

    private Player(String name, String description, int maxHitPoints, int minDamage, int maxDamage,
            int numPotions) {	// 이름, 설명, 최대hp, 최소공격력, 최대공격력
        this.name = name;
        this.description = description;
        this.maxHitPoints = maxHitPoints;
        this.minDamage = minDamage;
        this.maxDamage = maxDamage;
        this.numPotions = numPotions;
        this.hitPoints = maxHitPoints;
    }

    public int attack() {
        return random.nextInt(maxDamage - minDamage + 1) + minDamage;
    }

    public void defend(Monster monster) {
        int attackStrength = monster.attack();
        hitPoints = (hitPoints > attackStrength) ? hitPoints - attackStrength : 0; //히트포인트보다 어택이 더 크면 힛포-어택 또는 0
        System.out.printf("  " + name + "이 %d 의 데미지를 입었습니다. (%s)\n", attackStrength,
                getStatus());
        if (hitPoints == 0) {
            System.out.println("  " + name + " 이 패배하였습니다.");
        }
    }

    public void heal() {
        if (numPotions > 0) {
            hitPoints = Math.min(maxHitPoints, hitPoints + 20);
            System.out.printf("  %s 가 힐링 포션을 마셨습니다! (%s, %d 개 포션이 남았습니다.)\n", name,
                    getStatus(), --numPotions);
        } else {
            System.out.println("포션을 다 소비하였습니다.");
        }
    }

    public boolean isAlive() {
        return hitPoints > 0;
    }

    public String getStatus() {
        return "플레이어의 hp : " + hitPoints;
    }

    @Override
    public String toString() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public static Player newInstance() {
        return new Player("OOO",
                "이세계로 빠져버린 비운의 대학생", 40, 6, 20, 10);
    }
}

Room class

public final class Room {

    private final String description;
    private final Monster monster;
    private final Boolean isBossRoom;
    private final static Random random = new Random();
    private final static Set<Integer> roomsSeen = new HashSet<Integer>();
    private final static int NUM_ROOMS = 7;

    private Room(String description, Monster monster, Boolean isBossRoom) {
        this.description = description;
        this.monster = monster;
        this.isBossRoom = isBossRoom;
    }

    public static Room newRegularInstance() {
        if (roomsSeen.size() == NUM_ROOMS) {
            roomsSeen.clear();
        }
        int i;
        do {
            i = random.nextInt(NUM_ROOMS);
        } while (roomsSeen.contains(i));
        roomsSeen.add(i);

        String roomDescription = null;
        if (i == 0) {
            roomDescription = "더러운 짐승들이 돌아다니는 방";
        } else if (i == 1) {
            roomDescription = "식인독수리가 날아다니는 끝없는 산맥";
        } else if (i == 2) {
            roomDescription = "땀냄새인지 뭔지 여튼 냄새가 나는 늪";
        } else if (i == 3) {
            roomDescription = "사방이 용암으로 뒤덮인 화산";
        } else if (i == 4) {
            roomDescription =
                    "음침하고 나무들로 둘러싸인 숲";
        } else if (i == 5) {
            roomDescription =
                    "여기저기 사람의 뼈가 흩어져있는 유령선";
        } else if (i == 6) {
            roomDescription = "우리 아이는 약물을 먹이지 않겠다는 부모들로 가득찬 카페";
        } else {
        }
        return new Room(roomDescription, Monster.newRandomInstance(), false);
    }

    public static Room newBossInstance() {
        return new Room("냄새가 가득찬 동굴", Monster.newBossInstance(),
                true);
    }

    public boolean isBossRoom() {
        return isBossRoom;
    }

    public boolean isComplete() {
        return !monster.isAlive();
    }

    @Override
    public String toString() {
        return description;
    }

    public void enter(Player player) throws IOException {
        System.out.println("당신은 지금 " + description + " 에 있습니다.");
        if (monster.isAlive()) {
            new Battle(player, monster);
        }
    }

}

 

 

 

반응형

댓글

💲 추천 글