Class: Chess::Board

Inherits:
Object
  • Object
show all
Includes:
BoardFromFenCode, BoardPos
Defined in:
lib/chess/board/board.rb

Overview

Board

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BoardPos

#east_pos, #north_east_pos, #north_pos, #north_west_pos, #pos_in_range?, #pos_nil?, #south_east_pos, #south_pos, #south_west_pos, #west_pos

Methods included from BoardFromFenCode

#black_piece, #create_board, #create_piece, #fill_board, #fill_rank, #generate_data, #insert_piece, #white_piece

Constructor Details

#initialize(fen_code) ⇒ Board

Returns a new instance of Board.

Parameters:

  • fen_code (String)


31
32
33
34
35
# File 'lib/chess/board/board.rb', line 31

def initialize(fen_code)
  @white_pieces = []
  @black_pieces = []
  @data = generate_data(fen_code)
end

Instance Attribute Details

#black_kingObject

Returns the value of attribute black_king.



26
27
28
# File 'lib/chess/board/board.rb', line 26

def black_king
  @black_king
end

#black_piecesObject

Returns the value of attribute black_pieces.



26
27
28
# File 'lib/chess/board/board.rb', line 26

def black_pieces
  @black_pieces
end

#castling_rightsObject

Returns the value of attribute castling_rights.



26
27
28
# File 'lib/chess/board/board.rb', line 26

def castling_rights
  @castling_rights
end

#current_playerObject

Returns the value of attribute current_player.



26
27
28
# File 'lib/chess/board/board.rb', line 26

def current_player
  @current_player
end

#dataHash

‘a’ to ‘h’ are files, rnbqkp are black pieces and RNBQKP are white pieces along the 8 ranks.

Examples:

generated data looks like this

{
"a"=>["R", "P", "", "", "", "", "p", "r"],
"b"=>["N", "P", "", "", "", "", "p", "n"],
"c"=>["B", "P", "", "", "", "", "p", "b"],
"d"=>["Q", "P", "", "", "", "", "p", "q"],
"e"=>["K", "P", "", "", "", "", "p", "k"],
"f"=>["B", "P", "", "", "", "", "p", "b"],
"g"=>["N", "P", "", "", "", "", "p", "n"],
"h"=>["R", "P", "", "", "", "", "p", "r"]
}

Returns:

  • (Hash)

    board data



26
27
28
# File 'lib/chess/board/board.rb', line 26

def data
  @data
end

#full_moveObject

Returns the value of attribute full_move.



26
27
28
# File 'lib/chess/board/board.rb', line 26

def full_move
  @full_move
end

#half_moveObject

Returns the value of attribute half_move.



26
27
28
# File 'lib/chess/board/board.rb', line 26

def half_move
  @half_move
end

#possible_en_passant_targetObject

Returns the value of attribute possible_en_passant_target.



26
27
28
# File 'lib/chess/board/board.rb', line 26

def possible_en_passant_target
  @possible_en_passant_target
end

#white_kingObject

Returns the value of attribute white_king.



26
27
28
# File 'lib/chess/board/board.rb', line 26

def white_king
  @white_king
end

#white_piecesObject

Returns the value of attribute white_pieces.



26
27
28
# File 'lib/chess/board/board.rb', line 26

def white_pieces
  @white_pieces
end

Instance Method Details

#empty_at?(file, rank) ⇒ Boolean

checks if a pos is empty

Parameters:

  • file (String)
  • rank (Integer)

Returns:

  • (Boolean)


50
51
52
# File 'lib/chess/board/board.rb', line 50

def empty_at?(file, rank)
  piece_at(file, rank) == ''
end

#enemy_at?(file, rank) ⇒ Boolean

check if enemy at pos

Parameters:

  • file (String)
  • rank (Integer)

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/chess/board/board.rb', line 82

def enemy_at?(file, rank)
  piece = piece_at(file, rank)
  @current_player != piece.color.chr
end

#insert_piece_at(piece, file, rank) ⇒ void

This method returns an undefined value.

inserts given piece at given pos

Examples:

file and rank are decomposed from array

insert_piece_at(['d',0])

Parameters:



69
70
71
72
73
74
75
76
77
# File 'lib/chess/board/board.rb', line 69

def insert_piece_at(piece, (file, rank))
  @data[file][rank] = piece
  piece.pos = [file, rank]
  if @current_player == 'w'
    @white_pieces << piece unless @white_pieces.include?(piece)
  else
    @black_pieces << piece unless @white_pieces.include?(piece)
  end
end

#piece_at(file, rank) ⇒ Object

returns piece at the given position

Parameters:

  • file (String)
  • rank (Integer)

Returns:



41
42
43
44
45
# File 'lib/chess/board/board.rb', line 41

def piece_at(file, rank)
  return if @data[file].nil?

  @data[file][rank]
end

#remove_piece_at(file, rank) ⇒ void

This method returns an undefined value.

removes the piece from board and also from #black_pieces or #white_pieces

Examples:

file and rank are decomposed from array

remove_piece_at(['d',0])


58
59
60
61
62
# File 'lib/chess/board/board.rb', line 58

def remove_piece_at((file, rank))
  @data[file][rank] = ''
  @black_pieces.reject! { |piece| piece.pos == [file, rank] }
  @white_pieces.reject! { |piece| piece.pos == [file, rank] }
end

#reset_half_moveObject

resets half move to 0



88
89
90
# File 'lib/chess/board/board.rb', line 88

def reset_half_move
  @half_move = 0
end