fork(1) download
  1. import util.Random
  2. import collection.mutable
  3. import util.control.Exception.catching
  4.  
  5. object Puzzle15 {
  6. def main(args: Array[String]) = {
  7.  
  8. val game = (new GameBuilder).size(3).seed(-1).build
  9.  
  10. // ↓で人力プレイ
  11. game.play
  12.  
  13. // ソルバというか答え流し込むだけ
  14. //val answer = List(1,8,3,3,2,2,3,6,6,6,8,5,7,8,5,3,3,5,6,3,2,2,3,3,2,1,4,7,8)
  15. //game.solver(answer)
  16.  
  17. println("おしまい。")
  18. }
  19. }
  20.  
  21. class GameBuilder {
  22. private[this] var _size = 3
  23. private[this] var _seed = 0L
  24. def size(s: Int) = { _size = s; this }
  25. def seed(s: Long) = { _seed = s; this }
  26. def build = new Game(_size, _seed)
  27. }
  28.  
  29. class Game(size: Int, val seed: Long) {
  30. Random.setSeed(seed)
  31. private[this] var board = Board(size, (1 to (size*size-1)).toList ::: List(0))
  32. private[this] var prev_board: Board = Board.empty
  33. board = board.shuffle
  34.  
  35. def move(target: Int) = {
  36. print("move " + target + ": ")
  37. prev_board = board
  38. board = board.move(target)
  39. }
  40.  
  41. def judge(): Boolean = board match {
  42. case a if a.clear_? =>
  43. println("はいはいクリアクリア。")
  44. case a if prev_board == a =>
  45. println("動かせねぇよ。")
  46. case a =>
  47. println()
  48. }
  49.  
  50. def step(count: Int)(f: Int => Int): Unit = {
  51. board.print
  52. val in = f(count)
  53. if (in == 0) return
  54.  
  55. move(in)
  56.  
  57. if ( judge() ) step(count+1)(f) else board.print
  58. }
  59.  
  60. def solver(input: List[Int]) = step(0){
  61. i: Int => if (i < input.length) input(i) else 0
  62. }
  63.  
  64. def play = step(0){ i: Int => UConsole.readIntContinue }
  65.  
  66. }
  67.  
  68. object UConsole {
  69.  
  70. def readIntContinue(): Int = {
  71. catching(classOf[NumberFormatException]) opt {
  72. readLine("動かす数字を入れろ (quit 0) -> ").toInt
  73. } match {
  74. case Some(i) => i
  75. case None =>
  76. println("え?なに?聞こえない")
  77. readIntContinue()
  78. }
  79. }
  80. }
  81.  
  82. object Board {
  83. def empty = Board(0, Nil)
  84. }
  85.  
  86. case class Board(size: Int, self: List[Int]) {
  87. private[this] val length = size*size
  88.  
  89. def shuffle() = {
  90. val a = self.toArray
  91. var i = size * 10
  92. var idx = a.indexWhere(_ == 0)
  93.  
  94. while( {i=i-1; i!=0} ) {
  95. val next = Random.shuffle(candidates(idx)).head
  96. val t = a(idx)
  97. a(idx) = a(next)
  98. a(next) = t
  99. idx = next
  100. }
  101.  
  102. this.copy(self = a.toList)
  103. }
  104.  
  105. def swap(i: Int, j: Int) = {
  106. val a = self.toArray
  107. val t = a(i)
  108. a(i) = a(j)
  109. a(j) = t
  110. this.copy(self = a.toList)
  111. }
  112.  
  113. def candidates(idx: Int): List[Int] = {
  114. idx - size :: idx + size ::
  115. (if (idx%size==0) -1 else idx-1) ::
  116. (if ((idx+1)%size==0) -1 else idx+1) ::
  117. Nil
  118. } filter (i => 0<=i && i<length)
  119.  
  120. def move(target: Int): Board = {
  121. val idx = self.indexWhere(_ == target)
  122. candidates(idx) find { self(_) == 0 } match {
  123. case Some(zero) =>
  124. swap(idx, zero)
  125. case None =>
  126. }
  127. }
  128.  
  129. def clear_?(): Boolean =
  130. self.init.zipWithIndex.forall{ case (a,i) => a-1 == i }
  131.  
  132. def print {
  133. self.zipWithIndex.foreach{ case (i, idx) =>
  134. Predef.print((if (i!=0) i else " ") + "\t")
  135. if((idx+1)%size==0) println()
  136. }
  137. println()
  138. }
  139.  
  140. }
  141.  
  142. // おわり(^Ω^)
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/opt/scala/bin/scalac: line 50: /dev/null: Permission denied
spoj: The program compiled successfully, but Main.class was not found.
      Class Main should contain method: def main(args: Array[String]).
stdout
Standard output is empty