Class: Rchess::Board

Inherits:
Object
  • Object
show all
Includes:
Wisper::Publisher
Defined in:
lib/rchess/board.rb

Constant Summary collapse

BOUDARIES =
(0...8)
EMPTY_BOX =
""

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBoard

Returns a new instance of Board.



11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#boxesObject



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

def boxes
  @boxes ||= Array.new(BOUDARIES.count){ Array.new(BOUDARIES.count){ EMPTY_BOX }}
end

#loosed_piecesObject (readonly)

Returns the value of attribute loosed_pieces.



8
9
10
# File 'lib/rchess/board.rb', line 8

def loosed_pieces
  @loosed_pieces
end

Instance Method Details

#box_at_coord(coord) ⇒ Object



38
39
40
# File 'lib/rchess/board.rb', line 38

def box_at_coord(coord)
  @boxes[coord.y][coord.x]
end

#coord_for_type_and_color(type, color) ⇒ Object



66
67
68
69
70
# 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) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



42
43
44
45
46
47
48
# 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) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# 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) ⇒ Object



34
35
36
# 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) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rchess/board.rb', line 62

def valid_move?(piece, dstCoord)
  piece.can_goto_coord?(dstCoord) && !king_would_be_threatened?(piece, dstCoord)
end