fork(2) download
  1.  
  2. let x = 3
  3. let y = 3
  4.  
  5. protocol Printable{
  6. var symbol: String { get }
  7. }
  8.  
  9. class Object: Printable{
  10. let symbol: String
  11. let isSolid: Bool
  12.  
  13. init(symbol: String, isSolid: Bool){
  14. self.symbol = symbol
  15. self.isSolid = isSolid
  16. }
  17.  
  18. static func Rock() -> Object{
  19. return Object(symbol: "x", isSolid: true)
  20. }
  21.  
  22. static func Space() -> Object{
  23. return Object(symbol: " ", isSolid: false)
  24. }
  25.  
  26. static private func object(from: Character) -> Object{
  27. if from == "x" { return Rock() }
  28. if from == " " { return Space() }
  29. fatalError()
  30. }
  31.  
  32. static func objects(from: String) -> [Object]{
  33. var objects: [Object] = []
  34. for char in from.characters{
  35. objects.append(object(from: char))
  36. }
  37. return objects
  38. }
  39. }
  40.  
  41. class Player: Printable{
  42. let symbol: String = "@"
  43.  
  44. let x, y: Int
  45.  
  46. init(x: Int, y: Int) {
  47. self.x = x
  48. self.y = y
  49. }
  50. }
  51.  
  52. class MapLine{
  53. let objects: [Object]
  54.  
  55. init(input: String) {
  56. objects = Object.objects(from: input)
  57. }
  58.  
  59. func printLine(player: Player?){
  60. var line: [Printable] = objects
  61. if let player = player{
  62. line[player.x] = player
  63. }
  64. let result = line.reduce("") { res, obj in res + obj.symbol }
  65. print(result)
  66. }
  67. }
  68.  
  69. class Map{
  70. let lines: [MapLine]
  71. var player: Player!
  72.  
  73. init(input: String...) {
  74. var ls: [MapLine] = []
  75. for line in input{
  76. ls.append(MapLine(input: line))
  77. }
  78. lines = ls
  79. }
  80.  
  81. func printMap(){
  82. for (y, line) in lines.enumerated(){
  83. line.printLine(player: player.y == y ? player : nil)
  84. }
  85. }
  86.  
  87. private func objectAt(x: Int, y: Int) -> Object?{
  88. if y < 0 || y >= lines.count { return nil }
  89. let line = lines[y]
  90. if x < 0 || x >= line.objects.count { return nil }
  91. return line.objects[x]
  92. }
  93.  
  94. private func printDirection(direction: String, dx: Int, dy: Int){
  95. let nx = player.x + dx
  96. let ny = player.y + dy
  97. guard let object = objectAt(x: nx, y: ny), !object.isSolid else { return }
  98. print("\(direction): \nlet x = \(nx)\nlet y = \(ny)")
  99. }
  100.  
  101. func printOptions(){
  102. printDirection(direction: "right", dx: 1, dy: 0)
  103. printDirection(direction: "left", dx: -1, dy: 0)
  104. printDirection(direction: "up", dx: 0, dy: -1)
  105. printDirection(direction: "down", dx: 0, dy: 1)
  106. }
  107. }
  108.  
  109. let map = Map(input:
  110. "xxxxxxxxxxxxxx",
  111. "x xxx",
  112. "xx xx xx xx",
  113. "x x",
  114. "xxxxxxxxxxxxxx"
  115. )
  116. map.player = Player(x: x, y: y)
  117. map.printMap()
  118. map.printOptions()
  119.  
  120.  
  121.  
  122.  
Success #stdin #stdout 0s 57656KB
stdin
Standard input is empty
stdout
xxxxxxxxxxxxxx
x          xxx
xx xx   xx  xx
x  @         x
xxxxxxxxxxxxxx
right: 
let x = 4
let y = 3
left: 
let x = 2
let y = 3