Scala代码带来了一些我无法理解的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scala代码带来了一些我无法理解的问题相关的知识,希望对你有一定的参考价值。

女士们,先生们,我试图为我的游戏运行一个非常古老的Scala代码,但由于某些库已过期,因此它们无法工作,因此我无法运行它。

`import collection.mutable
import scala.concurrent.ops._

/**
 * Put numbers in a NxN board
 * from 1 to N*N
 * scala SayiYerlestirmece N
 */
object SayiYerlestirmece {
  private var current : Int = 0;

  def main(args: Array[String]) {
    var size = 5;
    //if board size is given use.
    if (args.nonEmpty){
       size = Integer.parseInt(args(0));
    }

    var i = 0;
    for (x <- 0 until size ){
      for(y <- 0 until size){
        //run every initial states in parallel.
        spawn{
          val arr = new Array[Int](size * size);
          arr(i) = 1;
          //create initial states
          val initialList :List[Model] = List(new Model(arr,size,x,y));
          solve(initialList);
        }
        i+=1;
      }
    }

  }

  /**
   * solve problem recursively
   * @param modelList - next states
   */
  def solve(modelList: List[Model]){
    modelList.foreach(p => {
       if (p.completed){
         current+=1;
         println(p);
         println(current);
       }
       solve(p.nextStepList());
    });
  }
}

/**
 *
 * @param data - current state of board
 * @param size - size of board 5x5 10x10 etc
 * @param x - current x position on the board
 * @param y - current y position on the board
 */
class Model(data:Array[Int], size:Int, x:Int, y:Int) {
  /**
   * convert multi dimensional x,y index to one dimensional index.
   * @param size - size of board
   * @param x - current x position
   * @param y - current y position
   * @return - corresponding array index
   */
  def xy2Idx(size:Int, x:Int, y:Int): Int = {
    if ( x < 0 || y < 0 || x >= size || y >= size)
      -1
    else
      size * x + y;
  }

  /**
   * convert one dimensional array index to multi dimensional x,y index
   * @param size
   * @param idx
   * @return
   */
  def idx2Xy(size:Int, idx:Int):(Int,Int) = {
    return (idx/size,idx%size);
  }

  /**
   *   Checks whether game is completed or not
   * @return  true if is game completed else false
   */
  def completed() :Boolean = { data(xy2Idx(size,x,y)) == size * size };

  /**
   * Position of next available empty cells.
   * @return - list of index of empty cells.
   */
  def nextStepIdxList():List[Int] = {
    return mutable.MutableList(
      xy2Idx(size,x+3,y),
      xy2Idx(size,x-3,y),
      xy2Idx(size,x,y+3),
      xy2Idx(size,x,y-3),
      xy2Idx(size,x+2,y+2),
      xy2Idx(size,x-2,y+2),
      xy2Idx(size,x+2,y-2),
      xy2Idx(size,x-2,y-2)
    ).filter(p => p > -1 && data(p) == 0).toList; //filter out of bounds and non-empty cells
  }

  /**
   * Next states of board. These are derived from indexes
   * which are returned by nextStepIdxList() function.
   * @return - Next possible states of the board
   */
  def nextStepList():List[Model] = {
    var modelList = mutable.MutableList[Model]()
    nextStepIdxList().foreach( p => {
      val newArr = data.clone();
      newArr(p) = data(xy2Idx(size,x,y)) + 1;
      modelList += new Model(newArr,size,idx2Xy(size,p)._1, idx2Xy(size,p)._2);
    });
    return modelList.sortWith(_.nextStepSize() < _.nextStepSize()).toList;  // sorts board states by least next step size
  }

  /**
   * Available cell count at next step.
   * This value is used to determine next move.
   * @return - Available empty cell count
   */
  def nextStepSize():Int = {
    return nextStepIdxList().size;
  }

  override def toString(): String = {
    val sb = new StringBuilder();
    data.indices.foreach(p =>{
      if (p % size == 0)
        sb.append("
");
      else
        sb.append(",");
      sb.append(data(p));

    });
    return sb.toString();
  }
}
`

当我运行它时说导入scala.concurrent.ops._在我删除它时不起作用当我添加另一个版本的spawn时,它找不到spawn,它不起作用。有没有人可以帮助我运行此代码。

答案

scala.concurrent.ops._在2012年的Scala Future中被Future弃用,以Futures, promises and execution contexts #200取代,并在2.11.0中被完全删除。尝试更换

2.10.0

spawn {
  // expressions
}

以上是关于Scala代码带来了一些我无法理解的问题的主要内容,如果未能解决你的问题,请参考以下文章

以下代码片段 C++ 的说明

如何使用构建器模式通过 Scala 宏返回内部对象

为啥片段类应该是公开的?

初学scala4——trait混入

无法从 onListItemClick 开始片段

scala slick 方法我目前无法理解