From 374ac7caca3456ba50af5a6c84a18d9b5bc7f39d Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Wed, 10 Jun 2015 12:02:31 -0400 Subject: [PATCH 1/8] Add Game#run_test. Game#run_test is a method that takes in the current data structure, the routine given by the player, and the target answer, and returns whether the target answer matches the output from the routine, or false if there was an exception. --- lib/rubies/game.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/rubies/game.rb b/lib/rubies/game.rb index 0bd92f2..7c1bbb5 100644 --- a/lib/rubies/game.rb +++ b/lib/rubies/game.rb @@ -155,6 +155,17 @@ def check_answer(current, input, target) end end + # test whether the answer provided by the given + # routine matches the target answer + def run_test(current, routine, target) + begin + output = routine.call + output == target + rescue Exception + false + end + end + def generate_data_structure rds = RandomDataStructure.new current = rds.generate From 178ee8b33e11d48c39ec07d84ba987d357077815 Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Wed, 10 Jun 2015 12:26:32 -0400 Subject: [PATCH 2/8] Add Game#tests Game#tests creates an array of values for which any answer given by the player should fail. These include nil, a newly created array, and a new random data structure. --- lib/rubies/game.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/rubies/game.rb b/lib/rubies/game.rb index 7c1bbb5..367c6fa 100644 --- a/lib/rubies/game.rb +++ b/lib/rubies/game.rb @@ -155,6 +155,15 @@ def check_answer(current, input, target) end end + # creates an array of values for which any given answer should fail + def tests(current, target) + [ nil, + Array.new, + Hash.new, + RandomDataStructure.new.generate + ] + end + # test whether the answer provided by the given # routine matches the target answer def run_test(current, routine, target) From cd04421e6db68d0775b247440ddb97d89056b42e Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Wed, 10 Jun 2015 12:56:15 -0400 Subject: [PATCH 3/8] Add Game#deep_delete deep_delete deletes a target element from a data structure, even if that element is nested inside multiple data structures. Perhaps this should be moved, as it is not really a game-related method; but I am not sure to where. --- lib/rubies/game.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/rubies/game.rb b/lib/rubies/game.rb index 367c6fa..0c868a3 100644 --- a/lib/rubies/game.rb +++ b/lib/rubies/game.rb @@ -155,6 +155,14 @@ def check_answer(current, input, target) end end + # deletes the target from the current data structure + def deep_delete(current, target) + return current unless current.is_a? Enumerable + current. + reject {|value| value == target}. + map {|value| deep_delete(value, target)} + end + # creates an array of values for which any given answer should fail def tests(current, target) [ nil, From 9aa85416ca6ccb17512709c884ea1e886fbb8554 Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Wed, 10 Jun 2015 12:57:54 -0400 Subject: [PATCH 4/8] Add 'current - target' test to Game#tests Added a test that uses deep_delete on the given data structure and target. Users' input should not remain correct when the target is removed from the given data structure. --- lib/rubies/game.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rubies/game.rb b/lib/rubies/game.rb index 0c868a3..2497104 100644 --- a/lib/rubies/game.rb +++ b/lib/rubies/game.rb @@ -168,7 +168,8 @@ def tests(current, target) [ nil, Array.new, Hash.new, - RandomDataStructure.new.generate + RandomDataStructure.new.generate, + deep_delete(current, target) ] end From 444aded18cc9fae8736099d80ee3cd5246644c1d Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Wed, 10 Jun 2015 13:06:55 -0400 Subject: [PATCH 5/8] Retab file to match indentation 4-wide indentations were retabbed to be 2-wide, to match the rest of the file (sorry!). --- lib/rubies/game.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/rubies/game.rb b/lib/rubies/game.rb index 2497104..e11d09c 100644 --- a/lib/rubies/game.rb +++ b/lib/rubies/game.rb @@ -157,31 +157,31 @@ def check_answer(current, input, target) # deletes the target from the current data structure def deep_delete(current, target) - return current unless current.is_a? Enumerable - current. - reject {|value| value == target}. - map {|value| deep_delete(value, target)} + return current unless current.is_a? Enumerable + current. + reject {|value| value == target}. + map {|value| deep_delete(value, target)} end # creates an array of values for which any given answer should fail def tests(current, target) - [ nil, - Array.new, - Hash.new, - RandomDataStructure.new.generate, - deep_delete(current, target) - ] + [ nil, + Array.new, + Hash.new, + RandomDataStructure.new.generate, + deep_delete(current, target) + ] end # test whether the answer provided by the given # routine matches the target answer def run_test(current, routine, target) - begin - output = routine.call - output == target - rescue Exception - false - end + begin + output = routine.call + output == target + rescue Exception + false + end end def generate_data_structure From c747d50290ae59072fe1cd8c03c5d78f602716d3 Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Wed, 10 Jun 2015 13:27:20 -0400 Subject: [PATCH 6/8] Game#run_test takes the input string, not a proc run_test now takes the user input, and creates the proc in its method body, so the resulting closure encloses the correct values. --- lib/rubies/game.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rubies/game.rb b/lib/rubies/game.rb index e11d09c..76b6d5d 100644 --- a/lib/rubies/game.rb +++ b/lib/rubies/game.rb @@ -175,7 +175,8 @@ def tests(current, target) # test whether the answer provided by the given # routine matches the target answer - def run_test(current, routine, target) + def run_test(current, input, target) + routine = lambda { eval(input) } begin output = routine.call output == target From c79a361552ab414f106849918d6cc5f20b8497d5 Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Wed, 10 Jun 2015 13:29:03 -0400 Subject: [PATCH 7/8] Add Game#test_answer test_answer tests the given user input against tests generated by Game#tests. The tests generated by Game#tests are data structures that no user input should return the target for (empty arrays, nil, etc.). test_answer returns true only if the user input fails for each of the tests. --- lib/rubies/game.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/rubies/game.rb b/lib/rubies/game.rb index 76b6d5d..d4a53f4 100644 --- a/lib/rubies/game.rb +++ b/lib/rubies/game.rb @@ -173,6 +173,15 @@ def tests(current, target) ] end + # test user input against various tests, for each of which the + # input should fail. Returns true only if the user input fails + # each of the tests. + def test_answer(current, input, target) + tests(current, target).all? do |test| + !run_test(test, input, target) + end + end + # test whether the answer provided by the given # routine matches the target answer def run_test(current, input, target) From 6fd8ed02154bd9d5627fe4b1e143a771c7886e5f Mon Sep 17 00:00:00 2001 From: Dylan Frese Date: Wed, 10 Jun 2015 13:35:03 -0400 Subject: [PATCH 8/8] Modify Game#play_round to use Game#test_answer Game#play_round will now call Game#cheater if test_answer fails. The cheater method prints out a short message about accessing the data structure. --- lib/rubies/game.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/rubies/game.rb b/lib/rubies/game.rb index d4a53f4..358f9ab 100644 --- a/lib/rubies/game.rb +++ b/lib/rubies/game.rb @@ -107,6 +107,16 @@ def itsright puts "Correct!".colorize(:green) end + def cheater + @num_wrong += 1 + puts "Please provide an answer "\ + "that operates on the given values. ".colorize(:light_red) + puts + puts "Try not to directly reference the value directly, ".colorize(:light_red) + puts + puts "Instead, try accessing the value by its index or key!".colorize(:light_red) + end + def prompter(answer) print "Write ruby code to find the following value".colorize(:light_blue) print " (or enter ".colorize(:light_blue) + 'NEW'.colorize(:green) @@ -214,8 +224,12 @@ def play_round # new, exit or check if right/wrong return else if check_answer(current, input, target) - itsright - correct = true + if !test_answer(current, input, target) + cheater + else + itsright + correct = true + end else itswrong(target) end