-
Notifications
You must be signed in to change notification settings - Fork 6
Codebreaker解いてみました #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kakusuke
wants to merge
8
commits into
kyotorb:master
Choose a base branch
from
kakusuke:sumi
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7bc57d7
copy feature files
kakusuke b8618a0
add src file
kakusuke 1b5bf1a
pass start game senario
kakusuke 63e0ff8
all test passed!
kakusuke d62bfa2
http -> https
kakusuke d365eea
memoize hit_count
kakusuke 47c3beb
revert memoize
kakusuke a85fc9c
tweak guess method
kakusuke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| source "https://rubygems.org" | ||
|
|
||
| gem "rspec" | ||
| gem "cucumber" |
This file contains hidden or 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 @@ | ||
| GEM | ||
| remote: http://rubygems.org/ | ||
| specs: | ||
| builder (3.2.2) | ||
| cucumber (1.3.9) | ||
| builder (>= 2.1.2) | ||
| diff-lcs (>= 1.1.3) | ||
| gherkin (~> 2.12) | ||
| multi_json (>= 1.7.5, < 2.0) | ||
| multi_test (>= 0.0.2) | ||
| diff-lcs (1.2.4) | ||
| gherkin (2.12.2) | ||
| multi_json (~> 1.3) | ||
| multi_json (1.8.2) | ||
| multi_test (0.0.2) | ||
| rspec (2.14.1) | ||
| rspec-core (~> 2.14.0) | ||
| rspec-expectations (~> 2.14.0) | ||
| rspec-mocks (~> 2.14.0) | ||
| rspec-core (2.14.7) | ||
| rspec-expectations (2.14.4) | ||
| diff-lcs (>= 1.1.3, < 2.0) | ||
| rspec-mocks (2.14.4) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| cucumber | ||
| rspec |
This file contains hidden or 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,40 @@ | ||
| class String | ||
| def fill(char, range) | ||
| self.each_char.to_a.tap {|target| | ||
| target.fill(char, range) | ||
| }.join | ||
| end | ||
| end | ||
|
|
||
| module Codebreaker | ||
| class Game | ||
| def initialize(output) | ||
| @output = output | ||
| end | ||
|
|
||
| def start(secret) | ||
| @secret = secret.each_char.to_a | ||
| @output.puts "Welcome to Codebreaker!" | ||
| @output.puts "Enter guess: " | ||
| end | ||
|
|
||
| def guess(numbers) | ||
| @numbers = numbers.each_char.to_a | ||
|
|
||
| ('+' * total_count).fill('-', hit_count..-1) | ||
| end | ||
|
|
||
| private | ||
| def total_count | ||
| (0..9).map(&:to_s).reduce(0) do |count, n| | ||
| count + [@secret.count(n), @numbers.count(n)].min | ||
| end | ||
| end | ||
|
|
||
| def hit_count | ||
| @secret.each_index.reduce(0) do |count, idx| | ||
| count + (@secret[idx] == @numbers[idx] ? 1 : 0) | ||
| end | ||
| end | ||
| end | ||
| end | ||
11 changes: 11 additions & 0 deletions
11
code_breaker/sumi/features/codebreaker_starts_game.feature
This file contains hidden or 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: codebreaker starts game | ||
| As a codebreaker | ||
| 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: " | ||
|
|
64 changes: 64 additions & 0 deletions
64
code_breaker/sumi/features/codebreaker_submits_guess.feature
This file contains hidden or 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,64 @@ | ||
| 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 | - | | ||
|
|
||
| Scenario: try and error | ||
| Given the secret code is "1234" | ||
| When I guess "4321" | ||
| Then the mark should be "----" | ||
| When I guess "1234" | ||
| Then the mark should be "++++" | ||
|
|
38 changes: 38 additions & 0 deletions
38
code_breaker/sumi/features/step_definitions/codebreaker_steps.rb
This file contains hidden or 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,38 @@ | ||
| 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 |
This file contains hidden or 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,2 @@ | ||
| $LOAD_PATH << File.expand_path('../..', File.dirname(__FILE__)) | ||
| require 'codebreaker' |
This file contains hidden or 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,79 @@ | ||
| require 'spec_helper' | ||
| require 'codebreaker' | ||
|
|
||
| module Codebreaker | ||
| describe Game do | ||
| let(:output) { double('output').as_null_object } | ||
| let(:game) { Game.new(output) } | ||
|
|
||
| describe "#start" do | ||
| it "sends a welcome message" do | ||
| output.should_receive(:puts).with('Welcome to Codebreaker!') | ||
| game.start('1234') | ||
| end | ||
|
|
||
| it "prompts for the first guess" do | ||
| output.should_receive(:puts).with('Enter guess: ') | ||
| game.start('1234') | ||
| end | ||
| end | ||
|
|
||
| describe "#guess" do | ||
| context "with no matches" do | ||
| it "sends a mark with ''" do | ||
| game.start('1234') | ||
| game.guess('5555').should == '' | ||
| end | ||
| end | ||
|
|
||
| context "with 1 number match" do | ||
| it "sends a mark with '-'" do | ||
| game.start('1234') | ||
| game.guess('2555').should == '-' | ||
| end | ||
| end | ||
|
|
||
| context "with 1 exact match" do | ||
| it "sends a mark with '+'" do | ||
| game.start('1234') | ||
| game.guess('1555').should == '+' | ||
| end | ||
| end | ||
|
|
||
| context "with 2 number matches" do | ||
| it "sends a mark with '--'" do | ||
| game.start('1234') | ||
| game.guess('2355').should == '--' | ||
| end | ||
| end | ||
|
|
||
| context "with 1 number match and 1 exact match (in that order)" do | ||
| it "sends a mark with '+-'" do | ||
| game.start('1234') | ||
| game.guess('2535').should == '+-' | ||
| end | ||
| end | ||
|
|
||
| context "with 1 exact match duplicated in guess" do | ||
| it "sends a mark with '+'" do | ||
| game.start('1234') | ||
| game.guess('1155').should == '+' | ||
| end | ||
| end | ||
|
|
||
| context "with 1 exact match duplicated in secret" do | ||
| it "sends a mark with '+'" do | ||
| game.start('1155') | ||
| game.guess('1234').should == '+' | ||
| end | ||
| end | ||
|
|
||
| context "with 1 number match duplicated in secret" do | ||
| it "sends a mark with '-'" do | ||
| game.start('1511') | ||
| game.guess('2134').should == '-' | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,13 @@ | ||
| # by default, rspec only loads lib/ into the path, not the root project directory | ||
| $LOAD_PATH << File.expand_path('..', File.dirname(__FILE__)) | ||
|
|
||
| RSpec.configure do |config| | ||
| # Use color in STDOUT | ||
| config.color_enabled = true | ||
|
|
||
| # Use color not only in STDOUT but also in pagers and files | ||
| # config.tty = true | ||
|
|
||
| # Use the specified formatter | ||
| config.formatter = :documentation # :progress, :html, :textmate | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この実装面白いですね。なるほどー、って感じです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
頭ひねってこのやり方しか出てこなかったです
どう実装するのが自然でしょうかね。。。