Merge branch 'finals'

This commit is contained in:
lov3b 2022-05-15 19:14:58 +02:00
commit 97f13d3572
7 changed files with 51 additions and 54 deletions

View File

@ -103,22 +103,22 @@ public class Board extends JPanel implements MouseListener {
@Override @Override
public void mousePressed(MouseEvent mouseEvent) { public void mousePressed(MouseEvent mouseEvent) {
int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE); final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE); final int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
Point clicked = new Point(mouseCoordinateX, mouseCoordinateY); final Point clickedCoordinate = new Point(mouseCoordinateX, mouseCoordinateY);
// Ifall vi har tryckt en pjäs och sedan ska dit // Ifall vi har tryckt en pjäs och sedan ska dit
if (validMovesToDraw.contains(clicked)) { if (validMovesToDraw.contains(clickedCoordinate)) {
final Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y]; final Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
if (selectedPiece == null) { if (selectedPiece == null) {
validMovesToDraw.clear(); validMovesToDraw.clear();
return; return;
} }
selectedPiece.move(pieces, clicked); selectedPiece.move(pieces, clickedCoordinate);
turnCount++; turnCount++;
whitesTurn = !whitesTurn; whitesTurn = !whitesTurn;
ArrayList<Point> allValidMoves = new ArrayList<>(); final ArrayList<Point> allValidMoves = new ArrayList<>();
for (Piece[] pieceArr : pieces) { for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) { for (Piece piece : pieceArr) {
if (piece == null || whitesTurn != piece.isWhite()) { if (piece == null || whitesTurn != piece.isWhite()) {
@ -132,7 +132,7 @@ public class Board extends JPanel implements MouseListener {
ArrayList<Point> opposingAttacks = checkAttacks(!whitesTurn); ArrayList<Point> opposingAttacks = checkAttacks(!whitesTurn);
boolean weCanMove = !allValidMoves.isEmpty(); final boolean weCanMove = !allValidMoves.isEmpty();
boolean inSchack = false; boolean inSchack = false;
for (Point attack : opposingAttacks) { for (Point attack : opposingAttacks) {
@ -147,7 +147,7 @@ public class Board extends JPanel implements MouseListener {
if (weCanMove) { if (weCanMove) {
JOptionPane.showMessageDialog(this, "Du står i schack"); JOptionPane.showMessageDialog(this, "Du står i schack");
} else { } else {
int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?"); final int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?");
if (choise == JOptionPane.YES_OPTION) { if (choise == JOptionPane.YES_OPTION) {
try { try {
restartGame(); restartGame();
@ -164,12 +164,12 @@ public class Board extends JPanel implements MouseListener {
} }
} else { } else {
previouslyClickedPoint = new Point(clicked); previouslyClickedPoint = new Point(clickedCoordinate);
validMovesToDraw.clear(); validMovesToDraw.clear();
} }
// Om vi inte redan har valt en pjäs klickar vi en pjäs // Om vi inte redan har valt en pjäs klickar vi en pjäs
if (!validMovesToDraw.contains(clicked)) { if (!validMovesToDraw.contains(clickedCoordinate)) {
final Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY]; final Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
@ -186,7 +186,7 @@ public class Board extends JPanel implements MouseListener {
} }
public ArrayList<Point> checkAttacks(boolean preferedColor) { public ArrayList<Point> checkAttacks(boolean preferedColor) {
ArrayList attacks = new ArrayList(); final ArrayList attacks = new ArrayList();
for (Piece[] pieceArr : pieces) { for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) { for (Piece piece : pieceArr) {
// Ifall det är tomrum skippa // Ifall det är tomrum skippa

View File

@ -12,13 +12,13 @@ public class Horse extends Piece {
@Override @Override
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) { public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
ArrayList<Point> movable = new ArrayList<>(); final ArrayList<Point> movable = new ArrayList<>();
for (int dx : new int[]{-2, -1, 1, 2}) { for (int dx : new int[]{-2, -1, 1, 2}) {
for (int direction : new int[]{-1, 1}) { for (int direction : new int[]{-1, 1}) {
int stepLength = (3 - Math.abs(dx)); final int stepLength = (3 - Math.abs(dx)),
int dy = direction * stepLength; dy = direction * stepLength;
Point potentialMove = new Point(this.position.x + dx, this.position.y + dy); final Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
addMovesIfCan(potentialMove, movable, pieces, allowedToRecurse); addMovesIfCan(potentialMove, movable, pieces, allowedToRecurse);
} }
} }

View File

@ -3,8 +3,6 @@ package schack;
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 java.util.Arrays;
import java.util.stream.Stream;
public final class King extends PieceKnownIfMoved { public final class King extends PieceKnownIfMoved {
@ -20,7 +18,7 @@ public final class King extends PieceKnownIfMoved {
* @return * @return
*/ */
private ArrayList<Point> getCastlingIfPossible(Piece[][] pieces) { private ArrayList<Point> getCastlingIfPossible(Piece[][] pieces) {
ArrayList<Point> possibleCastling = new ArrayList<>(); final ArrayList<Point> possibleCastling = new ArrayList<>();
if (this.isMoved()) { if (this.isMoved()) {
return possibleCastling; return possibleCastling;
} }
@ -85,8 +83,8 @@ public final class King extends PieceKnownIfMoved {
* @param shouldGoToLeftSide avgör ifall rockaden är åt vänster håll * @param shouldGoToLeftSide avgör ifall rockaden är åt vänster håll
*/ */
private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) { private void castle(Piece[][] pieces, boolean shouldGoToLeftSide) {
Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y]; final Piece rook = pieces[shouldGoToLeftSide ? 0 : 7][this.position.y];
Piece king = this; final Piece king = this;
// Null där de stod // Null där de stod
pieces[king.position.x][king.position.y] = null; pieces[king.position.x][king.position.y] = null;

View File

@ -19,14 +19,14 @@ public abstract class LongWalkers extends PieceKnownIfMoved {
* @return * @return
*/ */
ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) { ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) {
ArrayList<Point> movable = new ArrayList<>(); final ArrayList<Point> movable = new ArrayList<>();
for (int[] xy : directions) { for (int[] xy : directions) {
int loopX = this.position.x, loopY = this.position.y; int loopX = this.position.x, loopY = this.position.y;
while (loopX + xy[0] >= 0 && loopX + xy[0] <= 7 && loopY + xy[1] >= 0 && loopY + xy[1] <= 7) { while (loopX + xy[0] >= 0 && loopX + xy[0] <= 7 && loopY + xy[1] >= 0 && loopY + xy[1] <= 7) {
loopX += xy[0]; loopX += xy[0];
loopY += xy[1]; loopY += xy[1];
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, allowedToRecurse); final boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, allowedToRecurse);
if (shouldBreak) { if (shouldBreak) {
break; break;
} }

View File

@ -48,7 +48,7 @@ public class Pawn extends PieceKnownIfMoved {
// Kolla om man kan rakt frak // Kolla om man kan rakt frak
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) { for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY)); final Point pos = new Point(this.position.x, this.position.y + (this.isWhite() ? -pawnDY : pawnDY));
boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse); boolean shouldBreak = addMovesIfCan(pos, movable, pieces, allowedToRecurse);
if (shouldBreak) { if (shouldBreak) {
break; break;
@ -58,7 +58,7 @@ public class Pawn extends PieceKnownIfMoved {
// Kolla ifall vi kan ta någon // Kolla ifall vi kan ta någon
for (int pawnX : new int[]{-1, 1}) { for (int pawnX : new int[]{-1, 1}) {
// Position vi kollar just nu, snett upp åt höger & vänster // Position vi kollar just nu, snett upp åt höger & vänster
Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1)); final Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1));
movable.addAll(addAttackMovesIfCan(pos, pieces)); movable.addAll(addAttackMovesIfCan(pos, pieces));
} }
return movable; return movable;
@ -80,9 +80,9 @@ public class Pawn extends PieceKnownIfMoved {
return movable; return movable;
} }
Piece piece = pieces[pos.x][pos.y]; final Piece potentialEnemy = pieces[pos.x][pos.y];
// Ifall det är tomt här, gör ingenting // Ifall det är tomt här, gör ingenting
if (piece != null && piece.isWhite() != this.isWhite()) { if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) {
if (!isInSchack(pieces, pos)) { if (!isInSchack(pieces, pos)) {
movable.add(pos); movable.add(pos);
} }

View File

@ -27,13 +27,13 @@ public abstract class Piece {
* Bild av pjäsen som ritas ut bärdet * Bild av pjäsen som ritas ut bärdet
*/ */
protected BufferedImage icon; protected BufferedImage icon;
public Piece(boolean white, Point startingPosition) throws IOException { public Piece(boolean white, Point startingPosition) throws IOException {
this.isWhite = white; this.isWhite = white;
this.position = startingPosition; this.position = startingPosition;
setPieceIcon(); setPieceIcon();
} }
public Piece(boolean white) { public Piece(boolean white) {
this.isWhite = white; this.isWhite = white;
} }
@ -45,9 +45,9 @@ public abstract class Piece {
* @throws IOException ifall det inte finns någon bild pjäsen * @throws IOException ifall det inte finns någon bild pjäsen
*/ */
private void setPieceIcon() throws IOException { private void setPieceIcon() throws IOException {
String className = this.getClass().getSimpleName(); final String className = this.getClass().getSimpleName();
String colorName = this.isWhite() ? "White" : "Black"; final String colorName = this.isWhite() ? "White" : "Black";
String fileName = colorName + className + ".png"; final String fileName = colorName + className + ".png";
InputStream is = getClass().getResourceAsStream("/img/" + fileName); InputStream is = getClass().getResourceAsStream("/img/" + fileName);
icon = ImageIO.read(is); icon = ImageIO.read(is);
} }
@ -98,7 +98,7 @@ public abstract class Piece {
if (toMove.x >= pieces.length || toMove.y < 0 || position.x >= pieces[0].length || position.y < 0) { if (toMove.x >= pieces.length || toMove.y < 0 || position.x >= pieces[0].length || position.y < 0) {
return; return;
} }
pieces[toMove.x][toMove.y] = this; pieces[toMove.x][toMove.y] = this;
pieces[position.x][position.y] = null; pieces[position.x][position.y] = null;
this.position = new Point(toMove); this.position = new Point(toMove);
@ -118,8 +118,8 @@ public abstract class Piece {
if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) { if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) {
return false; return false;
} }
Piece pieceToCheck = pieces[pos.x][pos.y]; final Piece pieceToCheck = pieces[pos.x][pos.y];
// Detta är en tom plats // Detta är en tom plats
if (pieceToCheck == null) { if (pieceToCheck == null) {
@ -139,7 +139,7 @@ public abstract class Piece {
movable.add(pos); movable.add(pos);
} }
return true; return true;
} }
/** /**
@ -151,23 +151,23 @@ public abstract class Piece {
*/ */
protected boolean isInSchack(Piece[][] pieces, Point pos) { protected boolean isInSchack(Piece[][] pieces, Point pos) {
// Kom ihåg vart vi var // Kom ihåg vart vi var
Point previousPosition = new Point(this.position); final Point previousPosition = new Point(this.position);
// Kom ihåg motståndarpjäs // Kom ihåg motståndarpjäs
Piece guyThatsAlreadyHere = pieces[pos.x][pos.y]; final Piece guyThatsAlreadyHere = pieces[pos.x][pos.y];
// Testa att flytta // Testa att flytta
pieces[pos.x][pos.y] = this; pieces[pos.x][pos.y] = this;
pieces[previousPosition.x][previousPosition.y] = null; pieces[previousPosition.x][previousPosition.y] = null;
this.position = pos; this.position = pos;
boolean inSchack = isInSchack(pieces); final boolean inSchack = isInSchack(pieces);
// Flytta tillbaka // Flytta tillbaka
pieces[previousPosition.x][previousPosition.y] = this; pieces[previousPosition.x][previousPosition.y] = this;
pieces[pos.x][pos.y] = guyThatsAlreadyHere; pieces[pos.x][pos.y] = guyThatsAlreadyHere;
this.position = previousPosition; this.position = previousPosition;
return inSchack; return inSchack;
} }
@ -192,18 +192,17 @@ public abstract class Piece {
// Kollar ifall kungen står i schack just nu // Kollar ifall kungen står i schack just nu
for (Point enemyAttack : enemyAttacks) { for (Point enemyAttack : enemyAttacks) {
Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y]; final Piece attackedPiece = pieces[enemyAttack.x][enemyAttack.y];
if (attackedPiece != null && attackedPiece.supremeRuler) { if (attackedPiece != null && attackedPiece.supremeRuler) {
return true; return true;
} }
} }
return false; return false;
} }
@Override @Override
public String toString() { public String toString() {
return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}'; return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}';
// return "Piece{" + "position=" + position + ", isWhite=" + white + '}';
} }
/** /**
@ -222,5 +221,5 @@ public abstract class Piece {
public boolean isMoved() { public boolean isMoved() {
return false; return false;
} }
} }

View File

@ -20,7 +20,7 @@ import javax.swing.UIManager;
*/ */
public class Schack { public class Schack {
JFrame frame; final JFrame frame;
public Schack() throws IOException { public Schack() throws IOException {
// Set theme // Set theme
@ -42,18 +42,18 @@ public class Schack {
frame.setResizable(false); frame.setResizable(false);
// Might throw an IOException if the icon of the Pieces isn't embedded correctly // Might throw an IOException if the icon of the Pieces isn't embedded correctly
Board board = new Board(); final Board board = new Board();
frame.setContentPane(board); frame.setContentPane(board);
frame.getContentPane().addMouseListener(board); frame.getContentPane().addMouseListener(board);
// Create menu // Create menu
JMenuBar menuBar = new JMenuBar(); final JMenuBar menuBar = new JMenuBar();
JMenu gameMenu = new JMenu("Game"); final JMenu gameMenu = new JMenu("Game");
JMenu connectMenu = new JMenu("Connect"); final JMenu connectMenu = new JMenu("Connect");
JMenuItem connectToOpponent = new JMenuItem("Connect to opponent"); final JMenuItem connectToOpponent = new JMenuItem("Connect to opponent");
JMenuItem showLocalIP = new JMenuItem("Show IP"); final JMenuItem showLocalIP = new JMenuItem("Show IP");
JMenuItem askForRemi = new JMenuItem("Ask for remi"); final JMenuItem askForRemi = new JMenuItem("Ask for remi");
JMenuItem surrender = new JMenuItem("Surrender"); final JMenuItem surrender = new JMenuItem("Surrender");
// Actions // Actions
connectToOpponent.addActionListener((ActionEvent ae) -> { connectToOpponent.addActionListener((ActionEvent ae) -> {
@ -83,7 +83,7 @@ public class Schack {
}); });
surrender.addActionListener((ActionEvent ae) -> { surrender.addActionListener((ActionEvent ae) -> {
String whosGivingUp = board.isWhitesTurn() ? "Vit" : "Svart"; String whosGivingUp = board.isWhitesTurn() ? "Vit" : "Svart";
int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?"); final int choice = JOptionPane.showConfirmDialog(board, whosGivingUp + " har gett upp\nStarta om?");
if (choice == JOptionPane.YES_OPTION) { if (choice == JOptionPane.YES_OPTION) {
try { try {
board.restartGame(); board.restartGame();