Skip to content

Commit

Permalink
initial commit with some of tests completed, only gone through a few …
Browse files Browse the repository at this point in the history
…so far
  • Loading branch information
javierjulio committed May 29, 2011
0 parents commit 87dcdc3
Show file tree
Hide file tree
Showing 44 changed files with 3,759 additions and 0 deletions.
66 changes: 66 additions & 0 deletions GREED_RULES.txt
@@ -0,0 +1,66 @@
= Playing Greed

Greed is a dice game played among 2 or more players, using 5
six-sided dice.

== Playing Greed

Each player takes a turn consisting of one or more rolls of the dice.
On the first roll of the game, a player rolls all five dice which are
scored according to the following:

Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 points

A single die can only be counted once in each roll. For example,
a "5" can only count as part of a triplet (contributing to the 500
points) or as a single 50 points, but not both in the same roll.

Example Scoring

Throw Score
--------- ------------------
5 1 3 4 1 50 + 2 * 100 = 250
1 1 1 3 1 1000 + 100 = 1100
2 4 4 5 4 400 + 50 = 450

The dice not contributing to the score are called the non-scoring
dice. "3" and "4" are non-scoring dice in the first example. "3" is
a non-scoring die in the second, and "2" is a non-score die in the
final example.

After a player rolls and the score is calculated, the scoring dice are
removed and the player has the option of rolling again using only the
non-scoring dice. If all of the thrown dice are scoring, then the
player may roll all 5 dice in the next roll.

The player may continue to roll as long as each roll scores points. If
a roll has zero points, then the player loses not only their turn, but
also accumulated score for that turn. If a player decides to stop
rolling before rolling a zero-point roll, then the accumulated points
for the turn is added to his total score.

== Getting "In The Game"

Before a player is allowed to accumulate points, they must get at
least 300 points in a single turn. Once they have achieved 300 points
in a single turn, the points earned in that turn and each following
turn will be counted toward their total score.

== End Game

Once a player reaches 3000 (or more) points, the game enters the final
round where each of the other players gets one more turn. The winner
is the player with the highest score after the final round.

== References

Greed is described on Wikipedia at
http://en.wikipedia.org/wiki/Greed_(dice_game), however the rules are
a bit different from the rules given here.
Empty file added README
Empty file.
136 changes: 136 additions & 0 deletions README.rdoc
@@ -0,0 +1,136 @@
= EdgeCase Ruby Koans

The Ruby Koans walk you along the path to enlightenment in order to learn Ruby.
The goal is to learn the Ruby language, syntax, structure, and some common
functions and libraries. We also teach you culture. Testing is not just something we
pay lip service to, but something we live. It is essential in your quest to learn
and do great things in the language.

== The Structure

The koans are broken out into areas by file, hashes are covered in about_hashes.rb,
modules are introduced in about_modules.rb, etc. They are presented in order in the
path_to_enlightenment.rb file.

Each koan builds up your knowledge of Ruby and builds upon itself. It will stop at
the first place you need to correct.

Some koans simply need to have the correct answer substituted for an incorrect one.
Some, however, require you to supply your own answer. If you see the method +__+ (a
double underscore) listed, it is a hint to you to supply your own code in order to
make it work correctly.

== Installing Ruby

If you do not have Ruby setup, please visit http://ruby-lang.org/en/downloads/ for
operating specific instructions. In order to run this you need ruby and rake
installed. To check the installations simply type:

*nix platforms from any terminal window:

[~] $ ruby --version
[~] $ rake --version

Windows from the command prompt (cmd.exe)

c:\ruby --version
c:\rake --version

Any response for Ruby with a version number greater than 1.8 is fine (should be
around 1.8.6 or more). Any version of rake will do.

== The Path To Enlightenment

You can run the tests through rake or by calling the file itself (rake is the
recommended way to run them as we might build more functionality into this task).

*nix platforms, from the koans directory

[ruby_koans] $ rake # runs the default target :walk_the_path
[ruby_koans] $ ruby path_to_enlightenment.rb # simply call the file directly

Windows is the same thing

c:\ruby_koans\rake # runs the default target :walk_the_path
c:\ruby_koans\ruby path_to_enlightenment.rb # simply call the file directly

=== Red, Green, Refactor

In test-driven development the mantra has always been, red, green, refactor. Write a
failing test and run it (red), make the test pass (green), then refactor it (that is
look at the code and see if you can make it any better. In this case you will need
to run the koan and see it fail (red), make the test pass (green), then take a
moment and reflect upon the test to see what it is teaching you and improve the
code to better communicate its intent (refactor).

The very first time you run it you will see the following output:

[ ruby_koans ] $ rake
(in /Users/person/dev/ruby_koans)
cd koans

Thinking AboutAsserts
test_assert_truth has damaged your karma.

You have not yet reached enlightenment ...
<false> is not true.

Please meditate on the following code:
./about_asserts.rb:10:in `test_assert_truth'
path_to_enlightenment.rb:27

mountains are merely mountains

You have come to your first stage. If you notice it is telling you where to look for
the first solution:

Please meditate on the following code:
./about_asserts.rb:10:in `test_assert_truth'
path_to_enlightenment.rb:27

We then open up the about_asserts.rb file and look at the first test:

# We shall contemplate truth by testing reality, via asserts.
def test_assert_truth
assert false # This should be true
end

We then change the +false+ to +true+ and run the test again. After you are
done, think about what you are learning. In this case, ignore everything except
the method name (+test_assert_truth+) and the parts inside the method (everything
before the +end+).

In this case the goal is for you to see that if you pass a value to the +assert+
method, it will either ensure it is +true+ and continue on, or fail if in fact
the statement is +false+.

== Inspiration

A special thanks to Mike Clark and Ara Howard for inspiring this
project. Mike Clark wrote an excellent blog post about learning Ruby
through unit testing. This sparked an idea that has taken a bit to
solidify, that of bringing new rubyists into the community through
testing. Ara Howard then gave us the idea for the Koans in his ruby
quiz entry on Meta Koans (a must for any rubyist wanting to improve
their skills). Also, "The Little Lisper" taught us all the value of
the short questions/simple answers style of learning.

Mike Clark's post :: http://www.clarkware.com/cgi/blosxom/2005/03/18
Meta Koans :: http://rubyquiz.com/quiz67.html
The Little Lisper :: http://www.amazon.com/Little-LISPer-Third-Daniel-Friedman/dp/0023397632

== Other Resources

The Ruby Language :: http://ruby-lang.org
Try Ruby in your browser :: http://tryruby.org

Dave Thomas' introduction to Ruby Programming Ruby (the Pick Axe) :: http://pragprog.com/titles/ruby/programming-ruby

Brian Marick's fantastic guide for beginners Everyday Scripting with Ruby :: http://pragprog.com/titles/bmsft/everyday-scripting-with-ruby

= Other stuff

Author :: Jim Weirich <jim@weirichhouse.org>
Author :: Joe O'Brien <joe@edgecase.com>
Issue Tracker :: http://www.pivotaltracker.com/projects/48111
Requires :: Ruby 1.8.x or later and Rake (any recent version)
12 changes: 12 additions & 0 deletions Rakefile
@@ -0,0 +1,12 @@
#!/usr/bin/env ruby
# -*- ruby -*-

require 'rake/clean'
require 'rake/testtask'

task :default => :test

task :test do
ruby 'path_to_enlightenment.rb'
end

79 changes: 79 additions & 0 deletions about_array_assignment.rb
@@ -0,0 +1,79 @@
require File.expand_path(File.dirname(__FILE__) + '/edgecase')

class AboutArrayAssignment < EdgeCase::Koan
def test_non_parallel_assignment
names = ["John", "Smith"]
assert_equal ["John", "Smith"], names
end

def test_parallel_assignments
first_name, last_name = ["John", "Smith"]
assert_equal "John", first_name
assert_equal "Smith", last_name
end

# nice, comma delimited assignment matches the related indices on the right,
# in this case an array but can work with normal values too, for example if
# we had variables a and b that are integers it would be written as:
#
# a, b = b, a
#
# very handy and simple way to swap values that would have required a temporary
# variable in another language, so we would have to do something like:
#
# var a = 1;
# var b = 2;
# var temp = a;
# a = b;
# b = temp;
#
# Parallel assignment FTW!

def test_parallel_assignments_with_extra_values
first_name, last_name = ["John", "Smith", "III"]
assert_equal "John", first_name
assert_equal "Smith", last_name
end

# if it was first_name, last_name, other_name, then other_name would be "III"

def test_parallel_assignments_with_splat_operator
first_name, *last_name = ["John", "Smith", "III"]
assert_equal "John", first_name
assert_equal ["Smith","III"], last_name
end

# when I learned about methods, the * was for unlimited values so
# figured here it means from the matching index onwards to the end,
# and that was right

def test_parallel_assignments_with_too_few_variables
first_name, last_name = ["Cher"]
assert_equal "Cher", first_name
assert_equal nil, last_name
end

# no matching second element in the array so makes sense its nil

def test_parallel_assignments_with_subarrays
first_name, last_name = [["Willie", "Rae"], "Johnson"]
assert_equal ["Willie", "Rae"], first_name
assert_equal "Johnson", last_name
end

# remember the assignment order defined on the left matches the array
# on the right

def test_parallel_assignment_with_one_variable
first_name, = ["John", "Smith"]
assert_equal "John", first_name
end

def test_swapping_with_parallel_assignment
first_name = "Roy"
last_name = "Rob"
first_name, last_name = last_name, first_name
assert_equal "Rob", first_name
assert_equal "Roy", last_name
end
end

0 comments on commit 87dcdc3

Please sign in to comment.