class ConnectNGame::CLI
The Connect N Command Line Interface class.
Public Class Methods
new()
click to toggle source
Set up the command line interface
# File lib/cli/cli.rb, line 18 def initialize @players = [] @order = 4 end
Public Instance Methods
find_player(arg)
click to toggle source
Find the selected player.
# File lib/cli/select_players.rb, line 47 def find_player(arg) Player.players.find do |player| player.name.downcase == arg.downcase end end
main()
click to toggle source
The starting point for an interactive, command-line-driven session of the Connect N Game
!
# File lib/cli/cli.rb, line 25 def main welcome process_cmd_line top_up_players play_game rescue Interrupt puts end
pick_a_player()
click to toggle source
Have the user pick a player.
# File lib/cli/select_players.rb, line 20 def pick_a_player begin show_players print "\nEnter player #{@players.length+1} name: " input = STDIN.gets.strip player = find_player(input) puts "invalid entry #{input.inspect}" unless player end until player @players << player end
play_game()
click to toggle source
Play the game
# 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_line()
click to toggle source
Handle any command line parameters.
# 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 when "--help" fail "" end end fail "Invalid args #{ARGV.join(" ")}" unless ARGV.empty? rescue => err puts err.message show_help exit end
select_new_players()
click to toggle source
Select all new players
# File lib/cli/select_players.rb, line 9 def select_new_players @players = [] top_up_players end
show_help()
click to toggle source
Display the help message.
# File lib/cli/process_options.rb, line 49 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_players()
click to toggle source
Display the available players
# 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_players()
click to toggle source
Make sure we have two players
# File lib/cli/select_players.rb, line 15 def top_up_players pick_a_player while @players.length < 2 end
welcome()
click to toggle source
The welcome message.
# 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