Class: Chadet::Play

Inherits:
Object
  • Object
show all
Defined in:
lib/chadet.rb

Instance Method Summary collapse

Constructor Details

#initialize(secret_chars_object) ⇒ Play

Returns a new instance of Play.



6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Method Details

#answer(guess, guess_num) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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_useObject



63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

Method to check how many characters are correctly guessed



119
120
121
122
123
124
125
126
127
# 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) ⇒ Object

Method to check how many correct characters are presented in correct positions



130
131
132
133
134
135
136
137
138
# 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_hintObject



173
174
175
176
177
178
179
180
181
# 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_gameObject



89
90
91
92
93
94
95
96
97
98
99
# 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


29
30
31
32
33
# File 'lib/chadet.rb', line 29

def footer
  #Show Game Over footer at the end of the game
  print "\n"
  print "==========================(GAME OVER)==========================".code
end

#headerObject



18
19
20
21
22
23
24
25
26
27
# 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

#hintObject

Method to display hint



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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) ⇒ Object



183
184
185
186
187
188
189
190
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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_headerObject



76
77
78
79
80
81
82
83
84
85
86
87
# 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