Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions code_breaker/kameda/codebreaker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- encoding: utf-8 -*-


class Codebreaker
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ネームスペースとして使うなら module の方が向いているように思います。(他に理由があったらごめんなさい。)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにそうかも!
僕は特にこだわりなく、クラスにしてしまいます。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なんとなくRuby界隈ではネームスペースにmoduleを使う文化があるように思うのですが、この辺りは好みなのかもですね。

class Game
def initialize(output)
@output = output
end

def start(secret)
@secret = secret
end

def guess(guess)
secret_ary = @secret.split(//)
guess_ary = guess.split(//)
pluses, others = secret_ary.zip(guess_ary).partition {|sec, gue|
sec == gue
}
plus_count = pluses.size
other_secret_hash = Hash.new {|h,k| h[k] = 0}
other_guesses = []
others.each {|sec, gue|
other_secret_hash[sec] += 1
other_guesses << gue
}

minus_count = 0
other_guesses.each {|gue|
if other_secret_hash[gue] > 0
minus_count += 1
other_secret_hash[gue] -= 1
end
}
('+' * plus_count) + ('-' * minus_count)
end

end
end

case $0
when __FILE__
game = Codebreaker::Game.new(STDOUT)
game.start('1234')
while(code=gets.chomp)
puts game.guess(code)
end
when /spec[^\/]*$/
# {spec of the implementation}
end


57 changes: 57 additions & 0 deletions code_breaker/kameda/features/codebreaker_submits_guess.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Feature: codebreaker submits guess
The codebreaker submits a guess of four numbers. The game marks the guess with + and - signs.

For each number in the guess that matches the number and position of a number in the secret code, the mark includes one + sign. For each number in the guess that matches the number but not the position of a number in the secret code, the mark includes one - sign.

Each position in the secret code can only be matched once. For example, a guess of 1134 against a secret code of 1234 would get three plus signs: one for each of the exact matches in the first, third and fourth positions. The number match in the second position would be ignored.

Scenario Outline: submit a guess
Given the secret code is "<code>"
When I guess "<guess>"
Then the mark should be "<mark>"

Scenarios: no matches
| code | guess | mark |
| 1234 | 5555 | |

Scenarios: 1 number correct
| code | guess | mark |
| 1234 | 1555 | + |
| 1234 | 2555 | - |

Scenarios: 2 numbers correct
| code | guess | mark |
| 1234 | 5254 | ++ |
| 1234 | 5154 | +- |
| 1234 | 2545 | -- |

Scenarios: 3 numbers correct
| code | guess | mark |
| 1234 | 5234 | +++ |
| 1234 | 5134 | ++- |
| 1234 | 5124 | +-- |
| 1234 | 5123 | --- |

Scenarios: all numbers correct
| code | guess | mark |
| 1234 | 1234 | ++++ |
| 1234 | 1243 | ++-- |
| 1234 | 1423 | +--- |
| 1234 | 4321 | ---- |

Scenarios: matches with duplicates
| code | guess | mark |
| 1234 | 1155 | + |
| 1234 | 5115 | - |
| 1134 | 1155 | ++ |
| 1134 | 5115 | +- |
| 1134 | 5511 | -- |
| 1134 | 1115 | ++ |
| 1134 | 5111 | +- |
| 1155 | 1234 | + |
| 1111 | 1112 | +++ |
| 1113 | 1121 | ++- |
| 3111 | 1311 | ++-- |
| 3114 | 1251 | -- |
| 1511 | 2134 | - |

41 changes: 41 additions & 0 deletions code_breaker/kameda/features/step_definitions/codebreaker_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- encoding: utf-8 -*-

Given /^I am not yet playing$/ do
end

When /^I start a new game$/ do
game = Codebreaker::Game.new(output)
game.start('1234')
end

Then /^I should see "([^\"]*)"$/ do |message|
output.messages.should include(message)
end

Given /^the secret code is "([^\"]*)"$/ do |secret|
@game = Codebreaker::Game.new(output)
@game.start(secret)
end

When /^I guess "([^\"]*)"$/ do |guess|
@guess = guess
end

Then /^the mark should be "([^\"]*)"$/ do |mark|
@game.guess(@guess).should == mark
end

class Output
def messages
@messages ||= []
end

def puts(message)
messages << message
end
end

def output
@output ||= Output.new
end

6 changes: 6 additions & 0 deletions code_breaker/kameda/features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@



$LOAD_PATH << File.expand_path('../..', File.dirname(__FILE__))
require 'codebreaker'