Class: Minehunter::CLI

Inherits:
Object
  • Object
show all
Includes:
TTY::Option
Defined in:
lib/minehunter/cli.rb

Overview

The main interface to the game

Constant Summary collapse

LEVELS =
{
  "easy" => {width: 9, height: 9, mines: 10},
  "medium" => {width: 16, height: 16, mines: 40},
  "hard" => {width: 30, height: 16, mines: 99}
}.freeze

Instance Method Summary collapse

Instance Method Details

#run(argv = ARGV, input: $stdin, output: $stdout, env: {}, color: nil, screen_width: TTY::Screen.width, screen_height: TTY::Screen.height) ⇒ Object

Run the game

Parameters:

  • argv (Array<String>) (defaults to: ARGV)

    the command line parameters

  • input (IO) (defaults to: $stdin)

    the input stream, defaults to stdin

  • output (IO) (defaults to: $stdout)

    the output stream, defaults to stdout

  • env (Hash) (defaults to: {})

    the environment variables

  • color (Boolean) (defaults to: nil)

    whether or not to style the game

  • screen_width (Integer) (defaults to: TTY::Screen.width)

    the terminal screen width

  • screen_height (Integer) (defaults to: TTY::Screen.height)

    the terminal screen height



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
122
123
124
# File 'lib/minehunter/cli.rb', line 92

def run(argv = ARGV, input: $stdin, output: $stdout, env: {}, color: nil,
        screen_width: TTY::Screen.width, screen_height: TTY::Screen.height)
  parse(argv)

  if params[:help]
    output.print help
    exit
  elsif params[:version]
    output.puts VERSION
    exit
  elsif params.errors.any?
    output.puts params.errors.summary
    exit 1
  else
    level = LEVELS[params[:level]]
    decorator = Pastel.new(enabled: color).method(:decorate)
    game = Game.new(
      input: input,
      output: output,
      env: env,
      width: params[:width] || level[:width],
      height: params[:height] || level[:height],
      screen_width: screen_width,
      screen_height: screen_height,
      mines_limit: params[:mines] || level[:mines],
      decorator: decorator
    )
    game.run
  end
rescue Minehunter::Error => err
  output.puts "Error: #{err}"
  exit 1
end