diff --git a/src/main/kotlin/oop/Mediator/GameMediator.kt b/src/main/kotlin/oop/Mediator/GameMediator.kt new file mode 100644 index 0000000..e22b19a --- /dev/null +++ b/src/main/kotlin/oop/Mediator/GameMediator.kt @@ -0,0 +1,26 @@ +package oop.Mediator + +class GameMediator : Mediator { + override val recipients = mutableListOf() + + var lastTurn = 0 + + override fun add(recipient: Player) { + recipients.add(recipient) + } + + override fun performPlay(sender: Player) { + recipients.forEachIndexed { index, player -> + if (sender == player) { + player.isMyTurn = false + lastTurn = index + } + if (recipients[lastTurn+1] == player) { + player.apply { isMyTurn = true; receive("Is your turn") } + } else { + player.receive("Waiting for player ${lastTurn+1}") + } + } + } + +} diff --git a/src/main/kotlin/oop/Mediator/Mediator.kt b/src/main/kotlin/oop/Mediator/Mediator.kt new file mode 100644 index 0000000..5b65fc1 --- /dev/null +++ b/src/main/kotlin/oop/Mediator/Mediator.kt @@ -0,0 +1,7 @@ +package oop.Mediator + +interface Mediator { + val recipients: MutableList + fun add(recipient: T) + fun performPlay(sender: T) +} diff --git a/src/main/kotlin/oop/Mediator/Player.kt b/src/main/kotlin/oop/Mediator/Player.kt new file mode 100644 index 0000000..43ca5ff --- /dev/null +++ b/src/main/kotlin/oop/Mediator/Player.kt @@ -0,0 +1,12 @@ +package oop.Mediator + +data class Player(val name: String, + var isMyTurn: Boolean = false) { + fun receive(message: String) { + println("$name recieved: $message") + } + + fun play(mediator: Mediator) { + mediator.performPlay(this) + } +} diff --git a/src/test/kotlin/oop/Decorator/base/IngredientDecoratorTest.kt b/src/test/kotlin/oop/Decorator/base/IngredientDecoratorTest.kt index e2bb94d..9a0e80d 100644 --- a/src/test/kotlin/oop/Decorator/base/IngredientDecoratorTest.kt +++ b/src/test/kotlin/oop/Decorator/base/IngredientDecoratorTest.kt @@ -20,6 +20,7 @@ class IngredientDecoratorTest { fun `Ingredient Decorator should return ingredient cost plus base noodle cost`() = assertThat(Pork(UdonNoodles()).calculateCost(), `is`(8.00)) + @Test fun `check decorated Noodles is of type IngredientDecorator`() = assertTrue(Tuna(UdonNoodles()) is IngredientDecorator) diff --git a/src/test/kotlin/oop/Mediator/GameMediatorTest.kt b/src/test/kotlin/oop/Mediator/GameMediatorTest.kt new file mode 100644 index 0000000..7bc5b79 --- /dev/null +++ b/src/test/kotlin/oop/Mediator/GameMediatorTest.kt @@ -0,0 +1,48 @@ +package oop.Mediator + +import junit.framework.Assert.assertFalse +import junit.framework.Assert.assertTrue +import org.junit.Before +import org.junit.Test + +class GameMediatorTest { + private val mediator: Mediator = GameMediator() + + val players = generatePlayers(4) + + + @Before + fun init() { + players.forEach { mediator.add(it) } + } + + fun generatePlayers(number: Int): MutableList { + val players = mutableListOf() + (1..number).forEach { players.add(if (it == 1) Player("Player $it", true) else Player("Player $it")) } + return players + } + + + @Test + fun `Return last item of collection when call`() { + + } + + @Test + fun `when a player has played should't be his turn`() { + val firstPlayer = players.first() + firstPlayer.play(mediator) + assertFalse(firstPlayer.isMyTurn) + } + + @Test + fun `if player has played should give turn to next player`() { + val firstPlayer = players.first() + val secondPlayer = players[1] + firstPlayer.play(mediator) + assertFalse(firstPlayer.isMyTurn) + assertTrue(secondPlayer.isMyTurn) + } + + +}