💡 학습 목표
여기 까지 배웠던 부분에 핵심 개념들을 활용해 봅시다.
package basic.starcraft.ver01;
public class Marin {
private String name;
private int power;
private int hp;
public Marin(String name) {
this.name = name;
power = 6;
hp = 40;
}
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("이름 : " + this.name);
System.out.println("공격력 : " + this.power);
System.out.println("체력 : " + this.hp);
}
}
package basic.starcraft.ver01;
public class Zealot {
private String name;
private int power;
private int hp;
private int shiled;
public Zealot(String name) {
this.name = name;
power = 16;
hp = 100;
shiled = 60;
}
// 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 attackMarin(Marin m) {
System.out.println(this.name + " 이 " + m.getName() + " 을 공격합니다.");
m.beAttacked(this.power);
}
// 자신이 공격을 당합니다.
public void beAttacked(int power) {
// 방어적 코드
if(shiled <= power) {
hp = hp - (power - shiled);
return;
}
else if(hp <= 0) {
System.out.println(" [" + this.name + " ]이미 사망하였습니다.");
hp = 0;
return;
}
shiled -= power;
shiled++;
}
public void showInfo() {
System.out.println("이름 : " + this.name);
System.out.println("공격력 : " + this.power);
System.out.println("체력 : " + this.hp);
System.out.println("쉴드 : " + this.shiled);
}
}
package basic.starcraft.ver01;
public class Zergling {
private String name;
private int power;
private int hp;
public Zergling(String name) {
this.name = name;
power = 5;
hp = 35;
}
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;
}
// 저글링이 마린을 공격합니다. attackMarin
public void attackMarin(Marin m) {
System.out.println(this.name + " 이 " + m.getName() + " 을 공격합니다.");
m.beAttacked(this.power);
}
// 저글링이 질럿을 공격합니다. attackZealot
public void attackZealot(Zealot z) {
System.out.println(this.name + " 이 " + z.getName() + " 을 공격합니다.");
z.beAttacked(this.power);
}
// get, set
// 단축키 alt +shift +s
// 자신이 공격을 당합니다.
public void beAttacked(int power) {
// 방어적 코드
if(hp <= 0) {
System.out.println(" [" + this.name + " ]이미 사망하였습니다.");
hp = 0;
return;
}
hp -= power;
}
public void showInfo() {
System.out.println("이름 : " + this.name);
System.out.println("공격력 : " + this.power);
System.out.println("체력 : " + this.hp);
}
}
package basic.starcraft.ver01;
public class GateWay {
private int gateWayNumber;
private int count;
// 기능 - 질럿 생산하는 기능을 만들어보자
public GateWay(int gateWayNumber) {
this.gateWayNumber = gateWayNumber;
count = 0;
}
// 기능 - 질럿 생산하는 기능을 만들어 보세요
public Zealot createZealot(String name) {
count++;
return new Zealot(name);
}
public int getCount() {
return count;
}
public int getNumber() {
return gateWayNumber;
}
}
package basic.starcraft.ver01;
public class Hatchery {
private int egg;
private int number;
public Hatchery (int number) {
egg = 3;
this.number = number;
}
public Zergling createZergling (String name) {
egg--;
return new Zergling(name);
}
public int getEgg() {
return this.egg;
}
public int unit() {
return this.number;
}
}
package basic.starcraft.ver01;
import java.util.Scanner;
public class StarCraftTest1 {
public static void main(String[] args) {
final int ZEALOT = 1;
final int MARIN = 2;
final int ZERGLING = 3;
final int GAME_END = 0;
Hatchery hatChery1 = new Hatchery(1);
Marin marine1= new Marin("마린1");
Zergling zergling1 = hatChery1.createZergling("저글링1");
Zergling zergling2 = hatChery1.createZergling("저글링2");
System.out.println(zergling1.getName()+","+zergling2.getName()+" 생산중");
System.out.println("남아있는 에그 갯수 : " + hatChery1.getEgg());
GateWay gateway = new GateWay(1);
GateWay gateway2 = new GateWay(2);
Zealot zealot1 = gateway.createZealot("질럿1");
Zealot zealot2 = gateway.createZealot("질럿2");
System.out.println(gateway.getNumber() + " : " +gateway.getCount());
System.out.println(gateway2.getNumber() + " : " +gateway2.getCount());
System.out.println("-------------------------");
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) {
System.out.println("질럿이 생산되었습니다.");
} else if(unitChoice == MARIN) {
} else if(unitChoice == ZERGLING) {
} else {
System.out.println("프로그램을 종료 합니다.");
unitChoice = GAME_END;
}
} while(unitChoice != GAME_END);
// while <--, do while
// while --> 조건식을 확인하고 코드를 수행하는 녀석
// do while --> 무조건 한번은 수행을 하고 다시 조건을 확인하는 녀석
//do {
// 반복 수행 구문
//while(조건식);
} // end of main
} // end of class
package basic.starcraft.ver01;
import java.util.Iterator;
public class StarCraftTest2 {
public static void main(String[] args) {
Zealot zeratul = new Zealot("제라툴");
Marin jimRaynor = new Marin("짐 레이너");
Marin tychus = new Marin("타이커스");
Zergling Kerrigan = new Zergling("사라 케리건");
Zergling devouringOne = new Zergling("디바우링 원");
Zergling zagara = new Zergling("자가라");
Zergling abathur = new Zergling("아바투르");
for(int i = 0; i<10; i++) {
jimRaynor.attackZealot(zeratul);
}
zeratul.showInfo();
}
}
'Java' 카테고리의 다른 글
static 메소드(함수) (0) | 2024.04.19 |
---|---|
static 변수 (0) | 2024.04.19 |
this. 의 3가지 사용 방 (0) | 2024.04.19 |
접근 제어 지시자 (0) | 2024.04.17 |
객체지향 패러다임이란 (0) | 2024.04.16 |