Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ===========================================
- // CODE FOURNI
- // ===========================================
- /**
- Permet de placer 2 blocs à la position souhaitée
- */
- func placeTwoBlocks(pos: Coordinate) {
- world.place(Block(), at: pos)
- world.place(Block(), at: pos)
- }
- /**
- Fonction permettant de créer un triangle de murs à une position souhaitée dans une des 4 orientation possibles
- */
- func drawTriangle(pos: Coordinate, orientation: Int) {
- placeTwoBlocks(pos: pos)
- switch orientation {
- case 1: // -|
- placeTwoBlocks(pos: Coordinate(column: pos.column - 1, row: pos.row))
- placeTwoBlocks(pos: Coordinate(column: pos.column, row: pos.row - 1))
- break
- case 2: // |-
- placeTwoBlocks(pos: Coordinate(column: pos.column + 1, row: pos.row))
- placeTwoBlocks(pos: Coordinate(column: pos.column, row: pos.row - 1))
- break
- case 3: // _|
- placeTwoBlocks(pos: Coordinate(column: pos.column - 1, row: pos.row))
- placeTwoBlocks(pos: Coordinate(column: pos.column, row: pos.row + 1))
- break
- default: // |_
- placeTwoBlocks(pos: Coordinate(column: pos.column + 1, row: pos.row))
- placeTwoBlocks(pos: Coordinate(column: pos.column, row: pos.row + 1))
- break
- }
- }
- // Création de tous les murs du labyrinthe
- drawTriangle(pos: Coordinate(column: 1, row: 2), orientation: 1)
- drawTriangle(pos: Coordinate(column: 1, row: 5), orientation: 4)
- drawTriangle(pos: Coordinate(column: 3, row: 8), orientation: 3)
- drawTriangle(pos: Coordinate(column: 3, row: 0), orientation: 4)
- drawTriangle(pos: Coordinate(column: 4, row: 3), orientation: 1)
- drawTriangle(pos: Coordinate(column: 4, row: 6), orientation: 2)
- drawTriangle(pos: Coordinate(column: 5, row: 4), orientation: 2)
- drawTriangle(pos: Coordinate(column: 6, row: 0), orientation: 4)
- drawTriangle(pos: Coordinate(column: 6, row: 7), orientation: 1)
- drawTriangle(pos: Coordinate(column: 8, row: 2), orientation: 4)
- drawTriangle(pos: Coordinate(column: 9, row: 8), orientation: 1)
- // Placement des téléporteurs
- 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)
- // Placement de la gemme finale
- world.place(Gem(), at: Coordinate(column: 9, row: 3))
- // Création du personnage
- let character = Character(name: .hopper)
- world.place(character, facing: east, at: Coordinate(column: 0, row: 0))
- /**
- Fonction permettant de se déplacer d'une certaine distance et de se tourner ensuite soit à gauche soit à droite.
- On peut également répéter les 2 opérations en modifiant la valeur de "recurrence".
- */
- func moveAndTurn(direction: String, dist: Int, recurrence: Int = 1) {
- for i in 1 ... recurrence {
- character.move(distance: dist)
- if(direction == "Left") {
- character.turnLeft()
- } else {
- character.turnRight()
- }
- }
- }
- // ===========================================
- // ECRIRE VOTRE CODE EN DESSOUS
- // ===========================================
- // 1. Déplacement du personnage jusqu'à la gemme
- // 2. Version plus optimisée du déplacement
- // ===========================================
- // SOLUTION
- // ===========================================
- // Indication: Pour trouver le chemin le plus rapide, prenez bien en compte le téléporteur.
- // 1. Déplacement du personnage
- /*character.move(distance: 2)
- character.turnLeft()
- character.move(distance: 4)
- character.turnRight()
- character.move(distance: 4)
- character.turnLeft()
- character.move(distance: 2)
- character.turnRight()
- character.move(distance: 2)
- character.turnRight()
- character.move(distance: 1)
- character.collectGem()*/
- // 2. Version du déplacement optimisée
- /*moveAndTurn(direction: "Left", dist: 2)
- moveAndTurn(direction: "Right", dist: 4)
- moveAndTurn(direction: "Left", dist: 4)
- moveAndTurn(direction: "Right", dist: 2, recurrence: 2)
- character.move(distance: 1)
- character.collectGem()*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement