본문 바로가기

미래(2015-2016)/자습

오늘의 과제


1215자바과제.docx


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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.util.Random;
import java.util.Scanner;
 
 
public class Yutnori
{
    Scanner sc = new Scanner(System.in);
    Random rd = new Random();
    
    int[] player = { 0000 };
    int[] com = { 0000 };
    
    String[] yut = { "빽도""도""개""걸""윷""모" };
    String[] who = { "플레이어""컴퓨터" };
    
    int round = 1;
    int turn = 0;        // 0일경우 플레이어, 1일경우 컴퓨터
    int select = 0;
    int yut_n = 0;
    
    int p_start = 0;
    int p_playing = 0;
    int p_end = 0;
    
    int c_start = 0;
    int c_playing = 0;
    int c_end = 0;
 
    void roof()
    {
        while (true)
        {
            while (true)
            {
                System.out.println("말을 선택하시고 윷을 던지세요.");
                
                if (turn == 0)
                {
                    select = sc.nextInt() - 1;
                }
                else if (turn == 1)
                {
                    select = rd.nextInt(4);
                }
                
                if (player[select] >= 60 || com[select] >= 60)
                    continue;
                else
                    break;
            }
                        
            while(true)
            {
                if (turn == 0)
                {
                    yut_n = rd.nextInt(6);
                    
                    if(player[select] == 0 && yut_n == -1)
                        player[select] = 0;
                    else
                        player[select] += yut_n == 0 ? -1 : yut_n;                
                }
                else if (turn == 1)
                {
                    yut_n = rd.nextInt(6);
                    
                    if (com[select] == 0 && yut_n == -1)
                        com[select] = 0;
                    else
                        com[select] += yut_n == 0 ? -1 : yut_n;
                }
                
                System.out.println("윷 던지기  :" + yut[yut_n]);
                
                if (yut_n == 5 || checkmate())
                {
                    System.out.println("윷을 한 번 더 던져주세요.");
                    continue;
                }
                else
                {
                    break;
                }
            }
            
            drawBorad();
            
            if(nextRound())
                break;
        }            
    }
    
    boolean checkmate()
    {
        boolean result = false;
        
        p_start = 0;
        p_end = 0;
        p_playing = 0;
        c_start = 0;
        c_end = 0;
        c_playing = 0;
        
        for(int i = 0; i < player.length; i++)
        {
            if(turn == 1 && player[i] != 0 && player[i] == com[select])
            {
                player[i] = 0;
                System.out.println((i + 1+"번째 플레이어 말이 잡혔습니다.");
                result = true;
            }
                    
            if (player[i] == 0)
            {
                p_start++;
            }
            else if (player[i] >= 60)
            {
                p_end++;
            }
            else
            {
                p_playing++;
            }
        }
        
        for (int i = 0; i < com.length; i++)
        {
            if (turn == 0 && com[i] != 0 && com[i] == player[select])
            {
                com[i] = 0;
                System.out.println((i + 1+ "번째 컴퓨터 말이 잡혔습니다.");
                result = true;
            }
            
            if (com[i] == 0)
            {
                c_start++;
            }
            else if (com[i] == 60)
            {
                c_end++;
            }
            else
            {
                c_playing++;
            }
        }
        
        return result;
    }
    
    boolean nextRound()
    {
        boolean result = false;
        
        if (p_end == 4)
        {
            System.out.println("플레이어의 승리입니다!");
            result = true;
        }
        else if (c_end == 4)
        {
            System.out.println("컴퓨터의 승리입니다!");
            result = true;
        }
        else
        {
            round++;
            turn = turn == 0 ? 1 : 0;
        }
        
        return result;
    }
    
    void drawLine()
    {
        for (int i = 0; i <= 60; i++)
        {
            if (i == 0)
                System.out.print("S");
            else if (i == 60)
                System.out.print("E");
            else if (i % 5 == 0)
                System.out.print("+");
            else
                System.out.print("-");
        }
        
        System.out.println();
    }
    
    void drawBorad()
    {
        System.out.println(round + "회전 " + who[turn] + " 차례");
        System.out.println("O 플레이어 : 시작 전 " + p_start + "개, 진행 중  " + p_playing + "개, 도착 후" + p_end + "개");
        System.out.println("X 컴퓨터 : 시작 전 " + c_start + "개, 진행 중  " + c_playing + "개, 도착 후" + c_end + "개");
        
        for (int i = 0; i <= 60; i++)
        {
            System.out.print(i % 10); 
        }
        System.out.println();
        
        drawLine();
        
        System.out.println("O");
        
        for (int i = 0; i < player.length; i++)
        {
            for (int j = 0; j < player[i]; j++)
            {
                System.out.print(" ");
            }
            System.out.println(i + 1);
        }
        
        drawLine();
        
        System.out.println("X");
        
        for (int i = 0; i < com.length; i++)
        {
            for (int j = 0; j < com[i]; j++)
            {
                System.out.print(" ");
            }
            System.out.println(i + 1);
        }
        
        System.out.println();
        System.out.println();
    }
        
    public static void main(String[] args) 
    {
        new Yutnori().roof();
    }
 
}
 
cs

Yutnori.java


'미래(2015-2016) > 자습' 카테고리의 다른 글

토끼 속도 줄이기  (0) 2016.01.13
JTabbedPane  (0) 2015.11.03
룩앤필 사용하기  (0) 2015.11.03
JToolBar  (0) 2015.11.02
JTable, DefaultTableModel  (0) 2015.11.02