Skip to content

Commit

Permalink
Added src directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Dec 21, 2009
1 parent f3af22b commit 47c6c6f
Show file tree
Hide file tree
Showing 38 changed files with 2,745 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/GREED_RULES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
= Playing Greed

Greed is a dice game played amoung 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 six 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 there all no non-scoring dice), 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.
38 changes: 38 additions & 0 deletions src/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
= 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.


== The Path To Enlightenment

In order to achieve enlightenment you need to follow the path_to_enlightenment. This
can be done in two ways

*nix platforms, from the koans directory

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

Windows is the same thing

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

12 changes: 12 additions & 0 deletions src/Rakefile
Original file line number Diff line number Diff line change
@@ -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

38 changes: 38 additions & 0 deletions src/about_array_assignment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require '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

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

def test_parallel_assignments_with_extra_variables
first_name, last_name = ["Cher"]
assert_equal __("Cher"), first_name
assert_equal __(nil), last_name
end

def test_parallel_assignements_with_subarrays
first_name, last_name = [["Willie", "Rae"], "Johnson"]
assert_equal __(["Willie", "Rae"]), first_name
assert_equal __("Johnson"), last_name
end

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

end
85 changes: 85 additions & 0 deletions src/about_arrays.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require 'edgecase'

class AboutArrays < EdgeCase::Koan
def test_creating_arrays
empty_array = Array.new
assert_equal Array, empty_array.class
assert_equal __(0), empty_array.size
end

def test_array_literals
array = Array.new
assert_equal [], array

array[0] = 1
assert_equal [1], array

array[1] = 2
assert_equal [1, __(2)], array

array << 333
assert_equal __([1, 2, 333]), array
end

def test_accessing_array_elements
array = [:peanut, :butter, :and, :jelly]

assert_equal __(:peanut), array[0]
assert_equal __(:peanut), array.first
assert_equal __(:jelly), array[3]
assert_equal __(:jelly), array.last
assert_equal __(:jelly), array[-1]
assert_equal __(:butter), array[-3]
end

def test_slicing_arrays
array = [:peanut, :butter, :and, :jelly]

assert_equal __([:peanut]), array[0,1]
assert_equal __([:peanut, :butter]), array[0,2]
assert_equal __([:and, :jelly]), array[2,2]
assert_equal __([:and, :jelly]), array[2,20]
assert_equal __([]), array[4,0]
assert_equal __([]), array[4,100]
assert_equal __(nil), array[5,0]
assert_equal __(nil), array[5,0]
end

def test_arrays_and_ranges
assert_equal Range, (1..5).class
assert_not_equal [1,2,3,4,5], (1..5)
assert_equal [1,2,3,4,5], (1..5).to_a
assert_equal __([1,2,3,4]), (1...5).to_a
end

def test_slicing_with_ranges
array = [:peanut, :butter, :and, :jelly]

assert_equal __([:peanut, :butter, :and]), array[0..2]
assert_equal __([:peanut, :butter]), array[0...2]
assert_equal ([:and, :jelly]), array[2..-1]
end

def test_pushing_and_popping_arrays
array = [1,2]
array.push(:last)

assert_equal __([1, 2, :last]), array

popped_value = array.pop
assert_equal __(:last), popped_value
assert_equal __([1, 2]), array
end

def test_shifting_arrays
array = [1,2]
array.unshift(:first)

assert_equal __([:first, 1, 2]), array

shifted_value = array.shift
assert_equal __(:first), shifted_value
assert_equal __([1, 2]), array
end

end
40 changes: 40 additions & 0 deletions src/about_asserts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env ruby
# -*- ruby -*-

require 'edgecase'

class AboutAsserts < EdgeCase::Koan

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

# Enlightenment may be more easily achieved with appropriate
# messages.
def test_assert_with_message
assert __(true), "This should be true -- Please fix this"
end

# To understand reality, we must compare our expectations against
# reality.
def test_assert_equality
expected_value = __(2)
actual_value = 1 + 1

assert expected_value == actual_value
end

# Some ways of asserting equality are better than others.
def test_a_better_way_of_asserting_equality
expected_value = __(2)
actual_value = 1 + 1

assert_equal expected_value, actual_value
end

# Sometimes we will ask you to fill in the values
def test_fill_in_values
assert_equal __(2), 1 + 1
end
end
96 changes: 96 additions & 0 deletions src/about_blocks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require 'edgecase'

class AboutBlocks < EdgeCase::Koan
def method_with_block
result = yield
result
end

def test_methods_can_take_blocks
yielded_result = method_with_block { 1 + 2 }
assert_equal __(3), yielded_result
end

def test_blocks_can_be_defined_with_do_end_too
yielded_result = method_with_block do 1 + 2 end
assert_equal __(3), yielded_result
end

# ------------------------------------------------------------------

def method_with_block_arguments
yield("Jim")
end

def test_blocks_can_take_arguments
result = method_with_block_arguments do |argument|
assert_equal __("Jim"), argument
end
end

# ------------------------------------------------------------------

def many_yields
yield(:peanut)
yield(:butter)
yield(:and)
yield(:jelly)
end

def test_methods_can_call_yield_many_times
result = []
many_yields { |item| result << item }
assert_equal __([:peanut, :butter, :and, :jelly]), result
end

# ------------------------------------------------------------------

def yield_tester
if block_given?
yield
else
:no_block
end
end

def test_methods_can_see_if_they_have_been_called_with_a_block
assert_equal __(:with_block), yield_tester { :with_block }
assert_equal __(:no_block), yield_tester
end

# ------------------------------------------------------------------

def test_block_can_effect_variables_in_the_code_where_they_are_created
value = :initial_value
method_with_block { value = :modified_in_a_block }
assert_equal __(:modified_in_a_block), value
end

def test_blocks_can_be_assigned_to_variables_and_called_explicitly
add_one = lambda { |n| n + 1 }
assert_equal __(11), add_one.call(10)

# Alternative calling sequence
assert_equal __(11), add_one[10]
end

def test_stand_alone_blocks_can_be_passed_to_methods_expecting_blocks
make_upper = lambda { |n| n.upcase }
result = method_with_block_arguments(&make_upper)
assert_equal __("JIM"), result
end

# ------------------------------------------------------------------

def method_with_explict_block(&block)
block.call(10)
end

def test_methods_can_take_an_explicit_block_argument
assert_equal __(20), method_with_explict_block { |n| n * 2 }

add_one = lambda { |n| n + 1 }
assert_equal __(11), method_with_explict_block(&add_one)
end

end
Loading

0 comments on commit 47c6c6f

Please sign in to comment.