From 15094ec7923af043f89ac47a047fcb4131f1c201 Mon Sep 17 00:00:00 2001 From: m-pitera Date: Wed, 18 Jul 2018 16:18:47 -0500 Subject: [PATCH 01/14] calculating red and white pins for feedback --- lib/secret_code.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/secret_code.rb b/lib/secret_code.rb index 9aa9327..29ccc37 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -14,5 +14,52 @@ def gen_secret_code return (1..4).map { |_| randomness.rand(1..6) } end - def compare_to_secret_code; end + # most of this is built to acompany the presence of a class that stores all data + # with that in mind there will be no params necessary as feedback and guess + # and all these vars will be referring to themselves as values within a hash + def compare_to_secret_code(guess) + if guess == code + red_pin = 4 + return 'win' + else + feedback = Array.new(5) {'blank'} + feedback = check_for_red(guess, feedback) + feedback = check_for_white(guess, feedback) + count_pins(feedback) + end + end + + private + + def check_for_red(guess) + guess_index = 0 + + until guess_index == 4 do + feedback[guess_index] = 'red' if guess[guess_index] = code[guess_index] + guess_index += 1 + end + + return feedback + end + + def check_for_white(guess) + guess_index = 0 + code_index = 0 + + until guess_index == 4 do + until code_index == 4 do + next if feedback[code_index] != 'blank' + feedback[code_index] = 'white' if guess[guess_index] = code[code_index] + end + end + + return feedback + end + + def count_pins(feedback) + feedback.each do |pin| + red_pin += 1 if pin == 'red' + white_pin += 1 if pin == 'white' + end + end end From 11dcb53bff307351c67871192290834dc3171ccc Mon Sep 17 00:00:00 2001 From: m-pitera Date: Thu, 19 Jul 2018 11:03:30 -0500 Subject: [PATCH 02/14] created an outline for secret_code_spec file --- spec/secret_code_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 spec/secret_code_spec.rb diff --git a/spec/secret_code_spec.rb b/spec/secret_code_spec.rb new file mode 100644 index 0000000..dbb4d44 --- /dev/null +++ b/spec/secret_code_spec.rb @@ -0,0 +1,26 @@ +require '../lib/secret_code' + +RSpec.describe SecretCode do + describe '#initialize' do + xit 'generates a secrect code when initialized' do + + end + end + describe '#gen_secret_code' do + context 'creates a code that' do + xit 'is 4 characters' do + + end + xit 'only contains numbers 1-6' do + + end + end + end + describe '#compare_to_secret_code' do + context 'will correctly assign red and white pins' do + xit "WIP" do + + end + end + end +end From 2576c34fadfa44daa3f7865195a5d3e4061ee444 Mon Sep 17 00:00:00 2001 From: m-pitera Date: Thu, 19 Jul 2018 11:32:16 -0500 Subject: [PATCH 03/14] more tests, more outline --- spec/secret_code_spec.rb | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/spec/secret_code_spec.rb b/spec/secret_code_spec.rb index dbb4d44..00068a8 100644 --- a/spec/secret_code_spec.rb +++ b/spec/secret_code_spec.rb @@ -3,24 +3,42 @@ RSpec.describe SecretCode do describe '#initialize' do xit 'generates a secrect code when initialized' do - + # mock to check that gen_secret_code is called + # but would that be testing behavior? end end + describe '#gen_secret_code' do context 'creates a code that' do + code = gen_secret_code xit 'is 4 characters' do - + expect(code.length).to eq 4 end xit 'only contains numbers 1-6' do - + expect(code.all? { |val| val.to_i.between?(1, 6) }).to eq true end end end - describe '#compare_to_secret_code' do - context 'will correctly assign red and white pins' do - xit "WIP" do - end + describe '#compare_to_secret_code' do + context 'will correctly assign pins when' do + xit 'condition 1' do; end + xit 'condition 2' do; end + xit 'condition 3' do; end + end + end + describe '#check_for_white' do + context 'will correctly assign red pins when' do + xit 'condition 1' do; end + xit 'condition 2' do; end + xit 'condition 3' do; end + end + end + describe '#check_for_white' do + context 'will correctly assign white pins when' do + xit 'condition 1' do; end + xit 'condition 2' do; end + xit 'condition 3' do; end end end end From 87fd10e5470f3dd04525ca36002ae50407a2af6e Mon Sep 17 00:00:00 2001 From: m-pitera Date: Thu, 19 Jul 2018 12:03:17 -0500 Subject: [PATCH 04/14] pseudo tests? --- lib/secret_code.rb | 2 +- spec/secret_code_spec.rb | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/secret_code.rb b/lib/secret_code.rb index 29ccc37..13ebc4c 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -22,7 +22,7 @@ def compare_to_secret_code(guess) red_pin = 4 return 'win' else - feedback = Array.new(5) {'blank'} + feedback = Array.new(4) {'blank'} feedback = check_for_red(guess, feedback) feedback = check_for_white(guess, feedback) count_pins(feedback) diff --git a/spec/secret_code_spec.rb b/spec/secret_code_spec.rb index 00068a8..1af2577 100644 --- a/spec/secret_code_spec.rb +++ b/spec/secret_code_spec.rb @@ -11,31 +11,53 @@ describe '#gen_secret_code' do context 'creates a code that' do code = gen_secret_code - xit 'is 4 characters' do + it 'is 4 characters' do expect(code.length).to eq 4 end - xit 'only contains numbers 1-6' do + it 'only contains numbers 1-6' do expect(code.all? { |val| val.to_i.between?(1, 6) }).to eq true end end end - describe '#compare_to_secret_code' do - context 'will correctly assign pins when' do - xit 'condition 1' do; end + # not sure if i should be testing this? + describe '#check_for_red' do + context 'will correctly assign red pins when' do + it 'condition 1' do; end xit 'condition 2' do; end xit 'condition 3' do; end end end + + # not sure if i should be testing this? describe '#check_for_white' do - context 'will correctly assign red pins when' do + context 'will correctly assign white pins when' do xit 'condition 1' do; end xit 'condition 2' do; end xit 'condition 3' do; end end end - describe '#check_for_white' do - context 'will correctly assign white pins when' do + + # not sure if i should be testing this? + describe '#count_pins' do + context 'will correctly count up the pins' do + xit 'when all pins white' do + feedback = Array.new(4) {'red'} + count_pins(feedback) + expect(red_pin).to eq 4 && white_pin.to eq 0 + end + xit 'when all pins red' do + feedback = Array.new(4) {'white'} + count_pins(feedback) + expect(red_pin).to eq 4 && white_pin.to eq 0 + end + xit 'when ?' do + + end + end + end + describe '#compare_to_secret_code' do + context 'will correctly assign pins when' do xit 'condition 1' do; end xit 'condition 2' do; end xit 'condition 3' do; end From 7169170cdaaf3d790305c106a9038c0bc6facb22 Mon Sep 17 00:00:00 2001 From: m-pitera Date: Fri, 20 Jul 2018 10:36:10 -0500 Subject: [PATCH 05/14] WIP: attempting to implement feedback logic --- lib/game_status.rb | 2 +- lib/main.rb | 10 ++++++++-- lib/output.rb | 5 ++++- lib/secret_code.rb | 21 +++++++++++++++------ lib/user.rb | 34 +++++++++++++++------------------- 5 files changed, 43 insertions(+), 29 deletions(-) diff --git a/lib/game_status.rb b/lib/game_status.rb index 37b0e0f..097b590 100644 --- a/lib/game_status.rb +++ b/lib/game_status.rb @@ -15,7 +15,7 @@ def game_over?(guesses_left) private def guesses_left?(guesses_left) - !guesses_left.zero? + guesses_left != 0 end def win? diff --git a/lib/main.rb b/lib/main.rb index e4b8d96..ce3ddcd 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -20,8 +20,14 @@ def main Output.print_intro the_secret_code = SecretCode.new - the_user.take_input - the_user.give_feedback + the_secret_code.compare_to_secret_code(the_user.take_input) + again(the_user, the_secret_code) +end + +def again(user, code) + GameStatus.game_over?(user.guesses_left) + code.compare_to_secret_code(user.take_input) + again(user, code) end if __FILE__ == $PROGRAM_NAME diff --git a/lib/output.rb b/lib/output.rb index f662a05..7d7c73b 100644 --- a/lib/output.rb +++ b/lib/output.rb @@ -1,6 +1,5 @@ require_relative './user' require_relative './colors' -require_relative './secret_code' class Output class << self @@ -111,6 +110,10 @@ def print_win puts WIN end + def print_feedback(red_pin, white_pin) + puts "Red Pins: #{red_pin}\t White Pins: #{white_pin}" + end + private def instructions diff --git a/lib/secret_code.rb b/lib/secret_code.rb index 13ebc4c..78f2d61 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -18,20 +18,23 @@ def gen_secret_code # with that in mind there will be no params necessary as feedback and guess # and all these vars will be referring to themselves as values within a hash def compare_to_secret_code(guess) - if guess == code - red_pin = 4 + feedback = Array.new(4) {'blank'} + if guess == @code + feedback = Array.new(4) {'red'} + puts 'test win' return 'win' else - feedback = Array.new(4) {'blank'} feedback = check_for_red(guess, feedback) feedback = check_for_white(guess, feedback) - count_pins(feedback) end + + pins = count_pins(feedback) + Output.print_feedback(pins[0], pins[1]) end private - def check_for_red(guess) + def check_for_red(guess, feedback) guess_index = 0 until guess_index == 4 do @@ -42,24 +45,30 @@ def check_for_red(guess) return feedback end - def check_for_white(guess) + def check_for_white(guess, feedback) guess_index = 0 code_index = 0 until guess_index == 4 do until code_index == 4 do + code_index += 1 next if feedback[code_index] != 'blank' feedback[code_index] = 'white' if guess[guess_index] = code[code_index] end + guess_index += 1 end return feedback end def count_pins(feedback) + red_pin = 0 + white_pin = 0 feedback.each do |pin| red_pin += 1 if pin == 'red' white_pin += 1 if pin == 'white' end + + return [red_pin, white_pin] end end diff --git a/lib/user.rb b/lib/user.rb index c78f3d2..46c5d07 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -23,15 +23,15 @@ def take_input @guess = the_input @guesses_left -= 1 - next_command('input') + return @guess end - def give_feedback(p_input = @guess) - GameStatus.game_over?(@guesses_left) - - puts("I haven't coded the feedback yet :)") - next_command('feedback') - end + # def give_feedback(p_input = @guess) + # GameStatus.game_over?(@guesses_left) + # + # puts("I haven't coded the feedback yet :)") + # next_command('feedback') + # end def prompt_start system 'clear' @@ -41,19 +41,15 @@ def prompt_start return gets.chomp end - private + private - def next_command(previous) - if (previous.eql? 'input') - give_feedback - return - elsif (previous.eql? 'feedback') - take_input - return - else - return - end - end + # def next_command(previous) + # if (previous.eql? 'input') + # give_feedback + # elsif (previous.eql? 'feedback') + # take_input + # end + # end def is_quit?(user_input) if (user_input.downcase.eql? 'quit') From 89b2cc124b886e94829f0789e1553723e6dee8b2 Mon Sep 17 00:00:00 2001 From: m-pitera Date: Fri, 20 Jul 2018 11:45:42 -0500 Subject: [PATCH 06/14] fixed check_for_red, white is WIP --- lib/secret_code.rb | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/secret_code.rb b/lib/secret_code.rb index 78f2d61..20a9460 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -1,4 +1,5 @@ require './output' +require 'pry' class SecretCode attr_reader :code @@ -17,9 +18,18 @@ def gen_secret_code # most of this is built to acompany the presence of a class that stores all data # with that in mind there will be no params necessary as feedback and guess # and all these vars will be referring to themselves as values within a hash - def compare_to_secret_code(guess) + def compare_to_secret_code(guess_s) + guess = [0, 0, 0, 0] + i = 0 + guess_s.each do |val| + guess[i] = val.to_i + i += 1 + end + + print guess + print code feedback = Array.new(4) {'blank'} - if guess == @code + if guess == code feedback = Array.new(4) {'red'} puts 'test win' return 'win' @@ -38,10 +48,16 @@ def check_for_red(guess, feedback) guess_index = 0 until guess_index == 4 do - feedback[guess_index] = 'red' if guess[guess_index] = code[guess_index] + print guess[guess_index] + print code[guess_index] + if guess[guess_index] == code[guess_index] + feedback[guess_index] = 'red' + binding.pry + end guess_index += 1 end + print feedback return feedback end @@ -51,13 +67,17 @@ def check_for_white(guess, feedback) until guess_index == 4 do until code_index == 4 do - code_index += 1 - next if feedback[code_index] != 'blank' + binding.pry + if feedback[code_index] != 'blank' + code_index += 1 + next + end feedback[code_index] = 'white' if guess[guess_index] = code[code_index] + code_index += 1 end guess_index += 1 end - + # print feedback return feedback end From 1b3afdaef9dcd20b56944c0785ab8c846c93edca Mon Sep 17 00:00:00 2001 From: m-pitera Date: Tue, 24 Jul 2018 11:37:44 -0500 Subject: [PATCH 07/14] WIP white still not work completely weird bug that the game takes inupt without actually taking it when guess has all the correct nums, but in the wrong order --- lib/secret_code.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/secret_code.rb b/lib/secret_code.rb index 20a9460..f38b527 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -30,8 +30,8 @@ def compare_to_secret_code(guess_s) print code feedback = Array.new(4) {'blank'} if guess == code - feedback = Array.new(4) {'red'} puts 'test win' + feedback = Array.new(4) {'red'} return 'win' else feedback = check_for_red(guess, feedback) @@ -52,7 +52,6 @@ def check_for_red(guess, feedback) print code[guess_index] if guess[guess_index] == code[guess_index] feedback[guess_index] = 'red' - binding.pry end guess_index += 1 end @@ -66,18 +65,30 @@ def check_for_white(guess, feedback) code_index = 0 until guess_index == 4 do + code_index = 0 until code_index == 4 do - binding.pry if feedback[code_index] != 'blank' + if feedback[code_index] == 'red' + guess_index += 1 + end code_index += 1 next end - feedback[code_index] = 'white' if guess[guess_index] = code[code_index] + # binding.pry + if guess[guess_index] == code[code_index] + # binding.pry + feedback[code_index] = 'white' + guess_index += 1 + code_index = 0 + end code_index += 1 end + guess_index += 1 end - # print feedback + print feedback + print guess_index + print code_index return feedback end From b7b02102bf9008b0c98384d5b7f46828f68d1c04 Mon Sep 17 00:00:00 2001 From: m-pitera Date: Wed, 25 Jul 2018 10:01:12 -0500 Subject: [PATCH 08/14] IT WORKS, fixed white pin logic. Pin logic now implemented. The previous issue was that using the until loop I was treating as a while or something weird. --- lib/main.rb | 4 +- lib/secret_code.rb | 91 ++++++++++++++++++++-------------------------- 2 files changed, 42 insertions(+), 53 deletions(-) diff --git a/lib/main.rb b/lib/main.rb index ce3ddcd..235ba49 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -20,13 +20,13 @@ def main Output.print_intro the_secret_code = SecretCode.new - the_secret_code.compare_to_secret_code(the_user.take_input) + the_secret_code.compare_to_guess(the_user.take_input) again(the_user, the_secret_code) end def again(user, code) GameStatus.game_over?(user.guesses_left) - code.compare_to_secret_code(user.take_input) + code.compare_to_guess(user.take_input) again(user, code) end diff --git a/lib/secret_code.rb b/lib/secret_code.rb index f38b527..5a5cf3f 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -18,28 +18,20 @@ def gen_secret_code # most of this is built to acompany the presence of a class that stores all data # with that in mind there will be no params necessary as feedback and guess # and all these vars will be referring to themselves as values within a hash - def compare_to_secret_code(guess_s) - guess = [0, 0, 0, 0] - i = 0 - guess_s.each do |val| - guess[i] = val.to_i - i += 1 - end + def compare_to_guess(guess_s) + guess = format_guess(guess_s) print guess print code - feedback = Array.new(4) {'blank'} - if guess == code - puts 'test win' - feedback = Array.new(4) {'red'} - return 'win' - else + + unless win?(guess, code) + feedback = Array.new(4) {'blank'} feedback = check_for_red(guess, feedback) + print feedback feedback = check_for_white(guess, feedback) + pins = count_pins(feedback) + Output.print_feedback(pins[0], pins[1]) end - - pins = count_pins(feedback) - Output.print_feedback(pins[0], pins[1]) end private @@ -47,59 +39,56 @@ def compare_to_secret_code(guess_s) def check_for_red(guess, feedback) guess_index = 0 + # change 4 to global var + # potentially consider 4.times loop while still iterating thru until guess_index == 4 do - print guess[guess_index] - print code[guess_index] - if guess[guess_index] == code[guess_index] - feedback[guess_index] = 'red' - end + feedback[guess_index] = 'red' if guess[guess_index] == code[guess_index] guess_index += 1 end - print feedback return feedback end def check_for_white(guess, feedback) - guess_index = 0 - code_index = 0 - - until guess_index == 4 do - code_index = 0 - until code_index == 4 do + 4.times do |guess_index| + puts 'test1' + 4.times do |code_index| if feedback[code_index] != 'blank' - if feedback[code_index] == 'red' - guess_index += 1 - end - code_index += 1 + guess_index += 1 if feedback[code_index] == 'red' next + puts 'should not run' end - # binding.pry - if guess[guess_index] == code[code_index] - # binding.pry - feedback[code_index] = 'white' - guess_index += 1 - code_index = 0 - end - code_index += 1 - end - guess_index += 1 + feedback[code_index] = 'white' if guess[guess_index] == code[code_index] + end end - print feedback - print guess_index - print code_index return feedback end def count_pins(feedback) - red_pin = 0 - white_pin = 0 - feedback.each do |pin| - red_pin += 1 if pin == 'red' - white_pin += 1 if pin == 'white' - end + red_pin = feedback.count('red') + white_pin = feedback.count('white') return [red_pin, white_pin] end + + def format_guess(guess_s) + guess = [0, 0, 0, 0] + i = 0 + guess_s.each do |val| + guess[i] = val.to_i + i += 1 + end + return guess + end + + # these 2 methods are practically just conditionals + # should I simply move them to the case they are used? + def win?(guess, code) + return guess == code + end + + def is_red?(feedback, index) + return feedback[index] == 'red' + end end From 61d496206cda88e33918842b28ea251584444c8a Mon Sep 17 00:00:00 2001 From: m-pitera Date: Wed, 25 Jul 2018 11:46:56 -0500 Subject: [PATCH 09/14] added and revised tests for SecretCode the tests are mainly regarding dif cases that compare_to_guess will output correctly --- lib/secret_code.rb | 1 + spec/secret_code_spec.rb | 67 ++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/lib/secret_code.rb b/lib/secret_code.rb index 5a5cf3f..fe5c4c6 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -31,6 +31,7 @@ def compare_to_guess(guess_s) feedback = check_for_white(guess, feedback) pins = count_pins(feedback) Output.print_feedback(pins[0], pins[1]) + return pins end end diff --git a/spec/secret_code_spec.rb b/spec/secret_code_spec.rb index 1af2577..c5cc8b6 100644 --- a/spec/secret_code_spec.rb +++ b/spec/secret_code_spec.rb @@ -4,7 +4,7 @@ describe '#initialize' do xit 'generates a secrect code when initialized' do # mock to check that gen_secret_code is called - # but would that be testing behavior? + # but would that be testing behavior or simply ruby? end end @@ -20,24 +20,6 @@ end end - # not sure if i should be testing this? - describe '#check_for_red' do - context 'will correctly assign red pins when' do - it 'condition 1' do; end - xit 'condition 2' do; end - xit 'condition 3' do; end - end - end - - # not sure if i should be testing this? - describe '#check_for_white' do - context 'will correctly assign white pins when' do - xit 'condition 1' do; end - xit 'condition 2' do; end - xit 'condition 3' do; end - end - end - # not sure if i should be testing this? describe '#count_pins' do context 'will correctly count up the pins' do @@ -51,16 +33,49 @@ count_pins(feedback) expect(red_pin).to eq 4 && white_pin.to eq 0 end - xit 'when ?' do - - end + xit 'when ?' do; end end end - describe '#compare_to_secret_code' do + + # am I just testing random cases at this point? is this ok? + describe '#compare_to_guess' do context 'will correctly assign pins when' do - xit 'condition 1' do; end - xit 'condition 2' do; end - xit 'condition 3' do; end + xit '2 red and 2 white expected' do + code = SecretCode.new + code.code = [1, 1, 2, 2] + guess_s = ['1', '2', '2', '1'] + expect(code.compare_to_guess(guess_s)).to eq [2, 2] + end + xit '2 red and 0 white expected' do + code = SecretCode.new + code.code = [1, 1, 1, 1] + guess_s = ['1', '1', '2', '2'] + expect(code.compare_to_guess(guess_s)).to eq [2, 0] + end + xit '1 red and 2 white expected' do + code = SecretCode.new + code.code = [1, 2, 3, 4] + guess_s = ['1', '3', '2', '1'] + expect(code.compare_to_guess(guess_s)).to eq [1, 2] + end + xit '3 red and 0 white expected' do + code = SecretCode.new + code.code = [1, 1, 1, 2] + guess_s = ['1', '1', '1', '1'] + expect(code.compare_to_guess(guess_s)).to eq [3, 0] + end + xit '0 red and 4 white expected' do + code = SecretCode.new + code.code = [1, 2, 3, 4] + guess_s = ['4', '3', '2', '1'] + expect(code.compare_to_guess(guess_s)).to eq [0, 4] + end + xit '4 red and 0 white expected' do + code = SecretCode.new + code.code = [1, 1, 1, 1] + guess_s = ['1', '1', '1', '1'] + expect(code.compare_to_guess(guess_s)).to eq [4, 0] + end end end end From c3845e9f5cabb37509bc3328b980747d8d323ff2 Mon Sep 17 00:00:00 2001 From: m-pitera Date: Wed, 25 Jul 2018 12:13:51 -0500 Subject: [PATCH 10/14] cleaned up the secret_code class --- lib/secret_code.rb | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/lib/secret_code.rb b/lib/secret_code.rb index fe5c4c6..76f55ca 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -4,6 +4,8 @@ class SecretCode attr_reader :code + LENGTH_OF_CODE = 4; + def initialize @code = gen_secret_code Output.gimme_de_code(@code) @@ -12,7 +14,7 @@ def initialize def gen_secret_code randomness = Random.new Output.print_code_generated - return (1..4).map { |_| randomness.rand(1..6) } + return (1..LENGTH_OF_CODE).map { |_| randomness.rand(1..6) } end # most of this is built to acompany the presence of a class that stores all data @@ -21,13 +23,9 @@ def gen_secret_code def compare_to_guess(guess_s) guess = format_guess(guess_s) - print guess - print code - unless win?(guess, code) - feedback = Array.new(4) {'blank'} + feedback = Array.new(LENGTH_OF_CODE) {'blank'} feedback = check_for_red(guess, feedback) - print feedback feedback = check_for_white(guess, feedback) pins = count_pins(feedback) Output.print_feedback(pins[0], pins[1]) @@ -38,26 +36,19 @@ def compare_to_guess(guess_s) private def check_for_red(guess, feedback) - guess_index = 0 - - # change 4 to global var - # potentially consider 4.times loop while still iterating thru - until guess_index == 4 do + LENGTH_OF_CODE.times do |guess_index| feedback[guess_index] = 'red' if guess[guess_index] == code[guess_index] - guess_index += 1 end return feedback end def check_for_white(guess, feedback) - 4.times do |guess_index| - puts 'test1' - 4.times do |code_index| + LENGTH_OF_CODE.times do |guess_index| + LENGTH_OF_CODE.times do |code_index| if feedback[code_index] != 'blank' guess_index += 1 if feedback[code_index] == 'red' next - puts 'should not run' end feedback[code_index] = 'white' if guess[guess_index] == code[code_index] @@ -74,22 +65,18 @@ def count_pins(feedback) end def format_guess(guess_s) - guess = [0, 0, 0, 0] - i = 0 - guess_s.each do |val| - guess[i] = val.to_i - i += 1 + guess = Array.new(LENGTH_OF_CODE) + + LENGTH_OF_CODE.times do |val| + guess[val] = guess_s[val].to_i end + return guess end - # these 2 methods are practically just conditionals - # should I simply move them to the case they are used? + # this method is practically just a conditional statement + # should I move it to the case they in which it's used? def win?(guess, code) return guess == code end - - def is_red?(feedback, index) - return feedback[index] == 'red' - end end From 386d441aa1b723f2aeb2e16560b79cdc4a0755ca Mon Sep 17 00:00:00 2001 From: m-pitera Date: Wed, 25 Jul 2018 12:14:18 -0500 Subject: [PATCH 11/14] updated README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 692b3f6..d847678 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,4 @@ ## Current Capabilities - The game's current capabilities are that it will prompt at start to begin and react accordingly. After the instructions it will process guesses as valid or invalid and give instructions according to what was incorrect. The User will not receive feedback yet and it is impossible to win the game, but after all 10 guesses the secret code will be presented and the user will be prompted whether they'd like to restart. The user may also type 'quit' during the guess phase at any time and the game will exit. + The game's current capabilities are that it will prompt at start to begin and react accordingly. After the instructions it will process guesses as valid or invalid and give instructions according to what was incorrect. The User will receive feedback, but it is impossible to win the game. After all 10 guesses the secret code will be presented and the user will be prompted whether they'd like to restart. The user may also type 'quit' during the guess phase at any time and the game will exit. From ee3fdda609f5efc855b3113ec317d0a1390c0e58 Mon Sep 17 00:00:00 2001 From: m-pitera Date: Wed, 25 Jul 2018 12:41:52 -0500 Subject: [PATCH 12/14] fixed require statements and suspended spec tests forgot to revise some of the tests --- lib/game_functions.rb | 2 +- lib/game_status.rb | 6 +++--- lib/input.rb | 2 +- lib/main.rb | 14 +++++++------- lib/output.rb | 4 ++-- lib/secret_code.rb | 1 - lib/user.rb | 6 +++--- lib/validate.rb | 2 +- spec/secret_code_spec.rb | 26 +++++++++++++------------- 9 files changed, 31 insertions(+), 32 deletions(-) diff --git a/lib/game_functions.rb b/lib/game_functions.rb index 2dc4556..2d66834 100644 --- a/lib/game_functions.rb +++ b/lib/game_functions.rb @@ -1,4 +1,4 @@ -require './output' +require_relative 'output' class GameFunctions class << self diff --git a/lib/game_status.rb b/lib/game_status.rb index 097b590..a55d722 100644 --- a/lib/game_status.rb +++ b/lib/game_status.rb @@ -1,5 +1,5 @@ -require './output' -require './game_functions' +require_relative 'output' +require_relative 'game_functions' module GameStatus # will have 0 params once I implement a class that holds all in-game info @@ -15,7 +15,7 @@ def game_over?(guesses_left) private def guesses_left?(guesses_left) - guesses_left != 0 + guesses_left.positive? end def win? diff --git a/lib/input.rb b/lib/input.rb index 76d29a1..f15bdca 100644 --- a/lib/input.rb +++ b/lib/input.rb @@ -1,4 +1,4 @@ -require './output' +require_relative 'output' class Input end diff --git a/lib/main.rb b/lib/main.rb index 235ba49..f0ff1f0 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -1,8 +1,8 @@ -require './user' -require './game_status' -require './colors' -require './secret_code' -require './output' +require_relative 'user' +require_relative 'game_status' +require_relative 'colors' +require_relative 'secret_code' +require_relative 'output' # infinite refactoring in progress # ironically it's messier than ever before @@ -21,10 +21,10 @@ def main Output.print_intro the_secret_code = SecretCode.new the_secret_code.compare_to_guess(the_user.take_input) - again(the_user, the_secret_code) + ask_again(the_user, the_secret_code) end -def again(user, code) +def ask_again(user, code) GameStatus.game_over?(user.guesses_left) code.compare_to_guess(user.take_input) again(user, code) diff --git a/lib/output.rb b/lib/output.rb index 7d7c73b..c9ca4f1 100644 --- a/lib/output.rb +++ b/lib/output.rb @@ -1,5 +1,5 @@ -require_relative './user' -require_relative './colors' +require_relative 'user' +require_relative 'colors' class Output class << self diff --git a/lib/secret_code.rb b/lib/secret_code.rb index 76f55ca..c54ecdc 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -1,5 +1,4 @@ require './output' -require 'pry' class SecretCode attr_reader :code diff --git a/lib/user.rb b/lib/user.rb index 46c5d07..d344af8 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,6 +1,6 @@ -require './output' -require './validate' -require './game_status' +require_relative 'output' +require_relative 'validate' +require_relative 'game_status' # User class knows too much, i made a mess and now refactoring class User diff --git a/lib/validate.rb b/lib/validate.rb index a2a8b0a..6519a00 100644 --- a/lib/validate.rb +++ b/lib/validate.rb @@ -1,4 +1,4 @@ -require './output' +require_relative 'output' module Validate def is_valid?(the_input) diff --git a/spec/secret_code_spec.rb b/spec/secret_code_spec.rb index c5cc8b6..e71a657 100644 --- a/spec/secret_code_spec.rb +++ b/spec/secret_code_spec.rb @@ -1,4 +1,4 @@ -require '../lib/secret_code' +require 'secret_code' RSpec.describe SecretCode do describe '#initialize' do @@ -11,11 +11,11 @@ describe '#gen_secret_code' do context 'creates a code that' do code = gen_secret_code - it 'is 4 characters' do - expect(code.length).to eq 4 + xit 'is 4 characters' do + expect(code.length).to_eq 4 end - it 'only contains numbers 1-6' do - expect(code.all? { |val| val.to_i.between?(1, 6) }).to eq true + xit 'only contains numbers 1-6' do + expect(code.all? { |val| val.to_i.between?(1, 6) }).to_eq true end end end @@ -26,12 +26,12 @@ xit 'when all pins white' do feedback = Array.new(4) {'red'} count_pins(feedback) - expect(red_pin).to eq 4 && white_pin.to eq 0 + # expect(red_pin).to_eq 4 && white_pin.to_eq 0 end xit 'when all pins red' do feedback = Array.new(4) {'white'} count_pins(feedback) - expect(red_pin).to eq 4 && white_pin.to eq 0 + # expect(red_pin).to_eq 4 && white_pin.to_eq 0 end xit 'when ?' do; end end @@ -44,37 +44,37 @@ code = SecretCode.new code.code = [1, 1, 2, 2] guess_s = ['1', '2', '2', '1'] - expect(code.compare_to_guess(guess_s)).to eq [2, 2] + expect(code.compare_to_guess(guess_s)).to_eq [2, 2] end xit '2 red and 0 white expected' do code = SecretCode.new code.code = [1, 1, 1, 1] guess_s = ['1', '1', '2', '2'] - expect(code.compare_to_guess(guess_s)).to eq [2, 0] + expect(code.compare_to_guess(guess_s)).to_eq [2, 0] end xit '1 red and 2 white expected' do code = SecretCode.new code.code = [1, 2, 3, 4] guess_s = ['1', '3', '2', '1'] - expect(code.compare_to_guess(guess_s)).to eq [1, 2] + expect(code.compare_to_guess(guess_s)).to_eq [1, 2] end xit '3 red and 0 white expected' do code = SecretCode.new code.code = [1, 1, 1, 2] guess_s = ['1', '1', '1', '1'] - expect(code.compare_to_guess(guess_s)).to eq [3, 0] + expect(code.compare_to_guess(guess_s)).to_eq [3, 0] end xit '0 red and 4 white expected' do code = SecretCode.new code.code = [1, 2, 3, 4] guess_s = ['4', '3', '2', '1'] - expect(code.compare_to_guess(guess_s)).to eq [0, 4] + expect(code.compare_to_guess(guess_s)).to_eq [0, 4] end xit '4 red and 0 white expected' do code = SecretCode.new code.code = [1, 1, 1, 1] guess_s = ['1', '1', '1', '1'] - expect(code.compare_to_guess(guess_s)).to eq [4, 0] + expect(code.compare_to_guess(guess_s)).to_eq [4, 0] end end end From 03029be2ddeac975030503edb29c8d8c4fcb7096 Mon Sep 17 00:00:00 2001 From: m-pitera Date: Wed, 25 Jul 2018 14:16:43 -0500 Subject: [PATCH 13/14] revised tests again --- lib/secret_code.rb | 2 +- spec/secret_code_spec.rb | 62 ++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/lib/secret_code.rb b/lib/secret_code.rb index c54ecdc..20c4de5 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -1,7 +1,7 @@ require './output' class SecretCode - attr_reader :code + attr_accessor :code LENGTH_OF_CODE = 4; diff --git a/spec/secret_code_spec.rb b/spec/secret_code_spec.rb index e71a657..7121276 100644 --- a/spec/secret_code_spec.rb +++ b/spec/secret_code_spec.rb @@ -1,6 +1,8 @@ require 'secret_code' RSpec.describe SecretCode do + let(:secret_code) {SecretCode.new} + describe '#initialize' do xit 'generates a secrect code when initialized' do # mock to check that gen_secret_code is called @@ -10,11 +12,11 @@ describe '#gen_secret_code' do context 'creates a code that' do - code = gen_secret_code - xit 'is 4 characters' do + code = secret_code.gen_secret_code + it 'is 4 characters' do expect(code.length).to_eq 4 end - xit 'only contains numbers 1-6' do + it 'only contains numbers 1-6' do expect(code.all? { |val| val.to_i.between?(1, 6) }).to_eq true end end @@ -23,58 +25,50 @@ # not sure if i should be testing this? describe '#count_pins' do context 'will correctly count up the pins' do - xit 'when all pins white' do + it 'when all pins white' do feedback = Array.new(4) {'red'} - count_pins(feedback) - # expect(red_pin).to_eq 4 && white_pin.to_eq 0 + expect(count_pins(feedback)).to_eq [4, 0] end - xit 'when all pins red' do + it 'when all pins red' do feedback = Array.new(4) {'white'} count_pins(feedback) - # expect(red_pin).to_eq 4 && white_pin.to_eq 0 + expect(count_pins(feedback)).to_eq [4, 0] end - xit 'when ?' do; end end end - # am I just testing random cases at this point? is this ok? describe '#compare_to_guess' do + # am I just testing random cases at this point? is this ok? context 'will correctly assign pins when' do - xit '2 red and 2 white expected' do - code = SecretCode.new - code.code = [1, 1, 2, 2] + it '2 red and 2 white expected' do + secret_code.code = [1, 1, 2, 2] guess_s = ['1', '2', '2', '1'] - expect(code.compare_to_guess(guess_s)).to_eq [2, 2] + expect(secret_code.compare_to_guess(guess_s)).to_eq [2, 2] end - xit '2 red and 0 white expected' do - code = SecretCode.new - code.code = [1, 1, 1, 1] + it '2 red and 0 white expected' do + secret_code.code = [1, 1, 1, 1] guess_s = ['1', '1', '2', '2'] - expect(code.compare_to_guess(guess_s)).to_eq [2, 0] + expect(secret_code.compare_to_guess(guess_s)).to_eq [2, 0] end - xit '1 red and 2 white expected' do - code = SecretCode.new - code.code = [1, 2, 3, 4] + it '1 red and 2 white expected' do + secret_code.code = [1, 2, 3, 4] guess_s = ['1', '3', '2', '1'] - expect(code.compare_to_guess(guess_s)).to_eq [1, 2] + expect(secret_code.compare_to_guess(guess_s)).to_eq [1, 2] end - xit '3 red and 0 white expected' do - code = SecretCode.new - code.code = [1, 1, 1, 2] + it '3 red and 0 white expected' do + secret_code.code = [1, 1, 1, 2] guess_s = ['1', '1', '1', '1'] - expect(code.compare_to_guess(guess_s)).to_eq [3, 0] + expect(secret_code.compare_to_guess(guess_s)).to_eq [3, 0] end - xit '0 red and 4 white expected' do - code = SecretCode.new - code.code = [1, 2, 3, 4] + it '0 red and 4 white expected' do + secret_code.code = [1, 2, 3, 4] guess_s = ['4', '3', '2', '1'] - expect(code.compare_to_guess(guess_s)).to_eq [0, 4] + expect(secret_code.compare_to_guess(guess_s)).to_eq [0, 4] end - xit '4 red and 0 white expected' do - code = SecretCode.new - code.code = [1, 1, 1, 1] + it '4 red and 0 white expected' do + secret_code.code = [1, 1, 1, 1] guess_s = ['1', '1', '1', '1'] - expect(code.compare_to_guess(guess_s)).to_eq [4, 0] + expect(secret_code.compare_to_guess(guess_s)).to_eq [4, 0] end end end From 73eecd798c18ad40abba6a52db76734608b5b7c0 Mon Sep 17 00:00:00 2001 From: m-pitera Date: Wed, 25 Jul 2018 14:36:00 -0500 Subject: [PATCH 14/14] removed count_pins method from tests this method was private and didn't provide much behaviour to test --- lib/secret_code.rb | 2 +- lib/user.rb | 6 +++--- spec/secret_code_spec.rb | 15 --------------- 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/lib/secret_code.rb b/lib/secret_code.rb index 20c4de5..fe1bfee 100644 --- a/lib/secret_code.rb +++ b/lib/secret_code.rb @@ -1,4 +1,4 @@ -require './output' +require 'output' class SecretCode attr_accessor :code diff --git a/lib/user.rb b/lib/user.rb index d344af8..e4acb23 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,6 +1,6 @@ -require_relative 'output' -require_relative 'validate' -require_relative 'game_status' +require 'output' +require 'validate' +require 'game_status' # User class knows too much, i made a mess and now refactoring class User diff --git a/spec/secret_code_spec.rb b/spec/secret_code_spec.rb index 7121276..ec9050a 100644 --- a/spec/secret_code_spec.rb +++ b/spec/secret_code_spec.rb @@ -22,21 +22,6 @@ end end - # not sure if i should be testing this? - describe '#count_pins' do - context 'will correctly count up the pins' do - it 'when all pins white' do - feedback = Array.new(4) {'red'} - expect(count_pins(feedback)).to_eq [4, 0] - end - it 'when all pins red' do - feedback = Array.new(4) {'white'} - count_pins(feedback) - expect(count_pins(feedback)).to_eq [4, 0] - end - end - end - describe '#compare_to_guess' do # am I just testing random cases at this point? is this ok? context 'will correctly assign pins when' do