Class: ConnectNGame::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/cli/cli.rb,
lib/cli/select_players.rb,
lib/cli/process_options.rb

Overview

The Connect N Command Line Interface class.

Instance Method Summary collapse

Constructor Details

#initializeCLI

Set up the command line interface



18
19
20
21
# File 'lib/cli/cli.rb', line 18

def initialize
  @players = []
  @order = 4
end

Instance Method Details

#find_player(arg) ⇒ Object

Find the selected player.



47
48
49
50
51
# File 'lib/cli/select_players.rb', line 47

def find_player(arg)
  Player.players.find do |player|
    player.name.downcase == arg.downcase
  end
end

#mainObject

The starting point for an interactive, command-line-driven session of the Connect N Game!



25
26
27
28
29
30
31
32
33
34
# File 'lib/cli/cli.rb', line 25

def main
  welcome
  process_cmd_line
  top_up_players

  play_game

rescue Interrupt
  puts
end

#pick_a_playerObject

Have the user pick a player.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cli/select_players.rb', line 20

def pick_a_player
  begin
    show_players
    print "\nEnter player #{@players.length+1} name: "
    input = gets.strip
    player = find_player(input)
    puts "invalid entry #{input.inspect}" unless player
  end until player

  @players << player
end

#play_gameObject

Play the game



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cli/cli.rb', line 37

def play_game
  @game = Game.new(@players[0], @players[1], @order)
  @game.game_initialize

  begin
    current = @game.current
    puts
    result = @game.next_move
    puts "On turn ##{@game.turn}, " +
         "player #{current} " +
         "plays channel #{Utl.channel_to_name(@game.last_move)}."
    @game.rack.cli_display
  end while result == :continue

  if result == :victory
    puts "Player #{@game.current}, #{@game.current_player.name} wins!"
    puts
    puts @game.current_player.winners_comments
    puts @game.previous_player.losers_comments
  elsif result == :stalemate
    puts "No winner, it's a tie!"
  else
    puts "Result is #{result}"
  end

  puts
end

#process_cmd_lineObject

Handle any command line parameters.



11
12
13
14
15
16
17
18
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
# File 'lib/cli/process_options.rb', line 11

def process_cmd_line

  opts = GetoptLong.new(
    [ "--help",   "-h", "-?", GetoptLong::NO_ARGUMENT ],
    [ "--player", "-p",       GetoptLong::REQUIRED_ARGUMENT ],
    [ "--order",  "-o",       GetoptLong::REQUIRED_ARGUMENT ],
    [ "--debug",  "-d",       GetoptLong::NO_ARGUMENT ])

  # Translate the parsed options into fOOrth.
  opts.each do |opt, arg|

    case opt
    when "--player"
      fail "Error: Too many players!" if @players.length >= 2

      puts "Player ##{@players.length + 1} is #{arg}"
      fail "Error: Unknown player: #{arg}" unless (player = find_player(arg))
      @players << player
    when "--order"
      @order = arg.to_i
      fail "Invalid order #{arg}" unless @order.between?(4,8)
    when "--debug"
      puts "Debug mode is enabled."
      $game_debug = true
    else
      fail ""
    end
  end

rescue => err
  puts err.message
  show_help
  exit
end

#select_new_playersObject

Select all new players



9
10
11
12
# File 'lib/cli/select_players.rb', line 9

def select_new_players
  @players = []
  top_up_players
end

#show_helpObject

Display the help message.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cli/process_options.rb', line 47

def show_help
  puts
  puts "Usage info: connect_n_game <options>"
  puts
  puts "--help, -h, -?      -- Display this message and quit."
  puts "--player, -p <name> -- Select player or automaton 'name'"
  puts "                       Note: Limit of two players"
  puts "--order, -o <4..8>  -- The winning run length. Default=4"
  puts "--debug, -d         -- Display debug info."

  show_players
end

#show_playersObject

Display the available players



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cli/select_players.rb', line 33

def show_players
  puts
  puts "Player Selection: "

  width = (Player.players.map do |player|
    player.name.length
  end).max

  Player.players.each do |player|
    puts "  #{player.name.ljust(width+1)}  #{player.description}"
  end
end

#top_up_playersObject

Make sure we have two players



15
16
17
# File 'lib/cli/select_players.rb', line 15

def top_up_players
  pick_a_player while @players.length < 2
end

#welcomeObject

The welcome message.



66
67
68
69
# File 'lib/cli/cli.rb', line 66

def welcome
  puts "Welcome to the Connect N Command Line Interface."
  puts "This is game version: #{VERSION}."
end