From e8581663c7cae44c67a52e1f1dfc7b0ff105973e Mon Sep 17 00:00:00 2001 From: lov3b Date: Thu, 24 Nov 2022 14:53:11 +0100 Subject: [PATCH] =?UTF-8?q?Fixa=20s=C3=A5=20att=20boden=20kan=20bli=20anna?= =?UTF-8?q?n=20pj=C3=A4s=20vid=20kanten=20(f=C3=B6rlorade=20p=C3=A5=20det?= =?UTF-8?q?=20h=C3=A4r)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schack/Board.java | 4 ++-- src/schack/Horse.java | 2 +- src/schack/Pawn.java | 46 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/schack/Board.java b/src/schack/Board.java index 5cf3ac2..0b57165 100644 --- a/src/schack/Board.java +++ b/src/schack/Board.java @@ -41,12 +41,12 @@ public class Board extends JPanel implements MouseListener { private Piece[][] getPieces() throws IOException { Piece[][] piecesRet = { {new Rook(false, new Point(0, 0)), null, null, null, null, null, null, new Rook(true, new Point(0, 7))}, - {new Horse(false, true, new Point(1, 0)), null, null, null, null, null, null, new Horse(true, true, new Point(1, 7))}, + {new Horse(false, new Point(1, 0)), null, null, null, null, null, null, new Horse(true, new Point(1, 7))}, {new Bishop(false, new Point(2, 0)), null, null, null, null, null, null, new Bishop(true, new Point(2, 7))}, {new Queen(false, new Point(3, 0)), null, null, null, null, null, null, new Queen(true, new Point(3, 7))}, {new King(false, new Point(4, 0)), null, null, null, null, null, null, new King(true, new Point(4, 7))}, {new Bishop(false, new Point(5, 0)), null, null, null, null, null, null, new Bishop(true, new Point(5, 7))}, - {new Horse(false, true, new Point(6, 0)), null, null, null, null, null, null, new Horse(true, true, new Point(6, 7))}, + {new Horse(false, new Point(6, 0)), null, null, null, null, null, null, new Horse(true, new Point(6, 7))}, {new Rook(false, new Point(7, 0)), null, null, null, null, null, null, new Rook(true, new Point(7, 7))} }; diff --git a/src/schack/Horse.java b/src/schack/Horse.java index a5f8b28..ca9996f 100644 --- a/src/schack/Horse.java +++ b/src/schack/Horse.java @@ -6,7 +6,7 @@ import java.util.ArrayList; public class Horse extends Piece { - public Horse(boolean isWhite, boolean isLeft, Point startingPosition) throws IOException { + public Horse(boolean isWhite, Point startingPosition) throws IOException { super(isWhite, startingPosition); } diff --git a/src/schack/Pawn.java b/src/schack/Pawn.java index 7d28249..dea912c 100644 --- a/src/schack/Pawn.java +++ b/src/schack/Pawn.java @@ -1,8 +1,10 @@ package schack; +import java.awt.HeadlessException; import java.awt.Point; import java.io.IOException; import java.util.ArrayList; +import javax.swing.JOptionPane; public class Pawn extends Piece { @@ -13,7 +15,7 @@ public class Pawn extends Piece { /** * Ger tillbaks alla ställen pjäsen kan attackera * - * @param pieces + * @param pieces * @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man ska kolla ifall * det är något i möjliga attackrutor ifall * @return Alla lämpliga attackMoves @@ -105,4 +107,46 @@ public class Pawn extends Piece { return true; } + + @Override + public void move(Piece[][] pieces, Point toMove) { + super.move(pieces, toMove); + + // Check if the pawn has moved to the end and should be transformed + if (this.position.y == 0 && this.isWhite() + || this.position.y == 7 && !this.isWhite()) { + transform(pieces); + } + } + + private void transform(Piece[][] pieces) throws HeadlessException { + String[] transformations = {"Queen", "Rook", "Bishop", "Horse"}; + int choosenTransformations = JOptionPane.showOptionDialog(null, + "What do you want to transform into?", + "Click a button", + JOptionPane.DEFAULT_OPTION, + JOptionPane.INFORMATION_MESSAGE, + null, + transformations, + transformations[0] + ); + try { + switch (choosenTransformations) { + case 0: + pieces[position.x][position.y] = new Queen(this.isWhite(), this.position); + break; + case 1: + pieces[position.x][position.y] = new Rook(this.isWhite(), this.position); + break; + case 2: + pieces[position.x][position.y] = new Bishop(this.isWhite(), this.position); + break; + default: + pieces[position.x][position.y] = new Horse(this.isWhite(), this.position); + break; + } + } catch (IOException ioe) { + System.out.println(ioe); + } + } }