Class: Rubies::Game

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Game

Returns a new instance of Game.



3
4
5
6
7
8
9
# File 'lib/rubies/game.rb', line 3

def initialize(opts={})
  @num_right = 0
  @num_wrong = 0
  @playing = true
  @in = opts.fetch(:in, $stdin)
  @out = opts.fetch(:out, $stdout)
end

Instance Method Details

#byebyeObject



119
120
121
122
123
# File 'lib/rubies/game.rb', line 119

def byebye
  puts
  puts "Thanks for using ".colorize(:green) + "rubies!".colorize(:light_red)
  display_score
end

#check_answer(current, input, target) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rubies/game.rb', line 145

def check_answer(current, input, target)
  begin
    routine = lambda { eval(input) }
    output = routine.call
    puts "=> #{output}"
    puts
    output == target
  rescue Exception => e
    eprinter(e)
    false
  end
end

#clear_screenObject



129
130
131
# File 'lib/rubies/game.rb', line 129

def clear_screen
  puts "\e[H\e[2J"
end

#continuerObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubies/game.rb', line 49

def continuer
  begin
    puts "Press enter to continue . . . "
    gets.chomp
    puts "\e[H\e[2J"
  rescue Interrupt => e
    @playing = false
    byebye
    exit
  end
end

#display_scoreObject



125
126
127
# File 'lib/rubies/game.rb', line 125

def display_score
  scoreboard(@num_right, @num_wrong)
end

#display_splashObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubies/game.rb', line 19

def display_splash
  begin
    puts "\e[H\e[2J"
    puts "
  .______       __    __  .______    __   _______     _______.
  |   _  \\     |  |  |  | |   _  \\  |  | |   ____|   /       |
  |  |_)  |    |  |  |  | |  |_)  | |  | |  |__     |   (----`
  |      /     |  |  |  | |   _  <  |  | |   __|     \\   \\
  |  |\\  \\----.|  `--'  | |  |_)  | |  | |  |____.----)   |
  | _| `._____| \\______/  |______/  |__| |_______|_______/

  ".colorize(:light_magenta)
    puts "
  ================================================================
                             LEGEND
                 NEW : get a new data structure
                 EXIT: exit program
  ================================================================
  ".colorize(:light_magenta)
    puts "Press enter to continue . . . "

    gets.chomp
    puts "\e[H\e[2J"
  rescue Interrupt => e
    @playing = false
    byebye
    exit
  end
end

#eprinter(error) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/rubies/game.rb', line 85

def eprinter(error)
  encoding_options = {
  :invalid           => :replace,  # Replace invalid byte sequences
  :undef             => :replace,  # Replace anything not defined in ASCII
  :replace           => '',        # Use a blank for those replacements
  }
  puts
  puts "Sorry, that code resulted in an error:".colorize(:light_red)
  puts "#{error}".encode(Encoding.find('ASCII'), encoding_options).colorize(:red)
end

#gameObject

should rename to ‘run’



194
195
196
197
198
199
200
# File 'lib/rubies/game.rb', line 194

def game
  display_splash
  until gameover?
    play_round
  end
  byebye
end

#gameover?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/rubies/game.rb', line 189

def gameover?
  !@playing
end

#generate_data_structureObject



158
159
160
161
162
163
# File 'lib/rubies/game.rb', line 158

def generate_data_structure
  rds = RandomDataStructure.new
  current = rds.generate
  target = rds.all_values.sample
  [current, target]
end

#getsObject



15
16
17
# File 'lib/rubies/game.rb', line 15

def gets
  @in.gets
end

#itsrightObject



105
106
107
108
# File 'lib/rubies/game.rb', line 105

def itsright
  @num_right += 1
  puts "Correct!".colorize(:green)
end

#itswrong(answer) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/rubies/game.rb', line 96

def itswrong(answer)
  @num_wrong += 1
  puts "Sorry, that code is incorrect. ".colorize(:light_red)
  puts
  puts "The right answer is . . . ".colorize(:light_red)
  puts answer.to_s
  puts "Try again!".colorize(:light_red)
end

#play_roundObject

new, exit or check if right/wrong



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rubies/game.rb', line 165

def play_round # new, exit or check if right/wrong
  clear_screen
  correct = false
  current, target = generate_data_structure
  until correct
    input = prompt(current, target).rstrip.lstrip
    if input == "NEW" || input == "new"
      return
    elsif input == "EXIT" || input == "exit"
      @playing = false
      return
    else
      if check_answer(current, input, target)
        itsright
        correct = true
      else
        itswrong(target)
      end
    end
    scoreboard(@num_right, @num_wrong)
    continuer
  end
end

#prompt(data_structure, target) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rubies/game.rb', line 133

def prompt(data_structure, target)
  begin
    questioner(data_structure)
    prompter(target)
    gets.chomp.gsub("\"", "\'")
  rescue Interrupt => e
    @playing = false
    byebye
    exit
  end
end

#prompter(answer) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/rubies/game.rb', line 110

def prompter(answer)
  print "Write ruby code to find the following value".colorize(:light_blue)
  print " (or enter ".colorize(:light_blue) + 'NEW'.colorize(:green)
  puts " for a new challenge): ".colorize(:light_blue)
  puts answer.to_s
  puts
  print "[1] rubies(main)> "
end

#puts(message = '') ⇒ Object



11
12
13
# File 'lib/rubies/game.rb', line 11

def puts(message='')
  @out.puts message
end

#questioner(current) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubies/game.rb', line 69

def questioner(current)
  puts
  puts "We have some questions for you about this #{current.class.to_s.downcase}:".colorize(:light_blue)
  puts "current = "
  if current.is_a? Hash
    ap current, index: false
  elsif current.first.is_a? Array
    PP.pp current
  elsif current.first.is_a? Fixnum
    PP.pp current
  else
    ap current, index: false
  end
  puts
end

#scoreboard(num_right, num_wrong) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/rubies/game.rb', line 61

def scoreboard(num_right, num_wrong)
  puts
  puts "==============================".colorize(:light_yellow)
  puts "Number correct this session: ".colorize(:green) + num_right.to_s
  puts "Number wrong this session  : ".colorize(:light_red) + num_wrong.to_s
  puts "==============================".colorize(:light_yellow)
end