JAVA

[자바] 공격 프로그램 구현해보기

yujin0517 2021. 7. 11. 10:24

Warrior, Archer, Wizard 등의 공격수를 클래스로 생성하여 서로를 공격하는 프로그램 만들기.

Warrior, Archer, Wizard 클래스는 공통된 멤버 변수를 가지고 있으며, 하나의 키워드로 묶을 수 있다.

Hero라는 클래스를 3개의 공격수 클래스의 부모 클래스로 선언한다. (3개의 공격수 클래스는 자식 클래스)

 

- Warrior 공격수comboAttack 능력을 가지고 있음.

package ex01;

public class Warrior extends Hero{
	
	public Warrior(String name, int hp) {
		super(name, hp);
	}
	public void comboAttack() {
		System.out.println("Warrior의 공격입니다.");
	}

}

 

- Archer 공격수fireArrow 능력을 가지고 있음.

package ex01;

public class Archer extends Hero{
	
	public Archer(String name, int hp) {
		super(name, hp);
	}
	public void fireArrow() {
		System.out.println("Archer의 공격입니다. ");
	}

}

 

- Wizard 공격수freeaing 능력을 가지고 있음.

package ex01;

public class Wizard extends Hero{
	
	public Wizard(String name, int hp) {
		super(name, hp);
	}
	public void freeaing() {
		System.out.println("Wizard의 공격입니다.");
	}
	
}

 

- Hero 클래스에는 attack 메소드와 showInfo 메소드 그리고 setter, getter메소드가 있음. 

* attack 메소드는 상대에게 공격을 받았을 때, 데미지 만큼 hp을 감소하여 상태를 출력함.

* showInfo 메소드는 공격수의 이름과 hp를 출력함. 

package ex01;

public class Hero {
	
	String name;
	int hp;
	int power;
	
	public void setPower(int power) {
		this.power = power;
	}
	public int getPower() {
		return this.power;
	}
	
	public Hero(String name, int hp) {
		this.name = name;
		this.hp = hp;
	}
	
	public void attack(Hero heroAttack) {
		heroAttack.hp -= power;
		if(heroAttack.hp <= 50 && heroAttack.hp > 20) {
			System.out.println(heroAttack.hp + "");
			System.out.println("위험합니다.");
			
		}
		else if(heroAttack.hp <= 20 && heroAttack.hp > 0) {
			System.out.println(heroAttack.hp + "");
			System.out.println("hp를 보충해야 합니다.");
			
		}
		else if(heroAttack.hp <= 0) {
			//heroAttack.hp = 0;
			System.out.println(heroAttack.hp + "");
			System.out.println("사망했습니다.");
			
		}
		else {
			System.out.println(heroAttack.hp + "");
			System.out.println("공격을 당했습니다.");
			
		}
	}
	public void showInfo() {
		System.out.println("name : " + name);
		System.out.println("hp : " + hp);
	}
}

 

- MainTest1

package ex01;

public class MainTest1 {

	public static void main(String[] args) {
		//배열로 변수를 선언해 사용자가 이름과 hp를 입력하도록 함.
		Hero[] heros = new Hero[3];
		heros[0] = new Warrior("Warrior", 70);
		heros[1] = new Archer("Archer", 65);
		heros[2] = new Wizard("Wizard", 60);

		for (int i = 0; i < heros.length; i++) {

			if (heros[i] instanceof Warrior) { 
				Warrior temp = (Warrior) heros[i]; // 다운캐스팅
				temp.comboAttack();
				heros[0].setPower(20);
				heros[0].attack(heros[1]);  
				System.out.println("Archer 공격당함.");
				System.out.println("===================");
			} else if (heros[i] instanceof Archer) {
				Archer temp = (Archer) heros[i];
				temp.fireArrow();
				heros[1].setPower(21);
				heros[1].attack(heros[2]);  
				System.out.println("Wizard 공격당함.");
				System.out.println("===================");
			} else {
				Wizard temp = (Wizard) heros[i];
				temp.freeaing();
				heros[2].setPower(22);
				heros[2].attack(heros[0]); 
				System.out.println("Warrior 공격당함.");
				System.out.println("===================");
			}
		}
		// 공격수의 정보 출력
		heros[0].showInfo(); 
		System.out.println("===================");
		heros[1].showInfo(); 
		System.out.println("===================");
		heros[2].showInfo(); 
	}

}

MainTest1 콘솔창

 

-MainTest2 : Scanner클래스를 사용하여 공격할 상대를 사용자가 정하고, hp가 랜덤으로 감소하도록 구현함. 

package ex01;

import java.util.Scanner;

public class MainTest2 {

	public static void main(String[] args) {

		System.out.println("Warrior : 1, Archer : 2, Wizard : 3");
		System.out.println("공격할 상대 입력 =>");
		Scanner sc = new Scanner(System.in);
		System.out.print("Warrior의 공격 상대 : ");
		int hero1 = sc.nextInt();
		System.out.print("Archer의 공격 상대 : ");
		int hero2 = sc.nextInt();
		System.out.print("Wizard의 공격 상대 : ");
		int hero3 = sc.nextInt();
		sc.close();
		System.out.println();

		Hero[] heros = new Hero[3];
		heros[0] = new Warrior("Warrior", 50);
		heros[1] = new Archer("Archer", 50);
		heros[2] = new Wizard("Wizard", 50);

		for (int i = 0; i < heros.length; i++) {
			if (heros[i] instanceof Warrior) {  // instanceof 사용
				Warrior war = (Warrior) heros[0];
				war.comboAttack();
				heros[0].setPower((int) (Math.random() * 10) + 11);
				if (hero1 == 2) {
					heros[0].attack(heros[1]);
					System.out.println("Archer 공격당함.");
					System.out.println("===================");
				} else if (hero1 == 3) {
					heros[0].attack(heros[2]);
					System.out.println("Wizard 공격당함.");
					System.out.println("===================");
				}
			} else if (heros[i] instanceof Archer) {   // instanceof 사용
				Archer arc = (Archer) heros[1];
				arc.fireArrow();
				heros[1].setPower((int) (Math.random() * 10) + 6);
				if (hero2 == 1) {
					heros[1].attack(heros[0]);
					System.out.println("Warrior 공격당함.");
					System.out.println("===================");
				} else if (hero2 == 3) {
					heros[1].attack(heros[2]);
					System.out.println("Wizard 공격당함.");
					System.out.println("===================");
				}
			} else {
				Wizard wiz = (Wizard) heros[2];
				wiz.freeaing();
				heros[2].setPower((int) (Math.random() * 10) + 21);
				if (hero3 == 1) {
					heros[1].attack(heros[0]);
					System.out.println("Warrior 공격당함.");
					System.out.println("===================");
				} else if (hero3 == 2) {
					heros[2].attack(heros[1]);
					System.out.println("Archer 공격당함.");
					System.out.println("===================");
				}
			}
			heros[0].showInfo();
			System.out.println("===================");
			heros[1].showInfo();
			System.out.println("===================");
			heros[2].showInfo();
			System.out.println();
		}
	}
}

공격할 상대 정하기
Warrior이 Archer를 공격함
Archer이 Warrior을 공격함.

 

Wizard가 Warrior을 공격함.

 

 

 

감사합니다!

2021.07.06 

'JAVA' 카테고리의 다른 글

[자바] 싱글톤 패턴(Singleton pattern)  (0) 2021.07.14
예외 처리  (0) 2021.07.13
[자바] 상속  (0) 2021.07.09
[자바] 업캐스팅, 다운캐스팅 / instanceof 연산자  (0) 2021.07.08
this 레퍼런스  (0) 2021.07.08