Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- var map = "10,5\n++++++++++\n+-P------+\n+--------+\n+-----o--+\n++++++++++\n"
- func parseData(mapString: String) -> Data {
- var converted = mapString.components(separatedBy: "\n").dropLast()
- var dict = [String:String]()
- let widthHeight = converted[0].components(separatedBy: ",")
- dict["width"] = widthHeight[0]
- dict["height"] = widthHeight[1]
- converted.remove(at: 0)
- dict["data"] = converted.joined()
- let json = try! JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
- return json
- }
- func drawMap(json: Data) {
- let decoded = try! JSONSerialization.jsonObject(with: json, options: [])
- let dictFromJson = decoded as! [String:String]
- let tempArray = Array(dictFromJson["data"]!.characters)
- print(Array(dictFromJson["data"]!.characters))
- for index in 0...tempArray.count - 1 {
- if tempArray[index] == "-" {
- print(" ", terminator: "")
- } else {
- print(tempArray[index], terminator: "")
- }
- if (index + 1) % Int(dictFromJson["width"]!)! == 0 {
- print()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement