Class: Chess::Pieces::King

Inherits:
Piece
  • Object
show all
Includes:
Save::FenCodeFromBoard
Defined in:
lib/chess/pieces/king.rb

Overview

King

Instance Attribute Summary collapse

Attributes inherited from Piece

#color, #pos, #valid_moves

Instance Method Summary collapse

Methods included from Save::FenCodeFromBoard

#fen_piece_placement, #fen_piece_placement_by_rank, #generate_fen_code, #letter_of

Methods inherited from Piece

#black?, #white?

Constructor Details

#initialize(color) ⇒ King

Returns a new instance of King.



13
14
15
16
# File 'lib/chess/pieces/king.rb', line 13

def initialize(color)
  super
  @in_check = false
end

Instance Attribute Details

#in_checkObject

Returns the value of attribute in_check.



11
12
13
# File 'lib/chess/pieces/king.rb', line 11

def in_check
  @in_check
end

Instance Method Details

#black_castling_moves(board) ⇒ Array

helper method for #castling_moves

Parameters:

Returns:

  • (Array)

    castling_moves_arr



107
108
109
110
111
112
# File 'lib/chess/pieces/king.rb', line 107

def black_castling_moves(board)
  moves = []
  moves << ['g', 7] if board.castling_rights.include?('k') && can_castle?(board, 'k')
  moves << ['c', 7] if board.castling_rights.include?('q') && can_castle?(board, 'q')
  moves
end

#can_castle?(board, castling_right) ⇒ Boolean

check if can castle(no_piece_in_between) for the given castling_right

Parameters:

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chess/pieces/king.rb', line 118

def can_castle?(board, castling_right)
  case castling_right
  when 'Q'
    no_piece_in_between?(board, ['a', 0], ['e', 0])
  when 'K'
    no_piece_in_between?(board, ['e', 0], ['h', 0])
  when 'q'
    no_piece_in_between?(board, ['a', 7], ['e', 7])
  when 'k'
    no_piece_in_between?(board, ['e', 7], ['h', 7])
  end
end

#castling_moves(board) ⇒ Array

finds possible castling moves

Parameters:

Returns:

  • (Array)

    castling_moves_arr



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chess/pieces/king.rb', line 70

def castling_moves(board)
  moves = []
  if valid_castling_rights?(board)
    if white?
      moves += white_castling_moves(board)
    elsif black?
      moves += black_castling_moves(board)
    end
  end
  moves
end

#get_moves_on_direction(board, directions) ⇒ Array

helper method for #one_step_all_direction_moves

Parameters:

Returns:

  • (Array)

    all_dir_possible_moves_arr



56
57
58
59
60
61
62
63
64
# File 'lib/chess/pieces/king.rb', line 56

def get_moves_on_direction(board, directions)
  moves = []
  directions.each do |direction|
    if board.pos_in_range?(direction) && (board.empty_at?(*direction) || board.enemy_at?(*direction))
      moves << direction
    end
  end
  moves
end

#in_check?(board, opponent_pieces) ⇒ Boolean

checks if king is in check by one or more opponent_piece

Parameters:

Returns:

  • (Boolean)


154
155
156
157
158
159
160
161
162
163
# File 'lib/chess/pieces/king.rb', line 154

def in_check?(board, opponent_pieces)
  @in_check = false
  opponent_pieces.each do |piece|
    break if @in_check

    possible_moves = piece.possible_moves(board)
    @in_check = true if possible_moves.include?(@pos)
  end
  @in_check
end

#no_piece_in_between?(board, start, finish) ⇒ Boolean

checks if no piece in between start and finish helper method for #can_castle?

Parameters:

Returns:

  • (Boolean)


137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/chess/pieces/king.rb', line 137

def no_piece_in_between?(board, start, finish)
  curr_file = start.first
  rank = start.last
  pieces = []
  loop do
    curr_file = (curr_file.ord + 1).chr
    break if curr_file == finish.first

    pieces << board.piece_at(curr_file, rank)
  end
  pieces.all?('')
end

#one_step_all_direction_moves(board, file, rank) ⇒ Array

gets one step possible move in all direction for king

Parameters:

Returns:

  • (Array)

    all_dir_possible_moves_arr



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chess/pieces/king.rb', line 37

def one_step_all_direction_moves(board, file, rank)
  north = board.north_pos(file, rank)
  south = board.south_pos(file, rank)
  west = board.west_pos(file, rank)
  east = board.east_pos(file, rank)
  north_west = board.north_west_pos(file, rank)
  south_east = board.south_east_pos(file, rank)
  south_west = board.south_west_pos(file, rank)
  north_east = board.north_east_pos(file, rank)
  directions = north, south, west, east, north_west, south_east, south_west, north_east

  get_moves_on_direction(board, directions)
end

#possible_moves(board) ⇒ Array

all possible_moves of Bishop

Parameters:

Returns:

  • (Array)

    possible_moves_arr



22
23
24
25
26
27
28
29
# File 'lib/chess/pieces/king.rb', line 22

def possible_moves(board)
  file = @pos[0]
  rank = @pos[1]
  moves = []
  moves += one_step_all_direction_moves(board, file, rank)
  moves += castling_moves(board)
  moves
end

#valid_castling_rights?(board) ⇒ Boolean

check if valid_castling_rights exits

Parameters:

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
# File 'lib/chess/pieces/king.rb', line 85

def valid_castling_rights?(board)
  return false if board.castling_rights == '-'

  board.castling_rights.include?('K') ||
    board.castling_rights.include?('Q') ||
    board.castling_rights.include?('k') ||
    board.castling_rights.include?('q')
end

#white_castling_moves(board) ⇒ Array

helper method for #castling_moves

Parameters:

Returns:

  • (Array)

    castling_moves_arr



97
98
99
100
101
102
# File 'lib/chess/pieces/king.rb', line 97

def white_castling_moves(board)
  moves = []
  moves << ['g', 0] if board.castling_rights.include?('K') && can_castle?(board, 'K')
  moves << ['c', 0] if board.castling_rights.include?('Q') && can_castle?(board, 'Q')
  moves
end