Class: Display
- Inherits:
-
Object
- Object
- Display
- Defined in:
- lib/fen/display.rb
Instance Attribute Summary collapse
-
#fen ⇒ Object
Returns the value of attribute fen.
-
#piece_array ⇒ Object
readonly
Returns the value of attribute piece_array.
Instance Method Summary collapse
- #board ⇒ Object
- #bottom_row ⇒ Object
- #convert_fen_to_piece_array ⇒ Object
- #file_notation_row ⇒ Object
-
#initialize(fen) ⇒ Display
constructor
A new instance of Display.
- #line_row ⇒ Object
- #piece_row(rank_notation) ⇒ Object
- #top_row ⇒ Object
- #unicode_board(key) ⇒ Object
- #unicode_piece(piece) ⇒ Object
Constructor Details
#initialize(fen) ⇒ Display
Returns a new instance of Display.
7 8 9 10 |
# File 'lib/fen/display.rb', line 7 def initialize(fen) @fen = fen @piece_array = convert_fen_to_piece_array end |
Instance Attribute Details
#fen ⇒ Object
Returns the value of attribute fen.
4 5 6 |
# File 'lib/fen/display.rb', line 4 def fen @fen end |
#piece_array ⇒ Object (readonly)
Returns the value of attribute piece_array.
5 6 7 |
# File 'lib/fen/display.rb', line 5 def piece_array @piece_array end |
Instance Method Details
#board ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fen/display.rb', line 24 def board board = file_notation_row board += top_row ChessConstants::RANK_ARRAY.reverse.each do |rank| board += piece_row(rank.to_i) board += line_row unless rank.to_i == 1 end board += bottom_row board += file_notation_row end |
#bottom_row ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fen/display.rb', line 109 def bottom_row counter = 0 bottom_row = " " bottom_row += unicode_board(:bottom_left_corner) bottom_row += unicode_board(:horizontal) bottom_row += unicode_board(:horizontal) loop do bottom_row += unicode_board(:bottom_mid) bottom_row += unicode_board(:horizontal) bottom_row += unicode_board(:horizontal) counter += 1 break if counter == ChessConstants::NUMBER_OF_COLUMNS - 1 end bottom_row += unicode_board(:bottom_right_corner) bottom_row += "\n" end |
#convert_fen_to_piece_array ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/fen/display.rb', line 12 def convert_fen_to_piece_array fen_array = @fen.split("/") first_rank_and_details = fen_array.last.split(" ") first_rank = first_rank_and_details[0] piece_array = fen_array piece_array[-1] = first_rank @piece_array = piece_array end |
#file_notation_row ⇒ Object
37 38 39 40 41 |
# File 'lib/fen/display.rb', line 37 def file_notation_row file_notation_row = " " ChessConstants::FILE_ARRAY.each { |value| file_notation_row += "#{value} "} file_notation_row += "\n" end |
#line_row ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/fen/display.rb', line 90 def line_row counter = 0 line_row = " " line_row += unicode_board(:left_mid) line_row += unicode_board(:horizontal) line_row += unicode_board(:horizontal) loop do line_row += unicode_board(:cross) line_row += unicode_board(:horizontal) line_row += unicode_board(:horizontal) counter += 1 break if counter == ChessConstants::NUMBER_OF_COLUMNS - 1 end line_row += unicode_board(:right_mid) line_row += "\n" end |
#piece_row(rank_notation) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/fen/display.rb', line 61 def piece_row(rank_notation) piece_row = rank_notation.to_s piece_row += " " @piece_array.reverse[rank_notation - 1].each_char do |board_square| piece_row += unicode_board(:vertical) if board_square.to_i > 1 (board_square.to_i - 1).times do piece_row += " " piece_row += unicode_board(:vertical) end piece_row += " " elsif board_square.to_i == 1 piece_row += " " else piece_row += unicode_piece(board_square.to_sym) end piece_row += " " end piece_row += unicode_board(:vertical) piece_row += " " piece_row += rank_notation.to_s piece_row += "\n" end |
#top_row ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fen/display.rb', line 43 def top_row counter = 0 top_row = " " top_row += unicode_board(:top_left_corner) top_row += unicode_board(:horizontal) top_row += unicode_board(:horizontal) loop do top_row += unicode_board(:top_mid) top_row += unicode_board(:horizontal) top_row += unicode_board(:horizontal) counter += 1 break if counter == ChessConstants::NUMBER_OF_COLUMNS - 1 end top_row += unicode_board(:top_right_corner) top_row += "\n" end |
#unicode_board(key) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/fen/display.rb', line 129 def unicode_board(key) hash = {horizontal: "\u2500", vertical: "\u2502", top_left_corner: "\u250C", top_right_corner: "\u2510", bottom_left_corner: "\u2514", bottom_right_corner: "\u2518", top_mid: "\u252c", left_mid: "\u251c", right_mid: "\u2524", bottom_mid: "\u2534", cross: "\u253c"} return hash.fetch(key).encode('utf-8') end |
#unicode_piece(piece) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/fen/display.rb', line 145 def unicode_piece(piece) hash = {K: "\u2654", Q: "\u2655", R: "\u2656", B: "\u2657", N: "\u2658", P: "\u2659", k: "\u265A", q: "\u265B", r: "\u265C", b: "\u265D", n: "\u265E", p: "\u265F"} return hash.fetch(piece).encode('utf-8') end |