Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Mediator Pattern #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/kotlin/oop/Mediator/GameMediator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package oop.Mediator

class GameMediator : Mediator<Player> {
override val recipients = mutableListOf<Player>()

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}")
}
}
}

}
7 changes: 7 additions & 0 deletions src/main/kotlin/oop/Mediator/Mediator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package oop.Mediator

interface Mediator<T> {
val recipients: MutableList<T>
fun add(recipient: T)
fun performPlay(sender: T)
}
12 changes: 12 additions & 0 deletions src/main/kotlin/oop/Mediator/Player.kt
Original file line number Diff line number Diff line change
@@ -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<Player>) {
mediator.performPlay(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class IngredientDecoratorTest {
fun `Ingredient Decorator should return ingredient cost plus base noodle cost`() =
assertThat(Pork(UdonNoodles()).calculateCost(), `is`(8.00))


Copy link
Collaborator Author

@ConradoMateu ConradoMateu May 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups

@Test
fun `check decorated Noodles is of type IngredientDecorator`() =
assertTrue(Tuna(UdonNoodles()) is IngredientDecorator)
Expand Down
48 changes: 48 additions & 0 deletions src/test/kotlin/oop/Mediator/GameMediatorTest.kt
Original file line number Diff line number Diff line change
@@ -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<Player> = GameMediator()

val players = generatePlayers(4)


@Before
fun init() {
players.forEach { mediator.add(it) }
}

fun generatePlayers(number: Int): MutableList<Player> {
val players = mutableListOf<Player>()
(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)
}


}