String 以元组形式返回坐标的Scala

String 以元组形式返回坐标的Scala,string,scala,dictionary,pattern-matching,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{

我有一个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的整数时,就会输入一个从未在字符串中出现过的数字。下面是我的输出:

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
被输入到映射中


谢谢你的帮助

您正在将ASCII码用于
Char
。您需要使用
str(0).asDigit
,将其转换为数字。话虽如此,你把这条路弄得太复杂了。您可以通过以下方式获得您想要的:

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

您正在将ASCII代码用于
字符
。您需要使用
str(0).asDigit
,将其转换为数字。话虽如此,你把这条路弄得太复杂了。您可以通过以下方式获得您想要的:

str.zipWithIndex.map{case (digit, index) => ((index / 7, index % 7), digit.asDigit)}.toMap
str(0)
返回字符串的第一个字符。56是字符“8”的ASCII码
str(0)
返回字符串的第一个字符。56是字符“8”的ASCII码