Class: ChessVwong::Board
- Inherits:
-
Object
- Object
- ChessVwong::Board
- Defined in:
- lib/chess_vwong/board.rb,
lib/chess_vwong/preload.rb
Instance Attribute Summary collapse
-
#chosen_piece ⇒ Object
Returns the value of attribute chosen_piece.
-
#get_value ⇒ Object
Returns the value of attribute get_value.
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
-
#node_path ⇒ Object
Returns the value of attribute node_path.
Instance Method Summary collapse
-
#clear_path?(start, dest) ⇒ Boolean
Checks if path is clear [x,y] = grid[x].
- #falling_path(start, dest) ⇒ Object
-
#formatted_grid ⇒ Object
Print’s out Board.
-
#get_piece(input = gets.chomp, player) ⇒ Object
Get piece & generate neighbours, extra helper-methods required for Pawns due to unique nature.
- #horizontal_path(start, dest) ⇒ Object
-
#initialize ⇒ Board
constructor
A new instance of Board.
-
#preload_pieces ⇒ Object
White on the Bottom, Black on Top.
-
#process_input(input) ⇒ Object
Convert letter/number input into grid coordinates, if improper coordinates: empty array is returned.
- #rising_path(start, dest) ⇒ Object
- #scan_for_check ⇒ Object
-
#set_piece(input = gets.chomp, player) ⇒ Object
Sets piece if within neighbour range & valid, also checks if pawn can beome Queen.
-
#valid_path?(start, dest) ⇒ Boolean
Checks if path is valid.
- #vertical_path(start, dest) ⇒ Object
Constructor Details
#initialize ⇒ Board
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_piece ⇒ Object
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_value ⇒ Object
Returns the value of attribute get_value.
4 5 6 |
# File 'lib/chess_vwong/board.rb', line 4 def get_value @get_value end |
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
3 4 5 |
# File 'lib/chess_vwong/board.rb', line 3 def grid @grid end |
#node_path ⇒ Object
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
#clear_path?(start, dest) ⇒ Boolean
Checks if path is clear [x,y] = grid[x]
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/chess_vwong/board.rb', line 75 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 return process_path end |
#falling_path(start, dest) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/chess_vwong/board.rb', line 144 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_grid ⇒ Object
Print’s out Board
51 52 53 54 55 |
# File 'lib/chess_vwong/board.rb', line 51 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() chosen_piece.instance_of?(Pawn) ? pawn_kill_helper(chosen_piece, get_value) : @chosen_piece.generate_neighbours(get_value) return @chosen_piece end end end |
#horizontal_path(start, dest) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/chess_vwong/board.rb', line 106 def horizontal_path(start, dest) @node_path = [] # RIGHT if start[0] < dest[0] (start[0]...dest[0]).each do |i| node_path << grid[start[1]][i] end # LEFT elsif start[0] > dest[0] start[0].step(dest[0]+1, -1) do |i| node_path << grid[start[1]][i] end end node_path end |
#preload_pieces ⇒ Object
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([0,7], "w"), Knight.new([1,7], "w"), Bishop.new([2,7], "w"), Queen.new([3,7], "w"), King.new([4,7], "w"), Bishop.new([5,7], "w"), Knight.new([6,7], "w"), Rook.new([7,7], "w")] black_pieces = [Rook.new([0,0], "b"), Knight.new([1,0], "b"), Bishop.new([2,0], "b"), Queen.new([3,0], "b"), King.new([4,0], "b"), Bishop.new([5,0], "b"), Knight.new([6,0], "b"), Rook.new([7,0], "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
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! {|i|i.to_i} else return [] end end |
#rising_path(start, dest) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/chess_vwong/board.rb', line 122 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 |
#scan_for_check ⇒ Object
70 71 72 |
# File 'lib/chess_vwong/board.rb', line 70 def scan_for_check # execute it for selected piece that was most recently moves AND every king move end |
#set_piece(input = gets.chomp, player) ⇒ Object
Sets piece if within neighbour range & valid, also checks if pawn can beome Queen
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/chess_vwong/board.rb', line 36 def set_piece(input=gets.chomp, player) value = process_input(input) if value.count ==2 chosen_node = grid[value[1]][value[0]] if chosen_piece.neighbours.include?(value) && valid_path?(@get_value, value) chosen_piece.current_space = value chosen_node.occupied << chosen_piece player.kill_list << chosen_node.occupied.shift() if chosen_node.occupied.count > 1 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
60 61 62 63 64 65 66 67 |
# File 'lib/chess_vwong/board.rb', line 60 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/chess_vwong/board.rb', line 90 def vertical_path(start, dest) @node_path = [] # DOWN if start[1] < dest[1] (start[1]...dest[1]).each do |i| node_path << grid[i][start[0]] end # UP elsif start[1] > dest[1] start[1].step(dest[1]+1, -1) do |i| node_path << grid[i][start[0]] end end node_path end |