Scala-更新游戏世界

Scala-更新游戏世界,scala,Scala,我来自Java,正在学习Scala。我对游戏和虚拟世界编程感兴趣,所以我决定我的第一个程序是一个小型游戏世界模拟器。在我看来,所有游戏元素通常都存在于以下阶段:创建、更新、删除。在Java或其他OOP中,这对我来说是绝对清楚的。现在我来到斯卡拉。。。到目前为止,我所实现的只是一个容器,里面有许多细胞,每个细胞周期都会发生变异。代码如下: //init val rand : Random = new Random //mutation variations def mutF(f:Int=>

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

//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()
    }
}
//初始化
val rand:Random=新随机
//突变变异
defmutf(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)
//变异方差
val mutFS:List[Int=>Int]=List(mutFA、mutFB、mutFC)
//循环突变函数
def mutFF(f:Int=>Int):Int=>Int={
val i=mutFS.indexOf(f)
如果(iInt){//TODO:如果不使用curry,会是什么???
def mutate():单元格=新单元格(f(值))(f)
def输出(){
打印(“[”+值+“]”)
}
}
//主课
类繁殖(生成:Int,数量:Int,边距:Int,单元格:列表[单元格]){

def this(num:Int,margins:Int)=this(0,num,margins,build())//由于无法在对象初始化之前调用该对象上的方法,因此必须将
build
移动到其他位置。它的自然位置将是伴生对象。请注意
build
使用
num
,在调用
build
时该方法尚未初始化,因此必须传递它作为一个参数。

您可能会对涵盖您的一些更一般问题的文章感兴趣。哇,谢谢!我现在就看!一个更好的代码审阅地方是。一个非常好的建议,Daniel,我不知道。很快也将访问那里)好的,我想,我将重写该内容并访问stackexchange上的codereview!