클래스 설계 자유 실습

전사와 매지션의 전투, 한턴에 서로 공격을 한번씩 주고 받고

각각 특수한 스킬 한가지를 가지고 있음

 

클래스 전사

package basic.ch09;

public class Warrior {

	String name;
	int healthPower;
	int attackPower;
	int deffencePower;
	int defaultHP;
	boolean checkBerserk;

	public Warrior(String name) {
		this.name = name;
		for (int i = 0; i < 10; i++) {
			healthPower += (int) (Math.random() * 1000);
			attackPower += (int) (Math.random() * 40);
			deffencePower += (int) (Math.random() * 50);
		}
		defaultHP = healthPower;
	}

	public void attacked(Magician magician) {
		// 마법사의 크리티컬 확률을 이용해 랜덤적용
		magician.critical();
		int realDamage = magician.attackPower - deffencePower;
		magician.offCritical();
		if (realDamage < 0) {
			realDamage = 0;
		}
		healthPower -= realDamage;
	}

	public void berserkMode() {
		// 이미 버서크 모드 상태 인지 확인 후 돌입
		if (!checkBerserk) {
			attackPower *= 2;
			healthPower *= 3;
			checkBerserk = true;
			System.out.println(name + "이(가) 버서크모드 돌입. 현재 체력 : " + healthPower);
		}

	}
	
	public boolean ifHP_under_quarter(){
		return healthPower <= (int)(defaultHP * 0.25);
	}


	public void showInfo() {
		System.out.println("----------상태창----------");
		System.out.println("전사의 이름 : " + name);
		System.out.println("체력 : " + healthPower);
		System.out.println("공격력 : " + attackPower);
		System.out.println("방어력 : " + deffencePower);
	}
}

클래스 매지션

package basic.ch09;

public class Magician {

	String name;
	int healthPower;
	int attackPower;
	int deffencePower;
	double critical;
	boolean check;
	
	public Magician(String name) {
		this.name = name;
		for (int i = 0; i < 10; i++) {
			healthPower += (int) (Math.random() * 400);
			attackPower += (int) (Math.random() * 75);
			deffencePower += (int) (Math.random() * 10);
		}
		critical = 0.3;
	}
	public void attacked(Warrior warrior) {
		int realDamage = warrior.attackPower - deffencePower;
		if (realDamage < 0){
			realDamage = 0;
		}
		healthPower -= realDamage;
	}
	
	public void critical() {
		if (Math.random() > critical) {
			attackPower *= 4;
			check = true;
			System.out.println(name + "이(가) 크리티컬 공격을 시전");
		}
	}
	
	public void offCritical() {
		if (check) {
			attackPower = attackPower - (int)(attackPower * 0.75);
			check = false;
		}
	}
	
	public void showInfo() {
		System.out.println("----------상태창----------");
		System.out.println("마법사의 이름 : " + name);
		System.out.println("체력 : " + healthPower);
		System.out.println("공격력 : " + attackPower);
		System.out.println("방어력 : " + deffencePower);
	}
}

각 진영에서 한명씩 출전 하여 최종 n번 승리한 쪽이 우승

package basic.ch09;

public class BattleMainTest {

	public static void main(String[] args) {
		// 한쪽이 n번 이길때 까지 반복
		int n = 10;
		int checkWar = 0;
		int checkMag = 0;
		while (true) {
			Warrior warrior1 = new Warrior("오크");
			Magician magician1 = new Magician("엘프");
			System.out.println(checkWar + checkMag + 1 + "번째 선수");
			warrior1.showInfo();
			magician1.showInfo();
			int checkRound = 0;
			while (true) {
				// 전사의 체력이 25% 이하라면 버서크 모드 돌입.
				if (warrior1.ifHP_under_quarter()) {
					warrior1.berserkMode();
				}
				warrior1.attacked(magician1);
				magician1.attacked(warrior1);
				System.out.println(++checkRound + "번째 턴 결과----------");
				System.out.println(warrior1.name + "의 체력 : " + warrior1.healthPower);
				System.out.println(magician1.name + "의 체력 : " + magician1.healthPower);
				if (warrior1.healthPower <= 0 && magician1.healthPower <= 0) {
					System.out.println("비겼습니다.");
					checkWar++;
					checkMag++;
					break;
				} else if (warrior1.healthPower <= 0) {
					System.out.println(magician1.name + "의 승리!");
					checkMag++;
					break;
				} else if (magician1.healthPower <= 0) {
					System.out.println(warrior1.name + "의 승리!");
					checkWar++;
					break;
				}
			}
			if (checkWar == n && checkMag == n) {
				System.out.println("최종 결과 " + n + " : " + n + " 비겼습니다.");
				break;
			} else if (checkWar == n) {
				System.out.print("최종 결과 " + n + " : " + checkMag);
				System.out.println(" " + warrior1.name + "가 이겼습니다.");
				break;
			} else if (checkMag == n) {
				System.out.print("최종 결과 " + n + " : " + checkWar);
				System.out.println(" " + magician1.name + "가 이겼습니다.");
				break;
			}
		}
	}
}

 

'Java > Java 객체 지향 핵심' 카테고리의 다른 글

this 3가지 사용 방법 - 10  (0) 2024.04.18
접근 제어 지시자 - 9  (0) 2024.04.17
객체지향 패러다임이란 - 8  (0) 2024.04.16
생성자(constructor) - 7  (0) 2024.04.16
RunTime Data Area - 6  (0) 2024.04.15