class Rchess::Board

Constants

BOUDARIES
EMPTY_BOX

Attributes

boxes[W]
loosed_pieces[R]

Public Class Methods

new() click to toggle source
# File lib/rchess/board.rb, line 11
def initialize
  @boxes =[[:R, :C, :B, :Q, :K, :B, :C, :R],
           [:P, :P, :P, :P, :P, :P, :P, :P],
           ["", "", "", "", "", "", "", ""],
           ["", "", "", "", "", "", "", ""],
           ["", "", "", "", "", "", "", ""],
           ["", "", "", "", "", "", "", ""],
           [:p, :p, :p, :p, :p, :p, :p, :p],
           [:r, :c, :b, :q, :k, :b, :c, :r]]
end

Public Instance Methods

box_at_coord(coord) click to toggle source
# File lib/rchess/board.rb, line 38
def box_at_coord(coord)
  @boxes[coord.y][coord.x]
end
boxes() click to toggle source
# File lib/rchess/board.rb, line 26
def boxes
  @boxes ||= Array.new(BOUDARIES.count){ Array.new(BOUDARIES.count){ EMPTY_BOX }}
end
coord_for_type_and_color(type, color) click to toggle source
# File lib/rchess/board.rb, line 66
def coord_for_type_and_color(type, color)
  piece_type  = Piece.type_to_color(type, color) #TODO: make the type dynamic ~> piece::TYPES['king']
  piece_index = self.boxes.flatten.index(piece_type)
  coord_from_index(piece_index)
end
coord_is_threatened_by_color?(target_coord, color) click to toggle source
# File lib/rchess/board.rb, line 50
def coord_is_threatened_by_color?(target_coord, color)
  binding.pry
  paths_from_target = Paths::Base.threaten_destinations_from_coord(target_coord, self)

  paths_from_target.flatten(1).select{|p| !p.empty?}.any? do |paths|
    paths.any? do |coord|
      dstPiece = self.piece_at_coord(coord)
      dstPiece && dstPiece.color != color
    end
  end
end
move_src_to_dst!(srcCoord, dstCoord) click to toggle source
# File lib/rchess/board.rb, line 42
def move_src_to_dst!(srcCoord, dstCoord)
  srcPiece      = piece_at_coord(srcCoord)
  dstPiece      = piece_at_coord(dstCoord)
  src_box_value = dstPiece.nil? || dstPiece.color != srcPiece.color ? EMPTY_BOX : dstPiece.type # if diff color or nil we erase, else we swap

  @boxes[srcPiece.y][srcPiece.x], @boxes[dstCoord.y][dstCoord.x] = src_box_value, srcPiece.type
end
movement_within_board?(srcCoord, dstCoord) click to toggle source
# File lib/rchess/board.rb, line 30
def movement_within_board?(srcCoord, dstCoord)
  coord_within_boundaries?(srcCoord) && coord_within_boundaries?(dstCoord)
end
piece_at_coord(coord) click to toggle source
# File lib/rchess/board.rb, line 34
def piece_at_coord(coord)
  box_at_coord(coord) == EMPTY_BOX ? nil : Piece.new({coord: coord, board: self})
end
valid_move?(piece, dstCoord) click to toggle source
# File lib/rchess/board.rb, line 62
def valid_move?(piece, dstCoord)
  piece.can_goto_coord?(dstCoord) && !king_would_be_threatened?(piece, dstCoord)
end

Private Instance Methods

coord_from_index(index) click to toggle source
# File lib/rchess/board.rb, line 90
def coord_from_index(index)
  width = BOUDARIES.count
  Coord.new({y: index/width, x: index%width})
end
coord_within_boundaries?(coord) click to toggle source
# File lib/rchess/board.rb, line 86
def coord_within_boundaries?(coord)
  BOUDARIES.include?(coord.x) && BOUDARIES.include?(coord.y)
end
king_would_be_threatened?(piece, dstCoord) click to toggle source
# File lib/rchess/board.rb, line 74
def king_would_be_threatened?(piece, dstCoord)
  #simulate the next board
  next_board = Board.new
  #TODO: need a more efficient way of copying the values
  next_board.boxes = Marshal.load(Marshal.dump(@boxes))
  next_board.move_src_to_dst!(piece.coord, dstCoord)

  king_coord = coord_for_type_and_color(:k, piece.color)
  oponent_color = piece.color == Piece::WHITE_COLOR ? Piece::BLACK_COLOR : Piece::WHITE_COLOR
  coord_is_threatened_by_color?(king_coord, oponent_color)
end