ビンゴを作る 問題1状態 実装まで考えることが多いので、問題が悪かったらしい 作り変える 問題2状態 スケルトンを作る $ mkdir stack $ cd stack $ mkdir lib $ mkdir test $ vi test/bingo_spec.rb ファイルとクラスの名前はstackにする。 test/bingo_speck.rb $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib")) require 'rubygems' require 'spec' require 'bingo' describe "bingo-game" do end $ spec -cfs test/bing_spec.rb /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- bingo (LoadError) bingoクラスがないよー $ vi lib/bingo.rb class Bingo end $ spec -cfs test/bing_spec.rb Finished in 0.002498 seconds 0 examples, 0 failures グリーン! ビンゴしてたらtrueを返す $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib")) require 'rubygems' require 'spec' require 'bingo' describe "bingo-game" do describe "ビンゴしている場合" do it "#bingo? にtrueを返すこと" do @game = Bingo.new @game.bingo?.should be_true end end end $ spec -cfs test/bing_spec.rb bingo-game ビンゴしている場合 - #bingo? にtrueを返すこと (FAILED - 1) 1) NoMethodError in 'bingo-game ビンゴしている場合 #bingo? にtrueを返すこと' undefined method `bingo?' for #<Bingo:0xb7d5ed98> ./test/bing_spec.rb:10: Finished in 0.006818 seconds 1 example, 1 failure ビンゴ無い! class Bingo def bingo? end end レッド! $ spec -cfs test/bing_spec.rb bingo-game ビンゴしている場合 - #bingo? にtrueを返すこと (FAILED - 1) 1) 'bingo-game ビンゴしている場合 #bingo? にtrueを返すこと' FAILED expected true, got nil ./test/bing_spec.rb:10: Finished in 0.002933 seconds 1 example, 1 failure class Bingo def bingo? true end end $ spec -cfs test/bing_spec.rb bingo-game ビンゴしている場合 - #bingo? にtrueを返すこと Finished in 0.004947 seconds 1 example, 0 failures グリーン! $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib")) require 'rubygems' require 'spec' require 'bingo' describe "bingo-game" do describe "ビンゴしている場合" do it "#bingo? にtrueを返すこと" do @game = Bingo.new @game.bingo?.should be_true end end describe "たて一列ビンゴの場合" do it "#bingo? にtrueを返すこと" do @game = Bingo.new 1.upto(5){|i| @game.push(1, i) } @game.bingo?.should be_true end end end ふるまいをかいた $ spec -cfs test/bing_spec.rb bingo-game ビンゴしている場合 - #bingo? にtrueを返すこと bingo-game たて一列ビンゴの場合 - #bingo? にtrueを返すこと (FAILED - 1) 1) NoMethodError in 'bingo-game たて一列ビンゴの場合 #bingo? にtrueを返すこと' undefined method `push' for #<Bingo:0xb7d5fe78> ./test/bing_spec.rb:18: ./test/bing_spec.rb:17:in `upto' ./test/bing_spec.rb:17: Finished in 0.003635 seconds 2 examples, 1 failure レッド!pushがないよ class Bingo def bingo? true end def push(x, y) end end pushを実装 $ spec -cfs test/bing_spec.rb bingo-game ビンゴしている場合 - #bingo? にtrueを返すこと bingo-game たて一列ビンゴの場合 - #bingo? にtrueを返すこと Finished in 0.007959 seconds 2 examples, 0 failures グリーン! $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib")) require 'rubygems' require 'spec' require 'bingo' describe "bingo-game" do describe "ビンゴしている場合" do it "#bingo? にtrueを返すこと" do @game = Bingo.new @game.bingo?.should be_true end end describe "たて一列ビンゴの場合" do it "#bingo? にtrueを返すこと" do @game = Bingo.new 1.upto(5){|i| @game.push(1, i) } @game.bingo?.should be_true end it "#bingo? にfalseを返すこと" do @game = Bingo.new @game.push(2, 2) 1.upto(4){|i| @game.push(1, i) } @game.bingo?.should be_false end end end たてビンゴしないパターン $ spec -cfs test/bing_spec.rb bingo-game ビンゴしている場合 - #bingo? にtrueを返すこと bingo-game たて一列ビンゴの場合 - #bingo? にtrueを返すこと - #bingo? にfalseを返すこと (FAILED - 1) 1) 'bingo-game たて一列ビンゴの場合 #bingo? にfalseを返すこと' FAILED expected false, got true ./test/bing_spec.rb:29: Finished in 0.008156 seconds 3 examples, 1 failure 通るようにフェイクを作る class Bingo def initialize @stocks = [] end def bingo? @stocks.each {|point| return false if point[0] != 1 } return true end def push(x, y) @stocks << [x, y] end end $ spec -cfs test/bing_spec.rb bingo-game ビンゴしている場合 - #bingo? にtrueを返すこと bingo-game たて一列ビンゴの場合 - #bingo? にtrueを返すこと - #bingo? にfalseを返すこと Finished in 0.008849 seconds 3 examples, 0 failures グリーン。 point[0]っていやな感じなので class Bingo def initialize @stocks = [] end def bingo? @stocks.each {|point| return false if point['x'] != 1 } return true end def push(x, y) @stocks << { 'x' => x, 'y' => y, } end end ハッシュにする。 グリーン。今日はここまで。 たてと横と斜めをなめていくつもりだったんだけど ticktack.rb at 90544bea77966eec25ed7a3efa214737c58cd2af from tsuyoshikawa's misc - GitHub これ見て思ったのは5*5なら12パターン羅列したほうが圧倒的に早いな もとい。リーチとか3列ビンゴで勝ちとかやろうとするとなめるほうがいいのかも。頑張るべ。 |
bingo >