class CLI
Constants
- OFFSET
Public Class Methods
new(input=$stdin, output=$stdout)
click to toggle source
# File lib/cli.rb, line 12 def initialize(input=$stdin, output=$stdout) @input = input @output = output run_app end
Public Instance Methods
choose_player_type(options)
click to toggle source
# File lib/cli.rb, line 22 def choose_player_type(options) get_valid_input(options) end
get_valid_input(options)
click to toggle source
# File lib/cli.rb, line 31 def get_valid_input(options) choice = @input.gets.chomp.to_i if options.include? choice choice else @output.puts "Invalid Selection" get_valid_input(options) end end
place_marker(options)
click to toggle source
# File lib/cli.rb, line 26 def place_marker(options) offset_spaces = options.map {|space| space + OFFSET} get_valid_input(offset_spaces) - OFFSET end
select_board_size(options)
click to toggle source
# File lib/cli.rb, line 18 def select_board_size(options) get_valid_input(options) end
Private Instance Methods
add_separator_where_applicable(space_index, board_split_into_rows, single_row, formatted_number)
click to toggle source
# File lib/cli.rb, line 120 def add_separator_where_applicable(space_index, board_split_into_rows, single_row, formatted_number) space_index == board_split_into_rows.size - 1 ? single_row += " #{formatted_number}" : single_row += " #{formatted_number} |" end
add_space_to_single_digit_number(space)
click to toggle source
# File lib/cli.rb, line 116 def add_space_to_single_digit_number(space) space.to_s.length == 1 ? "#{space} " : "#{space}" end
board_with_index_in_empty_spaces()
click to toggle source
# File lib/cli.rb, line 103 def board_with_index_in_empty_spaces @game.board.spaces.each_with_index.map {|space, index| !space.nil? ? space : index + OFFSET } end
choose_player(marker)
click to toggle source
# File lib/cli.rb, line 73 def choose_player(marker) clear_screen print_choose_player(marker) choice = choose_player_type([1,2,3]) return HumanPlayer.new(marker, self) if choice == 1 return SimpleComputer.new(marker) if choice == 2 return PerfectComputer.new(marker) if choice == 3 end
clear_screen()
click to toggle source
# File lib/cli.rb, line 151 def clear_screen @output.puts "\e[2J\e[f" end
format_single_row(space, space_index, board_split_into_rows, single_row)
click to toggle source
# File lib/cli.rb, line 111 def format_single_row(space, space_index, board_split_into_rows, single_row) formatted_number = add_space_to_single_digit_number(space) single_row = add_separator_where_applicable(space_index, board_split_into_rows, single_row, formatted_number) end
print_board()
click to toggle source
# File lib/cli.rb, line 94 def print_board board_split_into_rows = split_board_into_rows(board_with_index_in_empty_spaces) board_split_into_rows.each_with_index do |row, row_index| single_row = "" row.each_with_index {|space, space_index| single_row = format_single_row(space, space_index, board_split_into_rows, single_row)} print_board_rows_with_separator(row_index, row, single_row) end end
print_board_rows_with_separator(row_index, row, single_row)
click to toggle source
# File lib/cli.rb, line 124 def print_board_rows_with_separator(row_index, row, single_row) @output.puts row_index == row.size - 1 ? single_row : single_row + "\n" + "-" * single_row.length end
print_board_size()
click to toggle source
# File lib/cli.rb, line 146 def print_board_size @output.puts "Choose the number of rows on the board" @output.puts "Select 3 or 4" end
print_choose_player(marker)
click to toggle source
# File lib/cli.rb, line 139 def print_choose_player(marker) @output.puts "Choose player type for #{marker}" @output.puts "1) Human" @output.puts "2) Simple Computer" @output.puts "3) Expert Computer" end
print_game_over()
click to toggle source
# File lib/cli.rb, line 128 def print_game_over @output.puts "Game Over" end
print_outcome()
click to toggle source
# File lib/cli.rb, line 132 def print_outcome clear_screen print_game_over print_board @output.puts @game.board.winner ? "#{@game.board.winner} is the winner" : "Tied Game" end
print_players_turn()
click to toggle source
# File lib/cli.rb, line 90 def print_players_turn @output.puts "#{@game.current_player.marker}, take your turn" end
print_welcome()
click to toggle source
# File lib/cli.rb, line 86 def print_welcome @output.puts "Tic Tac Toe" end
run_app()
click to toggle source
# File lib/cli.rb, line 43 def run_app start_of_game until @game.game_over? single_turn end print_outcome end
setup_board()
click to toggle source
# File lib/cli.rb, line 67 def setup_board print_board_size choice = select_board_size([3,4]) return Board.new(Array.new(choice * choice)) end
setup_game(board, player_x, player_o)
click to toggle source
# File lib/cli.rb, line 82 def setup_game(board, player_x, player_o) @game = Game.new(board, player_x, player_o) end
single_turn()
click to toggle source
# File lib/cli.rb, line 60 def single_turn clear_screen print_board print_players_turn @game.take_turn end
split_board_into_rows(spaces)
click to toggle source
# File lib/cli.rb, line 107 def split_board_into_rows(spaces) spaces.each_slice(@game.board.number_of_rows) end
start_of_game()
click to toggle source
# File lib/cli.rb, line 51 def start_of_game clear_screen print_welcome board = setup_board player_x = choose_player("X") player_o = choose_player("O") setup_game(board, player_x, player_o) end