Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor piece model #26

Merged
merged 34 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9fb7998
Removed the piece model from the rubocop.yml file
Learningstuff98 Jul 4, 2023
e9eaf1b
Did some refactoring
Learningstuff98 Jul 4, 2023
62d3295
Refactored horizontal_move? function
Learningstuff98 Jul 6, 2023
832cbc7
Refactored verticle_move? function
Learningstuff98 Jul 6, 2023
c3e3f2d
Refactored diagonal_move? function
Learningstuff98 Jul 6, 2023
c99220d
Refactored king_move? function
Learningstuff98 Jul 6, 2023
aa16d30
Refactored knight_move? function
Learningstuff98 Jul 6, 2023
cda3da2
Did SOME refactoring for the horizontal_or_verticle_path function
Learningstuff98 Jul 7, 2023
49c3977
Did some more refactoring for the horizontal_or_verticle_path function
Learningstuff98 Jul 7, 2023
6c0cdf7
Finished refactoring the horizontal_or_verticle_path function
Learningstuff98 Jul 9, 2023
1ed6082
Finished refactoring the horizontal_or_verticle_path function again
Learningstuff98 Jul 9, 2023
b2735f4
refactored the tests for the diagonal_path function
Learningstuff98 Jul 10, 2023
a35b2d8
Refactored the diagonal_path function
Learningstuff98 Jul 12, 2023
a20444e
Refactored the path_clear? function
Learningstuff98 Jul 13, 2023
45dc7a1
Refactored the friendly_capture? function
Learningstuff98 Jul 13, 2023
f2c3c3e
Refactored the tile_has_piece? function
Learningstuff98 Jul 14, 2023
a68d03a
Refactored the forward_pawn_move? function
Learningstuff98 Jul 16, 2023
fba9bf7
Small refactor
Learningstuff98 Jul 16, 2023
b957a02
Refactored the double_jump? function
Learningstuff98 Jul 16, 2023
f305d63
Refactored the pawn_capturing? function
Learningstuff98 Jul 16, 2023
5099e0b
Refactored the promoted? function
Learningstuff98 Jul 17, 2023
2b266c8
Refactored the on_row? function
Learningstuff98 Jul 17, 2023
9378d20
Added tests for the current_turn? function
Learningstuff98 Jul 17, 2023
b9d98ae
Refactored the current_turn? function
Learningstuff98 Jul 18, 2023
3191977
Added tests for the correct_color? function
Learningstuff98 Jul 18, 2023
1077335
Refactored the correct_color? function
Learningstuff98 Jul 18, 2023
dae4310
Started refactoring the valid_move? function
Learningstuff98 Jul 18, 2023
6470335
Refactored the rooks part of the valid_move? function
Learningstuff98 Jul 18, 2023
0593829
Refactored the bishop part of the valid_move? function
Learningstuff98 Jul 18, 2023
3650329
Refactored the queen part of the valid_move? function
Learningstuff98 Jul 19, 2023
4fa9dfc
Refactored the king part of the valid_move? function
Learningstuff98 Jul 27, 2023
d739856
Refactored the knight part of the valid_move? function
Learningstuff98 Jul 27, 2023
110f88c
Refactored the pawn part of the valid_move? function
Learningstuff98 Jul 27, 2023
f0919db
Small refactor for the valid_move? function
Learningstuff98 Jul 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ AllCops:
- 'spec/factories.rb'
- 'node_modules/**/*'
- 'app/models/user.rb'
- 'app/models/piece.rb'

Metrics/BlockLength:
IgnoredMethods: ['describe', 'context', 'it']
Expand All @@ -28,3 +27,9 @@ Style/FrozenStringLiteralComment:

Documentation:
Enabled: false

Metrics/ClassLength:
Enabled: false

Layout/LineLength:
Enabled: false
301 changes: 128 additions & 173 deletions app/models/piece.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,246 +2,201 @@ class Piece < ApplicationRecord
belongs_to :game

def update_x_and_y
self.update_attribute(:x, self.destination_x)
self.update_attribute(:y, self.destination_y)
update(x: destination_x, y: destination_y)
end

def capture_piece
self.game.pieces.each do |piece|
if self != piece
if piece.x == self.x && piece.y == self.y
piece.update_attribute(:x, 100)
piece.update_attribute(:in_play, false)
end
end
game.pieces.each do |piece|
next if self == piece

piece.update(x: 100, in_play: false) if piece.x == x && piece.y == y
end
end

def horizontal_move?
self.destination_y == self.y && self.destination_x != self.x
destination_y == y && destination_x != x
end

def verticle_move?
self.destination_y != self.y && self.destination_x == self.x
destination_y != y && destination_x == x
end

def diagonal_move?
(self.x - self.destination_x).abs == (self.y - self.destination_y).abs
(x - destination_x).abs == (y - destination_y).abs
end

def king_move?
distance_of_x = (self.destination_x - self.x).abs
distance_of_y = (self.destination_y - self.y).abs
distance_of_x = (destination_x - x).abs
distance_of_y = (destination_y - y).abs
return [0, 1].include?(distance_of_x) if distance_of_y == 1
return distance_of_x == 1 if distance_of_y.zero?

distance_of_x == 1 if distance_of_y.zero?
end

def knight_move?
distance_of_x = (self.destination_x - self.x).abs
distance_of_y = (self.destination_y - self.y).abs
distance_of_x = (destination_x - x).abs
distance_of_y = (destination_y - y).abs
return distance_of_y == 1 if distance_of_x == 2
return distance_of_y == 2 if distance_of_x == 1
end

def get_horizontal_or_verticle_path
coordinates = []
if self.destination_x != self.x
x_value = self.x + 1 if self.destination_x > self.x
x_value = self.x - 1 if self.destination_x < self.x
while x_value != self.destination_x
coordinates.push([x_value, self.y])
x_value += 1 if self.destination_x > self.x
x_value -= 1 if self.destination_x < self.x
end

distance_of_y == 2 if distance_of_x == 1
end

def horizontal_or_verticle_path
if x != destination_x
horizontal_path
else
y_value = self.y + 1 if self.destination_y > self.y
y_value = self.y - 1 if self.destination_y < self.y
while y_value != self.destination_y
coordinates.push([self.x, y_value])
y_value += 1 if self.destination_y > self.y
y_value -= 1 if self.destination_y < self.y
end
verticle_path
end
coordinates
end

def get_diagonal_path
coordinates = []
if self.destination_x > self.x
x_value = self.x + 1
y_value = self.y + 1 if self.destination_y > self.y
y_value = self.y - 1 if self.destination_y < self.y
while x_value < self.destination_x
coordinates.push([x_value, y_value])
x_value += 1
y_value += 1 if self.destination_y > self.y
y_value -= 1 if self.destination_y < self.y
end
end

def range(value, destination_value)
if destination_value > value
((value + 1)..(destination_value - 1)).to_a
else
x_value = self.x - 1
y_value = self.y - 1 if self.destination_y < self.y
y_value = self.y + 1 if self.destination_y > self.y
while x_value > self.destination_x
coordinates.push([x_value, y_value])
x_value -= 1
y_value -= 1 if self.destination_y < self.y
y_value += 1 if self.destination_y > self.y
end
((destination_value + 1)..(value - 1)).to_a
end
end

def horizontal_path
range(x, destination_x).map do |x_value|
[x_value, y]
end
end

def verticle_path
range(y, destination_y).map do |y_value|
[x, y_value]
end
end

def diagonal_path
y_values = diagonal_y_values
range(x, destination_x).map.with_index do |x_value, index|
[x_value, y_values[index]]
end
coordinates
end

def diagonal_y_values
y_values = range(y, destination_y)
if destination_x > x
y_values = y_values.reverse if destination_y < y
elsif destination_y > y
y_values = y_values.reverse
end
y_values
end

def path_clear?(path)
path.each do |coord_pair|
self.game.pieces.each do |piece|
if piece.x == coord_pair[0] && piece.y == coord_pair[1]
return false
end
game.pieces.each do |piece|
return false if piece.x == coord_pair.first && piece.y == coord_pair.last
end
end
true
end

def friendly_capture?
self.game.pieces.each do |piece|
if piece.x == self.destination_x && piece.y == self.destination_y
return piece.color == self.color
end
game.pieces.each do |piece|
return piece.color == color if piece.x == destination_x && piece.y == destination_y
end
false
end

def tile_has_piece?(tile_x, tile_y)
self.game.pieces.each do |piece|
if piece.x == tile_x && piece.y == tile_y
return true
end
game.pieces.each do |piece|
return true if piece.x == tile_x && piece.y == tile_y
end
false
end

def forward_pawn_move?(operation)
if self.x == self.destination_x
if self.destination_y == self.y.send(operation, 1)
!self.tile_has_piece?(self.destination_x, self.destination_y)
end
end
x == destination_x &&
destination_y == y.send(operation, 1) &&
!tile_has_piece?(destination_x, destination_y)
end

def double_jump?(operation, starting_y)
if self.x == self.destination_x
if self.y == starting_y
if self.destination_y == starting_y.send(operation, 2)
!self.tile_has_piece?(self.destination_x, self.destination_y) &&
!self.tile_has_piece?(self.destination_x, self.y.send(operation, 1))
end
end
end
x == destination_x &&
y == starting_y &&
destination_y == starting_y.send(operation, 2) &&
!tile_has_piece?(destination_x, destination_y) &&
!tile_has_piece?(destination_x, y.send(operation, 1))
end

def pawn_capturing?(operation)
if self.destination_y == self.y.send(operation, 1)
if [self.x + 1, self.x - 1].include?(self.destination_x)
self.tile_has_piece?(self.destination_x, self.destination_y)
end
end
destination_y == y.send(operation, 1) &&
[x + 1, x - 1].include?(destination_x) &&
tile_has_piece?(destination_x, destination_y)
end

def promoted?(origional_piece_type)
origional_piece_type != self.piece_type
origional_piece_type != piece_type
end

def on_row?(promotion_row)
self.y == promotion_row
y == promotion_row
end

def current_turn?(current_user)
if self.game.whites_turn
return current_user.username == self.game.as_white
else
current_user.username == self.game.as_black
end
return current_user.username == game.as_white if game.whites_turn

current_user.username == game.as_black
end

def correct_color?(current_user)
if self.color === "white"
return current_user.username == self.game.as_white
return current_user.username == game.as_white if color == "white"

current_user.username == game.as_black
end

def move_horizontaly_or_vertically
return unless horizontal_move? || verticle_move?

update_x_and_y && game.invert_turn if path_clear?(horizontal_or_verticle_path)
end

def move_diagonally
return unless diagonal_move?

update_x_and_y && game.invert_turn if path_clear?(diagonal_path)
end

def move_queen
move_diagonally || move_horizontaly_or_vertically
end

def move_king
update_x_and_y && game.invert_turn if king_move?
end

def move_knight
update_x_and_y && game.invert_turn if knight_move?
end

def handle_pawn_movement
if color == "white"
move_pawn(:+, 8, 2)
else
current_user.username == self.game.as_black
move_pawn(:-, 1, 7)
end
end

def valid_move?(current_user)
if self.correct_color?(current_user)
if !self.friendly_capture? && self.current_turn?(current_user)
if self.piece_type == "rook"
if self.horizontal_move? || self.verticle_move?
if self.path_clear?(self.get_horizontal_or_verticle_path)
self.update_x_and_y
self.game.invert_turn
end
end
end
if self.piece_type == "bishop"
if self.diagonal_move?
if self.path_clear?(self.get_diagonal_path)
self.update_x_and_y
self.game.invert_turn
end
end
end
if self.piece_type == "queen"
if self.diagonal_move?
if self.path_clear?(self.get_diagonal_path)
self.update_x_and_y
self.game.invert_turn
end
end
if self.horizontal_move? || self.verticle_move?
if self.path_clear?(self.get_horizontal_or_verticle_path)
self.update_x_and_y
self.game.invert_turn
end
end
end
if self.piece_type == "king"
if self.king_move?
self.update_x_and_y
self.game.invert_turn
end
end
if self.piece_type == "knight"
if self.knight_move?
self.update_x_and_y
self.game.invert_turn
end
end
if self.piece_type == "pawn"
if self.color == "white"
if self.forward_pawn_move?(:+) || self.pawn_capturing?(:+)
self.update_x_and_y
if !self.on_row?(8)
self.game.invert_turn
end
end
if self.double_jump?(:+, 2)
self.update_x_and_y
self.game.invert_turn
end
else
if self.forward_pawn_move?(:-) || self.pawn_capturing?(:-)
self.update_x_and_y
if !self.on_row?(1)
self.game.invert_turn
end
end
if self.double_jump?(:-, 7)
self.update_x_and_y
self.game.invert_turn
end
end
end
end
def move_pawn(operation, promotion_row, starting_row)
if forward_pawn_move?(operation) || pawn_capturing?(operation)
update_x_and_y
game.invert_turn unless on_row?(promotion_row)
end
update_x_and_y && game.invert_turn if double_jump?(operation, starting_row)
end

def valid_move?(current_user)
return unless correct_color?(current_user) && !friendly_capture? && current_turn?(current_user)

move_horizontaly_or_vertically if piece_type == "rook"
move_diagonally if piece_type == "bishop"
move_queen if piece_type == "queen"
move_king if piece_type == "king"
move_knight if piece_type == "knight"
handle_pawn_movement if piece_type == "pawn"
end
end
Loading