class Game

Constants

BEGINNING_ADVANCE_RATE
DIF_BTWN_LEVEL
MINIMUM_ADVANCE_RATE
REFRESH_RATE

Public Instance Methods

add_win_condition(condition, value) click to toggle source
# File lib/game.rb, line 132
def add_win_condition(condition, value)
  case condition
  when "Timed"
    @time_limit = value * 60
  when "Lines"
    @line_limit = value
  end
end
auto_advance() click to toggle source
# File lib/game.rb, line 76
def auto_advance
  @last_advanced = Time.now
  @board.move_selected_down
end
auto_advance_needed?() click to toggle source
# File lib/game.rb, line 72
def auto_advance_needed?
  Time.now - @last_advanced > current_advance_rate
end
current_advance_rate() click to toggle source
# File lib/game.rb, line 68
def current_advance_rate
  [BEGINNING_ADVANCE_RATE - (@level * DIF_BTWN_LEVEL), MINIMUM_ADVANCE_RATE].max
end
do_turn() click to toggle source
# File lib/game.rb, line 56
def do_turn
  auto_advance if auto_advance_needed?
  system "clear" or system "cls"
  @board.render
  action = get_input
  take_action(action) if action
end
end_game() click to toggle source
# File lib/game.rb, line 64
def end_game
  STDIN.echo = true
end
force_quit?() click to toggle source
# File lib/game.rb, line 90
def force_quit?
  @force_quit
end
get_input() click to toggle source
# File lib/game.rb, line 81
def get_input
  action = nil
  begin
    action = Timeout::timeout(REFRESH_RATE) { STDIN.getch }
  rescue
  end
  action
end
get_mode() click to toggle source
# File lib/game.rb, line 23
def get_mode
  options = [
    {title: "Unlimited", type: :none, value: true},
    {title: "Timed", type: :increment, value: 3, unit: 'min.'},
    {title: "Lines", type: :increment, value: 40}
  ]
  prompt = "Welcome to Rubtris.\nSelect the mode you want to play!"
  menu = Menu.new(options, prompt)
  @game_mode = menu.open
end
over?() click to toggle source
# File lib/game.rb, line 120
def over?
  force_quit? || @board.over? || over_time? || over_lines?
end
over_lines?() click to toggle source
# File lib/game.rb, line 128
def over_lines?
  @line_limit && @board.completed_lines >= @line_limit
end
over_time?() click to toggle source
# File lib/game.rb, line 124
def over_time?
  @time_limit && (@last_advanced - @start_time) >= @time_limit
end
play_again() click to toggle source
# File lib/game.rb, line 34
def play_again
  options = [
    {title: "Yes", type: :none, value: true},
    {title: "No", type: :none, value: false}
  ]
  prompt = "#{@board.summary_s}\nPlay again?"
  menu = Menu.new(options, prompt)
  menu.open[:value]
end
run() click to toggle source
# File lib/game.rb, line 14
def run
  set_up_game
  until over?
    do_turn
  end
  end_game
  run if !force_quit? && play_again
end
set_up_game() click to toggle source
# File lib/game.rb, line 44
def set_up_game
  @board = Board.new
  STDIN.echo = false
  @force_quit = false
  @board.add_block
  @board.render
  @start_time, @last_advanced, @level = Time.now, Time.now, 0
  @time_limit, @line_limit = nil, nil
  config = get_mode
  add_win_condition(config[:title], config[:value])
end
take_action(action) click to toggle source
# File lib/game.rb, line 94
def take_action(action)
  case action.downcase
  when "w"
    @board.move_to_bottom
  when "a"
    @board.move_selected_left
  when "s"
    @board.move_selected_down
  when "d"
    @board.move_selected_right
  when "\r"
    @board.push_selected_down
  when "e"
    @board.rotate_selected_right
  when "q"
    @board.rotate_selected_left
  when "["
    @board.rotate_selected_left
  when "]"
    @board.rotate_selected_right
  when "\e"
    @force_quit = true
  end
  nil
end