diff --git a/.gitignore b/.gitignore index c5b6239..8d691f4 100644 --- a/.gitignore +++ b/.gitignore @@ -47,6 +47,7 @@ download .settings .cache .classpath +/.metadata # emacs *~ @@ -63,4 +64,4 @@ replay/ *.exe *.o *.hi -/.metadata +result.txt \ No newline at end of file diff --git a/src/main/scala/net/aicomp/terraforming/scene/MainScene.scala b/src/main/scala/net/aicomp/terraforming/scene/MainScene.scala index d65c015..0745c4a 100644 --- a/src/main/scala/net/aicomp/terraforming/scene/MainScene.scala +++ b/src/main/scala/net/aicomp/terraforming/scene/MainScene.scala @@ -82,25 +82,19 @@ class MainScene(nextScene: Scene[GameEnvironment], def writeJson() = if (jsonStream != null) jsonStream.println(game.toJson(game.currentPlayer)) - - override def release() { + override def release() { val scores = game.players.map(player => (player.id, game.field.calculateScore(player))) - val sortedScores = Sorting.stableSort(scores, (a:(Int,Int), b:(Int,Int)) => a._2 > b._2 || (a._2 == b._2 && a._1 > b._1)) - val ranking = Map.empty[Int,Int] - for((sortedScores, rank) <- sortedScores.zipWithIndex) { - ranking.updated(sortedScores._1, rank) - } + val sortedScores = Sorting.stableSort(scores, + (a: (Int, Int), b: (Int, Int)) => a._2 > b._2 || (a._2 == b._2 && a._1 > b._1)) + val id2Rank = sortedScores.zipWithIndex.map { case ((id, score), rank) => (id, rank + 1) }.toMap var stream: PrintStream = null try { - val fileName = "result.txt" - stream = new PrintStream(fileName) - game.players.foreach { player => - stream.print(ranking.apply(player.id)) - } + stream = new PrintStream("result.txt") + stream.println(game.players.map(p => id2Rank(p.id)).mkString(" ")) } catch { - case _ => new PrintStream(new ByteArrayOutputStream()) + case _: Throwable => () } finally { - if(stream != null) { + if (stream != null) { stream.close() } } diff --git a/src/test/scala/net/aicomp/terraforming/scene/MainSceneSpec.scala b/src/test/scala/net/aicomp/terraforming/scene/MainSceneSpec.scala index 942ee1c..2177e1c 100644 --- a/src/test/scala/net/aicomp/terraforming/scene/MainSceneSpec.scala +++ b/src/test/scala/net/aicomp/terraforming/scene/MainSceneSpec.scala @@ -45,19 +45,17 @@ class MainSceneSpec extends SpecificationWithJUnit { } "generate a result text (1 2 3)" in new TestSceneInitializer { playerScene.acceptAll(env, "abc" :: "def" :: "ghi" :: Nil) - mainScene.accept(env, "move " + initialPoints(0).x + " " + initialPoints(0).y + " l 4") - mainScene.accept(env, "move " + initialPoints(1).x + " " + initialPoints(1).y + " l 4") + mainScene.accept(env, "move " + initialPoints(2).x + " " + initialPoints(2).y + " l 1") mainScene.accept(env, "finish") - mainScene.accept(env, "move " + initialPoints(0).x + " " + initialPoints(0).y + " r 1") - mainScene.acceptAll(env, "finish" :: "finish" :: Nil) - mainScene.acceptAll(env, "finish" :: "finish" :: "finish" :: Nil) - mainScene.acceptAll(env, "finish" :: "finish" :: "finish" :: Nil) - mainScene.acceptAll(env, "finish" :: "finish" :: "finish" :: Nil) + mainScene.accept(env, "move " + initialPoints(1).x + " " + initialPoints(1).y + " l 1") + mainScene.accept(env, "finish") + mainScene.accept(env, "finish") + mainScene.accept(env, "move " + initialPoints(2).x + " " + initialPoints(2).y + " r 1") mainScene.acceptAll(env, "finish" :: "finish" :: "finish" :: Nil) val fileName = "result.txt" val file = new File(fileName); - val line = Source.fromFile(file).getLines() - line.toString must_== "1 2 3 " + val line = Source.fromFile(file).getLines().toSeq.head + line.toString must_== "3 2 1" } } } diff --git a/src/test/scala/net/aicomp/terraforming/scene/TestScene.scala b/src/test/scala/net/aicomp/terraforming/scene/TestScene.scala index 6ad06ba..5251b17 100644 --- a/src/test/scala/net/aicomp/terraforming/scene/TestScene.scala +++ b/src/test/scala/net/aicomp/terraforming/scene/TestScene.scala @@ -35,7 +35,7 @@ trait TestSceneInitializer extends Scope { val env = GameEnvironment() val players = Vector(Player(0), Player(1), Player(2)) val field = Field(7, players, new Random(0)) - env.game = new Game(field, players, 2 * 3) + env.game = new Game(field, players, 2) env.getSceneManager().setFps(1000) val mainScene = new MainScene(null) with TestScene val playerScene = new PlayerScene(mainScene) with TestScene