Class: MailChess::Piece::Piece

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_chess/pieces/base.rb

Overview

Represents a chesspiece.

Direct Known Subclasses

Bishop, King, Knight, Pawn, Queen, Rook

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, piece_id, board) ⇒ Piece

Create a new Piece object.

Arguments:

db: Sequel database object
piece_id: (Integer)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mail_chess/pieces/base.rb', line 30

def initialize db, piece_id, board
  @db = db
  
  Piece.create_tables(@db) if Piece.need_to_create_tables?(@db)
    
  @db_entry = @db[:pieces].filter(:id => piece_id).first
  
  @id = @db_entry[:id].to_i
  @type = @db_entry[:type]
  @color = @db_entry[:color].to_sym
  @board = board
  @board.add @db_entry[:position], self
end

Instance Attribute Details

#boardObject (readonly)

Board where this piece standing.



20
21
22
# File 'lib/mail_chess/pieces/base.rb', line 20

def board
  @board
end

#colorObject (readonly)

Piece color.



17
18
19
# File 'lib/mail_chess/pieces/base.rb', line 17

def color
  @color
end

#htmlObject (readonly)

HTML tag of this piece.



23
24
25
# File 'lib/mail_chess/pieces/base.rb', line 23

def html
  @html
end

#idObject (readonly)

Piece ID in database.



11
12
13
# File 'lib/mail_chess/pieces/base.rb', line 11

def id
  @id
end

#typeObject (readonly)

Piece type.



14
15
16
# File 'lib/mail_chess/pieces/base.rb', line 14

def type
  @type
end

Class Method Details

.create_tables(db) ⇒ Object

Create tables.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mail_chess/pieces/base.rb', line 52

def self.create_tables db
  db.create_table? :boards do
    primary_key :id
  end
  
  db.create_table? :pieces do
    primary_key :id
    String :type
    String :color
    String :position
    foreign_key :board_id, :boards, :key => :id
  end
end

.load!(db, piece_id, board) ⇒ Object

Load the pieces to a board.

Arguments:

db: Sequel database object
board: (Board)

Returns: (Piece)



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mail_chess/pieces/base.rb', line 93

def self.load! db, piece_id, board
  p = db[:pieces].filter(:id => piece_id).first
  color = p[:color].to_sym
  position = p[:position]
  case p[:type]
    when 'bishop' then Bishop.new(db, piece_id, board)
    when 'king' then King.new(db, piece_id, board)
    when 'knight' then Knight.new(db, piece_id, board)
    when 'pawn' then Pawn.new(db, piece_id, board)
    when 'rook' then Rook.new(db, piece_id, board)
    when 'queen' then Queen.new(db, piece_id, board)
  end
end

.need_to_create_tables?(db) ⇒ Boolean

Checks if needed to create tables.

Returns: (Boolean)

Returns:

  • (Boolean)


47
48
49
# File 'lib/mail_chess/pieces/base.rb', line 47

def self.need_to_create_tables? db
  return (db.tables + [:boards, :pieces]).uniq.size != db.tables.size
end

.new!(db, board, position, color) ⇒ Object

ABSTRACT Create a new piece and add to a table.

Arguments:

db: Sequel database object
board: (Board)
position: (String) eg.: 'f3'
color: (Symbol) :white or :black

Returns: (Piece)



82
83
84
# File 'lib/mail_chess/pieces/base.rb', line 82

def self.new! db, board, position, color
  Piece.create_tables(db) if Piece.need_to_create_tables?(db)
end

Instance Method Details

#delete!Object

Delete piece – save! won’t fix it!



113
114
115
# File 'lib/mail_chess/pieces/base.rb', line 113

def delete!
  @db[:pieces].filter(:id => @id).delete
end

#positionObject

Find out the position of this piece.

Returns: (String)



69
70
71
# File 'lib/mail_chess/pieces/base.rb', line 69

def position
  return @board.pieces.key(self)
end

#save!Object

Save piece – position.



108
109
110
# File 'lib/mail_chess/pieces/base.rb', line 108

def save!
  @db[:pieces].filter(:id => @id).update(:position => position)
end