Class: GamesAndRpgParadise::Minesweeper::Terminal::CLI

Inherits:
Object
  • Object
show all
Includes:
TTY::Option
Defined in:
lib/games_and_rpg_paradise/games/minesweeper/terminal/cli.rb

Overview

GamesAndRpgParadise::Minesweeper::Terminal::CLI

Constant Summary collapse

LEVELS =
#

GamesAndRpgParadise::Minesweeper::Terminal::CLI::LEVELS

#
{
  'easy'   => { width:  9, height:  9, mines: 10 },
  'medium' => { width: 16, height: 16, mines: 40 },
  'hard'   => { width: 30, height: 16, mines: 99 }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject

#

GamesAndRpgParadise::Minesweeper::Terminal::CLI.run

Start the game.

#


149
150
151
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/cli.rb', line 149

def self.run
  CLI.new.run
end

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

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/games_and_rpg_paradise/games/minesweeper/terminal/cli.rb', line 106

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 GamesAndRpgParadise::Minesweeper::Terminal::Error => err
  output.puts "Error: #{err}"
  exit 1
end