class Chadet::Play
Public Class Methods
new(secret_chars_object)
click to toggle source
# File lib/chadet.rb, line 6 def initialize secret_chars_object @chars_set = secret_chars_object.chars_set @num_of_chars = secret_chars_object.num_of_chars @secret_chars = secret_chars_object.secret_chars @loaded = false @used_true = "" @used_false = "" @max_hint = (@chars_set.length/@num_of_chars).to_i @hint_used = 0 @moves = [] end
Public Instance Methods
answer(guess, guess_num)
click to toggle source
# File lib/chadet.rb, line 101 def answer guess, guess_num # Display number of correct characters and number of correct positions _G_ = guess_num.abs.to_s _g_ = _G_.length _N_ = @num_of_chars.to_s _n_ = _N_.length _B_ = checkCC(guess).to_s _b_ = _B_.length _U_ = checkCP(guess).to_s _u_ = _U_.length output = (guess_num == -1 ? ' ANS' : " "*(4-_g_) + _G_) + "| #{guess.yellow}" \ + " [" + ("0"*(_n_-_b_) + _B_).green + "] [" \ + ("0"*(_n_-_u_) + _U_).green + "]" @moves << [guess, "0"*(_n_-_b_) + _B_, "0"*(_n_-_u_) + _U_] return output end
chars_to_use()
click to toggle source
# File lib/chadet.rb, line 63 def chars_to_use box_width = @chars_set.length >= 17 ? @chars_set.length + 2 : 19 end_pos = 65 start_pos = end_pos - box_width puts " "*start_pos + "+" + "-"*(box_width-2) + "+" + "\n"\ + " "*start_pos + "|Set of characters" + " "*(box_width - 19) + "|\n" \ + " "*start_pos + "|to guess with:" + " "*(box_width - 16) + "|\n" \ + " "*start_pos + "+" + "-"*(box_width-2) + "+" + "\n" \ + " "*start_pos + "|" + @chars_set.yellow + " "*(box_width - @chars_set.length - 2) + "|\n" \ + " "*start_pos + "+" + "-"*(box_width-2) + "+" print "\r\e[6A" end
checkCC(guess)
click to toggle source
Method to check how many characters are correctly guessed
# File lib/chadet.rb, line 119 def checkCC guess cc = 0 guess.each_char do |x| if @secret_chars.include? x.to_s cc += 1 end end return cc end
checkCP(guess)
click to toggle source
Method to check how many correct characters are presented in correct positions
# File lib/chadet.rb, line 130 def checkCP guess cp = 0 @num_of_chars.times do |i| if @secret_chars[i] == guess[i] cp += 1 end end return cp end
do_hint()
click to toggle source
# File lib/chadet.rb, line 173 def do_hint if @hint_used != @max_hint hint.flash @hint_used += 1 else ("Sorry, you've used #{@max_hint == 1 ? 'the' : 'up all'} #{@max_hint.to_s + " " unless @max_hint == 1}"\ + "hint#{'s' unless @max_hint == 1}.").red.flash end end
end_game()
click to toggle source
# File lib/chadet.rb, line 89 def end_game # table bottom horizontal line cp_pos = 12 + @num_of_chars + 2*@num_of_chars.to_s.length table_width = cp_pos + 2 print " " table_width.times do print "-" sleep 0.015 end print "\n" end
header()
click to toggle source
# File lib/chadet.rb, line 18 def header puts "=================================================================\n" \ + " Chadet (Characters Detective) ".code + "\n" \ + "=================================================================\n" \ + "Requires "+"bash".code + " and " + "xterm".code + " to run properly. "\ + "In the terminal, \ntype " + "chadet --help".code + " for more options and "\ + "chadet --rules".code + " to see\nthe rules of the game. "\ + "To quit this game, type " + "quit".code + ".\n" puts "°º¤ø,¸¸,ø¤°``°º¤ø,¸¸,ø¤º°``°º¤ø,¸¸,ø¤º°``°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤°" end
hint()
click to toggle source
Method to display hint
# File lib/chadet.rb, line 141 def hint picked_number = 0 clue = "" chance = rand 1..100 if chance < (@num_of_chars.to_f/@chars_set.length.to_f*100) \ && chance > (@num_of_chars.to_f/@chars_set.length.to_f*20) #display one correct number picked_number = rand 0...(@num_of_chars-@used_true.length) || 0 true_characters = @secret_chars.tr(@used_true, '') picked_true = true_characters[picked_number] || "" @used_true += picked_true if picked_true == "" clue = "You already knew #{@num_of_chars == 1 ? 'the' : 'all'} true "\ + "character#{'s' unless @num_of_chars == 1}." else clue = "'#{picked_true}' is#{@num_of_chars == 1 ? '' : ' among'} the true "\ + "character#{'s' unless @num_of_chars == 1}." end else picked_number = rand 0...(@chars_set.length - @num_of_chars - @used_false.length) || 0 false_characters = @chars_set.tr(@secret_chars, '').tr(@used_false, '') || "" picked_false = false_characters[picked_number] || "" @used_false += picked_false if picked_false == "" clue = "You've already known #{(@chars_set.length - @num_of_chars) == 1 ? 'the' : 'all'} "\ + "false character#{'s' unless (@chars_set.length - @num_of_chars) == 1}." else clue = "One can simply omit '#{picked_false}'." end end return clue.yellow end
quit_game(guess_num)
click to toggle source
# File lib/chadet.rb, line 183 def quit_game guess_num print "\r" "¯\\(°_o)/¯ I give up.".yellow.flash 1 guess_num = -1 puts answer @secret_chars, guess_num end_game return guess_num end
result(guess_num)
click to toggle source
# File lib/chadet.rb, line 35 def result guess_num # Congratulate the player for finishing the game quit = "Try it again!" lucky = "Wow! (˚o˚) Lucky guess." congratulate = "★·.·´¯`·.·★·.·´¯`·.·★\n ░G░O░O░D░ ░J░O░B░!░\n ★·.·´¯`·.·★·.·´¯`·.·★\n" \ + " You did it in " + guess_num.to_s + " steps." a_bit_slow = "(-。ー;) Finally..\n But don't beat yourself up. Try it again!\n I know you "\ + "can do better than " + guess_num.to_s + " guesses." # To decide what message to display after the game finished case guess_num when -1 message = quit when 1 message = lucky when 2..@chars_set.length message = congratulate else message = a_bit_slow end if message == a_bit_slow puts "\n " + message.yellow else message.blink end end
table_header()
click to toggle source
# File lib/chadet.rb, line 76 def table_header chars_pos = (5 + @num_of_chars - "chars".length)/2 cc_pos = 10 + @num_of_chars cp_pos = 12 + @num_of_chars + 2*@num_of_chars.to_s.length table_width = cp_pos + 2 puts " " + "-"*table_width + "\n"\ + " no.|" + " "*chars_pos + "chars" \ + " "*(cc_pos - chars_pos - "chars".length - " no.|".length) \ + "cc." + " "*(cp_pos - cc_pos - "cc.".length)\ + "cp." + "\n"\ + " " + "-"*table_width end