-
Notifications
You must be signed in to change notification settings - Fork 0
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
Thomas Brand
committed
Dec 30, 2009
0 parents
commit cf649c0
Showing
18 changed files
with
347 additions
and
0 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,3 @@ | ||
== cb | ||
|
||
Put appropriate LICENSE for your project here. |
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 @@ | ||
== cb | ||
|
||
You should document your project here. |
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,50 @@ | ||
# | ||
# To change this template, choose Tools | Templates | ||
# and open the template in the editor. | ||
|
||
|
||
require 'rubygems' | ||
require 'rake' | ||
require 'rake/clean' | ||
require 'rake/gempackagetask' | ||
require 'rake/rdoctask' | ||
require 'rake/testtask' | ||
require 'spec/rake/spectask' | ||
|
||
spec = Gem::Specification.new do |s| | ||
s.name = 'cb' | ||
s.version = '0.0.1' | ||
s.has_rdoc = true | ||
s.extra_rdoc_files = ['README', 'LICENSE'] | ||
s.summary = 'Your summary here' | ||
s.description = s.summary | ||
s.author = '' | ||
s.email = '' | ||
# s.executables = ['your_executable_here'] | ||
s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*") | ||
s.require_path = "lib" | ||
s.bindir = "bin" | ||
end | ||
|
||
Rake::GemPackageTask.new(spec) do |p| | ||
p.gem_spec = spec | ||
p.need_tar = true | ||
p.need_zip = true | ||
end | ||
|
||
Rake::RDocTask.new do |rdoc| | ||
files =['README', 'LICENSE', 'lib/**/*.rb'] | ||
rdoc.rdoc_files.add(files) | ||
rdoc.main = "README" # page to start on | ||
rdoc.title = "cb Docs" | ||
rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder | ||
rdoc.options << '--line-numbers' | ||
end | ||
|
||
Rake::TestTask.new do |t| | ||
t.test_files = FileList['test/**/*.rb'] | ||
end | ||
|
||
Spec::Rake::SpecTask.new do |t| | ||
t.spec_files = FileList['spec/**/*.rb'] | ||
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,3 @@ | ||
#! /usr/bin/env ruby | ||
require 'codebreaker/game' | ||
require 'codebreaker/generator' |
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,11 @@ | ||
Feature: code-breaker starts game | ||
|
||
As a code-breaker | ||
I want to start a game | ||
So that I can break the code | ||
|
||
Scenario: start game | ||
Given I am not yet playing | ||
When I start a new game | ||
Then I should see "Welcome to Codebreaker!" | ||
And I should see "Enter guess:" |
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,50 @@ | ||
Feature: code-breaker submits guess | ||
|
||
The code-breaker submits a guess of four colored | ||
pegs. The game marks the guess with black and | ||
white "marker" pegs. | ||
|
||
For each peg in the guess that matches the color | ||
and position of a peg in the secret code, the | ||
mark includes one black peg. For each additional | ||
peg in the guess that matches the color but not | ||
the position of a peg in the secret code, a | ||
white peg is added to the mark. | ||
|
||
Scenario Outline: submit guess | ||
Given the secret code is <code> | ||
When I guess <guess> | ||
Then the mark should be <mark> | ||
|
||
Scenarios: all colors correct | ||
| code | guess | mark | | ||
| r g y c | r g y c | bbbb | | ||
| r g y c | r g c y | bbww | | ||
| r g y c | y r g c | bwww | | ||
| r g y c | c r g y | wwww | | ||
|
||
Scenarios: 3 colors correct | ||
| code | guess | mark | | ||
| r g y c | w g y c | bbb | | ||
| r g y c | w r y c | bbw | | ||
| r g y c | w r g c | bww | | ||
| r g y c | w r g y | www | | ||
|
||
Scenarios: 2 colors correct | ||
| code | guess | mark | | ||
| r g y c | w g w c | bb | | ||
| r g y c | w r w c | bw | | ||
| r g y c | g w c w | ww | | ||
|
||
Scenarios: 1 color correct | ||
| code | guess | mark | | ||
| r g y c | r w w w | b | | ||
| r g y c | w w r w | w | | ||
|
||
Scenarios: dups in guess match color in code | ||
| code | guess | mark | | ||
| r y g c | r y g g | bbb | | ||
| r y g c | r y c c | bbb | | ||
| r y g c | g y r g | bww | | ||
|
||
|
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,11 @@ | ||
Feature: game generates random secret code | ||
In order to keep the codebreaker game interesting | ||
I want the game to generate a random secret code each time I play | ||
|
||
Scenario: 10,000 games | ||
Given 6 colors | ||
And 4 positions | ||
When I play 10,000 games | ||
Then each color should appear between 1500 and 1800 times in each position | ||
And each color should appear no more than once in each secret code | ||
|
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,67 @@ | ||
def messenger | ||
@messenger ||= StringIO.new | ||
end | ||
|
||
def game | ||
@game ||= Codebreaker::Game.new(messenger) | ||
end | ||
|
||
def messages_should_include(message) | ||
messenger.string.split("\n").should include(message) | ||
end | ||
|
||
|
||
def fixnum_from(string) | ||
string.scan(/\d/).join.to_i | ||
end | ||
|
||
|
||
Given /^I am not yet playing$/ do | ||
end | ||
|
||
|
||
Given /^the secret code is (. . . .)$/ do |code| | ||
game.start(stub('generator', :code => code.split)) | ||
end | ||
|
||
|
||
When /^I guess (. . . .)$/ do |code| | ||
game.guess(code.split) | ||
end | ||
|
||
|
||
When /^I start a new game$/ do | ||
game.start(stub('generator', :code => %[r g y c])) | ||
end | ||
|
||
|
||
Then /^I should see "([^\"]*)"$/ do |message| | ||
messages_should_include(message) | ||
end | ||
|
||
Then /^the mark should be (.*)$/ do |mark| | ||
messages_should_include(mark) | ||
end | ||
|
||
Given /^6 colors$/ do | ||
end | ||
|
||
Given /^4 positions$/ do | ||
end | ||
|
||
|
||
|
||
When /^I play (.*) games$/ do |number| | ||
generator = Codebreaker::Generator.new | ||
fixnum_from(number).times do | ||
game.start(generator) | ||
end | ||
end | ||
|
||
Then /^each color should appear between 1500 and 1800 times in each position$/ do | ||
pending | ||
end | ||
|
||
Then /^each color should appear no more than once in each secret code$/ do | ||
pending | ||
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,3 @@ | ||
$LOAD_PATH << File.join(File.dirname(__FILE__),"..","..","lib") | ||
require 'codebreaker' | ||
require 'spec/stubs/cucumber' |
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 @@ | ||
require 'codebreaker/game' |
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,26 @@ | ||
module Codebreaker | ||
class Game | ||
def initialize(messenger) | ||
@messenger = messenger | ||
end | ||
|
||
def start(generator) | ||
@code = generator.code | ||
@messenger.puts "Welcome to Codebreaker!" | ||
@messenger.puts "Enter guess:" | ||
end | ||
|
||
def guess(guess) | ||
result = [nil,nil,nil,nil] | ||
guess.each_with_index do |peg, index| | ||
if @code[index] == peg | ||
result[index] = "b" | ||
elsif @code.include?(peg) | ||
result[@code.index(peg)] ||= "w" | ||
end | ||
end | ||
@messenger.puts result.compact.sort.join | ||
end | ||
|
||
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,6 @@ | ||
module Codebreaker | ||
class Generator | ||
def code | ||
end | ||
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,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project-private xmlns="http://www.netbeans.org/ns/project-private/1"> | ||
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/> | ||
</project-private> |
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,19 @@ | ||
clean=Remove any temporary products. | ||
clobber=Remove any generated file. | ||
clobber_package=Remove package products | ||
clobber_rdoc=Remove rdoc products | ||
doc= | ||
doc/rdoc= | ||
doc/rdoc/index.html= | ||
gem=Build the gem file cb-0.0.1.gem | ||
package=Build all the packages | ||
pkg= | ||
pkg/cb-0.0.1= | ||
pkg/cb-0.0.1.gem= | ||
pkg/cb-0.0.1.tgz= | ||
pkg/cb-0.0.1.zip= | ||
rdoc=Build the rdoc HTML Files | ||
repackage=Force a rebuild of the package files | ||
rerdoc=Force a rebuild of the RDOC files | ||
spec=Run specs | ||
test=Run tests |
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,6 @@ | ||
main.file= | ||
platform.active=Ruby | ||
source.encoding=UTF-8 | ||
spec.src.dir=spec | ||
src.dir=lib | ||
test.src.dir=test |
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,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://www.netbeans.org/ns/project/1"> | ||
<type>org.netbeans.modules.ruby.rubyproject</type> | ||
<configuration> | ||
<data xmlns="http://www.netbeans.org/ns/ruby-project/1"> | ||
<name>cb</name> | ||
<source-roots> | ||
<root id="src.dir"/> | ||
</source-roots> | ||
<test-roots> | ||
<root id="test.src.dir"/> | ||
<root id="spec.src.dir"/> | ||
</test-roots> | ||
</data> | ||
</configuration> | ||
</project> |
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,65 @@ | ||
require File.join(File.dirname(__FILE__), "..","spec_helper") | ||
|
||
module Codebreaker | ||
describe Game do | ||
before(:each) do | ||
@messenger = mock("messenger").as_null_object | ||
@game = Game.new(@messenger) | ||
end | ||
|
||
context "starting up" do | ||
|
||
it "should send a welcome message" do | ||
@messenger.should_receive(:puts).with("Welcome to Codebreaker!") | ||
@game.start(stub('generator', :code => %w[r g y c])) | ||
end | ||
|
||
it "should prompt for the first guess" do | ||
@messenger.should_receive(:puts).with("Enter guess:") | ||
@game.start(stub('generator', :code => %w[r g y c])) | ||
end | ||
end | ||
|
||
context "marking a guess" do | ||
context "with all 4 colors correct in the correct places" do | ||
it "should mark the guess with bbbb" do | ||
@game.start(stub('generator', :code => %w[r g y c])) | ||
@messenger.should_receive(:puts).with("bbbb") | ||
@game.guess(%w[r g y c]) | ||
end | ||
end | ||
context "with all 4 colors correct and 2 in the correct places" do | ||
it "should mark the guess with bbww" do | ||
@game.start(stub('generator', :code => %w[r g y c])) | ||
@messenger.should_receive(:puts).with("bbww") | ||
@game.guess(%w[r g c y]) | ||
end | ||
end | ||
context "with all 4 colors correct and 1 in the correct place" do | ||
it "should mark the guess with bwww" do | ||
@game.start(stub('generator', :code => %w[r g y c])) | ||
@messenger.should_receive(:puts).with("bwww") | ||
@game.guess(%w[y r g c]) | ||
end | ||
end | ||
context "with three colors correct in the correct places" do | ||
it "should mark the guess with bbb" do | ||
@game.start(stub('generator', :code => %w[r g y c])) | ||
@messenger.should_receive(:puts).with("bbb") | ||
@game.guess(%w[r g y w]) | ||
end | ||
end | ||
context "with duplicates in the guess that match a peg in the code" do | ||
context "by color and position" do | ||
it "should add a single b to the mark" do | ||
@game.start(stub('generator', :code => %w[r y g c])) | ||
@messenger.should_receive(:puts).with("bbb") | ||
@game.guess(%w[r y g g]) | ||
end | ||
end | ||
end | ||
end | ||
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,3 @@ | ||
$LOAD_PATH << File.join(File.dirname(__FILE__),"..","lib") | ||
require 'spec' | ||
require 'codebreaker' |