본문 바로가기

JAVA/배열 문제

배열(Baseball)만들기(주석달기)


문제


Baesball 게임을 만들어보자


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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
Scanner sc = new Scanner(System.in);
        
            Baseball
 
            10
 
            9    random number    n1 != n2 != n3
            
            user number             u1 != u2 != u3
            -> ball
            -> strike
            -> strike ball
            3
 
            2
            2    X
            2    
            1
            3
 
        
 
        // start
        int r_num[] = new int[3];
        int u_num[] = new int[3];
        
        boolean clear;
        boolean swit[] = new boolean[10]; 
 
        int r, w;
 
        // init
        clear = false;
        w = 0;
 
        for(int i = 0;i < swit.length; i++){
            swit[i] = false;    // 00000 00000
        }
  // while 방법1
        // random(1 ~ 10) -> 3개     같은 수를 check
        /*
        while(true){
            r_num[0] = (int)(Math.random() * 10) + 1;
            r_num[1] = (int)(Math.random() * 10) + 1;
            r_num[2] = (int)(Math.random() * 10) + 1;            
            if(r_num[0] != r_num[1] 
                && r_num[0] != r_num[2] 
                && r_num[1] != r_num[2] ){
                break;
            }
        }*/
     // while 방법2
        while(w < 3){    
            r = (int)(Math.random() * 10);    // 0 ~ 9
            if(swit[r] == false){
                swit[r] = true;        // 00011 00000
                r_num[w] = r + 1;            // 1 ~ 10
                w++;
            }
        }
 
        for(int i = 0;i < r_num.length; i++){
            System.out.println("r_num[" + i + "] = " + r_num[i]);
        }
 
        ////////////////////////////// loop 10개
 
        int strike, ball;
        boolean check;
 
        int loop = 0;
 
        while(loop < 10){
 
            strike = ball = 0;
 
            while(true){
                w = 0;
                check = false;
                // user input -> 3개     같은 수를 check
                while(w < 3){
                    System.out.print((w + 1+ "번째 수 = ");
                    u_num[w] = sc.nextInt();
                    w++;
                }
 
                // 같은 입력 숫자의 첵크
                out:for(int i = 0;i < 3; i++){
                    for(int j = 0;j < 3; j++){
                        if(u_num[i] == u_num[j] && i != j){
                            check = true;
                            break out;
                        }
                    }
                }
 
                if(check == false){
                    break;
                }
                System.out.println("같은 숫자가 있습니다 다시 입력해 주십시오");
            }
 
            // finding
            // ball
            for(int i = 0;i < u_num.length; i++){
                for(int j = 0;j < r_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++;
                }
            }
 
            // message -> ? strike ? ball
            if(strike > 2){
                clear = true;
                break;
            }
 
            System.out.println(strike + "스트라이크 " + ball + "볼입니다");
 
            loop++;
        }
 
        ////////////////////////////// loop
 
        // result
        if(clear == true){
            System.out.println("Game Clear!!");
        }
        else{
            System.out.println("Game Over~~");
        }
 
        // end play again?
    }
}
 
cs