This file contains 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
class Mastermind | |
COLOURS = 'A'.upto('Z').to_a | |
def initialize(colour_count = 6, size = 4) | |
fail "Size must be <= #{COLOURS.size}" unless size <= COLOURS.size | |
@size = size | |
@colours = COLOURS.first(colour_count) | |
@code = size.times.map { @colours.sample } | |
@moves = 0 | |
puts "Guess the code, size #{@size}, letters are (#{@colours.join(', ')})" |
This file contains 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
# Memoization made easy. Use it the following way | |
# | |
# Memoize all calls | |
# memoize def my_function(n) | |
# ... | |
# end | |
# | |
# Memoize only a ratio of calls (5% here): | |
# memoize 0.05 def my_function(n) | |
# ... |