-
-
Save apneadiving/8834c88e0b85d9b86093f1611d987870 to your computer and use it in GitHub Desktop.
ruby warrior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LEVEL 9 | |
class Player { | |
constructor() { | |
// This code will be executed only once, at the beginning of the level. | |
this.health = 20; | |
this.isRescued = 0 | |
} | |
/** | |
* Plays a warrior turn. | |
* | |
* @param {Warrior} warrior The warrior. | |
*/ | |
playTurn(warrior) { | |
if (this.isRescued == 1) { | |
this.usualDrill(warrior, 'forward') | |
} | |
else { | |
this.usualDrill(warrior, 'backward') | |
} | |
this.health = warrior.health() | |
} | |
usualDrill(warrior, direction) { | |
var currentHealth = warrior.health() | |
var space = warrior.feel(direction) | |
if (space.isEmpty()) { | |
if (currentHealth < 20) { | |
if (currentHealth < this.health) { // taking damages | |
if (currentHealth < 13) { | |
warrior.walk(this.oppositeDirection(direction)) | |
} | |
else { | |
warrior.walk(direction) | |
} | |
} | |
else { | |
warrior.rest(direction) | |
} | |
} | |
else { | |
if (this.isEnemyInSight(warrior, direction)) { | |
warrior.shoot(direction) | |
} | |
else { | |
warrior.walk(direction) | |
} | |
} | |
} | |
else { | |
if (space.getUnit()) { | |
if (space.getUnit().isEnemy()) { | |
if (currentHealth < 5) { | |
warrior.walk(this.oppositeDirection(direction)) | |
} | |
else { | |
warrior.attack(direction) | |
} | |
} | |
else { | |
warrior.rescue(direction) | |
this.isRescued += 1 | |
} | |
} | |
else { | |
warrior.pivot() | |
} | |
} | |
} | |
oppositeDirection(direction) { | |
if (direction == 'forward') { return 'backward'} | |
else { return 'forward'} | |
} | |
isEnemyInSight(warrior, direction) { | |
const spaceWithUnit = warrior.look(direction).find(space => space.isUnit()); | |
return spaceWithUnit && spaceWithUnit.getUnit().isEnemy(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LEVEL 2 | |
class Player | |
def play_turn(warrior) | |
if warrior.feel.enemy? | |
warrior.attack! | |
else | |
warrior.walk! | |
end | |
end | |
end | |
# LEVEL 3 | |
class Player | |
def play_turn(warrior) | |
if warrior.feel.enemy? | |
warrior.attack! | |
elsif warrior.health <= 8 | |
warrior.rest! | |
else | |
warrior.walk! | |
end | |
end | |
end | |
# LEVEL 4 | |
class Player | |
def play_turn(warrior) | |
if warrior.feel.enemy? | |
warrior.attack! | |
elsif warrior.health <= 8 | |
warrior.rest! | |
else | |
warrior.walk! | |
end | |
end | |
end | |
#LEVEL 5 | |
class Player | |
def play_turn(warrior) | |
if warrior.feel.enemy? | |
warrior.attack! | |
elsif warrior.health <= 15 && warrior.health >= @health | |
warrior.rest! | |
else | |
warrior.walk! | |
end | |
@health = warrior.health | |
end | |
end | |
#LEVEL 6 | |
class Player | |
def play_turn(warrior) | |
if warrior.feel.enemy? | |
warrior.attack! | |
elsif warrior.feel.captive? | |
warrior.rescue! | |
elsif warrior.health <= 15 && warrior.health >= @health | |
warrior.rest! | |
else | |
warrior.walk! | |
end | |
@health = warrior.health | |
end | |
end | |
#LEVEL 7 | |
class Player | |
def play_turn(warrior) | |
if defined?(@rescued_friend) | |
if warrior.health <= 7 && warrior.health < @health | |
warrior.walk!(:backward) | |
elsif warrior.feel.enemy? | |
warrior.attack! | |
elsif warrior.feel.captive? | |
warrior.rescue! | |
elsif warrior.health < 20 && warrior.health >= @health | |
warrior.rest! | |
else | |
warrior.walk! | |
end | |
@health = warrior.health | |
else | |
if warrior.feel(:backward).captive? | |
warrior.rescue!(:backward) | |
@rescued_friend = true | |
else | |
warrior.walk!(:backward) | |
end | |
end | |
end | |
end | |
#LEVEL 8 | |
class Player | |
def play_turn(warrior) | |
if defined?(@pivoted) | |
if warrior.health <= 7 && warrior.health < @health | |
warrior.walk!(:backward) | |
elsif warrior.feel.enemy? | |
warrior.attack! | |
elsif warrior.feel.captive? | |
warrior.rescue! | |
elsif warrior.health <= 19 && warrior.health >= @health | |
warrior.rest! | |
else | |
warrior.walk! | |
end | |
@health = warrior.health | |
else | |
warrior.pivot! | |
@pivoted = true | |
end | |
end | |
end | |
#LEVEL 9 | |
class Player | |
def play_turn(warrior) | |
if warrior.health <= 7 && warrior.health < @health | |
warrior.walk!(:backward) | |
elsif warrior.look.any?(&:enemy?) && warrior.look.none?(&:captive?) | |
warrior.shoot! | |
elsif warrior.feel.captive? | |
warrior.rescue! | |
elsif warrior.health <= 19 && warrior.health < @health | |
warrior.rest! | |
else | |
warrior.walk! | |
end | |
@health = warrior.health | |
end | |
end | |
#LEVEL 10 | |
class Player | |
def play_turn(warrior) | |
if never_moved? | |
warrior.walk! | |
@moved = true | |
elsif warrior.feel.wall? | |
warrior.pivot! | |
elsif critical?(warrior.health) && !safe?(warrior.health) | |
warrior.walk!(:backward) | |
elsif warrior.look.any?(&:enemy?) | |
if warrior.feel.enemy? | |
warrior.attack! | |
else | |
warrior.shoot! | |
end | |
elsif warrior.feel.captive? | |
warrior.rescue! | |
elsif warrior.health <= 19 && safe?(warrior.health) | |
warrior.rest! | |
else | |
warrior.walk! | |
end | |
@health = warrior.health | |
end | |
def critical?(current_health) | |
current_health <= 4 | |
end | |
def safe?(current_health) | |
current_health >= @health | |
end | |
def never_moved? | |
@moved.nil? | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment