Skip to content

Commit 34a9b88

Browse files
committed
Fix bug with attack_moves output
1 parent 03df243 commit 34a9b88

9 files changed

Lines changed: 12 additions & 9 deletions

File tree

lib/ai_computer_player.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def choose_a_position_to_move(my_piece)
3131
else
3232
position = my_piece.attack_moves.sample
3333
end
34-
position
34+
35+
convert_inverse(position)
3536
end
3637

3738
def convert_inverse(input)

lib/pieces/bishop.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def find_all_attacks_diagonally(the_chessboard)
4444
bottom_left = find_each_attack_diagonally(bottom_left_algorithm, the_chessboard)
4545
all_attacks.concat(bottom_left)
4646

47-
all_attacks.select { |attack| attack.length > 1 }
47+
all_attacks.select { |attack| attack[0] >= 0 and attack[0] <= 7 and attack[1] >= 0 and attack[1] <= 7}
4848

4949
end
5050

lib/pieces/findable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def find_allowed_moves_for_knights(the_chessboard)
198198
if the_chessboard.data.dig(position[0], position[1])&.color == @color
199199
next
200200
elsif the_chessboard.data.dig(position[0], position[1])&.color.is_a?(String) && the_chessboard.data.dig(position[0], position[1])&.color != @color
201-
@attack_moves << position
201+
202202
else
203203
allowed_moves << position
204204
end

lib/pieces/king.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ def find_all_attack_moves_for_the_king(the_chessboard = @the_chessboard)
4444
all_attacks << position
4545
end
4646
end
47-
all_attacks
47+
all_attacks.select { |attack| attack[0] >= 0 and attack[0] <= 7 and attack[1] >= 0 and attack[1] <= 7}
4848
end
4949
end

lib/pieces/knight.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def find_all_attack_moves_for_knights(the_chessboard)
3838
all_attacks << position
3939
end
4040
end
41-
all_attacks
41+
all_attacks.select { |attack| attack[0] >= 0 and attack[0] <= 7 and attack[1] >= 0 and attack[1] <= 7}
4242
end
4343

4444
end

lib/pieces/pawn.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ def attack_moves(the_chessboard = @the_chessboard)
2424
attack_moves = possible_attack.select do |position|
2525
the_chessboard.data.dig(position[0], position[1])&.color.is_a?(String) && the_chessboard.data.dig(position[0], position[1])&.color != @color
2626
end
27-
return attack_moves
27+
return attack_moves.select { |attack| attack[0] >= 0 and attack[0] <= 7 and attack[1] >= 0 and attack[1] <= 7}
2828
else
2929
possible_attack = [[x + 1, y - 1], [x + 1, y + 1]]
3030
attack_moves = possible_attack.select do |position|
3131
the_chessboard.data.dig(position[0], position[1])&.color.is_a?(String) && the_chessboard.data.dig(position[0], position[1])&.color != @color
3232
end
33-
return attack_moves
33+
return attack_moves.select { |attack| attack[0] >= 0 and attack[0] <= 7 and attack[1] >= 0 and attack[1] <= 7}
3434
end
3535
end
3636

lib/pieces/queen.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def find_all_attacks_diagonally(the_chessboard)
104104
bottom_left = find_each_attack_diagonally(bottom_left_algorithm, the_chessboard)
105105
all_attacks.concat(bottom_left)
106106

107-
all_attacks.select { |attack| attack.length > 1 }
107+
all_attacks.select { |attack| attack[0] >= 0 and attack[0] <= 7 and attack[1] >= 0 and attack[1] <= 7}
108108

109109
end
110110

lib/pieces/rook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ def find_all_attacks_perpendicularly(the_chessboard)
7171
break
7272
end
7373
end
74-
attack_moves
74+
attack_moves.select { |attack| attack[0] >= 0 and attack[0] <= 7 and attack[1] >= 0 and attack[1] <= 7}
7575
end
7676
end

main.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

0 commit comments

Comments
 (0)