Java로 강화 시뮬레이터

Kashinar·2022년 10월 8일

저번에 파이썬으로 만든 강화 시뮬레이터를 자바로 구현해 보았습니다.

import java.util.Random;
public class ReinforceSimulator {
	
	//아이템 확률
	public static int Level1_item() {
		Random rand = new Random();
		int Chance = rand.nextInt(100);
		return Chance;
	}
	
	public static int Level2_item() {
		Random rand = new Random();
		int Chance = rand.nextInt(100);
		return Chance;
	}
	
	public static int Level3_item() {
		Random rand = new Random();
		int Chance = rand.nextInt(100);
		return Chance;
	}
	
	public static int Level4_item() {
		Random rand = new Random();
		int Chance = rand.nextInt(100);
		return Chance;
	}
	
	public static String Level5_item() {
		return "You Win";
	}
	
	public static void main(String[] args) {
		boolean i = true;
		while (i == true) {
		if(Level1_item()+1 >= 30) {
			System.out.println("Success you reach lv2");
			if(Level2_item()+1 >= 40) {
				System.out.println("Success you reach lv3");
				if(Level3_item()+1 >= 50) {
					System.out.println("Success you reach lv4");
					if(Level4_item()+1 >= 60) {
						System.out.println("Success you reach lv5");
						System.out.println("You have lv5 Item");
						System.out.println(Level5_item());
						i=false;
						} else if (Level4_item()+1 <60) {
							System.out.println("Fail Try again");
							i=false;
						}
					} else if (Level3_item()+1 < 50) {
						System.out.println("Fail Try again");
						i= false;
					}
				} else if (Level2_item()+1 < 40) {
					System.out.println("Fail Try again");
					i = false;
				}
			} else if (Level1_item()+1 < 30) {
				System.out.println("Fail Try again");
				i = false;
				
			}
		}
	}
}

근데, 이상하게 레벨2와 3으로 가는 시점에서 자꾸 뒤로 되돌아가는 현상이 있는데..
그 문제가 뭔지 아직 발견을 못했습니다..

이렇게 되더라구요

일단 더 코드를 찬찬히 보면서 어떤 실수를 한 걸까 되짚어 보아야겠네요.

그리고 이번에 만들면서 저번에 파이썬 코드를 다시 살펴봤는데,
실제로 사용되지 않는 코드가 작성되있고, 그게 사용되고 있다는 것을 알았습니다.
코드가 돌아가는 원리를 제대로 이해를 못하고 있었다라는 증거겠지요.

수정해서 해보니 줄도 더 줄어들고 간략해진것 같습니다.

from random import randint

#Item chance
Level1_item = randint(1,100)
Level2_item = randint(1,100)
Level3_item = randint(1,100)
Level4_item = randint(1,100)

#Game Start
playing = True

#Reinforce Logic
while playing:
    if Level1_item >= 30:
        print("Success you reach Lv.2")
        if Level2_item >= 40:
            print("Success you reach Lv.3")
            if Level3_item >= 50:
                print("Success you reach Lv.4")
                if Level4_item >= 60:
                    print("Success you reach Lv.5")
                    print("Item Level_5")
                    print("You Win")
                    playing = False
                elif Level4_item < 60:
                    print("Fail Try again")
                    playing = False
            elif Level3_item <50:
                print("Fail Try again")
                playing = False
        elif Level2_item <40:
            print("Fail Try again")
            playing = False
    elif Level1_item < 30:
        print("Fail Try again")
        playing = False

다음엔 유저가 강화하고 싶을 때 강화할 수 있도록 만드는 것도 재밌어보이네요
기능을 더 추가해보고 싶습니다.

아직 많이 부족하지만, 이렇게 뭔가 동작하는 코드를 보니까 재밌네요.

profile
Java , Python

0개의 댓글