Advertisement
suresh505

SPTerrain

Jun 29th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. package streams
  2.  
  3. import common._
  4.  
  5. /**
  6. * This component implements a parser to define terrains from a
  7. * graphical ASCII representation.
  8. *
  9. * When mixing in that component, a level can be defined by
  10. * defining the field `level` in the following form:
  11. *
  12. * val level =
  13. * """------
  14. * |--ST--
  15. * |--oo--
  16. * |--oo--
  17. * |------""".stripMargin
  18. *
  19. * - The `-` character denotes parts which are outside the terrain
  20. * - `o` denotes fields which are part of the terrain
  21. * - `S` denotes the start position of the block (which is also considered
  22. inside the terrain)
  23. * - `T` denotes the final position of the block (which is also considered
  24. inside the terrain)
  25. *
  26. * In this example, the first and last lines could be omitted, and
  27. * also the columns that consist of `-` characters only.
  28. */
  29. trait StringParserTerrain extends GameDef {
  30.  
  31. /**
  32. * A ASCII representation of the terrain. This field should remain
  33. * abstract here.
  34. */
  35. val level: String
  36.  
  37. /**
  38. * This method returns terrain function that represents the terrain
  39. * in `levelVector`. The vector contains parsed version of the `level`
  40. * string. For example, the following level
  41. *
  42. * val level =
  43. * """ST
  44. * |oo
  45. * |oo""".stripMargin
  46. *
  47. * is represented as
  48. *
  49. * Vector(Vector('S', 'T'), Vector('o', 'o'), Vector('o', 'o'))
  50. *
  51. * The resulting function should return `true` if the position `pos` is
  52. * a valid position (not a '-' character) inside the terrain described
  53. * by `levelVector`.
  54. */
  55. def terrainFunction(levelVector: Vector[Vector[Char]]): Pos => Boolean = {
  56. (position: Pos) => {
  57. if (position.row < 0 ||
  58. position.col < 0 ||
  59. position.row >= levelVector.size ||
  60. position.col >= levelVector(position.row).size)
  61. false
  62. else levelVector(position.row)(position.col) != '-'
  63. }
  64. }
  65.  
  66. /**
  67. * This function should return the position of character `c` in the
  68. * terrain described by `levelVector`. You can assume that the `c`
  69. * appears exactly once in the terrain.
  70. *
  71. * Hint: you can use the functions `indexWhere` and / or `indexOf` of the
  72. * `Vector` class
  73. */
  74. def findChar(c: Char, levelVector: Vector[Vector[Char]]): Pos = {
  75. for(row <- levelVector)
  76. for (column <- row)
  77. if (column == c) {
  78. return Pos(levelVector.indexOf(row), row.indexOf(c))
  79. }
  80. throw new Exception("Not found")
  81. }
  82.  
  83. private lazy val vector: Vector[Vector[Char]] =
  84. Vector(level.split("\n").map(str => Vector(str: _*)): _*)
  85.  
  86. lazy val terrain: Terrain = terrainFunction(vector)
  87. lazy val startPos: Pos = findChar('S', vector)
  88. lazy val goal: Pos = findChar('T', vector)
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement