Ruby Warrior是一个用Ruby代码控制一个小人打怪升级刷装备的小游戏,可以帮助小伙伴们了解Ruby
传送门
最近正好有同事想了解一下Ruby,就推荐给他们这个小游戏,同时把我的答案也分享一下,大家可以交流一下更漂亮的写法。
# level 1
class Player
def play_turn(warrior)
warrior.walk!
end
end
# level 2
class Player
def play_turn(warrior)
warrior.feel.empty? ? warrior.walk! : warrior.attack!
end
end
# level 3
class Player
def play_turn(warrior)
if warrior.feel.empty?
warrior.health < 10 ? warrior.rest! : warrior.walk!
else
warrior.attack!
end
end
end
# level 4
class Player
def play_turn(warrior)
@health ||= warrior.health
if warrior.feel.empty?
warrior.health < 15 && @health <= warrior.health ? warrior.rest! : warrior.walk!
else
warrior.attack!
end
@health = warrior.health
end
end
# level 5
class Player
def play_turn(warrior)
@health ||= warrior.health
if warrior.feel.empty?
warrior.health < 15 && @health <= warrior.health ? warrior.rest! : warrior.walk!
else
warrior.feel.enemy? ? warrior.attack! : warrior.rescue!
end
@health = warrior.health
end
end
# level 6
class Player
def play_turn(warrior)
@backward = true if @backward.nil?
if @backward
if warrior.feel(:backward).captive?
@backward = false
return warrior.rescue! :backward
else
return warrior.walk! :backward
end
end
@health ||= warrior.health
if warrior.feel.empty?
if warrior.health < 20 && @health <= warrior.health
warrior.rest!
else
direction = warrior.health < 10 ? :backward : :forward
warrior.walk! direction
end
else
warrior.feel.enemy? ? warrior.attack! : warrior.rescue!
end
@health = warrior.health
end
end
# level 7
class Player
def play_turn(warrior)
@health ||= warrior.health
if warrior.feel.empty?
if warrior.health < 20 && @health <= warrior.health
warrior.rest!
else
direction = warrior.health < 10 ? :backward : :forward
warrior.walk! direction
end
else
if warrior.feel.enemy?
warrior.attack!
elsif warrior.feel.wall?
warrior.pivot!
else
warrior.rescue!
end
end
@health = warrior.health
end
end
# level 8
class Player
def play_turn(warrior)
warrior.look.each do |space|
break if space.captive?
return warrior.shoot! if space.enemy?
end
warrior.feel.captive? ? warrior.rescue! : warrior.walk!
end
end
# level 9
class Player
def initialize
@first_turn, @archer_dead, @melee = true, false, false
end
def archer_dead?; @archer_dead; end
def play_turn(warrior)
# Kill archer with hardcode
if @first_turn
@first_turn = false
return warrior.pivot!
end
if !archer_dead?
if warrior.feel.enemy?
@melee = true
return warrior.attack!
else # feel.empty?
if @melee
@archer_dead = true
return warrior.pivot!
else
return warrior.walk!
end
end
end
warrior.look.each do |space|
break if space.captive?
return warrior.shoot! if space.enemy?
end
if warrior.feel.captive?
warrior.rescue!
elsif warrior.feel.wall?
warrior.pivot!
else
warrior.walk!
end
end
end
分享到:
相关推荐
Ruby Warrior 是一个游戏,它能让 Ruby 语言和人工智能以一种很有意思的方式互动。你所扮演的是一个爬上高塔顶层获得 Ruby 宝石的战士。每到一层你都要写下 Ruby 脚本指导战士与敌人战斗,拯救俘虏,获取梯子。你在...
codewarrior破解license
本手册详细介绍了利用Freescale CodeWarrior 10.6 IDE 处理器专家系统(Processor Expert)快速建立KEA工程和调试的步骤,以及该IDE常用的编程及调试技巧,旨在帮助用户快速熟悉和掌握CodeWarrior 10.6的使用,利用...
视频介绍codewarrior 10.5创建工程的方法 后续上传资源包括codewarrior的烧写和debug,是以此为基础的。
官网免费codewarrior5.2 官网免费codewarrior5.2 官网免费codewarrior5.2
codewarrior快速入门介绍了codewarrior基本用法
CodeWarrior5.1特别版,适用于飞思卡尔s12系列芯片
CodeWarrior for S12(X) V5.1是用来编写飞思卡尔系列单片机程序的集成开发工具,根据收费情况可以分为收费版和免费版,其中免费版可以分为评估板和特别版。评估板的codewarrior可以无限制的使用30天,而特别版则没有...
CodeWarrior IDE入门指导.pdf
CodeWarrior 使用教程 CodeWarrior 使用教程 CodeWarrior 使用教程
详细介绍codewarrior的使用,主要介绍codewarrior的功能,使用方法已经一些使用案例
本例程为飞思卡尔编译软件codewarrior生成库例程,适合做程序保护等场合使用
CodeWarrior for S12(X) V4.7是用来编写飞思卡尔系列单片机程序的集成开发工具,根据收费情况可以分为收费版和免费版,其中免费版可以分为评估板和特别版。评估板的codewarrior可以无限制的使用30天,而特别版则没有...
codewarrior安装需要许可文件,中间可能会遇到一些麻烦,此文件说明安装codewarrior后配置机器设置
codewarrior使用说明文档中文 CodeWarrior Development Studio(开发工作室)是完整的用于编程应用中硬件bring-up的集成开发环境。 采用CodeWarrior IDE,开发人员可以得益于采用各种处理器和平台(从Motorola到TI到...
codewarrior 10.5 录制的调试方法,和资源里其他两个视频配套的。有问题可以私信我~
1.IDE.exe是CodeWarrior v5.1 special的主程序,进行了图标替换,美化其主程序和关联文件图标 2.MyCodeWarriorComponent是一些调试时很有用的组件,v5.1所没有的仿真组件(比如LCD,Io_led,IT_keyboard)在该目录下 3...
CodeWarrior使用教程
CodeWarrior使用教程
CodeWarrior license 集合