Skip to content

Commit

Permalink
Add a feature to write result.txt file.
Browse files Browse the repository at this point in the history
  • Loading branch information
exKAZUu committed Jul 20, 2013
1 parent 31a3712 commit 6e4a368
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ download
.settings
.cache
.classpath
/.metadata

# emacs
*~
Expand All @@ -63,4 +64,4 @@ replay/
*.exe
*.o
*.hi
/.metadata
result.txt
22 changes: 8 additions & 14 deletions src/main/scala/net/aicomp/terraforming/scene/MainScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
16 changes: 7 additions & 9 deletions src/test/scala/net/aicomp/terraforming/scene/MainSceneSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6e4a368

Please sign in to comment.