【问题标题】:Scala - update a game worldScala - 更新游戏世界
【发布时间】:2011-12-23 13:34:52
【问题描述】:

来自 Java,我正在学习 Scala。我对游戏和虚拟世界编程很感兴趣,所以我决定我的第一个程序是一个小型游戏世界模拟器。在我看来,所有游戏元素通常都存在以下几个阶段:创建、更新、删除。在 Java 或其他 OOP 中对我来说绝对清楚。现在我来到 Scala ......到目前为止,我已经实现的只是一个包含许多单元的容器,这些单元应该在每个周期中发生变异。代码如下:

//init
val rand : Random = new Random

//mutation variations
def mutF(f:Int=>Int, v: Int) : Int = {f(v)}

def mutFA(v:Int) : Int = mutF(x => x, v)
def mutFB(v:Int) : Int = mutF(x => x + x, v)
def mutFC(v:Int) : Int = mutF(x => x - x, v)

    //mutation variance
val mutFS : List[Int=>Int] = List(mutFA, mutFB, mutFC)

    //cycle through mutation functions
def mutFF(f:Int=>Int) : Int=>Int = {
    val i = mutFS.indexOf(f)
    if(i < mutFS.length) mutFS(i + 1)
    else mutFS(0)
}

//objects
class Cell(value:Int)(f:Int => Int){    //TODO: what will be without currying???
    def mutate() : Cell = new Cell(f(value))(f)
    def output() {
        print("[" + value + "]")
    }
}

//the main class
class Breed(generation:Int, num:Int, margins:Int, cells: List[Cell]) {

    def this(num:Int, margins:Int) = this(0, num, margins, build()) //<<<<<

    //make 1 cell
    def makeCell() : Cell = {
        val mutF:Int=>Int = mutFS(rand.nextInt(mutFS.length))
        val v = rand.nextInt(margins)
        println("BREED: making cell " + v)
        new Cell(v)(mutF)
    }

    //fill with random cells
    def build() : List[Cell] = {
        def addCell(acc:Int, list:List[Cell]) : List[Cell] = {
            println("BREED: build(), acc= " + acc + " list=" + list)
            if(acc <= 0) list
            else addCell(acc - 1, makeCell :: list)
        }
        addCell(num, List())
    }

//  val cells : List[Cell] = build()

    //go several generations ahead, print every generation
    def mutate(generations:Int) {
        def mutateF(acc:Int, breed : Breed) : Breed = {
            if (acc == 0) breed
            else {
                print("BREED: mutating, ")
                breed.output()
                mutateF(acc - 1, mutate(breed))
            }
        }
        mutateF(generations, this)
    }

    //mutate this breed
    def mutate(breed : Breed) : Breed = {
        def mutateF(l : List[Cell]) : List[Cell] = {
            l match {
                case Nil => Nil
                case y :: yx => y.mutate() :: mutateF(yx)
            }
        }
        new Breed(generation, num, margins, mutateF(build))
    }

    def output() {
        print("BREED: [" + generation + "] ")
        for(i <- 0 to num - 1) cells(i).output()
        println()
    }
}

首先 - 我的问题是 - 如何让 'build()' 函数在辅助构造函数中工作?在 Java 中,这没有问题。 Scala 解决这个问题的方法是什么?其次,您能从Scala函数式方法的角度评论我的错误吗?

更新:我会很感激重写这段代码,因为你会用纯 Scala 方式编写它。

【问题讨论】:

  • 您可能对"Purely Functional Retrogames" 的文章感兴趣,这些文章涵盖了您的一些更一般的问题。
  • 哇,谢谢!我马上就读!
  • 代码审查更好的地方是codereview.stackexchange.com
  • 一个非常好的建议,丹尼尔,我不知道。很快也会去那里)

标签: scala


【解决方案1】:

由于您不能在对象初始化之前调用该对象的方法,因此您必须将build 移至其他位置。它的自然位置将是伴生对象。请注意build 使用num,在您调用build 时尚未初始化,因此您必须将其作为参数传递。

【讨论】:

  • 好吧,我想,我会重写这件事并访问stackexchange上的codereview!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 2011-01-06
  • 1970-01-01
相关资源
最近更新 更多