new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
28
Task/Forest-fire/Scala/forest-fire-1.scala
Normal file
28
Task/Forest-fire/Scala/forest-fire-1.scala
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import scala.util.Random
|
||||
|
||||
class Forest(matrix:Array[Array[Char]]){
|
||||
import Forest._
|
||||
val f=0.01; // auto combustion probability
|
||||
val p=0.1; // tree creation probability
|
||||
val rows=matrix.size
|
||||
val cols=matrix(0).size
|
||||
|
||||
def evolve():Forest=new Forest(Array.tabulate(rows, cols){(y,x)=>
|
||||
matrix(y)(x) match {
|
||||
case EMPTY => if (Random.nextDouble<p) TREE else EMPTY
|
||||
case BURNING => EMPTY
|
||||
case TREE => if (neighbours(x, y).exists(_==BURNING)) BURNING
|
||||
else if (Random.nextDouble<f) BURNING else TREE
|
||||
}
|
||||
})
|
||||
|
||||
def neighbours(x:Int, y:Int)=matrix slice(y-1, y+2) map(_.slice(x-1, x+2)) flatten
|
||||
override def toString()=matrix map (_.mkString("")) mkString "\n"
|
||||
}
|
||||
|
||||
object Forest{
|
||||
val TREE='T'
|
||||
val BURNING='#'
|
||||
val EMPTY='.'
|
||||
def apply(x:Int=30, y:Int=15)=new Forest(Array.tabulate(y, x)((y,x)=> if (Random.nextDouble<0.5) TREE else EMPTY))
|
||||
}
|
||||
9
Task/Forest-fire/Scala/forest-fire-2.scala
Normal file
9
Task/Forest-fire/Scala/forest-fire-2.scala
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
object ForestFire{
|
||||
def main(args: Array[String]): Unit = {
|
||||
var l=Forest()
|
||||
for(i <- 0 until 20){
|
||||
println(l+"\n-----------------------")
|
||||
l=l.evolve
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue