Module: Printer

Included in:
Board
Defined in:
lib/printer.rb

Constant Summary collapse

@@subrow =

Reference to current row within a cell

0
@@cell_number =

Reference to the current cell

1

Instance Method Summary collapse

Instance Method Details

#clear_allObject



53
54
55
56
57
# File 'lib/printer.rb', line 53

def clear_all
  # Reset counters and clear terminal
  @@cell_number = 1
  system "clear" or system "cls"
end

#piece_to_string(piece_name) ⇒ Object



89
90
91
92
93
94
# File 'lib/printer.rb', line 89

def piece_to_string(piece_name)
  # Print pieces as two characters
  #   "pawn" -> "pa" , "bishop" -> "BI" , "king" -> "KI" , ...
  #   print nil as "  " so it takes up a two character width on the printed board
  piece_name == nil ? "  " : piece_name[0..1]
end


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/printer.rb', line 96

def print_board(piece_locations)
  printer do |tile_text, row_num|

    print_start_of_row(row_num)

    4.times do

      # Print cell and next neighboring cell,
      # then loop until 4 pairs of 2 cells have been printed, completing row
      color      = piece_locations[@@cell_number][:color]     || :black
      next_color = piece_locations[@@cell_number + 1][:color] || :black

      # Print two rows at a time as every two rows repeat
      #  alternating tile colors
      #    ________________________
      #   |   ###   ###   ###   ###| -> subrow 0
      #   |   ###   ###   ###   ###| -> subrow 1
      #   |   ###   ###   ###   ###| -> subrow 2
      #   |###   ###   ###   ###   | -> subrow 3
      #   |###   ###   ###   ###   | -> subrow 4
      #   |###   ###   ###   ###   | -> subrow 5
      #

      if @@subrow < 3
        print substitute_pieces(
          tile_text, @@cell_number, color, :white, piece_locations
        )
        print substitute_pieces(
          tile_text, @@cell_number + 1, next_color, :black, piece_locations
        )
      else
        print substitute_pieces(
          tile_text, @@cell_number, color, :black, piece_locations
        )
        print substitute_pieces(
          tile_text, @@cell_number + 1, next_color, :white, piece_locations
        )
      end

      if tile_text.include? "XX"
        # Incremenet cell_number unless last cell is being printed
        # to avoid an out of range error
        @@cell_number += 2 unless @@cell_number == 63
      end
    end

    print_end_of_row(row_num)

    # Incriment row index.
    # Reset once n reaches 6 (i.e., two complete cell rows have been printed - the pattern to repeat)
    @@subrow += 1
    @@subrow = 0 if @@subrow == 6
    puts
  end
end


46
47
48
49
50
51
# File 'lib/printer.rb', line 46

def print_end_of_row(row_num)
  # Print row number 1...8 at end of each row
  if (@@subrow == 1 || @@subrow == 4) && row_num
    print " #{row_num}"
  end
end


29
30
31
32
33
34
35
# File 'lib/printer.rb', line 29

def print_footer
  # Print chess board footer (Column Labels A to H)
  print "\s\s\s" # cell padding

  COLS.each { |c| print "  #{c}   " }
  puts
end


20
21
22
23
24
25
26
27
# File 'lib/printer.rb', line 20

def print_header
  # Print chess board Header (Title and then Column Labels A to H)
  print "\n\t>> Welcome to Terminal Chess v#{TerminalChess::VERSION}\n\n"
  print "\s\s\s" # cell padding

  COLS.each { |c| print "  #{c}   " }
  puts
end


37
38
39
40
41
42
43
44
# File 'lib/printer.rb', line 37

def print_start_of_row(row_num)
  # Pad the start of each row with spacing or the row numbers 1..8
  if row_num
    print " #{row_num} "
  else
    print " " * 3
  end
end

#printerObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/printer.rb', line 60

def printer
  # Prints the board to terminal, based on layout defined by piece_locations

  clear_all
  print_header

  # Print first cell of each row, with row number
  (1..8).each do |row|
    yield "      "
    yield "  XX  ", "#{row}"
    yield "      "
  end

  print_footer
end

#substitute_pieces(text, index, color, background_color, piece_locations) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/printer.rb', line 76

def substitute_pieces(text, index, color, background_color, piece_locations)
  piece = piece_to_string(piece_locations[index][:type])

  piece.upcase! unless piece == "pa"
  piece = piece.colorize(color)

  if background_color == :white
    text.gsub("XX", piece).on_light_white
  else
    text.gsub("XX", piece).on_light_black
  end
end