Odin Project Ruby game project ran on terminal (command line).
- In this game there are two ways to play it
- Become a Hacker and crack the code !!! OR
- BE THE CREATOR create it...
Note
Thisis not the original version of the Mastermind game, here instead of guessing or creating color choices you must either guess an integer-based code of 4 digits or create it and play against the computor to see the original rules for mastermind check here
- YOU create the code
- You must crack the code
2 Classes
- => For computor to crack the code (Computor)
- => For you to crack the code (Player)
The Classes will inherit from one module (CrackCode)
-
How to rearrang elemnts in an array in a random way ?
-
How to remove whitspace in an array ?
-
How to generate random elements from an array
-
=> you use the
.shuffle()
Array instance method to return a copy of rearranged elements from the array
numbers = [1, 2, 3, 4, 5]
numbers_shuf = numbers.shuffle # store in var
print numbers_shuf => [1, 4, 2, 5, 3] # random order of elements
- => To remove elements of whitespace in an array you use the
reject()
method that removess any element that meets its condition from an array (it does not mutate the caller)
arr = ["hello", " ", "hy"]
arr = arr.reject {|el| el.strip.empty?}
print arr # => ["hello", "hy"] returns array of non-whitespace elements
- The
reject
method works with boolean values - If each element has whitespace that is not needed you can use the
gsub(" ", "")
method with arguments to replace
- => To get a random element from an array you use the
sample
method which returns a single random element from the array
numbers = [1, 2, 3, 4, 5]
random_number = numbers.sample
print random_number # => returns random number
- To store more than one you can use a loop with another empty array
numbers = [1, 2, 3, 4, 5]
choices = Array.new
4.times do
random_number = numbers.sample
choices.push(random_number)
end
print choices
- You cannot call private or any other method with class methods