Class: MailChess::Board
- Inherits:
-
Object
- Object
- MailChess::Board
- Defined in:
- lib/mail_chess/board.rb
Overview
Represents a chessboard.
Instance Attribute Summary collapse
-
#board ⇒ Object
Board itself.
-
#id ⇒ Object
readonly
Board ID in database.
-
#pieces ⇒ Object
Pieces on this board.
Class Method Summary collapse
-
.create_tables(db) ⇒ Object
Create tables.
-
.load!(db, filter, value) ⇒ Object
Load game from database.
-
.need_to_create_tables?(db) ⇒ Boolean
Checks if needed to create tables.
-
.new!(db) ⇒ Object
Create new board object and save it to database.
Instance Method Summary collapse
-
#add(field, piece) ⇒ Object
Add a new piece to board.
-
#delete! ⇒ Object
Delete game –
save!
won’t fix it!. -
#generate_pieces_moves ⇒ Object
Generates the available hits and moves for every pieces.
-
#initialize(db, board_id) ⇒ Board
constructor
Create a new Board object.
-
#remove(field) ⇒ Object
Removes a piece from board.
-
#save! ⇒ Object
Save game – save the pieces.
-
#start! ⇒ Object
Place the start position of pieces.
-
#to_html(player = :white) ⇒ Object
Generate HTML output.
Constructor Details
#initialize(db, board_id) ⇒ Board
Create a new Board object.
Arguments:
db: Sequel database object
board_id: (Integer)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mail_chess/board.rb', line 29 def initialize db, board_id @db = db Board.create_tables(@db) if Board.need_to_create_tables?(@db) @db_entry = @db[:boards].filter(:id => board_id).first @id = @db_entry[:id].to_i @pieces = {} @db[:pieces].filter(:board_id => @id).all.each do |p| Piece::Piece.load! db, p[:id], self end generate_pieces_moves end |
Instance Attribute Details
#board ⇒ Object
Board itself.
19 20 21 |
# File 'lib/mail_chess/board.rb', line 19 def board @board end |
#id ⇒ Object (readonly)
Board ID in database.
16 17 18 |
# File 'lib/mail_chess/board.rb', line 16 def id @id end |
#pieces ⇒ Object
Pieces on this board.
22 23 24 |
# File 'lib/mail_chess/board.rb', line 22 def pieces @pieces end |
Class Method Details
.create_tables(db) ⇒ Object
Create tables.
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mail_chess/board.rb', line 54 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, filter, value) ⇒ Object
Load game from database.
Arguments:
db: Sequel database object
filter: (Symbol) :id only (for now)
value: filters value
Returns: (Board)
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mail_chess/board.rb', line 90 def self.load! db, filter, value raise ArgumentError unless [:id, :reference].index(filter) dataset = db[:games].filter(filter => value) raise ArgumentError unless dataset.all.size == 1 id = dataset.first[:id] b = Board.new db, id return b end |
.need_to_create_tables?(db) ⇒ Boolean
Checks if needed to create tables.
Returns: (Boolean)
49 50 51 |
# File 'lib/mail_chess/board.rb', line 49 def self.need_to_create_tables? db return (db.tables + [:boards, :pieces]).uniq.size != db.tables.size end |
.new!(db) ⇒ Object
Create new board object and save it to database.
Arguments:
db: Sequel database object
Returns: (Board)
74 75 76 77 78 79 80 |
# File 'lib/mail_chess/board.rb', line 74 def self.new! db Board.create_tables(db) if Board.need_to_create_tables?(db) id = db[:boards].insert return Board.new db, id end |
Instance Method Details
#add(field, piece) ⇒ Object
Add a new piece to board.
Arguments:
field: (String)
piece: (Piece)
Returns: (Piece)
128 129 130 131 132 |
# File 'lib/mail_chess/board.rb', line 128 def add field, piece @pieces[field] = piece return piece end |
#delete! ⇒ Object
Delete game – save!
won’t fix it!
110 111 112 113 114 |
# File 'lib/mail_chess/board.rb', line 110 def delete! @db[:pieces].filter(:board_id => @id).delete # board deleted in Game class end |
#generate_pieces_moves ⇒ Object
Generates the available hits and moves for every pieces.
117 118 119 |
# File 'lib/mail_chess/board.rb', line 117 def generate_pieces_moves @pieces.values.each { |p| p.generate_moves } end |
#remove(field) ⇒ Object
Removes a piece from board.
Arguments:
field: (String)
Returns: (Piece)
140 141 142 143 144 145 |
# File 'lib/mail_chess/board.rb', line 140 def remove field piece = @pieces[field] @pieces.delete(field) return piece end |
#save! ⇒ Object
Save game – save the pieces.
103 104 105 106 107 |
# File 'lib/mail_chess/board.rb', line 103 def save! pieces.values.each do |p| p.save! end end |
#start! ⇒ Object
Place the start position of pieces.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/mail_chess/board.rb', line 148 def start! COLS.each do |c| Piece::Pawn.new!(@db, self, c + '2', :white) Piece::Pawn.new!(@db, self, c + '7', :black) end %w[a h].each do |c| Piece::Rook.new!(@db, self, c + '1', :white) Piece::Rook.new!(@db, self, c + '8', :black) end %w[b g].each do |c| Piece::Knight.new!(@db, self, c + '1', :white) Piece::Knight.new!(@db, self, c + '8', :black) end %w[c f].each do |c| Piece::Bishop.new!(@db, self, c + '1', :white) Piece::Bishop.new!(@db, self, c + '8', :black) end Piece::Queen.new!(@db, self, 'd1', :white) Piece::Queen.new!(@db, self, 'd8', :black) Piece::King.new!(@db, self, 'e1', :white) Piece::King.new!(@db, self, 'e8', :black) end |
#to_html(player = :white) ⇒ Object
Generate HTML output.
Attributes:
player: (Symbol) which view
Returns: (String)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/mail_chess/board.rb', line 182 def to_html player = :white result = '<table border="0" cellpadding="0" cellspacing="0">' result << '<tr height="40"><td align="center" width="40"></td>' cols = (player == :white ? COLS : COLS.reverse) cols.each do |c| result << '<td align="center" width="40"><b>' result << c.capitalize + '</b></td>' end result << '<td align="center" width="40"></td></tr>' rows = (player == :white ? ROWS : ROWS.reverse) start_color = :white rows.each do |r| result << '<tr height="40"><td align="center"><b>' + r + '</b></td>' cell_color = start_color cols.each do |c| result << '<td align="center" bgcolor="' result << (cell_color == :white ? '#ffffff' : '#d0d0d0') result << '">' if @pieces[c + r] result << '<font size="6">' result << @pieces[c + r].html result << '</font>' end result << '</td>' cell_color = (cell_color == :white ? :black : :white) end result << '<td align="center"><b>' + r + '</b></td></tr>' start_color = (start_color == :white ? :black : :white) end result << '<tr height="40"><td align="center" width="40"></td>' cols.each do |c| result << '<td align="center" width="40"><b>' result << c.capitalize + '</b></td>' end result << '<td align="center" width="40"></td></tr>' result << '</table>' return result end |