Scala将坐标作为元组返回

时间:2016-03-28 17:00:33

标签: string scala dictionary pattern-matching

我有一个7乘7的网格,我必须解析填充网格的字符串。因此,如果我有一个包含49个元素的字符串,我需要将字符串转换为可用的网格。网格用一个如下所示的地图表示:

Map[(Int, Int), List[Int])]

这是我到目前为止所拥有的:

   def parseHelper(str: String, x: Int, y: Int, lst: Map[(Int, Int), List[Int]]): Map[(Int,Int), List[Int]] = {
      str match{
      case "" => lst
      case _ => if (x == 6){
          if (str.charAt(0).equals('.')){
            parseHelper(str.substring(1), 0, y+1, lst + ((x, y) -> List(-1)))
          } else {
            print("str = " + str + " int = " + str(0) + " list = " + lst.head + "\n")
            parseHelper(str.substring(1), 0, y+1, lst + ((x,y) -> List(str(0))))
          }
      } else {
        if (str.charAt(0).equals('!')){
          parseHelper(str.substring(1), x+1, y, lst+((x, y) -> List(-1)))
        } else {
          print("str = " + str + " int = " + str(0) + " list = " + lst.head + "\n")
          parseHelper(str.substring(1), x+1, y, lst+((x,y) -> List(str(0))))
        }
     }
    }
  }

!表示空白点,因此我们将-1表示为空白点。

示例字符串:!!!!8!3!!!6!!7!!84!3!5!!2!9!!!1!54!8!!!!!!!!!4!27!6!!!3!1!!7!4!72!!4!!6!!!4!1!!!3

我用于调试的打印语句。问题是当我输入一个不是-1的int时,输入一个从不在字符串中的数字。下面我有输出:

str = 8.3...6..7..84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 8 list = ((0,0),List(-1))
str = 3...6..7..84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 3 list = ((5,0),List(-1))
str = 6..7..84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 6 list = ((5,0),List(-1))
str = 7..84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 7 list = ((5,0),List(-1))
str = 84.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 8 list = ((5,0),List(-1))
str = 4.3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 3.5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 3 list = ((7,1),List(56))
str = 5..2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 5 list = ((7,1),List(56))
str = 2.9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 2 list = ((7,1),List(56))
str = 9...1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 9 list = ((7,1),List(56))
str = 1.54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 1 list = ((7,1),List(56))
str = 54.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 5 list = ((7,1),List(56))
str = 4.8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 8.........4.27.6...3.1..7.4.72..4..6...4.1...3 int = 8 list = ((7,1),List(56))
str = 4.27.6...3.1..7.4.72..4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 27.6...3.1..7.4.72..4..6...4.1...3 int = 2 list = ((7,1),List(56))
str = 7.6...3.1..7.4.72..4..6...4.1...3 int = 7 list = ((7,1),List(56))
str = 6...3.1..7.4.72..4..6...4.1...3 int = 6 list = ((7,1),List(56))
str = 3.1..7.4.72..4..6...4.1...3 int = 3 list = ((7,1),List(56))
str = 1..7.4.72..4..6...4.1...3 int = 1 list = ((7,1),List(56))
str = 7.4.72..4..6...4.1...3 int = 7 list = ((7,1),List(56))
str = 4.72..4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 72..4..6...4.1...3 int = 7 list = ((7,1),List(56))
str = 2..4..6...4.1...3 int = 2 list = ((7,1),List(56))
str = 4..6...4.1...3 int = 4 list = ((7,1),List(56))
str = 6...4.1...3 int = 6 list = ((7,1),List(56))
str = 4.1...3 int = 4 list = ((7,1),List(56))
str = 1...3 int = 1 list = ((7,1),List(56))
str = 3 int = 3 list = ((7,1),List(56))

出于某种原因,当字符串中的56不均匀时,数字56将输入到映射中。

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

您正在使用Char的ASCII代码。您想要的是使用str(0).asDigit将其转换为数字。话虽这么说,你这样做太复杂了。你可以通过以下方式获得你想要的东西:

str.zipWithIndex.map{case (digit, index) => ((index / 7, index % 7), digit.asDigit)}.toMap