class TicTacToe::Input

Constants

NOT_EMPTY_INPUT_POSITION
NOT_VALID_INPUT
ONE_PLAYER_MODE
TWO_PLAYER_MODE
VALID_INPUT

Public Instance Methods

input_line() click to toggle source
# File lib/tic_tac_toe/input.rb, line 37
def input_line
  STDIN.gets.chomp
end
input_mode() click to toggle source
# File lib/tic_tac_toe/input.rb, line 13
def input_mode
  display.display_modes_options
  enter_mode
end
input_name(args) click to toggle source
# File lib/tic_tac_toe/input.rb, line 18
def input_name(args)
  display.display_msg_ask_for_player_name(args)
  input_line
end
input_player_action(player, board) click to toggle source
# File lib/tic_tac_toe/input.rb, line 28
def input_player_action(player, board)
  begin
    input = select_position(player, board.get_empty_positions)
    validated_input = input_validation(input, board)
    input_error_message(validated_input)
  end while !valid?(validated_input)
  input.downcase
end
input_replay() click to toggle source
# File lib/tic_tac_toe/input.rb, line 23
def input_replay
  display.display_msg_replay
  input_line.downcase
end

Private Instance Methods

display() click to toggle source
# File lib/tic_tac_toe/input.rb, line 42
def display
  Display.instance
end
enter_mode() click to toggle source
# File lib/tic_tac_toe/input.rb, line 91
def enter_mode
  mode = input_line
  while !valid_mode?(mode.to_i)
    display.display_error_msg_mode
    mode = input_line
  end
  mode.to_i
end
input_error_message(validated_input) click to toggle source
# File lib/tic_tac_toe/input.rb, line 78
def input_error_message(validated_input)
 case validated_input
 when NOT_VALID_INPUT
   display.display_msg_not_valid_input
 when NOT_EMPTY_INPUT_POSITION
   display.display_msg_not_empty_position
 end 
end
input_validation(input, board) click to toggle source
# File lib/tic_tac_toe/input.rb, line 54
def input_validation(input, board)
  return VALID_INPUT if quit_reset_option?(input)
  if(Board.valid_position_string?(input))
    board.position_empty?(input) ? VALID_INPUT : NOT_EMPTY_INPUT_POSITION
  else
    NOT_VALID_INPUT
  end
end
quit_reset_option?(option) click to toggle source
# File lib/tic_tac_toe/input.rb, line 87
def quit_reset_option?(option)
  ['help','quit','reset'].include?(option)
end
receive_player_input() click to toggle source
# File lib/tic_tac_toe/input.rb, line 69
def receive_player_input
  display.display_msg_select_position
  input_line
end
select_position(player, posible_positions) click to toggle source
# File lib/tic_tac_toe/input.rb, line 46
def select_position(player, posible_positions)
  if player.computer?
    select_random_position(posible_positions)
  else
    receive_player_input
  end
end
select_random_position(posible_positions) click to toggle source
# File lib/tic_tac_toe/input.rb, line 63
def select_random_position(posible_positions)
  index = Kernel.rand(posible_positions.size)
  display.display_msg_computer_thinking
  posible_positions[index].position
end
valid?(validated_input) click to toggle source
# File lib/tic_tac_toe/input.rb, line 74
def valid?(validated_input)
  validated_input == VALID_INPUT
end
valid_mode?(mode) click to toggle source
# File lib/tic_tac_toe/input.rb, line 100
def valid_mode?(mode)
  [ONE_PLAYER_MODE, TWO_PLAYER_MODE].include?(mode)
end