• Stars
    star
    3,830
  • Rank 11,482 (Top 0.3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 16 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Game written in Ruby for learning Ruby.

Ruby Warrior

This is a game designed to teach the Ruby language in a fun and interactive way.

You play as a warrior climbing a tall tower to reach the precious Ruby at the top level. On each floor you need to write a Ruby script to instruct the warrior to battle enemies, rescue captives, and reach the stairs. You have some idea of what each floor contains, but you never know for certain what will happen. You must give the Warrior enough artificial intelligence up-front to find his own way.

Note: The player directory structure changed on July 18, 2009. If you have an old profile using the level-00* structure then move the contents of the last level into the parent directory.

Getting Started

First install the gem.

gem install rubywarrior

As of version 0.2.0, this gem has been updated to work with Ruby 3.3. If you have difficulty running it in an older version of Ruby, try using an older version of the gem.

Then run the rubywarrior command to setup your profile. This will create a rubywarrior directory in your current location where you will find a player.rb file in your profile's directory containing this:

class Player
  def play_turn(warrior)
    # your code goes here
  end
end

Your objective is to fill this play_turn method with commands to instruct the warrior what to do. With each level your abilities will grow along with the difficulty. See the README in your profile's directory for details on what abilities your warrior has available on the current level.

Here is a simple example which will instruct the warrior to attack if he feels an enemy, otherwise he will walk forward.

class Player
  def play_turn(warrior)
    if warrior.feel.enemy?
      warrior.attack!
    else
      warrior.walk!
    end
  end
end

Once you are done editing player.rb, save the file and run the rubywarrior command again to start playing the level. The play happens through a series of turns. On each one, your play_turn method is called along with any enemy's.

You cannot change your code in the middle of a level. You must take into account everything that may happen on that level and give your warrior the proper instructions from the start.

Losing all of your health will cause you to fail the level. You are not punished by this, you simply need to go back to your player.rb, improve your code, and try again.

Once you pass a level (by reaching the stairs), the profile README will be updated for the next level. Alter the player.rb file and run rubywarrior again to play the next level.

Scoring

Your objective is to not only reach the stairs, but to get the highest score you can. There are many ways you can earn points on a level.

  • defeat an enemy to add his max health to your score
  • rescue a captive to earn 20 points
  • pass the level within the bonus time to earn the amount of bonus time remaining
  • defeat all enemies and rescue all captives to receive a 20% overall bonus

A total score is kept as you progress through the levels. When you pass a level, that score is added to your total.

Don't be too concerned about scoring perfectly in the beginning. After you reach the top of the tower you will be able to re-run the tower and fine-tune your warrior to get the highest score. See the Epic Mode below for details.

Perspective

Even though this is a text-based game, think of it as two-dimensional where you are viewing from overhead. Each level is always rectangular in shape and is made up of a number of squares. Only one unit can be on a given square at a time, and your objective is to find the square with the stairs. Here is an example level map and key.

 ----
|C s>|
| S s|
|C @ |
 ----

> = Stairs
@ = Warrior (20 HP)
s = Sludge (12 HP)
S = Thick Sludge (24 HP)
C = Captive (1 HP)

Commanding the Warrior

When you first start, your warrior will only have a few abilities, but with each level your abilities will grow. A warrior has two kinds of abilities: actions and senses.

An action is something that effects the game in some way. You can easily tell an action because it ends in an exclamation mark. Only one action can be performed per turn, so choose wisely. Here are some examples of actions.

warrior.walk!
  Move in given direction (forward by default).

warrior.attack!
  Attack the unit in given direction (forward by default).

warrior.rest!
  Gain 10% of max health back, but do nothing more.

warrior.bind!
  Bind unit in given direction to keep him from moving (forward by default).

warrior.rescue!
  Rescue a captive from his chains (earning 20 points) in given direction (forward by default).

A sense is something which gathers information about the floor. You can perform senses as often as you want per turn to gather information about your surroundings and to aid you in choosing the proper action. Senses do NOT end in an exclamation mark.

warrior.feel
  Returns a Space for the given direction (forward by default).

warrior.health
  Returns an integer representing your health.

warrior.distance
  Returns the number of spaces the stairs are away.

warrior.listen
  Returns an array of all spaces which have units in them.

Since what you sense will change each turn, you should record what information you gather for use on the next turn. For example, you can determine if you are being attacked if your health has gone down since the last turn.

Spaces

Whenever you sense an area, often one or multiple spaces (in an array) will be returned. A space is an object representing a square in the level. You can call methods on a space to gather information about what is there. Here are the various methods you can call on a space.

space.empty?
  If true, this means that nothing (except maybe stairs) is at this location and you can walk here.

space.stairs?
  Determine if stairs are at that location

space.enemy?
  Determine if an enemy unit is at this location.

space.captive?
  Determine if a captive is at this location.

space.wall?
  Returns true if this is the edge of the level. You can't walk here.

space.ticking?
  Returns true if this space contains a bomb which will explode in time.

space.golem?
  Returns true if a golem is occupying this space.

You will often call these methods directly after a sense. For example, the feel sense returns one space. You can call captive? on this to determine if a captive is in front of you.

warrior.feel.captive?

Golem

Along your journey you may discover the ability to create a golem. This is a separate unit which you also control. The turn handling is done through a block. Here is an example.

warrior.form! do |golem|
  golem.attack! if golem.feel.enemy?
end

Complex logic can be placed in this block just like in the player turn method. You may want to move the logic into its own class or create multiple classes for different types of golems. You can create multiple golems in a level, but each one will take half of the warrior's health.

Epic Mode

Once you reach the top of the tower, you will enter epic mode. When running rubywarrior again it will run your current player.rb through all levels in the tower without stopping.

Your warrior will most likely not succeed the first time around, so use the -l option on levels you are having difficulty or want to fine-tune the scoring.

rubywarrior -l 3

Once your warrior reaches the top again you will receive an average grade, along with a grade for each level. The grades from best to worst are S, A, B, C, D and F. Try to get S on each level for the ultimate score.

Note: I'm in the process of fine-tuning the grading system. If you find the S grade to be too easy or too difficult to achieve on a given level, please add an issue for this on GitHub.

Tips

If you ever get stuck on a level, review the README documentation and be sure you're trying each ability out. If you can't keep your health up, be sure to rest when no enemy is around (while keeping an eye on your health). Also, try to use far-ranged weapons whenever possible (such as the bow).

Remember, you're working in Ruby here. Don't simply fill up the play_turn method with a lot of code. Organize it with methods and classes. The player directory is set up as a load path so you can include other ruby files from your player.rb file.

Senses are cheap, so use them liberally. Store the sensed information to help you better determine what actions to take in the future.

Running rubywarrior while you are in your profile directory will auto-select that profile so you don't have to each time.

If you're aiming for points, remember to sweep the area. Even if you're close to the stairs, don't go in until you've gotten everything (if you have the health). Use far-ranged senses (such as look and listen) to determine if there are any enemies left.

Make sure to try the different options you can pass to the rubywarrior command. Run rubywarrior --help to see them all.

More Repositories

1

cancan

Authorization Gem for Ruby on Rails.
Ruby
6,265
star
2

letter_opener

Preview mail in the browser instead of sending.
Ruby
3,686
star
3

dotfiles

config files for zsh, bash, completions, gem, git, irb, rails
Shell
2,312
star
4

nifty-generators

A collection of useful Rails generator scripts.
Ruby
1,984
star
5

nested_form

Rails plugin to conveniently handle multiple models in a single form.
Ruby
1,792
star
6

private_pub

Handle pub/sub messaging through private channels in Rails using Faye.
Ruby
864
star
7

railscasts-episodes

NOT MAINTAINED. See README.
Ruby
846
star
8

railscasts

railscasts.com in open source (outdated).
Ruby
760
star
9

populator

Mass populate an Active Record database.
Ruby
393
star
10

complex-form-examples

Various ways to handle multi-model forms in Rails.
Ruby
304
star
11

trusted-params

Rails plugin for overriding attr_accessible protection.
Ruby
149
star
12

mustard

Simple "must" expectations for tests and specs in Ruby.
Ruby
144
star
13

govsgo

Rails 3 app for playing the board game Go online.
Ruby
140
star
14

xapit

High level Ruby library for interacting with Xapian, a full text search engine.
Ruby
139
star
15

rails-templates

Template scripts for creating new rails applications.
Ruby
134
star
16

cocoa-web-app-example

A Cocoa application to demonstrate the interaction between Objective-C and JavaScript in a WebView.
Objective-C
96
star
17

importex

Import an Excel file using Ruby.
Ruby
90
star
18

uniquify

Generate a unique, random token for Active Record.
Ruby
87
star
19

textmate-themes

My TextMate themes (includes Railscasts theme)
69
star
20

acts-as-list

NOT MAINTAINED. Gem version of acts_as_list Rails plugin.
Ruby
65
star
21

abingo

Fork of A/Bingo plugin for Rails.
Ruby
55
star
22

railscasts-scripts

Scripts used internally when producing RailsCasts
Ruby
52
star
23

scope-builder

Build up named scopes conditionally.
Ruby
51
star
24

rmov

Ruby wrapper for the QuickTime C API.
C
48
star
25

render-caching

Cache render calls in Rails controllers.
Ruby
45
star
26

enlighten

Interactive ruby debugger in the browser.
Ruby
41
star
27

static_actions

Rails plugin to quickly make named routes for non-RESTful actions.
Ruby
39
star
28

searchify

Rails plugin to add extra searching functionality to models.
Ruby
37
star
29

selenium-on-rails

This repo is no longer maintained, see the official repository by paytonrules.
JavaScript
34
star
30

ryan-on-rails.tmbundle

Some TextMate snippets I use when working with Ruby and Rails.
26
star
31

dailystamp

Source code for my Rails Rumble 2009 submission
Ruby
23
star
32

url_formatter

Format and validate a URL in Active Record. Example gem for RailsCasts.
Ruby
18
star
33

association-freezer

Freeze a belongs_to association in Active Record.
Ruby
17
star
34

admiteer

Rails Rumble 2007 project by Jack Canty, Kelli Shaver, and Ryan Bates
17
star
35

todo-list.tmbundle

A simple TextMate bundle to manage a todo lists.
14
star
36

myideadrawer

Rails Rumble 2008 entry by Ryan Bates and Kelli Shaver
Ruby
13
star
37

advent-2022

Advent of Code in Elixir
Elixir
12
star
38

blog-screencast

Example blog application built in the offical 15 minute Rails screencast.
Ruby
12
star
39

xapit-sync

Rails plugin to automatically reload a Xapian database when models change.
Ruby
12
star
40

ryan-bates.tmbundle

Miscellaneous commands and snippets I use in TextMate.
11
star
41

maestro

Piano exercise game written in MacRuby.
Ruby
11
star
42

vscode-railscasts-theme

RailsCasts Theme for VS Code
7
star
43

ryanb.github.io

Personal site for Ryan Bates
5
star
44

vscode-erb-syntax

ERB Syntax for VS Code
5
star
45

xapit-server

Rack server for interacting with a Xapian database remotely through Xapit.
Ruby
4
star
46

swapper

Ruby script for swapping two elements on a line (to be used in text editors).
3
star
47

bookmarklets

JavaScript
1
star
48

wallaby-rails-7-1-2

Example Rails 7.1.2 app with Wallaby
Ruby
1
star