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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chadet.rb', line 103

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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chadet.rb', line 66

def chars_to_use
  @chars_set.length >= 17 ? box_width = @chars_set.length + 2 : box_width = 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



121
122
123
124
125
126
127
128
129
# File 'lib/chadet.rb', line 121

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



132
133
134
135
136
137
138
139
140
# File 'lib/chadet.rb', line 132

def checkCP guess
  cp = 0
  for i in 0...@num_of_chars
    if @secret_chars[i] == guess[i]
      cp += 1
    end
  end
  return cp
end

#do_hintObject



175
176
177
178
179
180
181
182
183
# File 'lib/chadet.rb', line 175

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

#do_quit(guess_num) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/chadet.rb', line 224

def do_quit guess_num
   if guess_num > 0
      yes_commands = %w{yes yse ys ye y save saev seav sav sev s}
      print "\r                              "
      print "\r " + "Save game? (yes/no): ".yellow
      save = gets.chomp
      print "\r\e[1A" + " "*27
      print "\r"
      if yes_commands.include? save.downcase
         guess_num = do_save guess_num
      else
         guess_num = quit_game guess_num
      end
   else
      guess_num = quit_game guess_num
   end
   return guess_num
end

#do_save(guess_num) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/chadet.rb', line 206

def do_save guess_num
  if guess_num > 0 
    guess_num = save_game guess_num
  else
    "No game to save.".red.flash 1
  end
  return guess_num
end

#end_gameObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chadet.rb', line 91

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



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
172
173
# File 'lib/chadet.rb', line 143

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



215
216
217
218
219
220
221
222
# File 'lib/chadet.rb', line 215

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
62
63
64
# File 'lib/chadet.rb', line 35

def result guess_num
  # Congratulate the player for finishing the game
  save = "Your game has been saved."
  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 -2
        message = save
     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

#save_game(guess_num) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/chadet.rb', line 185

def save_game guess_num
  end_game if guess_num > 0
  # create data directory if not exists
  Dir.chdir(File.dirname(__FILE__))
  Dir.chdir("..")
  Dir.mkdir("data") unless Dir.exists?("data")
  Dir.chdir("data")
  # generate filename
  time = Time.now
  filename = time.strftime("%y%m%d%H%M%S").to_i.to_s(36)
  # save file
  CSV.open(filename + ".csv", "wb") do |f|
    f << [@chars_set.to_i(18).to_s(36), @secret_chars.to_i(18).to_s(36), @hint_used.to_s]
    @moves.each do |move|
      f << move
    end
  end
  guess_num = -2
  return guess_num
end

#table_headerObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chadet.rb', line 79

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