Open
Conversation
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.
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.
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.
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.
4-wide indentations were retabbed to be 2-wide, to match the rest of the file (sorry!).
run_test now takes the user input, and creates the proc in its method body, so the resulting closure encloses the correct values.
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.
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.
Collaborator
There was a problem hiding this comment.
The use of eval is a serious security risk.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This fixes issue #15, where users could input the target answer and be marked correct. This solution tests the user's solution against various data structures for which it should fail, such as empty arrays, nil, and the provided data structure with all instances of the target answer removed. If the user's solution does not fail for each of these tests, it almost certainly means the user's answer is not a function of the provided data structure.
Personally, I think this is a design problem rather than a technical problem. Trying to pull a single piece of known data from a known set of data is not a very engaging task; it would be more interesting and 'like-coding' to have the user map 'every name to their phone number' or 'find the sum of any array', without knowing the exact data before hand.