Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/board.rb
Constant Summary collapse
- BackRow =
{ white: 7, black: 0 }
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
-
#en_passent ⇒ Object
Returns the value of attribute en_passent.
Class Method Summary collapse
Instance Method Summary collapse
- #at(x, y) ⇒ Object
- #enemies(piece) ⇒ Object
-
#initialize(flag = false) ⇒ Board
constructor
A new instance of Board.
- #move(piece, x, y) ⇒ Object
- #place(piece) ⇒ Object
- #remove(piece) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(flag = false) ⇒ Board
Returns a new instance of Board.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/board.rb', line 6 def initialize(flag = false) @board = [] 8.times do |y| row = [] 8.times {|x| row << EmptySpace.new(x,y)} @board << row end unless flag pieces = [] 8.times do |x| pieces << BlackPawn.new(x,6) << WhitePawn.new(x,1) end [0,7].each do |x| pieces << BlackRook.new(x,7) << WhitePawn.new(x,0) end [1,6].each do |x| pieces << BlackKnight.new(x,7) << WhiteKnight.new(x,0) end [2,5].each do |x| pieces << BlackBishop.new(x,7) << WhiteBishop.new(x,0) end pieces << BlackQueen.new(4,7) << WhiteQueen.new(3,0) pieces << BlackKing.new(3,7) << WhiteKing.new(4,0) pieces.each {|piece| place(piece)} end @en_passent = NullSpace.new end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
5 6 7 |
# File 'lib/board.rb', line 5 def board @board end |
#en_passent ⇒ Object
Returns the value of attribute en_passent.
5 6 7 |
# File 'lib/board.rb', line 5 def en_passent @en_passent end |
Class Method Details
.board_safe(points) ⇒ Object
61 62 63 64 65 |
# File 'lib/board.rb', line 61 def self.board_safe(points) points.select do |point| not(point.any? {|cord| cord < 0 or cord > 7}) end end |
Instance Method Details
#at(x, y) ⇒ Object
49 50 51 |
# File 'lib/board.rb', line 49 def at(x,y) board[y][x] end |
#enemies(piece) ⇒ Object
53 54 55 |
# File 'lib/board.rb', line 53 def enemies(piece) board.flatten.select {|other| other.enemy_of?(piece)} end |
#move(piece, x, y) ⇒ Object
57 58 59 |
# File 'lib/board.rb', line 57 def move(piece, x, y) piece.move(self, x, y) end |
#place(piece) ⇒ Object
41 42 43 |
# File 'lib/board.rb', line 41 def place(piece) piece.place_on(self) end |
#remove(piece) ⇒ Object
45 46 47 |
# File 'lib/board.rb', line 45 def remove(piece) piece.remove_from(self) end |
#to_s ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/board.rb', line 67 def to_s out = "\s\s" out << (0..7).to_a.join(' ') << "\n" @board.each_with_index do |row, i| out << "#{i} " << (row.collect{|pc| pc.to_s}).join('') << "\n" end out end |