Number Baseball Game ver_01

2018. 5. 19. 20:28·오랜된 포스팅/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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import java.util.Scanner;
public class BaseballGame_test {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // < BaseBall Game Program >
            System.out.println("< BaseBall GameProgram !! >");
            
        // 변수선언
            String _hideNumS;    // <문자열> 랜덤 숫자(인자)값을 담는 배열의 크기를 받는 변수 
            int hideNumS;    // String -> int 랜덤숫자(인자) 배열의 크기를 담는 변수
            int hideNum[];    // 선택된 랜덤 수를 담고 있는 배열
            
            int r_num;    // 0 ~n 까지의 랜덤 수를 받는 변수
            
            boolean swit[];
            
            String _userNum;
            int chUserNum;
            int userNum[];
            
            int strike;
            int ball;
            
            int chance;
            int round;
        
        // Continue
            while(true) {
            
        // 변수 초기화
            chance=0;
            round=0;
            
        
        // Start
            System.out.println("\tGame Start~~!!");
        
        // 랜덤 숫자의 범위를 입력하시오(업데이트 예정 : 사용자로부터 입력값을 받을 수 있다.)
            r_num=10;    // 테스트
            swit = new boolean[r_num];    // 랜덤숫자의 중복을 제거해주는 변수
            
            for(int i=0;i<swit.length;i++) {    // 배열안의 값을 모두 false로 바꿔준다.
                swit[i]=false;
            }
        
        // Input Random Number Array Size From User
            System.out.println();//개행
            //hideNumS=3;hideNum=new int[hideNumS];    // 테스트
            while(true) {
                char temp;    // 비교를 위한 변수
                boolean ox=true;    // 반복문 제어를 위한 변수
            System.out.print("@ 게임에 사용될 맞춰야 할 수의 갯수 입력하시오(3~5) : ");
                _hideNumS=sc.next();
                _hideNumS=_hideNumS.trim();
                    for(int i=0;i<_hideNumS.length();i++) {
                        temp=_hideNumS.charAt(i);
                            if(_hideNumS.length()==1) {
                                if((int)temp<51||(int)temp>53) {
                                    ox=false;
                                    break;
                                }
                            }else {
                                ox=false;
                                break;
                            }
                    }
                    if(ox==false) {
                        System.out.println("잘 못된 입력 값입니다.");
                        continue;
                    }else {
                        break;
                    }
            }
            hideNumS=Integer.parseInt(_hideNumS);    // 랜덤 숫자의 배열크기를 담은 변수
            hideNum=new int[hideNumS];
            
        // Computer Random Number
            System.out.println();//개행
            while(true) {    
                int i=0;
                while(i<hideNum.length) {
                    int temp = (int)(Math.random()*r_num);    // r_num의 입력값 만큼의 동적할당
                        if(swit[temp]==false) {
                            swit[temp]=true;
                            hideNum[i]=temp+1;    // 여기서 1은 랜덤변수의 인덱스값은 0~n까지 이기때문에 0을 제외하기위해
                            i++;
                        }else {
                            continue;
                        }
                }
                break;
            }
            /*for(int i=0;i<hideNum.length;i++) {
                System.out.println("hideNum["+i+"] = "+hideNum[i]);
            }    // 출력 테스트*/
        
        // Chance System.
            System.out.println(); // 개행
            while(true) {
                System.out.print("@ 몇 번 안에 맞출지 기회의 횟수를 입력하시오 (1~20)회 : ");
                chance=sc.nextInt();
                if(chance<1||chance>20) {
                    System.out.println(); // 개행
                    System.out.println("사용할 수 없는 범위의 기회입니다.");
                    continue;
                }break;
            }
            while(round<chance) {    // 리플레이 부분
                strike=0;
                ball=0;
                System.out.println(); // 개행
                System.out.println((chance-round)+" 번 기회가 남았습니다! ");
                
        // User Random Number
            System.out.println();//개행
                for(int i=0;i<swit.length;i++) {
                    swit[i]=false;
                }
                    userNum=new int[hideNumS];    // 사용자의 입력을 받는 수의 배열
                        while(true) {
                            int i=0;
                            char ch=0;
                            System.out.println("@ 타자를 공격할 "+userNum.length+"개의 수를 입력하시오.");
                            while(i<userNum.length) {    // 숫자 외의 데이터가 입력되었는지 구별하는 로직
                                System.out.print((i+1)+" 번째 공격할 수 (1 ~ "+r_num+") : ");
                                _userNum=sc.next();
                                _userNum=_userNum.trim();
                                    for(int j=0;j<_userNum.length();j++) {
                                        ch=_userNum.charAt(j);
                                            if((int)ch<48||(int)ch>57) {
                                                System.out.println("사용 할 수 없는 문자가 입력되었습니다.");
                                                continue;
                                            }
                                    }
                                    chUserNum=Integer.parseInt(_userNum); // 문자를 제외 후 숫자만 담는다.
                                        if(chUserNum<1||chUserNum>r_num) { // 1 ~ r_num
                                            System.out.println(); // 개행
                                            System.out.println("사용 할 수 없는 범위의 수 입니다.");
                                            continue;
                                        }else { // 1~r_num사이의 수만 들어온다.
                                            if(swit[chUserNum-1]==false) {
                                                swit[chUserNum-1]=true;
                                                userNum[i]=chUserNum;
                                                i++;
                                            }else {
                                                System.out.println(); // 개행
                                                System.out.println("중복된 수를 입력 할 수 없습니다.");
                                                continue;
                                            }
                                        }
                                }
                            break;
                            }
                        /*
                        for(int i=0;i<userNum.length;i++) {
                            System.out.println("userNum["+i+"] = "+userNum[i]);
                        } // 출력테스트*/
        
        // Finding
            System.out.println();//개행
                // strike
                    for(int i=0;i<hideNum.length;i++) {
                        if(hideNum[i]==userNum[i]) {
                            strike++;
                        }
                    }
                // ball
                    for(int i=0;i<hideNum.length;i++) {
                        for(int j=0;j<userNum.length;j++) {
                            if(hideNum[i] == userNum[j] && i != j){
                                ball++;
                            }
                        }
                    }
        
        // Result
            System.out.println();//개행
                if(strike==hideNum.length) {
                    System.out.println(" 축하합니다!! "+(chance-round)+" 기회를 남기고 타자를 "+hideNum.length+"strike으로 이겼습니다!! ");
                    break;
                }else {
                    System.out.println(" << "+strike+" Strike \t"+ball+" Ball !!! >>");
                    round++;
                }
        } // >>>chance replay point<<<
        if(round==chance) {
            System.out.println();//개행
            System.out.println(" Game Over!! 모든 기회를 소진하였습니다. ");
        }
        
        // Continue Or Bye
            System.out.println(); // 개행
            System.out.print(" 다시 게임을 하고 싶으시다면 아무키나 입력하세요.\n게임을 그만하고 싶으시면 ( N )를 입력하세요.");
                String str = sc.next();
                    if(str=="n"||str=="N") {
                        System.out.println(" --- See You Next Time --- ");
                        break;
                    }else {
                        System.out.println(" >>>>>>>> 게임을 다시 시작합니다 <<<<<<<< ");
                        continue;
                    }
        }
        sc.close();
 
    }
 
}
Colored by Color Scripter
cs


저작자표시 비영리 변경금지 (새창열림)

'오랜된 포스팅 > Java' 카테고리의 다른 글

ArrayList를 이용한 동적배열(Dynamic Array)  (0) 2018.05.23
알고리즘을 이용한 동적배열(Dynamic Array)  (0) 2018.05.23
정렬(sorting) - Bubble sort  (0) 2018.05.22
피보나치수열(fibonacci numbers) 정리  (0) 2018.05.21
배열(Array)의 중복값 방지  (3) 2018.05.19
'오랜된 포스팅/Java' 카테고리의 다른 글
  • 알고리즘을 이용한 동적배열(Dynamic Array)
  • 정렬(sorting) - Bubble sort
  • 피보나치수열(fibonacci numbers) 정리
  • 배열(Array)의 중복값 방지
Toycode
Toycode
오늘도 훌륭했던 시간을 보내길 바라며
  • Toycode
    오늘도 훌륭했어
    Toycode
  • 전체
    오늘
    어제
    • 분류 전체보기 (48)
      • 블록체인 (0)
      • 기초 CS 파훼하기 (2)
      • IT 트렌드 (1)
      • 오랜된 포스팅 (45)
        • Java (25)
        • SQL Developer (14)
        • eGovFramework (5)
        • IOS (1)
  • 링크

    • Online Resume
  • hELLO· Designed By정상우.v4.10.0
Toycode
Number Baseball Game ver_01
상단으로

티스토리툴바