본문

아이템 Drop 확률 조작 로직 만들기 version 02(JAVA)

반응형

# 아이템 Drop 확률 조작 로직 만들기 version 02 -readData() 추가


이전 예제에 외부파일(data.txt)로부터 각 요소의 가중치를 얻어오는 String[] readData()를 추가하였다. 

이전의 '아이템 Drop 확률 예제'와는 달리 요소의 개수나 가중치가 달라졌을 때 코드의 변경이나 컴파일 없이
data.txt파일만을 변경하고 실행하면 된다는 점이 다르다.
 

이 예제를 실행하기 위해서는 data.txt 파일을 미리 만들어야한다.


source) RandomEx03.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package randomEx;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
 
import javax.imageio.stream.FileImageInputStream;
 
public class RandomEx03 {
    // Properties : 데이터를 파일로부터 읽고 쓰는 편리한 기능을 제공
    public static Properties prop = new Properties();
    public static String fileName = "data.txt";
    static final int REPETITION = 100;
 
    public static void main(String[] args) {
        
        String[] data = readData();
        
        HashMap map = new HashMap();
 
        for (int i = 0; i < REPETITION; i++) {
            String temp = getRandArr(data);
            if (map.containsKey(temp)) {
                Integer value = (Integer) map.get(temp);
                map.put(temp, new Integer(value.intValue()+1));
            } else {
                map.put(temp, new Integer(1));
            }
        }
 
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            Integer value = (Integer) map.get(key);
            int intValue = value.intValue();
            System.out.println(key + " : " + printGraph('#', intValue) + " " + intValue);
        }
    }
 
    private static String printGraph(char ch, int value) {
        char[] bar = new char[value];
 
        for (int i = 0; i < bar.length; i++) {
            bar[i] = ch;
        }
        return new String(bar);
    }
 
    private static String getRandArr(String[] arr) {
        return arr[getRand(arr.length - 1)]; // 배열에 저장된 값 중 하나를 반환한다.
    }
 
    private static int getRand(int n) {
        return getRand(0, n);
    }
 
    private static int getRand(int from, int to) {
        return (int) (Math.random() * (Math.abs(to - from) + 1)) + Math.min(from, to);
    }
 
    private static String[] readData() {
        try {
            prop.load(new FileInputStream(fileName));
        } catch (IOException e) {
            System.out.println("지정된 파일을 찾을 수 없습니다.");
            System.exit(0);
        }
 
        Enumeration e = prop.propertyNames();
 
        int sum = 0;
 
        System.out.println("[아이템별 가중치]");
        
        // 각 요소의 값을 더해서 요소들을 담을 배열의 크기를 결정한다.
        while (e.hasMoreElements()) {
            String element = (String) e.nextElement();
            int value = Integer.parseInt(prop.getProperty(element));
            sum += value;
            System.out.print(element + "=" + value + "  ");
        }
        System.out.println();
        System.out.println();
 
        
        System.out.println("[아이템 Drop 확률]");
        
        String[] data = new String[sum];
 
        // 생성한 배열에 각 요소의 값만큼, 요소를 채운다.
        // 예를 들어 A=1,B=2,C-3이라면
        // String[] data = {"A","B","B","C","C","C","C"}; 와 같이 되는 셈이다.
        int i = 0;
        e = prop.propertyNames(); // Enumeration 다시 얻어와야 한다.
        
        while (e.hasMoreElements()) {
            String element = (String) e.nextElement();
            int value = Integer.parseInt(prop.getProperty(element));
 
            for (int x = 0; x < value; x++, i++) {
                data[i] = element;
            }
        }
 
        return data;
    }
 
}
cs


result)

1
2
3
4
5
6
7
8
[아이템별 가중치]
A=1  D=4  C=3  B=2  
 
[아이템 Drop 확률]
A : ######### 9
B : ########################## 26
C : ######################## 24
D : ######################################### 41
cs


[data.txt]

A=1

B=1

C=2

D=4



- 출처 : JAVA의정석(남궁성 저)

반응형

공유

댓글