본문 바로가기

JAVA/배열 문제

Basball static 문제

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
113
    int r_num[] = null;    // int * r_num = null; == (0) 000000
        boolean clear = false;         
        
        // start
        
        
        // 1. init 
        
        // 2. random
        r_num = bRandom();
        
        ///////////////////////////// loop
        clear = loop(r_num);        
        //////////////////////////////
        
        // 6. result
        result(clear);        
        
    }
    
    static int[] bRandom() {    // r_num[4] r_num[3]
        int rnum[] = new int[3];
        boolean swit[] = new boolean[10];
        int w, r;
        
        for (int i = 0; i < swit.length; i++) {
            swit[i] = false;
        }
        w = 0;
        
        while(w < 3) {
            r = (int)(Math.random() * 10);
            if(swit[r] == false) {
                swit[r] = true;
                rnum[w] = r + 1;
                w++;
            }
        }
        
        for (int i = 0; i < rnum.length; i++) {
            System.out.println("rnum[" + i + "] = " + rnum[i]);
        }
        
        return rnum;
    }
    
    static void userInput(int unum[]) {
        Scanner sc = new Scanner(System.in);
        
        for (int i = 0; i < unum.length; i++) {
            System.out.print((i + 1+ "번째 수 = ");
            unum[i] = sc.nextInt();
        }        
    }
    
    static boolean loop(int r_num[]) {
        
        int u_num[] = new int[3];
        int strike, ball;
        int w;
        boolean c = false;
        
        w = 0;
        while(w < 10) {
            
            strike = 0;
            ball = 0;
        
            // 3. user input
            userInput(u_num);            
            
            // 4. finding
            // ball
            for (int i = 0; i < u_num.length; i++) {
                for (int j = 0; j < u_num.length; j++) {
                    if(u_num[i] == r_num[j] && i != j) {
                        ball++;
                    }
                }                
            }
            
            // strike
            for (int i = 0; i < u_num.length; i++) {
                if(u_num[i] == r_num[i]) {
                    strike++;
                }
            }
            
            if(strike > 2) {
                c = true;
                break;
            }
                    
            // 5. message
            System.out.println(strike + "스트라이크 " + ball + "볼입니다");
                        
            w++;
        }
        
        return c;
    }
    
    static void result(boolean clear) {
        if(clear){
            System.out.println("게임 클리어!!");
        }else{
            System.out.println("게임 오버~~");
        }
    }
    
    
 
}
cs