Fixa så att boden kan bli annan pjäs vid kanten (förlorade på det här)

This commit is contained in:
Love 2022-11-24 14:53:11 +01:00
parent 749024734f
commit e8581663c7
No known key found for this signature in database
GPG Key ID: A3C10DC241C8FA9F
3 changed files with 48 additions and 4 deletions

View File

@ -41,12 +41,12 @@ public class Board extends JPanel implements MouseListener {
private Piece[][] getPieces() throws IOException { private Piece[][] getPieces() throws IOException {
Piece[][] piecesRet = { Piece[][] piecesRet = {
{new Rook(false, new Point(0, 0)), null, null, null, null, null, null, new Rook(true, new Point(0, 7))}, {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 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 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 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 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))} {new Rook(false, new Point(7, 0)), null, null, null, null, null, null, new Rook(true, new Point(7, 7))}
}; };

View File

@ -6,7 +6,7 @@ import java.util.ArrayList;
public class Horse extends Piece { 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); super(isWhite, startingPosition);
} }

View File

@ -1,8 +1,10 @@
package schack; package schack;
import java.awt.HeadlessException;
import java.awt.Point; import java.awt.Point;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Pawn extends Piece { public class Pawn extends Piece {
@ -13,7 +15,7 @@ public class Pawn extends Piece {
/** /**
* Ger tillbaks alla ställen pjäsen kan attackera * Ger tillbaks alla ställen pjäsen kan attackera
* *
* @param pieces * @param pieces
* @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man ska kolla ifall * @param shouldNotCareIfAttackSpaceIsEmptyOrNot Ifall man ska kolla ifall
* det är något i möjliga attackrutor ifall * det är något i möjliga attackrutor ifall
* @return Alla lämpliga attackMoves * @return Alla lämpliga attackMoves
@ -105,4 +107,46 @@ public class Pawn extends Piece {
return true; 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);
}
}
} }