From b442d9227d0716a823cede746fa7158f333ec50c Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 19 Dec 2022 22:36:27 -0500 Subject: [PATCH] Castling Fix --- src/chess/King.java | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/chess/King.java b/src/chess/King.java index eeb1369..b27b140 100644 --- a/src/chess/King.java +++ b/src/chess/King.java @@ -54,11 +54,17 @@ public class King extends ChessPiece { private void castleRight(){ // casting has to move the rook on the right size of the king from white's perspective - if (this.isWhite) - b.movePiece(b.size() - 1, 0, b.size() - 3, 0); - else - b.movePiece(b.size()-1, b.size()-1, b.size()-3, b.size()-1) - b.set(b.size()-3, b.size()-1, b.get(b.size()-1, b.size()-1)); + if (this.isWhite) { + var p = b.get(b.size() - 1, 0); + p.move(new Move(b.size() - 3, 0)); + b.set(b.size() - 3, 0, p); + b.set(b.size() - 1, 0, null); + } else { + var p = b.get(b.size() - 1, b.size() - 1); + p.move(new Move(b.size() - 3, b.size() - 1)); + b.set(b.size() - 3, b.size() - 1, p); + b.set(b.size() - 1, b.size() - 1, null); + } } private boolean checkIfRookValid(ChessPiece piece){ @@ -67,9 +73,16 @@ public class King extends ChessPiece { private void castleLeft(){ // casting has to move the rook on the left size of the king from white's perspective - if (this.isWhite) - b.set(3, 0, b.get(0, 0)); - else - b.set(3, b.size()-1, b.get(0, b.size()-1)); + if (this.isWhite) { + var p = b.get(0, 0); + p.move(new Move(3, 0)); + b.set(3, 0, p); + b.set(0, 0, null); + } else { + var p = b.get(0, b.size() - 1); + p.move(new Move(3, b.size() - 1)); + b.set(3, b.size() - 1, p); + b.set(0, b.size() - 1, null); + } } }