Skip to content

Instantly share code, notes, and snippets.

@alalwww
Last active August 29, 2015 13:56
Show Gist options
  • Save alalwww/9345800 to your computer and use it in GitHub Desktop.
Save alalwww/9345800 to your computer and use it in GitHub Desktop.
ショドッビレッレ・オペビードン (33.3%)
package bbop;
import java.util.List;
import java.util.Random;
import com.google.common.collect.Lists;
public class BBOP
{
static final String[] RANDOM_WORDS = { "ビ", "ビ", "ド", "レ", "ド", "オ", "ペ", "レ", "ショ" };
public static void main(String... args)
{
print("再利用有り", RandomWords.reuseOf(RANDOM_WORDS));
print("再利用無し", RandomWords.notReuseOf(RANDOM_WORDS));
System.out.println("-----------------------");
}
private static void print(String header, RandomWords random)
{
final MatchCounter c = new MatchCounter();
System.out.printf("%s: %s (%.1f%%)\n",
header,
new StringBuilder()
.append(c.test("ビ", random.get()))
.append(c.test("ビ", random.get()))
.append("ッ")
.append(c.test("ド", random.get()))
.append(c.test("レ", random.get()))
.append("ッ")
.append(c.test("ド", random.get()))
.append("・")
.append(c.test("オ", random.get()))
.append(c.test("ペ", random.get()))
.append(c.test("レ", random.get()))
.append("ー")
.append(c.test("ショ", random.get()))
.append("ン"),
c.percentRate());
}
/**
* 候補文字列の中の任意の文字列を返すモデル.
*
* @author alalwww
*/
public static final class RandomWords
{
/**
* 選択候補文字列を再利用する新しいモデルを取得.
*
* @param words 選択候補文字列の一覧
* @return 候補のなかから適当な文字列を1つ返すモデル
*/
public static RandomWords reuseOf(String... words)
{
return new RandomWords(words, true);
}
/**
* 選択候補文字列を再利用しない新しいモデルを取得.
*
* @param words 選択候補文字列の一覧
* @return 候補のなかから適当な文字列を1つ返すモデル
*/
public static RandomWords notReuseOf(String... words)
{
return new RandomWords(words, false);
}
private final boolean enableReuse;
private final Random rand = new Random();
private final List<String> words;
private RandomWords(String[] words, boolean enableReuse)
{
this.words = Lists.newArrayList(words);
this.enableReuse = enableReuse;
}
public String get()
{
return enableReuse
? words.get(rand.nextInt(words.size()))
: words.remove(rand.nextInt(words.size()));
}
}
/**
* 一致回数のカウンター.
*
* @author alalwww
*/
public static final class MatchCounter
{
private int total;
private int matches;
public String test(String expect, String actual)
{
total++;
if (expect.equals(actual))
matches++;
return actual;
}
public float percentRate()
{
return (float) matches / (float) total * 100f;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment