From 828cc7ed5be257ca4dcf787f325398fe8a9d963e Mon Sep 17 00:00:00 2001 From: loveb Date: Tue, 3 May 2022 15:46:04 +0200 Subject: [PATCH] Skriv bort try-catch --- src/schack/Pawn.java | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 565f8d3..14ab861 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -84,38 +84,17 @@ public class Pawn extends PieceKnownIfMoved { } @Override - protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) { + protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) { if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) { return false; } - // Instead of checking index and null, try-catch - try { - // Ifall vi kollar utanför brädet kommer detta att faila - Piece p = pieces[pos.x][pos.y]; - // Ifall pjäsen här har samma färg som oss, break - // Ifall det inte är någon pjäs här kommer det att gå ner till - // catch(NullPointerException) och då lägger vi till detta drag i listan - // Ifall det är inte är en pjäs här, kasta ett NullPointerException - // Detta är för att vara så lik super-implementationen som möjligt - if (p == null) { - throw new NullPointerException(); - } else { - // Detta betyder att det finns en pjäs här - // Vi kan ta men inte gå längre. - return true; - } - } catch (NullPointerException npe) { - // This is an empty spot + Piece pieceToCheck = pieces[pos.x][pos.y]; + if (pieceToCheck != null) { + return true; + } else { tryToMoveAndCheckIfCheck(pieces, movable, pos); - } catch (IndexOutOfBoundsException ioobe) { - // This means that the player is at the edge - System.out.println(pos); - } catch (Exception e) { - // For good meassure + return false; } - return false; - } - }