diff --git a/lib/rubies/game.rb b/lib/rubies/game.rb index 0bd92f2..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) @@ -155,6 +165,45 @@ 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, + Array.new, + Hash.new, + RandomDataStructure.new.generate, + deep_delete(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) + routine = lambda { eval(input) } + begin + output = routine.call + output == target + rescue Exception + false + end + end + def generate_data_structure rds = RandomDataStructure.new current = rds.generate @@ -175,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