Scala中的Spiral程序:把代码都螺旋在一起

  操练布局库所有这些元素好玩儿思路方法就是写个画给定数量边界螺旋这个Spiral展示在代码10.14中是这么做:

import Element.elem  
object Spiral {  
 val space = elem(" ")  
 val corner = elem("+")  
 def spiral(nEdges: Int, direction: Int): Element = {  
   (nEdges  1)  
   elem("+")  
   {  
   val sp = spiral(nEdges - 1, (direction + 3) % 4)  
   def verticalBar = elem('|', 1, sp.height)  
   def horizontalBar = elem('-', sp.width, 1)  
    (direction  0)  
    (corner beside horizontalBar) above (sp beside space)  
     (direction  1)  
    (sp above space) beside (corner above verticalBar)  
     (direction  2)  
    (space beside sp) above (horizontalBar beside corner)  
    
    (verticalBar above corner) beside (space above sp)  
  }  
 }  
 def (args: Array[String]) {  
  val nSides = args(0).toInt  
  prln(spiral(nSides, 0))  
 }  
}  
代码 10.14 Spiral

  Spiral是个带有合适签名思路方法独立对象所以它是个ScalaSpiral带个命令行参数个整数并且以特定数量边界画个螺旋例如可以像展示在下面左边那样画个 6边界螺旋或者右边那样更大螺旋:(略)

Tags: 

延伸阅读

最新评论

发表评论