Module: Console

Defined in:
lib/console.rb

Overview

Functions which create a user-interface in the terminal

Constant Summary collapse

@@won_text =

Some definitions which may change one day, become configurable one day, need to be accessed (one day) from somewhere else or not.

%q"__  __               _       __               __
\ \/ /___  __  __   | |     / /___  ____     / /
 \  / __ \/ / / /   | | /| / / __ \/ __ \   / / 
 / / /_/ / /_/ /    | |/ |/ / /_/ / / / /  /_/  
/_/\____/\__,_/     |__/|__/\____/_/ /_/  (_)"
@@fail_text =
%q"                                   __ 
 __ __            _               |  |
|  |  |___ _ _   | |___ ___ ___   |  |
|_   _| . | | |  | | . |_ -| -_|  |__|
|_| |___|___|  |_|___|___|___|  |__|"
@@win_sequence =
%w"won.. won.. Won.. WOn.. wON.. wON!. woN!! won!! won.!  won.. _on.. __n.. ___.. ___.. w__.. wo_.. " 
@@wait_sequence =
%w"U\  V\  Y\  T\  |\  ¦\  :\  .\  _\  .\  o\  O\ "
@@fail_sequence =
%w":-|\  :-|\  :-[\  :-[\  :-(\  :-(\  8-(\  8-(\  8-(\  8-(\  8-(\  8-(\  8-(\  8-(\  .... |... lo.. Los. LOso LOSe LOSE lOSE loSE lose"
@@points =
0

Instance Method Summary collapse

Instance Method Details

#a2i(array) ⇒ Object



151
152
153
# File 'lib/console.rb', line 151

def a2i(array)
  UserInput::a2i(array)
end

#ask_new_eventObject



162
163
164
165
# File 'lib/console.rb', line 162

def ask_new_event
  puts "\nEnter number for more information about an event, 'a' to play with one new, random event, 'q' to quit." 
  UserInput::input()
end

#clearObject



123
124
125
# File 'lib/console.rb', line 123

def clear
  system 'clear'
end

#compare_order(response) ⇒ Object



205
206
207
208
209
210
# File 'lib/console.rb', line 205

def compare_order(response)
  puts "\n... You say: " << bold(response.join(', '))
  devents = @m_events.dup.sort {|f, s| f.year <=> s.year } 
  cmt = "\tThe right order is:\n" << devents.collect {|ev| ev.disp_index.to_s << ') ' << ev.year.to_s }.join("\n").box()
  wait(@@wait_sequence, 1, cmt)
end

#detail(num) ⇒ Object

show information on an event



186
187
188
189
190
191
192
193
194
195
# File 'lib/console.rb', line 186

def detail(num)
  if(0 != num && num <= @m_events.length)
    det = @m_events.detect{|ev| ev.disp_index == num}.detail
    if(det && !det.empty?)
      puts ("\n" << num.to_s << ') ' << det).box(:color => :green)
    else
      puts " - No details known -"
    end
  end
end

#int_array_input(len, range) ⇒ Object



158
159
160
# File 'lib/console.rb', line 158

def int_array_input(len, range)
  UserInput::int_array_input(len, range ) 
end

#int_input(range = nil) ⇒ Object



155
156
157
# File 'lib/console.rb', line 155

def int_input(range = nil)
  UserInput::int_input(range)
end

#loseObject



172
173
174
# File 'lib/console.rb', line 172

def lose
  wait @@fail_sequence, 1, red(@@fail_text )
end

#new_eventObject

add a new event to the game



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/console.rb', line 129

def new_event
  known_events = @m_events.collect {|ev| ev.title}
  available = $events.collect {|ev| ev unless known_events.include?(ev[0]) }.compact
  if !available || available.empty?
    puts "\n\tNo more events available. Aborting. Bye"
    puts
    return :all_events_done
  end
  clear
  out = ''
  @m_events.each_with_index {|ev, i| out << (i + 1).to_s << ') ' << ev.title << "\n"}
  puts out.box(:left => 2)

  puts "Indicate the event from the previous list, which precedes the following, or '0' (zero) to put it in front:"
  srand(Time.new.usec)
  index = rand(available.length)
  ev = Event.new(available[index]) 
  @m_events << ev
  puts ev.title.box(:color => :yellow)
  return ev
end

#position_by_user(p) ⇒ Object



197
198
199
200
201
202
203
# File 'lib/console.rb', line 197

def position_by_user(p)
  if(p > 0)
    puts "\n... You say: " << bold("after \"" << @m_events[p-1].title << "\"")
  else
    puts "\n... You say: " << bold("Earlier than any of the listed events")
  end
end

#score(pts, all_done = false) ⇒ Object



167
168
169
170
# File 'lib/console.rb', line 167

def score(pts, all_done = false)
  intro = (all_done ? 'Final score' : 'Current score')
  puts ' (' << intro << ': %i %s)' %[pts, (pts == 1 ? 'point' : 'points')]
end

#show_correct_orderObject



180
181
182
183
# File 'lib/console.rb', line 180

def show_correct_order
  cmt = "\tThe right order is:\n" << @m_events.collect {|ev| (@m_events.index(ev) + 1).to_s << ') ' << ev.title << ' (' << ev.year.to_s << ')'}.join("\n").box()
  wait(@@wait_sequence, 1, cmt) 
end

#start_gameObject

start with some arbitrary events



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/console.rb', line 41

def start_game
  available = $events
  clear
  puts bold("\nPut the following events into the right chronological order (e.g.: 2 3 1).")
  @m_events = Array.new
  out = ''
  $num_events.times do |i|
    srand(Time.new.usec)
    index = rand(available.length)
    ev = Event.new(available.delete_at(index))
    ev.disp_index = (i + 1)
    @m_events << ev
    out << (i + 1).to_s << ') ' << ev.title << "\n"
  end
  puts out.box(:left => 2)
  response = nil

  response = int_array_input($num_events, (1..$num_events) ) 
  if response == :quit_game
    score @@points, true
    exit true
  end

  compare_order(response)
  # good order of the response-array
  if good_order(response)
    @@points += $num_events 
    win
  else
    lose
  end
  score(@@points)

  # Start the loop. Add events as requested.
  until @m_events.length == $MAX_NUM_EVENTS do
    ui = ask_new_event
    if ui == :quit_game
      score @@points, true
      exit true
    end
    if('a' == ui)
      ev = new_event
      if ev == :all_events_done
        score(@@points, true) 
        exit true
      end

      # ------> unresolved trouble with 0-based array.indices. <----------
      # 1) exclude new event from the choices (hence -1), allow 0 as 'earlier than any'.
      r = int_input(0..(@m_events.length - 1) )
      if r == :quit_game
        score @@points, true
        exit true
      end
      # User-input can be 0, which complicates the comparison with the
      # 0-based event-array.  

      # 2) in the range of known events, if not 0 (hence 0 is allowed).
      position_by_user(r)

      # 4) 'order' must fit in between the indices of the event-array (0-based, hence -1. Again.)
      ok = good_order(r - 1, ev)

      sort_years(true)
      # 5) show the resulting order as starting from 1, not 0.
      show_correct_order
      # <---------- This should be it. ------------>

      if ok
        @@points += 1
        win
      else
        lose
      end
      score(@@points)
    else
      detail( a2i(ui) )
    end
  end

end

#wait(seq, sec, cmt) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/console.rb', line 212

def wait(seq, sec, cmt)
  bi = BusyIndicator.new(false)
  bi.sequence = seq
  bi.run
  sleep sec
  bi.stop(cmt)
end

#winObject



176
177
178
# File 'lib/console.rb', line 176

def win
  wait(@@win_sequence, 1, green(@@won_text) )
end