From 17b506922a71f337d0558c955800ffd3f88ba003 Mon Sep 17 00:00:00 2001 From: JoffreTS Date: Mon, 13 Mar 2023 17:28:10 -0700 Subject: [PATCH] updating movement updating movement --- src/Queen.java | 20 ++++++++++---------- src/Rook.java | 13 +++++++------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Queen.java b/src/Queen.java index 0e43fd3..c641fa6 100644 --- a/src/Queen.java +++ b/src/Queen.java @@ -7,16 +7,16 @@ public class Queen extends Piece { public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); - for(int i = 1; i <= 7; i++) { - possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i)); - possibleMoves.add(new BoardCoordinate(position.x - i, position.y + i)); - possibleMoves.add(new BoardCoordinate(position.x + i, position.y - i)); - possibleMoves.add(new BoardCoordinate(position.x + i, position.y + i)); - possibleMoves.add(new BoardCoordinate(position.x - i, position.y)); - possibleMoves.add(new BoardCoordinate(position.x + i, position.y)); - possibleMoves.add(new BoardCoordinate(position.x, position.y - i)); - possibleMoves.add(new BoardCoordinate(position.x, position.y + i)); - } + + getPossibleMovesInDirection(0, 1, position, board, possibleMoves); + getPossibleMovesInDirection(0, -1, position, board, possibleMoves); + getPossibleMovesInDirection(1, 1, position, board, possibleMoves); + getPossibleMovesInDirection(1, 0, position, board, possibleMoves); + getPossibleMovesInDirection(1, -1, position, board, possibleMoves); + getPossibleMovesInDirection(-1, 1, position, board, possibleMoves); + getPossibleMovesInDirection(-1, 0, position, board, possibleMoves); + getPossibleMovesInDirection(-1, -1, position, board, possibleMoves); + return possibleMoves; } } diff --git a/src/Rook.java b/src/Rook.java index 0325bd9..5769e6d 100644 --- a/src/Rook.java +++ b/src/Rook.java @@ -7,12 +7,13 @@ public class Rook extends Piece { public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { ArrayList possibleMoves = new ArrayList<>(); - for(int i = 1; i <= 7; i++) { - possibleMoves.add(new BoardCoordinate(position.x - i, position.y)); - possibleMoves.add(new BoardCoordinate(position.x + i, position.y)); - possibleMoves.add(new BoardCoordinate(position.x, position.y - i)); - possibleMoves.add(new BoardCoordinate(position.x, position.y + i)); - } + + getPossibleMovesInDirection(0, 1, position, board, possibleMoves); + getPossibleMovesInDirection(0, -1, position, board, possibleMoves); + getPossibleMovesInDirection(1, 0, position, board, possibleMoves); + getPossibleMovesInDirection(-1, 0, position, board, possibleMoves); + + return possibleMoves; } } \ No newline at end of file