Module: HumanInputValidation

Included in:
Engine
Defined in:
lib/sapphire-chess/human_input_validation.rb

Instance Method Summary collapse

Instance Method Details

#convert_player_input(player_move_input) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sapphire-chess/human_input_validation.rb', line 120

def convert_player_input(player_move_input)
  if single_input?(player_move_input)
    piece = player_move_input
    target_square = prompt_target_square(piece)
  else
    piece = player_move_input.first
    target_square = player_move_input.last
  end

  [piece, target_square]
end

#double_input?(player_move_input) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/sapphire-chess/human_input_validation.rb', line 91

def double_input?(player_move_input)
  player_move_input.first.is_a?(Array)
end

#prompt_colorObject



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/sapphire-chess/human_input_validation.rb', line 2

def prompt_color
  puts 'What color would you like to play? ([W]hite/[B]lack)'
  choice = nil
  loop do
    choice = gets.chomp.strip.downcase
    break if valid_color?(choice)

    puts 'Please, enter a valid color choice.'
  end
  choice
end

#prompt_difficultyObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sapphire-chess/human_input_validation.rb', line 31

def prompt_difficulty
  display_difficulty_settings
  difficulty_input = nil
  loop do
    difficulty_input = gets.chomp.strip.downcase
    break if valid_difficulty?(difficulty_input)

    puts 'Please, enter a valid difficulty setting.'
  end

  case difficulty_input
  when 'easy' then 1
  when 'medium' then 2
  when 'hard' then 3
  else difficulty_input.to_i
  end
end

#prompt_game_modeObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sapphire-chess/human_input_validation.rb', line 18

def prompt_game_mode
  display_game_modes
  game_mode = nil
  loop do
    game_mode = gets.chomp.strip.to_i
    break if [1, 2, 3].include?(game_mode)

    puts 'Please, enter a valid game mode.'
  end
  new_line
  game_mode
end

#prompt_moveObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sapphire-chess/human_input_validation.rb', line 53

def prompt_move
  display_move_message

  player_move_input = nil
  loop do
    player_move_input = current_player.select_move
    break if valid_player_input?(player_move_input)

    puts 'Please, enter a valid movement.'
  end

  player_move_input
end

#prompt_target_square(piece) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sapphire-chess/human_input_validation.rb', line 67

def prompt_target_square(piece)
  target_square = nil
  puts "Where do you want to move the #{board[piece].class}?"
  loop do
    target_square = current_player.select_move
    break if valid_target_square?(piece, target_square)

    puts "The #{board[piece].class} selected can't move to that square."
  end

  target_square
end

#single_input?(player_move_input) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/sapphire-chess/human_input_validation.rb', line 95

def single_input?(player_move_input)
  player_move_input.first.is_a?(Integer)
end

#valid_castling?(side) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/sapphire-chess/human_input_validation.rb', line 116

def valid_castling?(side)
  current_player.castle_rights?(side)
end

#valid_color?(choice) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/sapphire-chess/human_input_validation.rb', line 14

def valid_color?(choice)
  %w[white black w b].include?(choice)
end

#valid_difficulty?(difficulty) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/sapphire-chess/human_input_validation.rb', line 49

def valid_difficulty?(difficulty)
  %w[easy medium hard 1 2 3].include?(difficulty)
end

#valid_piece_selection?(piece_location) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
# File 'lib/sapphire-chess/human_input_validation.rb', line 99

def valid_piece_selection?(piece_location)
  piece = board[piece_location]

  piece.is_a?(Piece) &&
    piece.color == current_player.color &&
    !piece.available_moves.empty? &&
    (!board.in_check?(current_player.color) || board.in_check?(current_player.color) && !piece.safe_moves.empty?)
end

#valid_player_input?(player_move_input) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
# File 'lib/sapphire-chess/human_input_validation.rb', line 80

def valid_player_input?(player_move_input)
  if double_input?(player_move_input)
    valid_piece_selection?(player_move_input.first) &&
      valid_target_square?(player_move_input.first, player_move_input.last)
  elsif player_move_input.first == :castle
    valid_castling?(player_move_input.last)
  else
    valid_piece_selection?(player_move_input)
  end
end

#valid_target_square?(piece_location, target_square) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
# File 'lib/sapphire-chess/human_input_validation.rb', line 108

def valid_target_square?(piece_location, target_square)
  piece = board[piece_location]
  valid_target = piece.available_moves.include?(target_square)

  (valid_target && !board.in_check?(current_player.color)) ||
    (valid_target && board.in_check?(current_player.color) && piece.safe_moves.include?(target_square))
end