Last active
May 22, 2017 09:29
-
-
Save hyjk2000/37045e948f1e19515794ef25cf62f218 to your computer and use it in GitHub Desktop.
Variable Sized ASCII Chess Board
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChessBoard | |
attr_accessor :rows, :columns, :square_size | |
def initialize(rows, columns, square_size) | |
@rows = rows | |
@columns = columns | |
@square_size = square_size | |
end | |
def render | |
puts border_top, board, border_bottom | |
end | |
private | |
def border_top | |
"⌈#{'-' * columns * square_size}⌉" | |
end | |
def border_bottom | |
"⌊#{'-' * columns * square_size}⌋" | |
end | |
def board | |
(1..rows).map do |row_num| | |
row(row_num) | |
end.join | |
end | |
def row(row_num) | |
content = (1..columns).map do |col_num| | |
cell(row_num, col_num) | |
end.join | |
"|#{content}|\n" * square_size | |
end | |
def cell(row_num, col_num) | |
fill = same_parity?([row_num, col_num]) ? '#' : ' ' | |
fill * square_size | |
end | |
def same_parity?(numbers) | |
numbers.all?(&:odd?) || numbers.all?(&:even?) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require './chess_board' | |
describe ChessBoard do | |
subject { described_class.new(8, 8, 3).render } | |
expected_output = <<~OUTPUT | |
⌈------------------------⌉ | |
|### ### ### ### | | |
|### ### ### ### | | |
|### ### ### ### | | |
| ### ### ### ###| | |
| ### ### ### ###| | |
| ### ### ### ###| | |
|### ### ### ### | | |
|### ### ### ### | | |
|### ### ### ### | | |
| ### ### ### ###| | |
| ### ### ### ###| | |
| ### ### ### ###| | |
|### ### ### ### | | |
|### ### ### ### | | |
|### ### ### ### | | |
| ### ### ### ###| | |
| ### ### ### ###| | |
| ### ### ### ###| | |
|### ### ### ### | | |
|### ### ### ### | | |
|### ### ### ### | | |
| ### ### ### ###| | |
| ### ### ### ###| | |
| ### ### ### ###| | |
⌊------------------------⌋ | |
OUTPUT | |
it 'outputs the expected output' do | |
expect { subject }.to output(expected_output).to_stdout | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment