teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

全文に修正

2015/10/16 12:36

投稿

退会済みユーザー
answer CHANGED
@@ -1,4 +1,8 @@
1
- import文です.
1
+ package, import文含めた全文です.
2
+ packageは必要に応じて変えてください.
3
+ ```Java
4
+ package shiritori;
5
+
2
6
  import java.io.BufferedReader;
3
7
  import java.io.IOException;
4
8
  import java.io.InputStreamReader;
@@ -8,4 +12,94 @@
8
12
  import java.util.Arrays;
9
13
  import java.util.Collections;
10
14
  import java.util.List;
11
- import java.util.Random;
15
+ import java.util.Random;
16
+
17
+ public class Shiritori {
18
+
19
+ private static final BufferedReader READER = new BufferedReader(
20
+ new InputStreamReader(System.in));
21
+
22
+ private static final List<String> DICTIONARY = Collections
23
+ .unmodifiableList(Arrays.asList("りんご", "ごりら", "らっぱ","ぱんつ","つくし", "しりとり","しめじ"));
24
+
25
+ private static final List<String> USED = new ArrayList<>();
26
+
27
+ private static final Random RANDOM;
28
+ static {
29
+ try {
30
+ RANDOM = SecureRandom.getInstanceStrong();
31
+ } catch (NoSuchAlgorithmException e) {
32
+ throw new RuntimeException(e);
33
+ }
34
+ }
35
+
36
+ public static void main(String[] args) throws IOException {
37
+ String yours = DICTIONARY.get(RANDOM.nextInt(DICTIONARY.size()));
38
+ System.out.println(new StringBuilder().append("じゃあねー最初は[").append(yours)
39
+ .append(']').append("だよ"));
40
+
41
+ for (;;) {
42
+ final String mine = READER.readLine();
43
+
44
+ if (mine.isEmpty()) {
45
+ continue;
46
+ }
47
+
48
+ if(mine.equals("!")){
49
+ System.out.println("へへん.勝ったー!");
50
+ return;
51
+ }
52
+
53
+ if (yours.charAt(yours.length() - 1) != mine.charAt(0)) {
54
+ System.out.println(new StringBuilder().append("その単語[")
55
+ .append(yours.charAt(yours.length() - 1))
56
+ .append("]で始まってないじゃん!"));
57
+ continue;
58
+ }
59
+
60
+ if (!DICTIONARY.contains(mine)) {
61
+ System.out.println("その単語知らない!知ってそうな単語を入力して!");
62
+ continue;
63
+ }
64
+
65
+ if(USED.contains(mine)){
66
+ System.out.println("その単語使ったよ!忘れたとはいわせないよ!");
67
+ continue;
68
+ }
69
+
70
+ USED.add(mine);
71
+
72
+ List<String> result = search(mine.charAt(mine.length() - 1));
73
+
74
+ if (result.isEmpty()) {
75
+ System.out.println("もうわかんない.負けたー");
76
+ return;
77
+ }
78
+
79
+ yours = result.get(RANDOM.nextInt(result.size()));
80
+
81
+ if(USED.contains(yours)){
82
+ System.out.println(new StringBuilder().append('[').append(yours).append(']')
83
+ .append("は言ったっけー.わかんない.まけたー"));
84
+ return;
85
+ }
86
+
87
+ System.out.println(new StringBuilder().append("じゃあ[")
88
+ .append(yours).append(']').append('!'));
89
+
90
+ }
91
+
92
+ }
93
+
94
+ public static final List<String> search(char firstChar) {
95
+ List<String> result = new ArrayList<>();
96
+ for (String word : DICTIONARY) {
97
+ if (word.charAt(0) == firstChar) {
98
+ result.add(word);
99
+ }
100
+ }
101
+ return result;
102
+ }
103
+ }
104
+
105
+ ```