Java/Java 객체지향 핵심 - 2

ver 0.0.1 Starcraft - 11

치치ㅤ 2024. 4. 18. 10:57
🔔 학습 목표

여기 까지 배웠던 부분에 핵심 개념들을 활용해 봅시다.

 

 

package starcraft.ver01;

public class Marine {
	
	private String name;
	private int power;
	private int hp;
	
	public Marine(String name) {
		this.name = name;
		power = 4;
		hp = 70;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPower() {
		return power;
	}
	public void setPower(int power) {
		this.power = power;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
	
	// 마린이 질럿을 공격합니다.  attackZealot
	public void attackZealot(Zealot z) {
		System.out.println(this.name + " 이 " + z.getName() + " 을 공격합니다." );
		z.beAttacked(this.power);
	}
	
	
	// 마린이 저글링을 공격합니다.  attackZergling
	public void attackZergling(Zergling z) {
		System.out.println(this.name + " 이 " +z.getName() + " 을 공격합니다.");
		z.beAttacked(this.power);
	}
	
	
	// 자신이 공격을 당합니다.
	public void beAttacked(int power) {
		// 방어적 코드
		if(hp <= 0) {
			System.out.println("[" + this.name + "] 이미 사망하였습니다.");
			hp = 0;
			return;
		}
		hp -= power;
	}
	
	public void showInfo() {
		System.out.println("====상태창====");
		System.out.println("이름 : " + this.name);
		System.out.println("공격력 : " + this.power);
		System.out.println("생명력 : " + this.hp);
	}
	

} // end of class
package starcraft.ver01;

public class Zealot {

	private String name;
	private int power;
	private int hp;
	
	public Zealot(String name) {
		this.name = name;
		power = 5;
		hp = 80;
	}
	
	// getter
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public int getPower() {
		return power;
	}
	
	public int getHp() {
		return hp;
	}
	
	// 질럿이 저글링을 공격합니다.
	public void attackZergling(Zergling z) {
		System.out.println(this.name + " 이 " + z.getName() + " 을 공격합니다.");
		z.beAttacked(this.power);
	}
	
	// 질럿이 마린을 공격합니다.
	
	public void attackMarine(Marine m) {
		System.out.println(this.name + " 이 " + m.getName() + " 을 공격합니다.");
		m.beAttacked(this.power);
	}
	
	// 자신이 공격을 당합니다.
	public void beAttacked(int power) {
		// 방어적 코드
		if(hp <= 0) {
			System.out.println("[" + this.name + "] 이미 사망하였습니다.");
			hp = 0;
			return;
		}
		hp -= power;
	}
	
	public void showInfo() {
		System.out.println("====상태창====");
		System.out.println("이름 : " + this.name);
		System.out.println("공격력 : " + this.power);
		System.out.println("생명력 : " + this.hp);
	}
	
} // end of class
package starcraft.ver01;

public class Zergling {
	
	private String name;
	private int power;
	private int hp;
	
	public Zergling(String name) {
		this.name = name;
		power = 3;
		hp = 50;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getPower() {
		return power;
	}
	public void setPower(int power) {
		this.power = power;
	}
	public int getHp() {
		return hp;
	}
	public void setHp(int hp) {
		this.hp = hp;
	}
	
	// 저글링이 질럿을 공격합니다. attackZealot
	public void attackZealot(Zealot z) {
		System.out.println(this.name + " 이 " + z.getName() + " 을 공격합니다.");
		z.beAttacked(this.power);
	}
	
	// 저글링이 마린을 공격합니다.  attackMarine
	public void attackMarine(Marine m) {
		System.out.println(this.name + " 이 " + m.getName() + " 을 공격합니다.");
		m.beAttacked(this.power);
	}
	
	
	// 자신이 공격을 당합니다.
	public void beAttacked(int power) {
		// 방어적 코드
		if(hp <= 0) {
			System.out.println("[" + this.name + "] 이미 사망하였습니다.");
			hp = 0;
			return;
		}
		hp -= power;
	}
	
	public void showInfo() {
		System.out.println("====상태창====");
		System.out.println("이름 : " + this.name);
		System.out.println("공격력 : " + this.power);
		System.out.println("생명력 : " + this.hp);
	}
	
	// get, set - 단축키 Alt + Shift + s
	
} // end of class
package starcraft.ver01;

public class StarCraftTest1 {

	// 메인함수
	public static void main(String[] args) {

		// 질럿2
		Zealot zealot1 = new Zealot("질럿1");
		Zealot zealot2 = new Zealot("질럿2");
		// 마린2
		Marine marine1 = new Marine("마린1");
		Marine marine2 = new Marine("마린2");
		// 저글링2
		Zergling zergling1 = new Zergling("저글링1");
		Zergling zergling2 = new Zergling("저글링2");
		
		for(int i = 0; i < 15; i++) {
			zealot1.attackMarine(marine2);
		}
		marine2.showInfo();
		
	} // end of main

} // end of class

 

 

		// while <--,   do while
		// while --> 조건식을 확인하고 코드를 수행하는 녀석 
		// do while --> 무조건 한번은 수행을 하고 다시 조건을 확인하는 녀석 
		do {
			// 반복 수행 구문 
		} while(조건식);
		
		
		while (조건식) {
			// 반복 수행 구문 
			
		}

 

생성기에서 유닛을 생산해 보자.

package starcraft.ver01;

public class Barrack {

	private int barrackNumber;
	private int count;
	
	public Barrack(int number) {
		this.barrackNumber = number;
		count = 0;
	}
	
	public int getCount() {
		return this.count;
	}
	
	public int getBarrackNumber() {
		return barrackNumber;
	}
	
	public Marine createMarine(String name) {
		count++;
		return new Marine(name);
	}
}
package starcraft.ver01;

public class SpawningPool {

	private int spawningPoolNumber;
	private int count;
	
	public SpawningPool(int number) {
		this.spawningPoolNumber = number;
		count = 0;
	}
	
	public int getCount() {
		return this.count;
	}
	
	public int getSpawningPool() {
		return spawningPoolNumber;
	}
	
	public Zergling createZergling(String name) {
		count++;
		return new Zergling(name);
	}
}
package starcraft.ver01;

public class GateWay {

	private int gateWayNumber;
	private int count;

	// 생성자
	public GateWay(int gateWayNumber) {
		this.gateWayNumber = gateWayNumber;
		count = 0;
	}
	
	public int getCount() {
		return this.count;
	}
	
	public int getGateWayNumber() {
		return gateWayNumber;
	}

	// 기능 - 질럿을 생산하는 기능을 만들어 보세요.
	public Zealot createZealot(String name) {
		count++;
		return new Zealot(name);
	}

}

 

package starcraft.ver01;

import java.util.Scanner;

public class StarCraftTest2 {

	// 메인함수
	public static void main(String[] args) {

		final int ZEALOT = 1;
		final int MARINE = 2;
		final int ZERGLING = 3;
		final int GAME_END = 0;

		GateWay gateWay1 = new GateWay(1);
		GateWay gateWay2 = new GateWay(2);

		Zealot zealot1 = gateWay1.createZealot("질럿1");
		Zealot zealot2 = gateWay1.createZealot("질럿2");
		System.out.println(gateWay1.getGateWayNumber() + " : " + gateWay1.getCount());
		System.out.println(gateWay2.getGateWayNumber() + " : " + gateWay2.getCount());
		System.out.println("===============");
		
		Barrack barrack1 = new Barrack(1);
		Barrack barrack2 = new Barrack(2);
		
		Marine marine1 = barrack1.createMarine("마린1");
		Marine marine2 = barrack1.createMarine("마린2");
		
		SpawningPool spawningPool1 = new SpawningPool(1);
		SpawningPool spawningPool2 = new SpawningPool(2);
		
		Zergling zergling1 = spawningPool1.createZergling("저글링1");
		Zergling zergling2 = spawningPool1.createZergling("저글링2");


		Scanner sc = new Scanner(System.in);
		int unitChoice = -1;

		do {

			System.out.println("유닛을 선택하세요");
			System.out.println("1. 질럿\t 2.마린\t 3.저글링\t 0.게임종료");
			unitChoice = sc.nextInt();

			if (unitChoice == ZEALOT) {

			} else if (unitChoice == MARINE) {

			} else if (unitChoice == ZERGLING) {

			} else {
				System.out.println("프로그램을 종료합니다");
				unitChoice = 0;
			}

		} while (unitChoice != GAME_END);

	} // end of main

} // end of class