-
Notifications
You must be signed in to change notification settings - Fork 6
亀田の回答 #1
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
kmdsbng
wants to merge
8
commits into
master
Choose a base branch
from
kameda
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
亀田の回答 #1
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
13de361
回答作成
kmdsbng 987d25d
Merge remote-tracking branch 'origin/master' into kameda
kmdsbng f4c8ba8
fix caller
kmdsbng 62349a8
module使うようにした
kmdsbng 908c2b3
refactoring:rename
kmdsbng 6094bbe
refactoring
kmdsbng 0ac54cb
refactoring
kmdsbng abdf193
trivial
kmdsbng 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,52 @@ | ||
| # -*- encoding: utf-8 -*- | ||
|
|
||
|
|
||
| class Codebreaker | ||
| 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
57
code_breaker/kameda/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,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
41
code_breaker/kameda/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,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 | ||
|
|
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,6 @@ | ||
|
|
||
|
|
||
|
|
||
| $LOAD_PATH << File.expand_path('../..', File.dirname(__FILE__)) | ||
| require 'codebreaker' | ||
|
|
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.
ネームスペースとして使うなら
moduleの方が向いているように思います。(他に理由があったらごめんなさい。)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.
なんとなくRuby界隈ではネームスペースにmoduleを使う文化があるように思うのですが、この辺りは好みなのかもですね。