Module: Chess::Display

Included in:
Game, PlayPawnMoves
Defined in:
lib/chess/display/board.rb,
lib/chess/display/color.rb,
lib/chess/display/prompt.rb

Overview

Console Display

Constant Summary collapse

COLORS =

Colors values for use in escape code.

{
  green: '119;149;86',
  dull_white: '235;236;208',
  white: '255;187;143',
  black: '1;1;1',
  red: '249;23;32'
}.freeze

Instance Method Summary collapse

Instance Method Details

#bg_color(string, name) ⇒ String

background color the string



21
22
23
24
# File 'lib/chess/display/color.rb', line 21

def bg_color(string, name)
  value = COLORS[name]
  "\e[48;2;#{value}m#{string}\e[0m"
end

#color(string, name) ⇒ String

Note:

will not reset the color like #bg_color

color the string



33
34
35
36
# File 'lib/chess/display/color.rb', line 33

def color(string, name)
  value = COLORS[name]
  "\e[38;2;#{value}m#{string}"
end

#display_board(board_data, valid_moves = []) ⇒ void

This method returns an undefined value.

displays board with ASCII characters



11
12
13
14
15
# File 'lib/chess/display/board.rb', line 11

def display_board(board_data, valid_moves = [])
  print_files(board_data)
  print_board_data(board_data, valid_moves)
  print_files(board_data)
end

#display_buttonsObject

display Save & Exit and Exit buttons return [void]



149
150
151
152
153
154
155
# File 'lib/chess/display/board.rb', line 149

def display_buttons
  print '    '
  print bg_color(color('Save & Exit', :green), :black)
  print '   '
  print bg_color(color('Exit', :green), :black)
  puts
end

#king_check_dot(king) ⇒ String

gives dot for king in check if given king is in check



139
140
141
142
143
144
145
# File 'lib/chess/display/board.rb', line 139

def king_check_dot(king)
  if king.in_check
    color("\u{29BE}", :red)
  else
    "\u00A0"
  end
end

#move_dots(square_pos, valid_moves) ⇒ String

gives dot for move for the pos if it’s inside valid_moves



127
128
129
130
131
132
133
# File 'lib/chess/display/board.rb', line 127

def move_dots(square_pos, valid_moves)
  if valid_moves.include?(square_pos)
    color("\u{2022}", :black)
  else
    "\u00A0"
  end
end

This method returns an undefined value.

prints the board data



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chess/display/board.rb', line 22

def print_board_data(board_data, valid_moves)
  shift_rank = 1
  (0..7).reverse_each do |rank|
    print rank + shift_rank
    ('a'..'h').each do |file|
      bg_color_name = square_bg_color_name(file, rank)
      print_square(board_data, file, rank, bg_color_name, valid_moves)
    end
    print rank + shift_rank
    puts
  end
end

This method returns an undefined value.

prints files above the board ‘a’ to ‘h’



39
40
41
42
43
# File 'lib/chess/display/board.rb', line 39

def print_files(board_data)
  print ' '
  board_data.each_key { |key| print " #{key} " }
  puts
end

This method returns an undefined value.

helper method for #print_square



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chess/display/board.rb', line 70

def print_piece_square(piece, bg_color_name, square_pos, valid_moves) # rubocop:disable Metrics/MethodLength
  case piece
  when Pieces::Rook
    print square_string(piece, "\u{265C}", bg_color_name, square_pos, valid_moves)
  when Pieces::Knight
    print square_string(piece, "\u{265E}", bg_color_name, square_pos, valid_moves)
  when Pieces::Bishop
    print square_string(piece, "\u{265D}", bg_color_name, square_pos, valid_moves)
  when Pieces::Queen
    print square_string(piece, "\u{265B}", bg_color_name, square_pos, valid_moves)
  when Pieces::King
    print square_string(piece, "\u{265A}", bg_color_name, square_pos, valid_moves)
  when Pieces::Pawn
    print square_string(piece, "\u{265F}", bg_color_name, square_pos, valid_moves)
  end
end

This method returns an undefined value.

prints a single square



53
54
55
56
57
58
59
60
61
# File 'lib/chess/display/board.rb', line 53

def print_square(board_data, file, rank, bg_color_name, valid_moves)
  piece = board_data[file][rank]
  square_pos = [file, rank]
  if piece == ''
    print square_string(piece, ' ', bg_color_name, square_pos, valid_moves)
  else
    print_piece_square(piece, bg_color_name, square_pos, valid_moves)
  end
end

#prompt_enter_codeString

Note:

method expects user is entering a valid FEN code.

Prompts User to enter a FEN code.



43
44
45
46
47
48
49
50
51
52
# File 'lib/chess/display/prompt.rb', line 43

def prompt_enter_code
  prompt = TTY::Prompt.new
  prompt.ask('Enter a valid self attested FEN code : ') do |question|
    regex = %r{\A[prnbqkPRNBQK0-9/ w\-a-h]+\z} # only checks if valid characters
    # are used like 1-8,kqbnrKQBNR, w for white chance(b=bisop already),
    # a-h(for en passant) and the slash(/).
    question.required true
    question.validate regex, 'Invalid FEN Code.            '
  end
end

#prompt_pawn_promotion_choicesString

Prompts User to choose piece to promote pawn



57
58
59
60
61
62
63
64
65
# File 'lib/chess/display/prompt.rb', line 57

def prompt_pawn_promotion_choices
  prompt = TTY::Prompt.new
  prompt.select('Select the piece for pawn promotion!') do |menu|
    menu.choice name: 'Queen', value: 'q'
    menu.choice name: 'Knight', value: 'n'
    menu.choice name: 'Bishop', value: 'b'
    menu.choice name: 'Rook', value: 'r'
  end
end

#prompt_save_nameString

Prompts User to enter a save name



70
71
72
73
74
75
76
77
# File 'lib/chess/display/prompt.rb', line 70

def prompt_save_name
  prompt = TTY::Prompt.new
  prompt.ask('Enter a valid save name : ') do |question|
    regex = /^[\w.]+$/
    question.required true
    question.validate regex, 'Only a-z , A-Z , 0-9 , _ allowed'
  end
end

#prompt_select_save(save_data) ⇒ String

Promps User to select between saved data.



28
29
30
31
32
33
34
35
36
37
# File 'lib/chess/display/prompt.rb', line 28

def prompt_select_save(save_data)
  names = save_data.keys
  prompt = TTY::Prompt.new
  selected_save_key = prompt.select('Select a save.', per_page: 15, filter: true) do |menu|
    names.each do |name|
      menu.choice name: name, value: name
    end
  end
  save_data[selected_save_key]
end

#prompt_start_choicesInteger

Prompts user to select between New Game /Load Save/Load Code(FEN) and returns the choice.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/chess/display/prompt.rb', line 11

def prompt_start_choices
  prompt = TTY::Prompt.new
  prompt.select('Create new game or load save/code?') do |menu|
    menu.choice name: 'New Game', value: 1
    if File.file?(SAVE_PATH) # if save.json exists
      menu.choice name: 'Load Save', value: 2
    else
      menu.choice name: 'Load Save', disabled: '(No Save Found.)'
    end
    menu.choice name: 'Load using Code (FEN)', value: 3
  end
end

#redraw_display(board_data, valid_moves = []) ⇒ void

This method returns an undefined value.

clears the display and redraws the board with given board_data



162
163
164
165
166
# File 'lib/chess/display/board.rb', line 162

def redraw_display(board_data, valid_moves = [])
  system 'clear'
  display_board(board_data, valid_moves)
  display_buttons
end

#square_bg_color_name(file, rank) ⇒ Symbol

returns background color name for the square at given file and rank.



93
94
95
96
97
98
99
# File 'lib/chess/display/board.rb', line 93

def square_bg_color_name(file, rank)
  if (file.ord.odd? && rank.odd?) || (file.ord.even? && rank.even?)
    :dull_white
  else
    :green
  end
end

#square_string(piece, unicode, bg_color_name, square_pos, valid_moves) ⇒ String

arrange and format strings for the square



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chess/display/board.rb', line 109

def square_string(piece, unicode, bg_color_name, square_pos, valid_moves)
  if piece == ''
    bg_color("#{move_dots(square_pos, valid_moves)}\u{00A0}\u{00A0}", bg_color_name)
  elsif piece.is_a?(Chess::Pieces::King)
    piece_color = piece.color
    bg_color("#{move_dots(square_pos, valid_moves)}#{color(unicode, piece_color.to_sym)}#{
        king_check_dot(piece)}", bg_color_name)
  else
    piece_color = piece.color
    bg_color("#{move_dots(square_pos, valid_moves)}#{color(unicode, piece_color.to_sym)} ", bg_color_name)
  end
end