블가다의 JAVA 실습) 클래스를 이용한 RPG전투 시스템 구현

2017. 5. 15. 23:03프로그래밍(이전)/JAVA

클래스, 객체선언을 이용한
공격력, 방어력, 체력 등으로 가상 전투를 해주는
간단한 콘솔 프로그램.

객체 선언

객체 정보 사용자에게 입력받음

여러개의 객체 생성

2가지를 골라 가상전투

코드는 아래와 같습니다.100 이상 가면 엄청난 메모리와 저사양 컴퓨터의 렉을 유발하기 때문에... 그냥 끊어버렸습니다. 애초에 100번 이상 때리는 RPG형 전투라니 아르피엘인가.

import java.io.*; import java.util.*; class ch{ int atk,def,hp; public String name; } public class test1 { public static void main(String[] args)throws IOException{ Scanner sr = new Scanner(System.in); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n1,s1; String p1; ch cha1 = new ch(); ch cha2 = new ch(); System.out.println("가상 전투 계산기"); System.out.printf("첫번째 캐릭터의 정보를 입력하십시오.\n이름 :\n"); cha1.name = in.readLine(); System.out.println("체력 : "); cha1.hp = sr.nextInt(); System.out.println("공격력 : "); cha1.atk = sr.nextInt(); System.out.println("방어력 : "); cha1.def = sr.nextInt(); System.out.printf("두번째 캐릭터의 정보를 입력하십시오.\n이름 :\n"); cha2.name = in.readLine(); System.out.println("체력 : "); cha2.hp = sr.nextInt(); System.out.println("공격력 : "); cha2.atk = sr.nextInt(); System.out.println("방어력 : "); cha2.def = sr.nextInt(); System.out.printf("두 캐릭터 성능을 비교하시겟습니까?\n(비교 이후 바로 전투를 실행합니다.)\nyes/no : "); p1 = in.readLine(); switch(p1){ case "yes": System.out.println("이 름\t" + cha1.name + " : " + cha2.name); System.out.println("체 력\t" + cha1.hp + " : " + cha2.hp); System.out.println("공격력\t" + cha1.atk + " : " + cha2.atk); System.out.println("방어력\t" + cha1.def + " : " + cha2.def); case "no": for(int i=1;;i++){ cha1.hp = cha1.hp - (cha2.atk * 100 / (cha1.def+100)); cha2.hp = cha2.hp - (cha1.atk * 100 / (cha2.def+100)); if(cha1.hp<=0){ System.out.printf(cha2.name+"(이)가 "+i+"번의 전투 교환 이후 승리\n"); break; }else if(cha2.hp<=0){ System.out.printf(cha1.name+"(이)가 "+i+"번의 전투 교환 이후 승리\n"); break; }else if(cha1.hp<=0 && cha2.hp<=0){ System.out.printf(i+"번의 전투 교환 이후 무승부. 러브샷\n"); break; }else if(i>100){ System.out.printf("100번의 전투 교환 이후 무승부. 승부 안남\n"); break; } } } } }