Module: Chess::PlayPawnMoves

Includes:
Display
Included in:
PlayMovesByType
Defined in:
lib/chess/play_moves/play_pawn_moves.rb

Overview

PawnMoves

Constant Summary

Constants included from Display

Display::COLORS

Instance Method Summary collapse

Methods included from Display

#bg_color, #color, #display_board, #display_buttons, #king_check_dot, #move_dots, #print_board_data, #print_files, #print_piece_square, #print_square, #prompt_enter_code, #prompt_pawn_promotion_choices, #prompt_save_name, #prompt_select_save, #prompt_start_choices, #redraw_display, #square_bg_color_name, #square_string

Instance Method Details

#attacking_en_passant?(piece, board, move_pos) ⇒ Boolean

check if move is attacking a enemy pawn at Board#possible_en_passant_target

Parameters:

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/chess/play_moves/play_pawn_moves.rb', line 43

def attacking_en_passant?(piece, board, move_pos)
  file = piece.pos.first
  rank = piece.pos.last
  moves = piece.attack_en_passant(board, file, rank)
  moves.include?(move_pos)
end

#move_two_step(piece, board, move_pos) ⇒ void

This method returns an undefined value.

play two step move

Parameters:



35
36
37
38
39
# File 'lib/chess/play_moves/play_pawn_moves.rb', line 35

def move_two_step(piece, board, move_pos)
  play_normal_move(piece, board, move_pos)
  set_board_possible_en_passant(piece, board)
  @remove_en_passant = false
end

#pawn_is_promoting?(piece) ⇒ Boolean

check if pawn is promoting

Parameters:

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/chess/play_moves/play_pawn_moves.rb', line 52

def pawn_is_promoting?(piece)
  rank = piece.pos.last
  return true if rank == 1 && piece.black?

  true if rank == 6 && piece.white?
end

#pawn_two_step_move?(piece, board, move_pos) ⇒ Boolean

check if moving two step

Parameters:

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/chess/play_moves/play_pawn_moves.rb', line 62

def pawn_two_step_move?(piece, board, move_pos)
  file = piece.pos.first
  rank = piece.pos.last
  moves = piece.two_step_forward(board, file, rank)
  moves.include?(move_pos)
end

#play_pawn_move(piece, board, move_pos, inside_valid_moves_flag) ⇒ void

This method returns an undefined value.

play appropriate type of pawn move

Parameters:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chess/play_moves/play_pawn_moves.rb', line 16

def play_pawn_move(piece, board, move_pos, inside_valid_moves_flag)
  if pawn_is_promoting?(piece)
    play_pawn_promotion(piece, board, move_pos, inside_valid_moves_flag)
  elsif attacking_en_passant?(piece, board, move_pos)
    play_normal_move(piece, board, move_pos)
    remove_enemy_en_passant_pawn(piece, board, move_pos)
  elsif pawn_two_step_move?(piece, board, move_pos)
    move_two_step(piece, board, move_pos)
  else
    play_normal_move(piece, board, move_pos)
  end
end

#play_pawn_promotion(piece, board, move_pos, inside_valid_moves_flag) ⇒ void

This method returns an undefined value.

play pawn promotion move

Parameters:



106
107
108
109
110
111
112
113
# File 'lib/chess/play_moves/play_pawn_moves.rb', line 106

def play_pawn_promotion(piece, board, move_pos, inside_valid_moves_flag)
  board.remove_piece_at(piece.pos)
  new_piece_letter = 'q'
  new_piece_letter = prompt_pawn_promotion_choices unless inside_valid_moves_flag
  new_piece_letter = new_piece_letter.upcase if piece.white?
  piece = board.create_piece(new_piece_letter)
  board.insert_piece_at(piece, move_pos)
end

#remove_en_passant_from_board(board) ⇒ void

This method returns an undefined value.

remove possible_en_passant_target pos from board

Parameters:



99
100
101
# File 'lib/chess/play_moves/play_pawn_moves.rb', line 99

def remove_en_passant_from_board(board)
  board.possible_en_passant_target = ''
end

#remove_enemy_en_passant_pawn(piece, board, move_pos) ⇒ void

This method returns an undefined value.

remove enemy pawn in attacking en passant

Parameters:



72
73
74
75
76
77
78
79
80
# File 'lib/chess/play_moves/play_pawn_moves.rb', line 72

def remove_enemy_en_passant_pawn(piece, board, move_pos)
  file = move_pos.first
  rank = if piece.white?
           move_pos.last - 1
         else
           move_pos.last + 1
         end
  board.remove_piece_at([file, rank])
end

#set_board_possible_en_passant(piece, board) ⇒ void

This method returns an undefined value.

set board possible_en_passant_target

Parameters:



86
87
88
89
90
91
92
93
94
# File 'lib/chess/play_moves/play_pawn_moves.rb', line 86

def set_board_possible_en_passant(piece, board)
  file = piece.pos.first
  rank = if piece.white?
           piece.pos.last - 1
         else
           piece.pos.last + 1
         end
  board.possible_en_passant_target = file + rank.to_s
end