Advertisement
Guest User

SiftMierda

a guest
Mar 26th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. // ===========================================
  2. // CODE FOURNI
  3. // ===========================================
  4.  
  5. /**
  6. Permet de placer 2 blocs à la position souhaitée
  7. */
  8. func placeTwoBlocks(pos: Coordinate) {
  9. world.place(Block(), at: pos)
  10. world.place(Block(), at: pos)
  11. }
  12.  
  13. /**
  14. Fonction permettant de créer un triangle de murs à une position souhaitée dans une des 4 orientation possibles
  15. */
  16. func drawTriangle(pos: Coordinate, orientation: Int) {
  17. placeTwoBlocks(pos: pos)
  18.  
  19. switch orientation {
  20. case 1: // -|
  21. placeTwoBlocks(pos: Coordinate(column: pos.column - 1, row: pos.row))
  22. placeTwoBlocks(pos: Coordinate(column: pos.column, row: pos.row - 1))
  23. break
  24. case 2: // |-
  25. placeTwoBlocks(pos: Coordinate(column: pos.column + 1, row: pos.row))
  26. placeTwoBlocks(pos: Coordinate(column: pos.column, row: pos.row - 1))
  27. break
  28. case 3: // _|
  29. placeTwoBlocks(pos: Coordinate(column: pos.column - 1, row: pos.row))
  30. placeTwoBlocks(pos: Coordinate(column: pos.column, row: pos.row + 1))
  31. break
  32. default: // |_
  33. placeTwoBlocks(pos: Coordinate(column: pos.column + 1, row: pos.row))
  34. placeTwoBlocks(pos: Coordinate(column: pos.column, row: pos.row + 1))
  35. break
  36. }
  37. }
  38.  
  39. // Création de tous les murs du labyrinthe
  40. drawTriangle(pos: Coordinate(column: 1, row: 2), orientation: 1)
  41. drawTriangle(pos: Coordinate(column: 1, row: 5), orientation: 4)
  42. drawTriangle(pos: Coordinate(column: 3, row: 8), orientation: 3)
  43. drawTriangle(pos: Coordinate(column: 3, row: 0), orientation: 4)
  44. drawTriangle(pos: Coordinate(column: 4, row: 3), orientation: 1)
  45. drawTriangle(pos: Coordinate(column: 4, row: 6), orientation: 2)
  46. drawTriangle(pos: Coordinate(column: 5, row: 4), orientation: 2)
  47. drawTriangle(pos: Coordinate(column: 6, row: 0), orientation: 4)
  48. drawTriangle(pos: Coordinate(column: 6, row: 7), orientation: 1)
  49. drawTriangle(pos: Coordinate(column: 8, row: 2), orientation: 4)
  50. drawTriangle(pos: Coordinate(column: 9, row: 8), orientation: 1)
  51.  
  52. // Placement des téléporteurs
  53. world.place(Portal(color: #colorLiteral(red: 0.5568627715110779, green: 0.3529411852359772, blue: 0.9686274528503418, alpha: 1.0)), atStartColumn: 4, startRow: 4, atEndColumn: 5, endRow: 2)
  54.  
  55. // Placement de la gemme finale
  56. world.place(Gem(), at: Coordinate(column: 9, row: 3))
  57.  
  58. // Création du personnage
  59. let character = Character(name: .hopper)
  60. world.place(character, facing: east, at: Coordinate(column: 0, row: 0))
  61.  
  62. /**
  63. Fonction permettant de se déplacer d'une certaine distance et de se tourner ensuite soit à gauche soit à droite.
  64. On peut également répéter les 2 opérations en modifiant la valeur de "recurrence".
  65. */
  66. func moveAndTurn(direction: String, dist: Int, recurrence: Int = 1) {
  67. for i in 1 ... recurrence {
  68. character.move(distance: dist)
  69. if(direction == "Left") {
  70. character.turnLeft()
  71. } else {
  72. character.turnRight()
  73. }
  74. }
  75. }
  76.  
  77. // ===========================================
  78. // ECRIRE VOTRE CODE EN DESSOUS
  79. // ===========================================
  80.  
  81. // 1. Déplacement du personnage jusqu'à la gemme
  82.  
  83.  
  84.  
  85.  
  86.  
  87. // 2. Version plus optimisée du déplacement
  88.  
  89.  
  90.  
  91.  
  92.  
  93. // ===========================================
  94. // SOLUTION
  95. // ===========================================
  96.  
  97. // Indication: Pour trouver le chemin le plus rapide, prenez bien en compte le téléporteur.
  98.  
  99. // 1. Déplacement du personnage
  100.  
  101. /*character.move(distance: 2)
  102. character.turnLeft()
  103. character.move(distance: 4)
  104. character.turnRight()
  105. character.move(distance: 4)
  106. character.turnLeft()
  107. character.move(distance: 2)
  108. character.turnRight()
  109. character.move(distance: 2)
  110. character.turnRight()
  111. character.move(distance: 1)
  112. character.collectGem()*/
  113.  
  114.  
  115. // 2. Version du déplacement optimisée
  116.  
  117. /*moveAndTurn(direction: "Left", dist: 2)
  118. moveAndTurn(direction: "Right", dist: 4)
  119. moveAndTurn(direction: "Left", dist: 4)
  120. moveAndTurn(direction: "Right", dist: 2, recurrence: 2)
  121. character.move(distance: 1)
  122. character.collectGem()*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement