Advertisement
NLinker

Merry Christmas scala program

Dec 24th, 2014
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.68 KB | None | 0 0
  1. import annotation.tailrec
  2.  
  3. abstract class Tree(val left: Tree) {
  4.   def o = new Ball(this)
  5.   def x = new Spike(this)
  6.   def * = new Candle(this)
  7.   def oxo = new BigBall(this)
  8.   def oo = new DoubleBall(this)
  9.   def *** = new ElectricCandle(this)
  10.  
  11.   def / = new LeftNeedle(this)
  12.   def \ = new RightNeedle(this)
  13.   def | = new Trunk(this)
  14.  
  15. }
  16. class Top(star: Star) extends Tree(star)
  17. abstract class Needle(left: Tree) extends Tree(left)
  18. class LeftNeedle(left: Tree) extends Needle(left)
  19. class RightNeedle(left: Tree) extends Needle(left) {
  20.  
  21.   def |||() {
  22.     |||(true)
  23.   }
  24.  
  25.   @tailrec
  26.   final def |||(sparkle: Boolean) {
  27.     val f = (t: Tree) =>
  28.       t match {
  29.         case _: LeftNeedle => "/"
  30.         case _: RightNeedle => "\\"
  31.         case _: Trunk => "|"
  32.         case _: Ball => "o"
  33.         case _: Spike => "x"
  34.         case _: Candle => "*"
  35.         case _: BigBall => "oxo"
  36.         case _: DoubleBall => "oo"
  37.         case _: ElectricCandle => "***"
  38.       }
  39.  
  40.     def walk(t: Tree, depth: Int): List[String] = {
  41.       def walkLevel(t: Tree, acc: String,
  42.                     f: (Tree) => String): (Tree, String) = {
  43.         val fx = (t: Tree) => if (sparkle) f(t).toUpperCase else f(t)
  44.         t match {
  45.           case l: LeftNeedle => (l.left, fx(l) + "." + acc)
  46.           case t: Tree => walkLevel(t.left, fx(t) + "." + acc, f)
  47.         }
  48.       }
  49.  
  50.       t match {
  51.         case r: RightNeedle =>
  52.           val l = walkLevel(r, "", f)
  53.           l._2 +: walk(l._1, depth + 1)
  54.         case s: Star =>
  55.           List("-->*<-- ", "\\-/.")
  56.       }
  57.     }
  58.  
  59.     val tree = "||| " +: walk(this, 0)
  60.  
  61.     tree.reverse.foreach({l =>
  62.       val numSpaces = 30 - (l.length() / 2)
  63.       val padding = " " * numSpaces
  64.       print(padding)
  65.       println(l)
  66.     })
  67.  
  68.     Thread.sleep(500)
  69.  
  70.     |||(!sparkle)
  71.   }
  72.  
  73. }
  74. class Trunk(parent: Tree) extends Tree(parent)
  75.  
  76. abstract class Decoration(left: Tree) extends Tree(left)
  77. class Star extends Decoration(null)
  78. class Candle(left: Tree) extends Decoration(left)
  79. class Ball(left: Tree) extends Decoration(left)
  80. class Spike(left: Tree) extends Decoration(left)
  81. class BigBall(left: Tree) extends Decoration(left)
  82. class DoubleBall(left: Tree) extends Decoration(left)
  83. class ElectricCandle(left: Tree) extends Decoration(left)
  84.  
  85. trait DecorationBuilder {
  86.  def \-/ = new PartialDecoration
  87. }
  88. class PartialDecoration {
  89.  def -->*<-- = new Star
  90. }
  91.  
  92. object ChristmasTree extends DecorationBuilder {
  93.  
  94.   def main(args: Array[String]) {
  95.            \-/.
  96.          -->*<--
  97.             .
  98.            /.\
  99.          ./.|.\.
  100.          /.oxo.\
  101.        ./.*.|.x.\.
  102.        /.oo.|.oo.\
  103.      ./.oxo.|.***.\.
  104.      /.*.oo.|.*.oo.\.
  105.            |||
  106.   }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement