Class: ChessVwong::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/chess_vwong/board.rb,
lib/chess_vwong/preload.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBoard

Returns a new instance of Board.



6
7
8
# File 'lib/chess_vwong/board.rb', line 6

def initialize
  @grid = default_grid
end

Instance Attribute Details

#chosen_pieceObject

Returns the value of attribute chosen_piece.



4
5
6
# File 'lib/chess_vwong/board.rb', line 4

def chosen_piece
  @chosen_piece
end

#get_valueObject

Returns the value of attribute get_value.



4
5
6
# File 'lib/chess_vwong/board.rb', line 4

def get_value
  @get_value
end

#gridObject (readonly)

Returns the value of attribute grid.



3
4
5
# File 'lib/chess_vwong/board.rb', line 3

def grid
  @grid
end

#node_pathObject

Returns the value of attribute node_path.



4
5
6
# File 'lib/chess_vwong/board.rb', line 4

def node_path
  @node_path
end

Instance Method Details

#apply_castling(rook) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/chess_vwong/board.rb', line 147

def apply_castling(rook)
  king = grid[rook.current_space[1]][5].occupied.pop
  if king.turns == 0 && rook.turns == 0
    if rook.current_space[0] == 1 # Long Castle
      switch_rook_castle(rook, king, 4, 3)
    elsif rook.current_space[0] == 8 # Short Castle
      switch_rook_castle(rook, king, 6, 7)
    end
  end
end

#clear_path?(start, dest) ⇒ Boolean

Checks if path is clear. [x,y] = grid[x]

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chess_vwong/board.rb', line 66

def clear_path?(start, dest)
  if (start[0] < dest[0] && start[1] > dest[1]) || (start[0] > dest[0] && start[1] < dest[1])
    rising_path(start, dest)
  elsif (start[0] < dest[0] && start[1] < dest[1]) || (start[0] > dest[0] && start[1] > dest[1])
    falling_path(start, dest)
  elsif start[1] != dest[1]
    vertical_path(start, dest)
  elsif start[0] != dest[0]
    horizontal_path(start, dest)
  end
  process_path
end

#falling_path(start, dest) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chess_vwong/board.rb', line 125

def falling_path(start, dest)
  @node_path = []
  i = 0
  j = start[0]
  # DOWN_RIGHT
  if start[0] < dest[0] && start[1] < dest[1]
    while j < dest[0]
      node_path << grid[start[1] + i][start[0] + i]
      i += 1
      j += 1
    end
  # UP_LEFT
  elsif start[0] > dest[0] && start[1] > dest[1]
    while j > dest[0]
      node_path << grid[start[1] - i][start[0] - i]
      i += 1
      j -= 1
    end
  end
  node_path
end

#formatted_gridObject

Print’s out Board



159
160
161
162
163
# File 'lib/chess_vwong/board.rb', line 159

def formatted_grid
  grid.each do |row|
    puts row.map { |node| node.occupied.empty? ? '_' : node.occupied.first.character }.join(' ')
  end
end

#get_piece(input = gets.chomp, player) ⇒ Object

Get piece & generate neighbours, extra helper-methods required for Pawns due to unique nature



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

def get_piece(input = gets.chomp, player)
  @get_value = process_input(input)
  if get_value.count == 2
    chosen_node = grid[get_value[1]][get_value[0]]
    unless chosen_node.occupied.empty? || chosen_node.occupied.first.color != player.color
      @chosen_piece = chosen_node.occupied.pop
      pawn_moves_helper(chosen_piece) #if pawn selected, check if unique moves apply
      return chosen_piece
    end
  end
end

#horizontal_path(start, dest) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chess_vwong/board.rb', line 91

def horizontal_path(start, dest)
  @node_path = []
  # RIGHT
  if start[0] < dest[0]
    (start[0]...dest[0]).each {|i| node_path << grid[start[1]][i] }
  # LEFT
  elsif start[0] > dest[0]
    start[0].step(dest[0] + 1, -1) {|i| node_path << grid[start[1]][i] }
  end
  node_path
end

#preload_piecesObject

private White on the Bottom, Black on Top



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chess_vwong/preload.rb', line 5

def preload_pieces
  white_pieces =  [Rook.new([1, 8], 'w'), Knight.new([2, 8], 'w'), Bishop.new([3, 8], 'w'), Queen.new([4, 8], 'w'),
                   King.new([5, 8], 'w'), Bishop.new([6, 8], 'w'), Knight.new([7, 8], 'w'), Rook.new([8, 8], 'w')]

  black_pieces =  [Rook.new([1, 1], 'b'), Knight.new([2, 1], 'b'), Bishop.new([3, 1], 'b'), Queen.new([4, 1], 'b'),
                   King.new([5, 1], 'b'), Bishop.new([6, 1], 'b'), Knight.new([7, 1], 'b'), Rook.new([8, 1], 'b')]

  # Load Coord-Displays
  load_alphabet(grid[0])
  load_num_coord

  


  # Black Pawns
  load_pawns(grid[2], 'b')
  # # White Pawns
  load_pawns(grid[7], 'w')
  # # White Pieces
  load_back_pieces(grid[1], black_pieces)
  # # Black Pieces
  load_back_pieces(grid[8], white_pieces)
end

#process_input(input) ⇒ Object

Convert letter/number input into grid coordinates, if improper coordinates: empty array is returned (bandaid fix) so loop continues



11
12
13
14
15
16
17
18
19
20
# File 'lib/chess_vwong/board.rb', line 11

def process_input(input)
  if input[0] =~ /[A-Za-z]/
    grid_values = input.tr!('87654321', '12345678')
    grid_values = input.upcase.tr!('ABCDEFGH', '12345678')
    grid_values = grid_values.split('')
    grid_values.map!(&:to_i)
  else
    return []
  end
end

#rising_path(start, dest) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chess_vwong/board.rb', line 103

def rising_path(start, dest)
  @node_path = []
  i = 0
  j = start[0]
  # UP_RIGHT
  if start[0] < dest[0] && start[1] > dest[1]
    while j < dest[0]
      node_path << grid[start[1] - i][start[0] + i]
      i += 1
      j += 1
    end
  # DOWN_LEFT
  elsif start[0] > dest[0] && start[1] < dest[1]
    while j > dest[0]
      node_path << grid[start[1] + i][start[0] - i]
      i += 1
      j -= 1
    end
  end
  node_path
end

#set_piece(input = gets.chomp, player) ⇒ Object

Sets piece if within neighbour range & valid, also checks for unique moves



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chess_vwong/board.rb', line 38

def set_piece(input = gets.chomp, player)
  value = process_input(input)
  if value.count == 2
    chosen_node = grid[value[1]][value[0]]
    #Castling
    return apply_castling(chosen_piece) if chosen_piece.instance_of?(Rook) && chosen_node.occupied.first.instance_of?(King) && clear_path?(get_value, value)
    #En Passant  
    return en_pass_kill(chosen_piece, player, chosen_node) if chosen_piece.instance_of?(Pawn) && chosen_node.occupied.empty? && !chosen_piece.ep_kill.empty?
    #Standard & Promotion
    if chosen_piece.neighbours.include?(value) && valid_path?(@get_value, value)
      standard_move(chosen_piece, chosen_node, value, player)
      pawn_to_queen(chosen_piece, chosen_node) if chosen_piece.instance_of?(Pawn)
      return chosen_node.occupied
    end
  end
end

#valid_path?(start, dest) ⇒ Boolean

Checks if path is valid

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
# File 'lib/chess_vwong/board.rb', line 56

def valid_path?(start, dest)
  # if End node is empty OR end_node is occupied with enemy_piece then check if path is clear
  if grid[dest[1]][dest[0]].occupied.empty? || !grid[dest[1]][dest[0]].occupied.empty? && grid[dest[1]][dest[0]].occupied.first.color != chosen_piece.color
    chosen_piece.instance_of?(Knight) ? true : clear_path?(start, dest)
  else
    false
  end
end

#vertical_path(start, dest) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chess_vwong/board.rb', line 79

def vertical_path(start, dest)
  @node_path = []
  # DOWN
  if start[1] < dest[1]
    (start[1]...dest[1]).each {|i| node_path << grid[i][start[0]] }
  # UP
  elsif start[1] > dest[1]
    start[1].step(dest[1] + 1, -1) {|i| node_path << grid[i][start[0]] }
  end
  node_path
end