Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Wealth {
- val money: Int
- val armyStrength: Int
- }
- trait GreatHouse {
- val name: String
- val wealth: Wealth
- }
- trait MakeWildFire {
- this: GreatHouse =>
- def makeWildFire: Wealth => Wealth = Wealth(this.wealth.money, this.wealth.armyStrength + 1000)
- }
- trait BorrowMoney {
- this: GreatHouse =>
- def borrowMoney: Wealth => Wealth = Wealth(this.wealth.money + 100, this.wealth.armyStrength)
- }
- trait CallDragon {
- this: GreatHouse =>
- def callDragon: Wealth => Wealth = Wealth(this.wealth.money, this.wealth.armyStrength * 2)
- }
- class GameOfThrones(house1: GreatHouse, house2: GreatHouse) {
- def nextTurn(strategy1: Wealth => Wealth)(strategy2: Wealth => Wealth): GameOfThrones = {
- val newWealth1 = strategy1(house1.wealth)
- val newWealth2 = strategy2(house2.wealth)
- new GameOfThrones(house1.copy(wealth = newWealth1), house2.copy(wealth = newWealth2))
- }
- }
- case class Targaryen(override val name: String, override val wealth: Wealth)
- extends GreatHouse with CallDragon {
- override def equals(that: Any): Boolean = that match {
- case Targaryen(n:String, w:Wealth) => this.name == n && this.wealth == w
- case _ => false
- }
- }
- case class Lannisters(override val name: String, override val wealth: Wealth)
- extends GreatHouse with BorrowMoney with MakeWildFire {
- override def equals(that: Any): Boolean = that match {
- case Lannisters(n: String, w: Wealth) => this.name == n && this.wealth == w
- case _ => false
- }
- }
- object solution {
- val targaryen: Targaryen = Targaryen("Targaryen", Wealth(1000, 200))
- val lannisters: Lannisters = Lannisters("Lannisters", Wealth(800, 300))
- private val gameOfThrones = new GameOfThrones(targaryen, lannisters);
- gameOfThrones.nextTurn(targaryen.callDragon)(lannisters.borrowMoney)
- gameOfThrones.nextTurn(targaryen.callDragon)(lannisters.makeWildFire)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement