回答編集履歴

1

全文に修正

2015/10/16 12:36

投稿

退会済みユーザー
test CHANGED
@@ -1,4 +1,12 @@
1
- import文です.
1
+ package, import文含めた全文です.
2
+
3
+ packageは必要に応じて変えてください.
4
+
5
+ ```Java
6
+
7
+ package shiritori;
8
+
9
+
2
10
 
3
11
  import java.io.BufferedReader;
4
12
 
@@ -19,3 +27,183 @@
19
27
  import java.util.List;
20
28
 
21
29
  import java.util.Random;
30
+
31
+
32
+
33
+ public class Shiritori {
34
+
35
+
36
+
37
+ private static final BufferedReader READER = new BufferedReader(
38
+
39
+ new InputStreamReader(System.in));
40
+
41
+
42
+
43
+ private static final List<String> DICTIONARY = Collections
44
+
45
+ .unmodifiableList(Arrays.asList("りんご", "ごりら", "らっぱ","ぱんつ","つくし", "しりとり","しめじ"));
46
+
47
+
48
+
49
+ private static final List<String> USED = new ArrayList<>();
50
+
51
+
52
+
53
+ private static final Random RANDOM;
54
+
55
+ static {
56
+
57
+ try {
58
+
59
+ RANDOM = SecureRandom.getInstanceStrong();
60
+
61
+ } catch (NoSuchAlgorithmException e) {
62
+
63
+ throw new RuntimeException(e);
64
+
65
+ }
66
+
67
+ }
68
+
69
+
70
+
71
+ public static void main(String[] args) throws IOException {
72
+
73
+ String yours = DICTIONARY.get(RANDOM.nextInt(DICTIONARY.size()));
74
+
75
+ System.out.println(new StringBuilder().append("じゃあねー最初は[").append(yours)
76
+
77
+ .append(']').append("だよ"));
78
+
79
+
80
+
81
+ for (;;) {
82
+
83
+ final String mine = READER.readLine();
84
+
85
+
86
+
87
+ if (mine.isEmpty()) {
88
+
89
+ continue;
90
+
91
+ }
92
+
93
+
94
+
95
+ if(mine.equals("!")){
96
+
97
+ System.out.println("へへん.勝ったー!");
98
+
99
+ return;
100
+
101
+ }
102
+
103
+
104
+
105
+ if (yours.charAt(yours.length() - 1) != mine.charAt(0)) {
106
+
107
+ System.out.println(new StringBuilder().append("その単語[")
108
+
109
+ .append(yours.charAt(yours.length() - 1))
110
+
111
+ .append("]で始まってないじゃん!"));
112
+
113
+ continue;
114
+
115
+ }
116
+
117
+
118
+
119
+ if (!DICTIONARY.contains(mine)) {
120
+
121
+ System.out.println("その単語知らない!知ってそうな単語を入力して!");
122
+
123
+ continue;
124
+
125
+ }
126
+
127
+
128
+
129
+ if(USED.contains(mine)){
130
+
131
+ System.out.println("その単語使ったよ!忘れたとはいわせないよ!");
132
+
133
+ continue;
134
+
135
+ }
136
+
137
+
138
+
139
+ USED.add(mine);
140
+
141
+
142
+
143
+ List<String> result = search(mine.charAt(mine.length() - 1));
144
+
145
+
146
+
147
+ if (result.isEmpty()) {
148
+
149
+ System.out.println("もうわかんない.負けたー");
150
+
151
+ return;
152
+
153
+ }
154
+
155
+
156
+
157
+ yours = result.get(RANDOM.nextInt(result.size()));
158
+
159
+
160
+
161
+ if(USED.contains(yours)){
162
+
163
+ System.out.println(new StringBuilder().append('[').append(yours).append(']')
164
+
165
+ .append("は言ったっけー.わかんない.まけたー"));
166
+
167
+ return;
168
+
169
+ }
170
+
171
+
172
+
173
+ System.out.println(new StringBuilder().append("じゃあ[")
174
+
175
+ .append(yours).append(']').append('!'));
176
+
177
+
178
+
179
+ }
180
+
181
+
182
+
183
+ }
184
+
185
+
186
+
187
+ public static final List<String> search(char firstChar) {
188
+
189
+ List<String> result = new ArrayList<>();
190
+
191
+ for (String word : DICTIONARY) {
192
+
193
+ if (word.charAt(0) == firstChar) {
194
+
195
+ result.add(word);
196
+
197
+ }
198
+
199
+ }
200
+
201
+ return result;
202
+
203
+ }
204
+
205
+ }
206
+
207
+
208
+
209
+ ```