Skip to content

Instantly share code, notes, and snippets.

@hishidama
Created January 7, 2012 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hishidama/1575794 to your computer and use it in GitHub Desktop.
Save hishidama/1575794 to your computer and use it in GitHub Desktop.
8面ダイスの各面の割り当てを試す
package dice
class Dice8(num: IndexedSeq[Int]) {
def this(n0: Int, n1: Int, n2: Int, n3: Int) = {
this(Array(n0, n1, n2, n3, 9 - n2, 9 - n3, 9 - n0, 9 - n1))
}
private val 隣接 = Array(
Array(1, 3, 4), //0
Array(0, 5, 2), //1
Array(1, 6, 3), //2
Array(0, 2, 7), //3
Array(0, 7, 5), //4
Array(4, 6, 1), //5
Array(5, 7, 2), //6
Array(4, 3, 6)) //7
def ばらつき度: Int = {
隣接.zipWithIndex.map {
case (a, i) => a.map(j => num(j) - num(i)).map(m => m * m).sum
}.sum
}
override def toString() = num.mkString("Dice8(", ",", ")")
def print(): Unit = {
printf("\%2d/%n", num(2))
printf("%2d×%2d%n", num(3), num(1))
printf("/%2d\%n", num(0))
printf("\%2d/%n", num(6))
printf("%2d×%2d%n", num(7), num(5))
printf("/%2d\%n", num(4))
println
}
}
object Dice8 {
private val N = 1 + 8
def main(args: Array[String]) {
check()
println("-" * 32)
checkAll()
}
//ある面と裏の面との合計が一定のパターンだけ試す
def check() {
val seq = Seq(2, 3, 4)
check(1, seq)
}
def check(n0: Int, ns: Seq[Int]) {
for (n <- ns) {
val seq = ns.filter(_ != n)
check(n0, n, seq)
check(n0, N - n, seq)
}
}
def check(n0: Int, n1: Int, ns: Seq[Int]) {
val n2 = ns.head
val n3 = ns.last
check(n0, n1, n2, n3)
check(n0, n1, n2, N - n3)
check(n0, n1, N - n2, n3)
check(n0, n1, N - n2, N - n3)
}
def check(n0: Int, n1: Int, n2: Int, n3: Int) {
val d = new Dice8(n0, n1, n2, n3)
val s = d.ばらつき度
if (s > 400 || s < 170) {
printf("%s\t%d%n", d, s)
d.print()
}
}
//全パターンを試す
def checkAll() {
for (ns <- (2 to 8).permutations) { //順列組み合わせ
checkAll(1 +: ns)
}
}
def checkAll(ns: IndexedSeq[Int]) {
val d = new Dice8(ns)
val s = d.ばらつき度
if (s > 460 /*|| s < 170*/ ) {
printf("%s\t%d%n", d, s)
d.print()
}
}
}
@hishidama
Copy link
Author

このプログラムを作ろうと思った経緯
http://blog.goo.ne.jp/hishidama/e/b85eb67ffee0fe9333d8e39020b4625f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment