반응형
package dayday12;
/**
* 탱크vsSipner (1)상위 클래스 : Unit 필드 : 이름, 생사여부, 체력 메소드 : 공격 메소드 (2)하위 클래스 : -탱크 :
* 체력 1000 공격 메소드 오버라이드 : 20%확률로 상대의 체력 30%감소시키고 나머지 확률로 체력 50깍임 -스나이퍼 : 체력 400
* 공격 메소드 오버라이드 : 15%확률로 헤드샷(상대가 즉사) 나머지 확률로 120데미지줌.
*
*
* 두 객체를 생성 - 랜덤하게 생성. - 두 객체중 한명이 죽을때 까지 공격반복 - 승자를 출력
*
* @author Administrator
*
*/
abstract class Unit {
String name;
int hp;
boolean isAllive;
abstract public void attack(Unit enemy);
}
class Tank extends Unit {
Tank() {
name = "탱크";
hp = 1000;
isAllive = true;
}
public void attack(Unit enemy) {
System.out.println("//////////ATTACK////////////");
System.out.println("적 " + enemy.name + "을 공격합니다.");
// 공격 메소드 오버라이드 : 20%확률로 상대의 체력 30%감소시키고 나머지 확률로 체력 50깍임
int i = (int) (Math.random() % 100 * 100 + 1);
if (i <= 20) {
System.out.println("//////////SKILL////////////");
System.out.println("***스킬발동! 적 30%체력 감소!");
enemy.hp -= (enemy.hp / 30);
} else {
System.out.println("적에게 50 데미지!");
enemy.hp -= 50;
if(enemy.hp <= 0) enemy.hp = 0;
}
System.out.println("적 hp : " + enemy.hp);
if (enemy.hp <= 0)
isAllive = false;
if (!isAllive) {
System.out.println("적이 사망하였습니다.");
}
}
}
class Sniper extends Unit {
Sniper() {
name = "스나이퍼";
hp = 400;
isAllive = true;
}
@Override
public void attack(Unit enemy) {
// TODO Auto-generated method stub
System.out.println("//////////ATTACK////////////");
System.out.println("적 " + enemy.name + "을 공격합니다.");
/*
* Math.random() > 0.5 ? ~ : ~ ;
*
*/
// 공격 메소드 오버라이드 : 15%확률로 헤드샷(상대가 즉사) , 나머지 확률로 120데미지줌.
int i = (int) (Math.random() % 100 * 100 + 1);
if (i <= 15) {
System.out.println("//////////SKILL////////////");
System.out.println("***스킬발동! <헤드샷>! 적은 즉사합니다!");
enemy.hp = 0;
} else {
System.out.println("적에게 120 데미지!");
enemy.hp -= 120;
if(enemy.hp <= 0) enemy.hp = 0;
}
System.out.println("적 hp : " + enemy.hp);
if (enemy.hp <= 0)
isAllive = false;
if (!isAllive) {
System.out.println("적이 사망하였습니다.");
}
}
}
public class Quiz01 {
static int checkAllive(Unit U1, Unit U2){
if ((U1.isAllive && U2.isAllive) == false) {
if (U1.isAllive) {
System.out.println("유저 1 : 탱크가 이겼습니다.");
return 1;
} else {
System.out.println("유저 2 : 탱크가 이겼습니다.");
return 1;
}
}
return 0;
}
public static void main(String[] args) throws InterruptedException {
/*
* 두 객체를 생성 - 랜덤하게 생성. - 두 객체중 한명이 죽을때 까지 공격반복 - 승자를 출력
*/
int round = 1;
int winner = 1;
int randomSelect1 =(int) ((Math.random() *100) %2 + 1); // 1,2
// 1:탱크,2:스나이퍼
int randomSelect2 =(int) ((Math.random() *100) %2 + 1); // 1,2
// 1:탱크,2:스나이퍼
if (randomSelect1 == 1 && randomSelect2 == 1) {
Unit U1 = new Tank();
Unit U2 = new Tank();
System.out.println("유저 1 : " + U1.name + "가 생성되었습니다.");
System.out.println("유저 2 : " + U2.name + "가 생성되었습니다.");
System.out.println("------------------------");
while (true) {
System.out.println("");
System.out.println((round++) + "번째 턴입니다.");
System.out.println("");
U1.attack(U2);
if(winner == checkAllive(U1, U2)){
return;
};
U2.attack(U1);
if(winner == checkAllive(U1, U2)){
return;
};
Thread.sleep(100);
}
} else if (randomSelect1 == 1 && randomSelect2 == 2) {
Unit U1 = new Tank();
Unit U2 = new Sniper();
System.out.println("유저 1 : " + U1.name + "가 생성되었습니다.");
System.out.println("유저 2 : " + U2.name + "가 생성되었습니다.");
System.out.println("------------------------");
while (true) {
System.out.println("");
System.out.println((round++) + "번째 턴입니다.");
System.out.println("");
U1.attack(U2);
if(winner == checkAllive(U1, U2)){
return;
};
U2.attack(U1);
if(winner == checkAllive(U1, U2)){
return;
};
Thread.sleep(100);
}
} else if (randomSelect1 == 2 && randomSelect2 == 1) {
Unit U1 = new Sniper();
Unit U2 = new Tank();
System.out.println("유저 1 : " + U1.name + "가 생성되었습니다.");
System.out.println("유저 2 : " + U2.name + "가 생성되었습니다.");
System.out.println("------------------------");
while (true) {
System.out.println("");
System.out.println((round++) + "번째 턴입니다.");
System.out.println("");
U1.attack(U2);
if(winner == checkAllive(U1, U2)){
return;
};
U2.attack(U1);
if(winner == checkAllive(U1, U2)){
return;
};
Thread.sleep(100);
}
} else if (randomSelect1 == 2 && randomSelect2 == 2) {
Unit U1 = new Sniper();
Unit U2 = new Sniper();
System.out.println("유저 1 : " + U1.name + "가 생성되었습니다.");
System.out.println("유저 2 : " + U2.name + "가 생성되었습니다.");
System.out.println("------------------------");
while (true) {
System.out.println("");
System.out.println((round++) + "번째 턴입니다.");
System.out.println("");
U1.attack(U2);
if(winner == checkAllive(U1, U2)){
return;
};
U2.attack(U1);
if(winner == checkAllive(U1, U2)){ Thread.sleep(100);
}
}
}
}
반응형
'IT > Programming' 카테고리의 다른 글
[JAVA] interface(인터페이스) 사용 예제 (0) | 2023.04.21 |
---|---|
[JAVA] polymorphism(다형성), up&down casting 사용 예제 (0) | 2023.04.21 |
[JAVA] instanceof 연산자 사용 예제 (0) | 2023.04.20 |
[JAVA] java.lang package class 사용 예제 (0) | 2023.04.20 |
[JAVA] Wrapper class 사용 예제 (0) | 2023.04.20 |