Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.92 KB | None | 0 0
  1. #lib/board.rb
  2.  
  3. class Board
  4.   LINES = 8
  5.   attr_accessor :board
  6.   attr_reader :black_bg, :white_bg, :end_color
  7.  
  8.   def initialize
  9.     @board = generate_board
  10.     @black_bg = "\e[0;39;100m"
  11.     @white_bg = "\e[0;39;107m"
  12.     @end_color = "\e[0m"
  13.   end
  14.  
  15.   def generate_board
  16.     grid = []
  17.     LINES.times do
  18.       row = []
  19.       LINES.times do
  20.         row.push("♟")
  21.       end
  22.       grid.push(row)
  23.     end
  24.     return grid
  25.   end
  26.  
  27.   def display_board
  28.     (0..board.length-1).each do |row|
  29.       line = ""
  30.       check = row % 2 == 0 ? 0 : 1
  31.       (0..board[row].length-1).each do |square|
  32.         if square % 2 == (0+check)
  33.           formatted_square = white_bg + board[row][square] + end_color
  34.         else
  35.           formatted_square = black_bg + board[row][square] + end_color
  36.         end
  37.         line.concat(formatted_square)
  38.       end
  39.       puts line
  40.     end
  41.   end
  42.  
  43. end
  44.  
  45. b = Board.new
  46. b.display_board
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement