GameMain_Method
package game_L;
import java.util.Random;
import java.util.Scanner;
public class GameMain_Method {
// 0. 메뉴 출력
public static void menuPrint() {
System.out.println("\n=== ABCDE_RPG ===");
System.out.println("1. 캐릭터 생성");
System.out.println("2. 캐릭터 정보");
System.out.println("3. 전직하기");
System.out.println("4. 기본 공격");
System.out.println("5. 스킬 사용");
System.out.println("0. 종료");
System.out.println("================");
}
// 1. 캐릭터 생성
public static void createCharacter(Novice novice) {
System.out.println("캐릭터를 생성합니다");
Scanner scan = new Scanner(System.in);
System.out.print("사용하실 아이디를 입력해주세요 :");
String name = scan.next();
novice.setName(name); // paremeter에서 받아온 Novice 인스턴스의 이름 설정
System.out.println("스탯을 부여합니다.");
Random r = new Random(); // 랜덤값 설정해주는 Random 인스턴스 설정
int str = 0, dex = 0, intel = 0; // 각 스탯 초기화
while (true) { // 사용자가 좋다고 할 때까지 반복
str = r.nextInt(0, 11);
dex = r.nextInt(0, 11);
intel = r.nextInt(0, 11); // 0~10 사이
if (str + dex + intel >= 15) { // 3개의 합이 15 이상이면
novice.setStrength(str); // 우선 스탯값을 다 넣기.
novice.setDexterity(dex);
novice.setIntelligence(intel);
System.out.println(String.format("부여된 스탯정보: 힘[%d], 민첩[%d], 지능[%d]", str, dex, intel));
System.out.print("스탯을 다시 받으시겠습니까? (y/n) :"); // y/n 받기
String tmp = scan.next();
if (tmp.equals("y"))
continue; // 다시 받고 싶어하면 continue 로 다시 반복
else
break; // 사용자가 만족하면 while 문 탈출
}
}
System.out.println(novice); // Novice Class 에 있는 toString() 덕분에 바로 넣어도 알아서 print 됨.
System.out.println("현재 정보로 저장합니다.");
}
// 2. 캐릭터 정보
public static void showCharacterInfo(Novice novice) {
if (novice.getName() == null) { // 이름이 없다는 뜻은 아직 생성하지 않았다는 뜻
System.out.println("캐릭터를 생성하세요");
} else if(novice instanceof Thief) { // 전직했을 경우, 클래스 이름은 Thief 처럼 영어로 뜨는데, 실제 출력은 한글로 떠야 하므로 따로 print
System.out.println(String.format("[ID: %s(도적), stat: 힘(%d), 민첩(%d), 지능(%d)]", novice.getName(), novice.getStrength(), novice.getDexterity(), novice.getIntelligence()));
} else if(novice instanceof Magician) {
System.out.println(String.format("[ID: %s(마법사), stat: 힘(%d), 민첩(%d), 지능(%d)]", novice.getName(), novice.getStrength(), novice.getDexterity(), novice.getIntelligence()));
} else if(novice instanceof Knight) {
System.out.println(String.format("[ID: %s(기사), stat: 힘(%d), 민첩(%d), 지능(%d)]", novice.getName(), novice.getStrength(), novice.getDexterity(), novice.getIntelligence()));
} else {
System.out.println(novice);
}
}
// 3. 전직하기
public static Novice changeJob(Novice novice) { // Novice 인스턴스의 Class 가 아예 바뀌어야 하기 때문에 Novice 인스턴스를 하나 설정하고 return
System.out.println("=== 직업 종류 ===");
System.out.println("1. 기사");
System.out.println("2. 도적");
System.out.println("3. 마법사");
System.out.print("전직할 직업의 번호를 선택하세요 :");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
Novice tmp; // 임시 Novice 설정
switch (num) {
case 1: // 임시 Novice 인스턴스에 Knight 인스턴스를 설정
tmp = new Knight(novice.getName(), novice.getStrength(), novice.getDexterity(), novice.getIntelligence());
break;
case 2:
tmp = new Thief(novice.getName(), novice.getStrength(), novice.getDexterity(), novice.getIntelligence());
break;
case 3:
tmp = new Magician(novice.getName(), novice.getStrength(), novice.getDexterity(), novice.getIntelligence());
break;
default:
System.out.println("잘못된 숫자입니다.");
tmp = novice; // 숫자를 잘못 누른 경우 이미 있는 Novice 인스턴스 그대로 return
}
return tmp;
}
// 4. 공격하기
public static void attack(Novice novice) {
if (novice.getName() == null) { // 이름이 없다는 뜻은 아직 캐릭터를 만들지 않았다는 뜻
System.out.println("캐릭터를 생성하세요");
} else {
novice.attack(); // Class 에 맞춰서 알아서 자동으로 실행됨.
}
}
// 5. 스킬사용
public static void skill(Novice novice) {
if (novice instanceof Thief) {
((Thief) novice).skill();
} else if (novice instanceof Magician) {
((Magician) novice).skill();
} else if (novice instanceof Knight) {
((Knight) novice).skill();
} else {
System.out.println("스킬을 사용할 수 없습니다.");
}
}
}
Seoul45_agme (메인)
package game_L;
import java.util.Scanner;
public class Seoul45_agme {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Novice novice = new Novice();
while (true) {
GameMain_Method.menuPrint(); //클래스를 통한 접근 static메서드라 가능
System.out.print("번호를 선택하세요 : ");
int num = scan.nextInt();
switch (num) {
case 1: // 캐릭터 생성
GameMain_Method.createCharacter(novice);
break;
case 2: // 캐릭터 정보
GameMain_Method.showCharacterInfo(novice);
break;
case 3: // 전직하기
novice = GameMain_Method.changeJob(novice);
break;
case 4: // 공격하기
GameMain_Method.attack(novice);
break;
case 5: // 스킬사용
GameMain_Method.skill(novice);
break;
case 0: // 종료
System.exit(0);
break;
default:
break;
}
}
}
}