본문 바로가기

Projects/Toy Project

자바로 텍스트 게임 만들기

반응형
package tutorials;

import java.util.Random;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// System objects
		Scanner in = new Scanner(System.in);
		Random rand = new Random();
		
		// Game variables
		String[] enemies = {"Skeleton", "Zombie", "Warrior", "Assassin"};
		int maxEnemyHealth = 75;
		int enemyAttackDamage = 25;
		
		// Player variables
		int health = 100;
		int attackDamage = 50;
		int numHealthPotions = 3;
		int healthPotionHealAmount = 30;
		int healthPotionDropChance = 60; // Percentage
		
		boolean running = true; // while loop에서 사용. 트루일 경우 계속 지속
		
		System.out.println("Welcome to the Dungeon!");
		
		// while loop에 대해서 GAME이라는 정의를 내린 것이다.
		GAME:
		while(running) {
			// 구분선
			System.out.println("-------------------------------------------");
			
			// 적의 hp는 다음 숫자 안에서 (위에서 정한 맥스) 랜덤하게 결정된다.
			int enemyHealth = rand.nextInt(maxEnemyHealth);
			// 적이 나오는 것인데, 위에서 정한 enemies 목록 중에서 인덱스 숫자를 이용해 nextInt로 랜덤하게 나온다
			String enemy = enemies[rand.nextInt(enemies.length)];
			// \t는 탭버튼이다.
			// # Skeleton has appeared! # 예시
			System.out.println("\t# " + enemy + " has appeared! #\n");
			
			// 적이 살아있다면
			while(enemyHealth > 0) {
				System.out.println("\tYOur HP: " + health);
				System.out.println("\t"+enemy+"'s HP: "+enemyHealth);
				System.out.println("\n\tWhat would you like to do?");
				System.out.println("\t1. Attack");
				System.out.println("\t2. Drink health potion");
				System.out.println("\t3. Run!");
				
				String input = in.nextLine();
				if(input.equals("1")) {
					int damageDealt = rand.nextInt(attackDamage);
					int damageTaken = rand.nextInt(enemyAttackDamage);
					
					// -= 연산자는 a = a - b;와 같은 의미
					enemyHealth -= damageDealt;
					health -= damageTaken;
					
					System.out.println("\t> You strike the " + enemy + " for " + damageDealt + " damage.");
					System.out.println("\t> You receive " + damageTaken + " in retaliation!");
					
					if(health < 1) {
						System.err.println("\t> You have taken too much damage, you are too weak to go on");
						break;
					}
				}
				
				
				else if(input.equals("2")) {
					if(numHealthPotions > 0) {
						health += healthPotionHealAmount;
						numHealthPotions--;
						System.out.println("\t> You drink a health potion, healing yourself for " + healthPotionHealAmount + "."
											+ "\n\t> You now have " + health + " HP. "
											+ "\n\t> You have " + numHealthPotions + " health potions left.\n");
						
					
					}
					else {
						System.out.println("\t> You have no health potions left! Defeat the enemy for a chance to get a potion.");
					}
					
				}
				else if(input.equals("3")) {
					System.out.println("\t You run away from the " + enemy + "!");
					// repeat the iteration
					// 새로운 이너미가 생겨난다.
					// 아까 레이블링한 덕분에 가능하다.
					continue GAME;
					
				}
				else {
					System.out.println("\tInvalid command!");
				}
			
				if(health < 1) {
					System.out.println("You limp out of the dungeon, weak from battle.");
					break;
					
					
				}
				
			
				
			}
			System.out.println("-------------------------------------------");
			System.out.println(" # " + enemy + " was defeated! #");
			System.out.println(" # You have " + health + " HP left. #");
			
			if(rand.nextInt(100) < healthPotionDropChance) {
				numHealthPotions++;
				System.out.println(" # The " + enemy + " dropped a health potion! # ");
				System.out.println(" # You now have " + numHealthPotions + " health potion(s). #");
				System.out.println();
				}
			System.out.println("-------------------------------------------");
			System.out.println("What would you like to do now?");
			System.out.println("1. Continue fighting");
			System.out.println("2. Exit dungeon");
			
			String input = in.nextLine();
			
			// ! 느낌표는 아니면 이라는 뜻이다.
			while((!input.equals("1") && !input.equals("2"))) {
				System.out.println("Invalid command!");
				// 잘못된 값을 넣으면 이렇게 다시 적도록 한다.
				input = in.nextLine();
				
				}
			if(input.equals("1")) {
				System.out.println("You continue on your adventure!");
				}
			else if(input.equals("2")) {
				System.out.println("You exit the dungeon, successful from your adventure!");
				break;
		} // while(소) 끝

	} // GAME 끝
		System.out.println("#######################");
		System.out.println("# THANKS FOR PLAYING! #");
		System.out.println("#######################");
}
}
반응형