Тема:
Циклы
Условные операторы
Переменные
Строки
Арифметика
Оператор «break»
Цели:
Доложите о побеждённых ограх в первой волне.
Доложите о собранном золоте.
Доложите о побеждённых ограх во второй волне.
Руководство:
Вступление:
Этот уровень имеет три фазы:
- Боритесь с ограми на протяжении 15 секунд.
- Собирайте монеты на протяжении 15 секунд.
- Бейте побольше огров на протяжении 15 секунд.
Сообщите Naria о ваших победах или о золоте после каждого этапа.
Используйте now() для проверки времени, и break для выхода из while цикла.
Обзор:
Вы не сможете выполнять действия до определённого времени, используй now() и break, как здесь:
Python:
1 2 3 4 |
while True: # Do something if self.now() > 15: break |
JavaScript:
1 2 3 4 5 6 |
while(true) { // Do something if(this.now() > 15) { break; } } |
CoffeScript:
1 2 3 4 |
while true # Do something if @now() > 15 break |
Clojure:
Код ниже это буквально набор букв, в Clojure нет break, так что каждый сам за себя. 🙂
По предыдущему примеру предлагалось просто делать условие и выполнить код в цикле, который должен идти после цикла.
1 2 3 4 5 6 7 |
(while true (when (> (.now this) 15) (when true ) ) ) |
LUA:
1 2 3 4 5 6 |
while true do if self:now() > 15 then break end end self:say("message") |
Так же вы должны сказать, сколько вы победили врагов, например так:
Python:
1 2 3 |
self.attack(enemy) if enemy.health <= 0: defeated += 1 |
JavaScript:
1 2 3 4 |
this.attack(enemy) if(enemy.health <= 0) { defeated += 1 } |
CoffeScript:
1 2 3 |
@attack(enemy) if enemy.health <= 0 defeated += 1 |
Clojure:
1 2 3 4 |
(.attack this enemy) (when (<= (.health enemy) 0) (def defeated (inc defeated)) ) |
LUA:
1 2 3 4 |
self:attack(enemy) if enemy.health<=0 then defeated = defeated + 1 end |
Награда:
Счетовод, прохождение:
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# Fight enemies for 15 seconds. # Keep count whenever an enemy is defeated. attainment = 0 while True: enemy = self.findNearestEnemy() if enemy: self.attack(enemy) if enemy.health <=0: attainment += 1 else: self.moveXY(58, 33) if self.now()>=15: break; # Tell Naria how many enemies you defeated. self.moveXY(58, 33) self.say(attainment) attainment=0 # Collect coins until the clock reaches 30 seconds. while True: item = self.findNearestItem() if item: self.moveXY(item.pos.x, item.pos.y) else: self.moveXY(58, 33) if self.now()>=30: break; # Tell Naria how much gold you collected. self.moveXY(58, 33) self.say(self.gold) # Fight enemies until the clock reaches 45 seconds. # Remember to reset the count of defeated enemies! attainment = 0; while True: enemy = self.findNearestEnemy() if enemy: self.attack(enemy) if enemy.health <=0: attainment += 1 else: self.moveXY(58, 33) if self.now()>=45: break; # Tell Naria how many enemies you defeated. self.moveXY(58, 33) self.say(attainment) |
JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
// Fight enemies for 15 seconds. // Keep count whenever an enemy is defeated. var attainment = 0; var enemy = this.findNearestEnemy(); while (true) { enemy = this.findNearestEnemy(); if (enemy){ this.attack(enemy); if (enemy.health <= 0) { attainment += 1; } } else { this.moveXY(58, 33); } if (this.now()>=15){ break; } } // Tell Naria how many enemies you defeated. this.moveXY(58, 33); this.say(attainment); attainment=0; // Collect coins until the clock reaches 30 seconds. while (true) { var item = this.findNearestItem(); if (item){ this.moveXY(item.pos.x, item.pos.y); } else { this.moveXY(58, 33); } if (this.now()>=30){ break; } } // Tell Naria how much gold you collected. this.moveXY(58, 33); this.say(this.gold); // Fight enemies until the clock reaches 45 seconds. // Remember to reset the count of defeated enemies! attainment = 0; while (true) { enemy = this.findNearestEnemy(); if (enemy){ this.attack(enemy); if (enemy.health <= 0) { attainment += 1; } } else { this.moveXY(58, 33); } if (this.now()>=45){ break; } } // Tell Naria how many enemies you defeated. this.moveXY(58, 33); this.say(attainment); |
CoffeScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# Fight enemies for 15 seconds. # Keep count whenever an enemy is defeated. attainment = 0 while true enemy = @findNearestEnemy() if enemy @attack enemy if enemy.health<=0 attainment +=1 else @moveXY 58, 33 if @now()>=15 break # Tell Naria how many enemies you defeated. @moveXY 58, 33 @say attainment # Collect coins until the clock reaches 30 seconds. while true item = @findNearestItem() if item @moveXY item.pos.x, item.pos.y else @moveXY 58, 33 if @now()>=30 break # Tell Naria how much gold you collected. @moveXY 58, 33 @say @gold # Fight enemies until the clock reaches 45 seconds. # Remember to reset the count of defeated enemies! attainment = 0 while true enemy = @findNearestEnemy() if enemy @attack enemy if enemy.health<=0 attainment +=1 else @moveXY 58, 33 if @now()>=45 break # Tell Naria how many enemies you defeated. @moveXY 58, 33 @say attainment |
Clojure:
Повторюсь break в clojure нет. я реализовал переменную break равную нулю, и пока она равна нулю цикл идёт. Когда надо прервать цикл я назначаю ей значение 1, и цикл прерывает свою работу.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
;; Fight enemies for 15 seconds. ;; Keep count whenever an enemy is defeated. (def attainment 0) (def break 0) (while (= break 0) (def enemy (.findNearestEnemy this)) (if enemy (when true (.attack this enemy) (when (<= (.health enemy) 0) (def attainment (inc attainment)) )) (.moveXY this 58 33)) (when (>= (.now this) 15) (def break 1) ) ) ;; Tell Naria how many enemies you defeated. (.moveXY this 58 33) (.say this attainment) (def break 0) ;; Collect coins until the clock reaches 30 seconds. (while (= break 0) (def item (.findNearestItem this)) (if item (.moveXY this (.x (.pos item)) (.y (.pos item))) (.moveXY this 58 33)) (when (>= (.now this) 30) (def break 1) ) ) ;; Tell Naria how much gold you collected. (.moveXY this 58 33) (.say this (.gold this)) ;; Fight enemies until the clock reaches 45 seconds. ;; Remember to reset the count of defeated enemies! (def attainment 0) (def break 0) (while (= break 0) (def enemy (.findNearestEnemy this)) (if enemy (when true (.attack this enemy) (when (<= (.health enemy) 0) (def attainment (+ attainment 1)) )) (.moveXY this 58 33)) (when (>= (.now this) 45) (def break 1) ) ) ;; Tell Naria how many enemies you defeated. (.moveXY this 58 33) (.say this attainment) |
LUA:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
-- Fight enemies for 15 seconds. -- Keep count whenever an enemy is defeated. attainment = 0 while true do enemy = self:findNearestEnemy() if enemy then self:attack(enemy) if enemy.health <= 0 then attainment= attainment +1 end else self:moveXY(58, 33) end if self:now()>=15 then break; end end -- Tell Naria how many enemies you defeated. self:moveXY(58, 33) self:say(attainment) -- Collect coins until the clock reaches 30 seconds. while true do item = self:findNearestItem() if item then self:moveXY(item.pos.x, item.pos.y) else self:moveXY(58, 33) end if self:now()>=30 then break; end end -- Tell Naria how much gold you collected. self:moveXY(58, 33) self:say(self.gold) -- Fight enemies until the clock reaches 45 seconds. -- Remember to reset the count of defeated enemies! attainment = 0 while true do enemy = self:findNearestEnemy() if enemy then self:attack(enemy) if enemy.health <= 0 then attainment= attainment +1 end else self:moveXY(58, 33) end if self:now()>=45 then break; end end -- Tell Naria how many enemies you defeated. self:moveXY(58, 33) self:say(attainment) |
Комментарии: