Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b4625e
commit 828a33d
Showing
56 changed files
with
5,534 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
use ruby-1.9.2 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class Bowling | ||
|
||
def self.score(rolls) | ||
new(rolls).score | ||
end | ||
|
||
def initialize(rolls) | ||
@rolls = rolls | ||
end | ||
|
||
def roll_amount(roll) | ||
case roll | ||
when "X" | ||
10 | ||
when /\d|-/ | ||
roll.to_i | ||
end | ||
end | ||
|
||
def split_frames(max_frames = 10) | ||
rolls = @rolls.chars.to_a | ||
[].tap do |generated_frames| | ||
while roll = rolls.shift and generated_frames.size < max_frames | ||
case roll | ||
when "X" | ||
frame = [10, roll_amount(rolls[0]), roll_amount(rolls[1])] | ||
|
||
generated_frames << frame | ||
when /\d|-/ | ||
frame = [roll.to_i] | ||
next_roll = rolls.shift | ||
case next_roll | ||
when "/" | ||
frame << 10 - frame[0] | ||
frame << roll_amount(rolls[0]) | ||
|
||
when /\d|-/ | ||
frame << next_roll.to_i | ||
end | ||
|
||
generated_frames << frame | ||
end | ||
end | ||
end | ||
end | ||
|
||
def score | ||
split_frames.flatten.inject {|a,b| a + b} | ||
end | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require "minitest/autorun" | ||
require "bowling" | ||
|
||
class BowlingTest < MiniTest::Unit::TestCase | ||
|
||
def assert_score(expected_result, rolls, msg = nil) | ||
actual_result = Bowling.score(rolls) | ||
assert_equal expected_result, actual_result, msg || "Expected #{expected_result} for rolls '#{rolls}' but was #{actual_result}" | ||
end | ||
|
||
def test_12_rolls_12_strikes | ||
assert_score 300, "XXXXXXXXXXXX" | ||
end | ||
|
||
def test_10_pairs_of_9_and_miss | ||
assert_score 90, "9-9-9-9-9-9-9-9-9-9-" | ||
end | ||
|
||
def test_10_pairs_of_5_and_spare_with_a_final_5 | ||
assert_score 150, "5/5/5/5/5/5/5/5/5/5/5" | ||
end | ||
|
||
def test_11_rolls_with_a_final_3 | ||
assert_score 95, "--34--1/425/XX112/3" | ||
end | ||
|
||
def test_split_frames | ||
assert_equal [[4, 5], [4, 6, 10], [10, 1, 0]], Bowling.new("454/X1-").split_frames(3) | ||
assert_equal 10, Bowling.new("XXXXXXXXXXXX").split_frames.size | ||
end | ||
end | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="amedio"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>Bowling</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#Sat Apr 16 17:02:20 CEST 2011 | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.6 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.6 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package core; | ||
|
||
public class Bowling { | ||
public static int getPoints(String throwings) { | ||
int result = 0; | ||
int actResult = 0; | ||
|
||
for (int i = 0; i < throwings.length(); i++) { | ||
String actValue = String.valueOf(throwings.charAt(i)); | ||
|
||
if ("-".equals(actValue)) { | ||
actResult = 0; | ||
} else if ("/".equals(actValue)) { | ||
actResult = 10 - actResult | ||
+ valueOfThrow(String.valueOf(throwings.charAt(i + 1))); | ||
} else if ("X".equals(actValue)) { | ||
if (i + 1 >= throwings.length() || i + 2 >= throwings.length()) | ||
break; | ||
actResult = 10 | ||
+ valueOfThrow(String.valueOf(throwings.charAt(i + 1))) | ||
+ valueOfThrow(String.valueOf(throwings.charAt(i + 2))); | ||
} else { | ||
if (i > 0 && i == (throwings.length() - 1) | ||
&& "/".equals(String.valueOf(throwings.charAt(i - 1)))) | ||
break; | ||
actResult = Integer.parseInt(actValue); | ||
} | ||
|
||
result += actResult; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static int valueOfThrow(String throwValue) { | ||
int result = 0; | ||
if ("X".equals(throwValue)) { | ||
result = 10; | ||
} else { | ||
result = Integer.parseInt(throwValue); | ||
} | ||
return result; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package core; | ||
|
||
import junit.framework.Assert; | ||
|
||
import org.junit.Test; | ||
|
||
public class BowlingTest { | ||
@Test | ||
public void getPointsTest() { | ||
int expected = 0; | ||
int actual = Bowling.getPoints(""); | ||
Assert.assertEquals(expected, actual); | ||
|
||
expected = 20; | ||
actual = Bowling.getPoints("11111111111111111111"); | ||
Assert.assertEquals(expected, actual); | ||
|
||
expected = 10; | ||
actual = Bowling.getPoints("1-1-1-1-1-1-1-1-1-1-"); | ||
Assert.assertEquals(expected, actual); | ||
|
||
expected = 150; | ||
actual = Bowling.getPoints("5/5/5/5/5/5/5/5/5/5/5"); | ||
Assert.assertEquals(expected, actual); | ||
|
||
expected = 300; | ||
actual = Bowling.getPoints("XXXXXXXXXXXX"); | ||
Assert.assertEquals(expected, actual); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.bundle | ||
db/*.sqlite3 | ||
log/*.log | ||
tmp/ | ||
.DS_Store | ||
coverage/ | ||
spec/report.html |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-f doc | ||
-p | ||
--colour |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rvm ruby-1.9.2-p180 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Implementación de la Kata Bowling | ||
============================================ | ||
|
||
Lenguaje: Ruby 1.9.2 | ||
Pruebas: Cucumber + RSpec + autotest | ||
|
||
== Ejecutar las pruebas | ||
|
||
- Unitarias: | ||
|
||
Verificar las pruebas (necesita: gem install rspec): | ||
rspec spec/ | ||
|
||
Para desarrollar/refactorizar, mejor arrancar autotest (necesita: gem install autotest-standalone rspec) | ||
autotest | ||
|
||
- Aceptación: | ||
|
||
Ejecutar cucumber (necesita: gem install cucumber) | ||
|
||
-- | ||
amuino |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# language: es | ||
Característica: Calcular la puntuación de una partida de bolos | ||
Para poder presumir delante de los amigotes | ||
Como jugador de bolos | ||
Quiero saber el valor numérico | ||
|
||
Esquema del escenario: sumar la puntuación | ||
Dado que las tiradas han sido "<tiradas>" | ||
Cuando calcule la sume | ||
Entonces el resultado es "<suma>" | ||
|
||
Ejemplos: | ||
| tiradas | suma | | ||
| 45454545454545454545 | 90 | | ||
| 0-0-0-0-0-0-0-0-0-0- | 0 | | ||
| 1-0-0-0-0-0-0-0-0-0- | 1 | | ||
| 3-0-0-0-0-0-0-0-0-0- | 3 | | ||
| 5/1-0-0-0-0-0-0-0-0- | 12 | | ||
| 0-0-0-0-0-0-0-0-0-5/1- | 11 | | ||
| X1-1-0-0-0-0-0-0-0- | 13 | | ||
| XXXXXXXXXXXX | 300 | | ||
| 9-9-9-9-9-9-9-9-9-9- | 90 | | ||
| 5/5/5/5/5/5/5/5/5/5/5 | 150 | | ||
| 11111111111111111111 | 20 | | ||
| 22222222222222222222 | 40 | | ||
| -------------------- | 0 | | ||
| 9-9-9-9-9-9-9-9-9-9- | 90 | | ||
| 4/-6---------------- | 16 | | ||
| 7/-6---------------- | 16 | | ||
| XXX00000000000000 | 60 | | ||
| X7/9-X-88/-6XX72 | 145 | | ||
| X34---------------- | 24 | | ||
| XXXXXXXXX00 | 240 | | ||
| 4/4/4/4/4/4/4/4/4/4/4 | 140 | | ||
| 5/5/5/5/5/5/5/5/5/5/5 | 150 | | ||
| X4/X4/X4/X4/X4/X | 200 | | ||
| XXXXXXXXXXXX | 300 | | ||
| 14456/5/X-17/6/X2/6 | 133 | | ||
| X7/9-X-88/-6XXX81 | 167 | | ||
| ------------------X23 | 15 | | ||
| ----------------X23 | 20 | |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "#{File.dirname(__FILE__)}/../../lib/bowling" | ||
|
||
# encoding: utf-8 | ||
Dado /^que las tiradas han sido "([^"]*)"$/ do |game| | ||
@totalizer = BowlingTotalizer.new(game) | ||
end | ||
|
||
Cuando /^calcule la sume$/ do | ||
@total = @totalizer.total | ||
end | ||
|
||
Entonces /^el resultado es "([^"]*)"$/ do |sum| | ||
@total.should == sum.to_i | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class Array | ||
def second | ||
self[1] | ||
end | ||
|
||
def to_frame | ||
if first == "X" | ||
Frame.new(:strike, [10, nil]) | ||
elsif second == "/" | ||
Frame.new(:spare, [first.to_i, 10-first.to_i]) | ||
elsif second == "-" | ||
Frame.new(:miss, [first.to_i, 0]) | ||
else | ||
Frame.new(:normal, [first.to_i, second.to_i]) | ||
end | ||
end | ||
end | ||
|
||
class Frame | ||
attr_accessor :kind, :simple_score | ||
|
||
def initialize(kind, simple_score) | ||
@kind = kind | ||
@simple_score = simple_score | ||
@score = simple_score.compact.inject(0, :+) | ||
end | ||
|
||
def score(next_frames = []) | ||
case kind | ||
when :miss then | ||
@score | ||
when :normal then | ||
@score | ||
else | ||
10 + bonus_throws(next_frames) | ||
end | ||
end | ||
|
||
def bonus_throws(next_frames) | ||
return 0 if next_frames.nil? || next_frames.empty? | ||
case kind | ||
when :spare then | ||
next_frames.first.simple_score.first.to_i | ||
when :strike then | ||
next_frames[0..1].collect { |frame| frame.simple_score }.flatten.compact[0..1].inject(0, :+) | ||
end | ||
end | ||
end | ||
|
||
class BowlingTotalizer | ||
def initialize(game) | ||
@frames = BowlingTotalizer.parse(game) | ||
end | ||
|
||
def total | ||
frames = @frames.clone | ||
accum = 0 | ||
10.times do | ||
accum += frames.shift.score(frames) | ||
end | ||
accum | ||
end | ||
|
||
def self.parse(game) | ||
raw_frames(game).collect &:to_frame | ||
end | ||
|
||
def self.raw_frames(game) | ||
game.scan(%r{(X)|(-|[0-9])([1-9]|-|/)?}).collect {|x| x.compact} | ||
end | ||
end |
Oops, something went wrong.