Programing_Java

JAVA#11 _ 게임 프로그램(Pokemon) 본문

JAVA_

JAVA#11 _ 게임 프로그램(Pokemon)

Joyfullyever 2025. 1. 6. 20:33

#1 게임 프로그램

여러 항목을 지정한 후, 사용자가 선택한 항목에 따라 원하는 내용 출력하기


public static void main(String[] args) {
		
Scanner sc = new Scanner(System.in);
Random r = new Random();
		
String [] poList = new String [3]; // 내 도감
int cnt = 0;
		
poList[0] = "꼬부기"; // "홍길동"
cnt++;
		
int [] poLvList = new int [3];
	poLvList[0] = 5; // "꼬부기"는 레벨 5로 시작
	poLvList[1] = 1;
	poLvList[2] = 1;
		
String [] pokemon = new String [5]; // 랜덤한 포켓몬 등장
	pokemon[0] = "야돈";
	pokemon[1] = "고라파덕";
	pokemon[2] = "메타몽";
	pokemon[3] = "두두";
	pokemon[4] = "네이티오";
		
String [] poName = new String [3]; // 포켓몬의 이름을 저장할 배열
		
		
while(true) {
	System.out.println("===== 메 뉴 ====="); //메뉴 출력
	System.out.println("1. 포켓몬 전체 출력");
	System.out.println("2. 포켓몬 잡기");
	System.out.println("3. 포켓몬 정보 출력");
	System.out.println("4. 포켓몬 시합");
	System.out.println("0. 프로그램 종료");
	System.out.println("================");
	System.out.print("메뉴 입력 >>> ");
	int action = sc.nextInt(); // 사용할 목록 선택
			
			
	if(action==0) { // 목록 중 0번 선택
		System.out.println("게임을 종료합니다..."); // 게임 종료
		break;
	}
	else if(action==1) { // 목록 중 1번 선택
	System.out.println("=== 내 포켓몬 목록 ==="); // 포켓몬 목록 출력
		for(int i = 0 ; i < cnt ; i++) {
			System.out.println((i+1) + ". " + poList[i]);
		}
	System.out.println("================");
	}
	else if(action==2) { // 목록 중 2번 선택
		int randNum = r.nextInt(5); // 0~4 // 랜덤으로 1마리 튀어나오게!
		System.out.println("야생의 " + pokemon[randNum] + "이(가) 나타났다!");
				
		if(poList[0] != null && poList[1] != null && poList[2] != null) { //만약 3칸이 모두 차있다면
			System.out.println("도감이 가득 찼습니다. 한마리를 버리고 새로 포획하시겠습니까?"); // 배열이 가득 찼다고 출력
			String recapans = sc.next();
			if(recapans.equals("NO")){ // 도감이 가득 찼는데 안잡을 경우 >> 포획 X
				System.out.println("포획을 포기합니다!");
				continue;
			}
			else {
				System.out.println("포기할 포켓몬을 선택해주세요..."); // 도감이 가득차도 잡을 경우
				int abandon = sc.nextInt(); // 포기할 포켓몬 선택
				poList[abandon-1] = null; //해당 포켓몬 칸 비우기
				poName[abandon-1] = null;
				cnt--; // 카운트 - 1
			}
		}	
		System.out.println("몬스터볼을 던질까? (YES, NO)"); // 잡을건지 
		System.out.print(">> ");
		String ans = sc.next(); // 대답 입력
				
			if(!ans.equals("YES")) { // 연산자로는 문자열 타입 계산 X // YES가 아닌경우 전부 도망
				System.out.println("도망쳤습니다...");
				continue;
			}
			System.out.println("!!!!!몬스터볼 던지기!!!!!");	
			int rNum = r.nextInt(100)+1; // 1~100 // 확률 설정			
			if(rNum <= 30) { // 실패
				System.out.println("ㅠㅠ... 실패했습니다...");
				continue;
			}
			System.out.println("신난다!!!" + pokemon[randNum] +"을(를) 잡았습니다!!!"); // 성공
			poList[cnt] = pokemon[randNum]; // 포획한 포켓몬을 도감 순서에 따라 저장
			poLvList[cnt++] = r.nextInt(4)+1; // 1~4 // 레벨 랜덤으로 부여
			System.out.println(pokemon[randNum] + " 포켓몬의 이름을 정하시겠습니까?"); // 이름 설정
			String nameans = sc.next(); // 대답 입력
				if(nameans.equals("YES")){ //만약 YES 라면
					System.out.println("포켓몬의 이름을 정해주세요!");
					poName[cnt-1] = sc.next(); 
					System.out.println(pokemon[randNum] + "의 이름은 " + poName[cnt-1] + " 입니다!");
				}
				else {
					System.out.println("포켓몬의 이름을 저장하지 못했습니다!");
				}
				continue;
			}
	else if(action==3) { // 목록 중 3번 선택
		int num;
		while(true) {
			System.out.print("번호 입력 >>> "); //유효성 검사
			num = sc.nextInt();
				if(1<=num && num<= cnt) {
					break;
				}
				System.out.println("없는 번호입니다!");
				}
			System.out.println(poList[num-1] + "은(는) Lv." + poLvList[num-1] + "입니다."); // 정보 출력
				if(poName[num-1]!=null) { // 만약 해당 자리의 포켓몬이 이름이 있다면 출력
					System.out.println(poList[num-1] + "의 이름은 " + poName[num-1] + "입니다.");
				}
		}	
	else if(action==4) { // 목록 중 4번 선택
		while(true) {
			System.out.println("시합에 내보낼 포켓몬을 선택해주세요!");
			int choice = sc.nextInt(); // 포켓몬 선택
				if(0 >= choice || choice >=4) { //유효성 검사
					System.out.println("내보낼 포켓몬을 다시 선택해주세요!");
					continue;
				}
			int winrate = r.nextInt(100)+1; // 승률 1~100
				if(winrate <= 50) { // 50 이하일 경우 패배
					System.out.println("패배했습니다. ㅠㅠ");
					break;
				}
				else { // 그 외 승리
					System.out.println("승리했습니다!!!!!");
					System.out.println(poList[choice-1] + "의 레벨이 1 상승합니다!");
					poLvList[choice-1] += 1; // 해당 포켓몬 레벨 + 1
					System.out.println(poList[choice-1] + "의 레벨은 현재 " + poLvList[choice-1] + "입니다!");
					break;
				}
				}
				}
				else {
					System.out.println("유효하지 않는 메뉴 번호 입니다!");
				}
	}	
}

'JAVA_' 카테고리의 다른 글

JAVA#13 _ 선택정렬과 함수화  (0) 2025.01.08
JAVA#12 _ 함수  (0) 2025.01.07
JAVA#9 _ 배열 속 최댓값 & 최솟값  (0) 2025.01.02
JAVA#8 _ 배열 문제  (0) 2025.01.02
JAVA#7 _ 변수와 배열  (0) 2025.01.02