我正在尝试返回以下函数中未满的所有列的列表。"isColumnFull"函数将检查列表是否已满。游戏状态是一个列表列表。我不确定错误在哪里。你能帮忙吗?
type GameState = List[List[String]]
case class ColumnNum(index: Int)
val count = 0 //not sure this is needed
def allViableColumns(game: GameState): List[ColumnNum] =
for((xs, count) <- game.zipWithIndex) yield {if(!isColumnFull(xs))List(count+1)}
如果需要列的索引:
type GameState = List[List[String]]
case class ColumnNum(index: Int)
def allViableColumns(game: GameState): List[ColumnNum] =
for((xs, i) <- game.zipWithIndex; if !isColumnFull(xs)) yield ColumnNum(i + 1)
如果你想要这些列,它只是:
def allViableColumns(game: GameState): List[List[String]] =
game filterNot isColumnFull
如果您决定使用第一个版本,请考虑将(i + 1)
更改为i
:通常没有充分的理由进行基于一个的索引。