Skip to content

Commit

Permalink
feat(ghost): add serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablito2020 committed Apr 25, 2022
1 parent c22526c commit a39bdfd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/kotlin/game/ghost/GhostGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import movements.Position
import movements.Rotation
import score.Points
import score.ScoreCalculator
import java.io.Serializable

class GhostGame(creator: BlockCreator, scoreCalculator: ScoreCalculator) : Game {
class GhostGame(creator: BlockCreator, scoreCalculator: ScoreCalculator) : Game, Serializable {

private val game = NormalGame(creator, scoreCalculator)

Expand Down Expand Up @@ -52,4 +53,14 @@ class GhostGame(creator: BlockCreator, scoreCalculator: ScoreCalculator) : Game
return ghostGrid
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as GhostGame
if (game != other.game) return false
return true
}

override fun hashCode() = game.hashCode()

}
67 changes: 67 additions & 0 deletions src/test/kotlin/game/ghost/GhostGameSerializeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package blocks.game.ghost

import block_factory.BlockCreator
import block_factory.BlockType
import blocks.Block
import blocks.deserialize
import blocks.implementation.IBlock
import blocks.serialize
import game.Game
import game.ghost.GhostGame
import game.normal.GAME_COLUMNS
import movements.Direction
import movements.Position
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import score.Points
import score.ScoreCalculator
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class GhostGameSerializeTest {

private lateinit var game: GhostGame

private class BlockCreatorSerialized : BlockCreator, java.io.Serializable {
override fun getBlock(): Block = IBlock(Position(0, (GAME_COLUMNS / 2) - 2))
override fun getNextBlockType(): BlockType = throw UnsupportedOperationException("not implemented")
}

private class PointsSerialized : ScoreCalculator, java.io.Serializable {
override fun getScore(cleanedRows: Int): Points = Points(0)
}

@BeforeEach
fun setUp() {
game = GhostGame(BlockCreatorSerialized(), PointsSerialized())
}

@Test
fun `serialize provide same object`() {
val bytes = serialize(game)
val deserialized = deserialize(bytes) as Game
assertEquals(game, deserialized)
}


@Test
fun `deserialize with changes doesn't provide same object as default`() {
game.generateNextBlock()
val bytes = serialize(game)
val deserialized = deserialize(bytes) as Game
assertNotEquals(deserialized, GhostGame(BlockCreatorSerialized(), PointsSerialized()))
}

@Test
fun `deserialize with changes provide the same changed object`() {
game.generateNextBlock()
game.moveBlock(Direction.DOWN)
val bytes = serialize(game)
val deserialized = deserialize(bytes) as Game
val newGame = GhostGame(BlockCreatorSerialized(), PointsSerialized())
newGame.generateNextBlock()
newGame.moveBlock(Direction.DOWN)
assertEquals(deserialized, newGame)
}

}

0 comments on commit a39bdfd

Please sign in to comment.