import util.
control .
Exception .
catching
def main
( args
: Array
[ String
] ) = {
val game
= ( new GameBuilder
) .
size ( 3 ) .
seed ( -
1 ) .
build
// ↓で人力プレイ
game.play
// ソルバというか答え流し込むだけ
//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)
//game.solver(answer)
println( "おしまい。" )
}
}
def size
( s
: Int
) = { _ size
= s
; this } def seed
( s
: Long
) = { _ seed
= s
; this } def build
= new Game
( _ size,
_ seed
) }
Random.setSeed ( seed)
private [ this ] var board
= Board
( size,
( 1 to
( size
* size-
1 ) ) .
toList ::: List
( 0 ) ) board = board.shuffle
def move
( target
: Int
) = { print( "move " + target + ": " )
prev_ board = board
board = board.move ( target)
}
println( "はいはいクリアクリア。" )
println( "動かせねぇよ。" )
println( )
}
def step
( count
: Int
) ( f
: Int
=> Int
) : Unit
= { board.print
move( in)
if ( judge
( ) ) step
( count+
1 ) ( f
) else board.
print }
def solver
( input
: List
[ Int
] ) = step
( 0 ) { i
: Int
=> if ( i
< input.
length ) input
( i
) else 0 }
def play
= step
( 0 ) { i
: Int
=> UConsole.
readIntContinue }
}
def readIntContinue
( ) : Int
= { catching( classOf[ NumberFormatException] ) opt {
readLine( "動かす数字を入れろ (quit 0) -> " ) .toInt
println( "え?なに?聞こえない" )
readIntContinue( )
}
}
}
def empty
= Board
( 0 , Nil
) }
case class Board
( size
: Int, self
: List
[ Int
] ) {
var idx
= a.
indexWhere ( _ == 0 )
val next
= Random.
shuffle ( candidates
( idx
) ) .
head a( idx) = a( next)
a( next) = t
idx = next
}
this .
copy ( self
= a.
toList ) }
def swap
( i
: Int, j
: Int
) = { a( i) = a( j)
a( j) = t
this .
copy ( self
= a.
toList ) }
def candidates
( idx
: Int
) : List
[ Int
] = { idx - size :: idx + size ::
( if ( idx
% size
== 0 ) -
1 else idx-
1 ) :: ( if ( ( idx+
1 ) % size
== 0 ) -
1 else idx+
1 ) :: Nil
} filter ( i => 0 <= i && i< length)
def move
( target
: Int
) : Board
= { val idx
= self.
indexWhere ( _ == target
) candidates
( idx
) find
{ self
( _ ) == 0 } match { swap( idx, zero)
}
}
self.
init .
zipWithIndex .
forall { case ( a,i
) => a-
1 == i
}
self.
zipWithIndex .
foreach { case ( i, idx
) => Predef.
print ( ( if ( i
!= 0 ) i
else " " ) +
"\t " ) if ( ( idx+
1 ) % size
== 0 ) println
( ) }
println( )
}
}
// おわり(^Ω^)
aW1wb3J0IHV0aWwuUmFuZG9tCmltcG9ydCBjb2xsZWN0aW9uLm11dGFibGUKaW1wb3J0IHV0aWwuY29udHJvbC5FeGNlcHRpb24uY2F0Y2hpbmcKCm9iamVjdCBQdXp6bGUxNSB7CiAgZGVmIG1haW4oYXJnczogQXJyYXlbU3RyaW5nXSkgPSB7CgogICAgdmFsIGdhbWUgPSAobmV3IEdhbWVCdWlsZGVyKS5zaXplKDMpLnNlZWQoLTEpLmJ1aWxkCgogICAgLy8g4oaT44Gn5Lq65Yqb44OX44Os44KkCiAgICBnYW1lLnBsYXkKCiAgICAvLyDjgr3jg6vjg5DjgajjgYTjgYbjgYvnrZTjgYjmtYHjgZfovrzjgoDjgaDjgZEKICAgIC8vdmFsIGFuc3dlciA9IExpc3QoMSw4LDMsMywyLDIsMyw2LDYsNiw4LDUsNyw4LDUsMywzLDUsNiwzLDIsMiwzLDMsMiwxLDQsNyw4KQogICAgLy9nYW1lLnNvbHZlcihhbnN3ZXIpCiAgICAKICAgIHByaW50bG4oIuOBiuOBl+OBvuOBhOOAgiIpCiAgfQp9CgpjbGFzcyBHYW1lQnVpbGRlciB7CiAgcHJpdmF0ZVt0aGlzXSB2YXIgX3NpemUgPSAzCiAgcHJpdmF0ZVt0aGlzXSB2YXIgX3NlZWQgPSAwTAogIGRlZiBzaXplKHM6IEludCkgID0geyBfc2l6ZSA9IHM7IHRoaXMgfQogIGRlZiBzZWVkKHM6IExvbmcpID0geyBfc2VlZCA9IHM7IHRoaXMgfQogIGRlZiBidWlsZCA9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HV0OiBMaXN0W0ludF0pID0gc3RlcCgwKXsKICAgIGk6IEludCA9PiBpZiAoaSA8IGlucHV0Lmxlbmd0aCkgaW5wdXQoaSkgZWxzZSAwCiAgfQoKICBkZWYgcGxheSA9IHN0ZXAoMCl7IGk6IEludCA9PiBVQ29uc29sZS5yZWFkSW50Q29udGludWUgfQogIAp9CgpvYmplY3QgVUNvbnNvbGUgewogIAogIGRlZiByZWFkSW50Q29udGludWUoKTogSW50ID0gewogICAgY2F0Y2hpbmcoY2xhc3NPZltOdW1iZXJGb3JtYXRFeGNlcHRpb25dKSBvcHQgewogICAgICByZWFkTGluZSgi5YuV44GL44GZ5pWw5a2X44KS5YWl44KM44KNIChxdWl0IDApIC0+ICIpLnRvSW50CiAgICB9IG1hdGNoIHsKICAgICAgY2FzZSBTb21lKGkpID0+IGkKICAgICAgY2FzZSBOb25lID0+CiAgICAgICAgcHJpbnRsbigi44GI77yf44Gq44Gr77yf6IGe44GT44GI44Gq44GEIikKICAgICAgICByZWFkSW50Q29udGludWUoKQogICAgfQogIH0KfQoKb2JqZWN0IEJvYXJkIHsKICBkZWYgZW1wdHkgPSBCb2FyZCgwLCBOaWwpCn0KCmNhc2UgY2xhc3MgQm9hcmQoc2l6ZTogSW50LCBzZWxmOiBMaXN0W0ludF0pIHsKICBwcml2YXRlW3RoaXNdIHZhbCBsZW5ndGggPSBzaXplKnNpemUKCiAgZGVmIHNodWZmbGUoKSA9IHsKICAgIHZhbCBhID0gc2VsZi50b0FycmF5CiAgICB2YXIgaSA9IHNpemUgKiAxMAogICAgdmFyIGlkeCA9IGEuaW5kZXhXaGVyZShfID09IDApCgogICAgd2hpbGUoIHtpPWktMTsgaSE9MH0gKSB7CiAgICAgIHZhbCBuZXh0ID0gUmFuZG9tLnNodWZmbGUoY2FuZGlkYXRlcyhpZHgpKS5oZWFkCiAgICAgIHZhbCB0ID0gYShpZHgpCiAgICAgIGEoaWR4KSA9IGEobmV4dCkKICAgICAgYShuZXh0KSA9IHQKICA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