Advertisement
GrandtherAzaMarks

GameOfThrones

Sep 17th, 2023
1,788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.85 KB | None | 0 0
  1. trait Wealth {
  2.   val money: Int
  3.   val armyStrength: Int
  4. }
  5.  
  6. trait GreatHouse {
  7.   val name: String
  8.   val wealth: Wealth
  9. }
  10.  
  11. trait MakeWildFire {
  12.   this: GreatHouse =>
  13.   def makeWildFire: Wealth => Wealth = Wealth(this.wealth.money, this.wealth.armyStrength + 1000)
  14. }
  15.  
  16. trait BorrowMoney {
  17.   this: GreatHouse =>
  18.   def borrowMoney: Wealth => Wealth = Wealth(this.wealth.money + 100, this.wealth.armyStrength)
  19. }
  20.  
  21. trait CallDragon {
  22.   this: GreatHouse =>
  23.   def callDragon: Wealth => Wealth = Wealth(this.wealth.money, this.wealth.armyStrength * 2)
  24. }
  25.  
  26. class GameOfThrones(house1: GreatHouse, house2: GreatHouse) {
  27.   def nextTurn(strategy1: Wealth => Wealth)(strategy2: Wealth => Wealth): GameOfThrones = {
  28.     val newWealth1 = strategy1(house1.wealth)
  29.     val newWealth2 = strategy2(house2.wealth)
  30.     new GameOfThrones(house1.copy(wealth = newWealth1), house2.copy(wealth = newWealth2))
  31.   }
  32. }
  33. case class Targaryen(override val name: String, override val wealth: Wealth)
  34.   extends GreatHouse with CallDragon {
  35.   override def equals(that: Any): Boolean = that match {
  36.     case Targaryen(n:String, w:Wealth) => this.name == n && this.wealth == w
  37.     case _ => false
  38.   }
  39. }
  40.  
  41. case class Lannisters(override val name: String, override val wealth: Wealth)
  42.   extends GreatHouse with BorrowMoney with MakeWildFire {
  43.   override def equals(that: Any): Boolean = that match {
  44.     case Lannisters(n: String, w: Wealth) => this.name == n && this.wealth == w
  45.     case _ => false
  46.   }
  47. }
  48.  
  49. object solution {
  50.   val targaryen: Targaryen = Targaryen("Targaryen", Wealth(1000, 200))
  51.   val lannisters: Lannisters = Lannisters("Lannisters", Wealth(800, 300))
  52.   private val gameOfThrones = new GameOfThrones(targaryen, lannisters);
  53.   gameOfThrones.nextTurn(targaryen.callDragon)(lannisters.borrowMoney)
  54.   gameOfThrones.nextTurn(targaryen.callDragon)(lannisters.makeWildFire)
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement