refaktor till maven
Before Width: | Height: | Size: 9.7 KiB After Width: | Height: | Size: 9.7 KiB |
Before Width: | Height: | Size: 9.8 KiB After Width: | Height: | Size: 9.8 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 6.2 KiB After Width: | Height: | Size: 6.2 KiB |
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 9.6 KiB After Width: | Height: | Size: 9.6 KiB |
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 5.9 KiB |
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 4.0 KiB After Width: | Height: | Size: 4.0 KiB |
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
@@ -13,10 +13,9 @@ public class Bishop extends LongWalkers {
|
||||
@Override
|
||||
public List<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
return getMoves(
|
||||
new int[][]{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}},
|
||||
new int[][] { { -1, -1 }, { 1, 1 }, { -1, 1 }, { 1, -1 } },
|
||||
pieces,
|
||||
allowedToRecurse
|
||||
);
|
||||
allowedToRecurse);
|
||||
}
|
||||
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
@@ -41,14 +41,22 @@ 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, 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, 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(0, 0)), null, null, null, null, null, null,
|
||||
new Rook(true, new Point(0, 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, 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)) }
|
||||
};
|
||||
|
||||
// Sätt ut bönder
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
@@ -15,8 +15,8 @@ public class Horse extends Piece {
|
||||
public List<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
List<Point> movable = new ArrayList<>();
|
||||
|
||||
for (int dx : new int[]{-2, -1, 1, 2}) {
|
||||
for (int direction : new int[]{-1, 1}) {
|
||||
for (int dx : new int[] { -2, -1, 1, 2 }) {
|
||||
for (int direction : new int[] { -1, 1 }) {
|
||||
int stepLength = (3 - Math.abs(dx)),
|
||||
dy = direction * stepLength;
|
||||
Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
@@ -26,7 +26,7 @@ public final class King extends Piece {
|
||||
|
||||
boolean[] somethingBetweenOrSchackOnTheWay = new boolean[2]; // Vänster, höger
|
||||
int leftModifier = -1, rightModifier = 1;
|
||||
for (int modifier : new int[]{leftModifier, rightModifier}) {
|
||||
for (int modifier : new int[] { leftModifier, rightModifier }) {
|
||||
for (int loopX = this.position.x + modifier; loopX > 0 && loopX < 7; loopX += modifier) {
|
||||
if (pieces[loopX][this.position.y] != null || isInSchack(pieces, new Point(loopX, this.position.y))) {
|
||||
somethingBetweenOrSchackOnTheWay[(modifier == leftModifier) ? 0 : 1] = true;
|
||||
@@ -36,7 +36,7 @@ public final class King extends Piece {
|
||||
}
|
||||
leftModifier = 0;
|
||||
rightModifier = 1;
|
||||
for (int direction : new int[]{leftModifier, rightModifier}) {
|
||||
for (int direction : new int[] { leftModifier, rightModifier }) {
|
||||
if (!somethingBetweenOrSchackOnTheWay[direction]) {
|
||||
Piece possibleRook = pieces[direction == leftModifier ? 0 : 7][this.position.y];
|
||||
if (possibleRook != null && !possibleRook.isMoved()) {
|
||||
@@ -87,7 +87,8 @@ public final class King extends Piece {
|
||||
if (loopY == 0 && loopX == 0) {
|
||||
continue;
|
||||
}
|
||||
addMovesIfCan(new Point(this.position.x + loopX, this.position.y + loopY), movable, pieces, allowedToRecurse);
|
||||
addMovesIfCan(new Point(this.position.x + loopX, this.position.y + loopY), movable, pieces,
|
||||
allowedToRecurse);
|
||||
}
|
||||
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
@@ -14,9 +14,15 @@ public abstract class LongWalkers extends Piece {
|
||||
/**
|
||||
* Generell metod för att generera möjliga drag för LongWalkers
|
||||
*
|
||||
* @param directions vilka håll. Exempel: <pre>
|
||||
* {@code new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}}</pre> för att gå
|
||||
* som ett torn
|
||||
* @param directions vilka håll. Exempel:
|
||||
*
|
||||
* <pre>
|
||||
* {@code
|
||||
* new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }
|
||||
* }</pre>
|
||||
*
|
||||
* för att gå
|
||||
* som ett torn
|
||||
*
|
||||
* @param pieces
|
||||
* @param allowedToRecurse
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.HeadlessException;
|
||||
import java.awt.Point;
|
||||
@@ -18,7 +18,8 @@ public class Pawn extends Piece {
|
||||
*
|
||||
* @param pieces
|
||||
* @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
|
||||
*/
|
||||
@Override
|
||||
@@ -26,7 +27,7 @@ public class Pawn extends Piece {
|
||||
List<Point> movable = new ArrayList<>();
|
||||
|
||||
// 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
|
||||
Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1));
|
||||
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
|
||||
@@ -59,7 +60,7 @@ public class Pawn extends Piece {
|
||||
}
|
||||
|
||||
// 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
|
||||
final Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite() ? -1 : 1));
|
||||
movable.addAll(addAttackMovesIfCan(pos, pieces));
|
||||
@@ -121,7 +122,7 @@ public class Pawn extends Piece {
|
||||
}
|
||||
|
||||
private void transform(Piece[][] pieces) throws HeadlessException {
|
||||
String[] transformations = {"Queen", "Rook", "Bishop", "Horse"};
|
||||
String[] transformations = { "Queen", "Rook", "Bishop", "Horse" };
|
||||
int choosenTransformations = JOptionPane.showOptionDialog(null,
|
||||
"What do you want to the pawn to transform into?",
|
||||
"Pawn about to transform",
|
||||
@@ -129,8 +130,7 @@ public class Pawn extends Piece {
|
||||
JOptionPane.INFORMATION_MESSAGE,
|
||||
null,
|
||||
transformations,
|
||||
transformations[0]
|
||||
);
|
||||
transformations[0]);
|
||||
try {
|
||||
switch (choosenTransformations) {
|
||||
case 0:
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
@@ -54,7 +54,7 @@ public abstract class Piece {
|
||||
String className = this.getClass().getSimpleName();
|
||||
String colorName = this.isWhite() ? "White" : "Black";
|
||||
String fileName = colorName + className + ".png";
|
||||
InputStream is = getClass().getResourceAsStream("/img/" + fileName);
|
||||
InputStream is = getClass().getResourceAsStream("/com/billenius/img/" + fileName);
|
||||
icon = ImageIO.read(is);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,8 @@ public abstract class Piece {
|
||||
*
|
||||
* @param pieces
|
||||
* @param shouldNotCareIfAttackSpaceIsEmptyOrNot För bönder ifall den ska
|
||||
* kolla ifall det är något i möjliga attackrutor ifall
|
||||
* kolla ifall det är något i
|
||||
* möjliga attackrutor ifall
|
||||
* @return Alla lämpliga attackMoves
|
||||
*/
|
||||
public List<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
|
||||
@@ -89,8 +90,7 @@ public abstract class Piece {
|
||||
icon,
|
||||
position.x * Board.SIZE_OF_TILE,
|
||||
position.y * Board.SIZE_OF_TILE,
|
||||
null
|
||||
);
|
||||
null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,11 +114,12 @@ public abstract class Piece {
|
||||
/**
|
||||
* Lägg till move ifall det går, alltså inte är schack där
|
||||
*
|
||||
* @param pos Drag att lägga till ifall det går
|
||||
* @param movable Lägger till drag i denna ArrayList
|
||||
* @param pieces Piece[][] över brädet
|
||||
* @param pos Drag att lägga till ifall det går
|
||||
* @param movable Lägger till drag i denna ArrayList
|
||||
* @param pieces Piece[][] över brädet
|
||||
* @param allowedToRecurse Behövs för att inte gå in i en evig loop där
|
||||
* <pre>{@code addMovesIfCan -> isInSchack -> validMoves -> getCastlingIfPossible(King) -> isInSchack}</pre>
|
||||
*
|
||||
* <pre>{@code addMovesIfCan -> isInSchack -> validMoves -> getCastlingIfPossible(King) -> isInSchack}</pre>
|
||||
*
|
||||
* @return true ifall man inte kan gå längre i denna riktning
|
||||
*/
|
||||
@@ -155,7 +156,7 @@ public abstract class Piece {
|
||||
* Kolla ifall det är schack vid den här positionen
|
||||
*
|
||||
* @param pieces Piece[][] över hela brädet
|
||||
* @param pos Kollar ifall det är schack om denna Piece flyttar hit
|
||||
* @param pos Kollar ifall det är schack om denna Piece flyttar hit
|
||||
* @return true ifall det är schack
|
||||
*/
|
||||
protected boolean isInSchack(Piece[][] pieces, Point pos) {
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
@@ -13,9 +13,8 @@ public class Queen extends LongWalkers {
|
||||
@Override
|
||||
public List<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
return getMoves(
|
||||
new int[][]{{1, 0}, {-1, 0}, {0, 1}, {-1, -1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}},
|
||||
new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { -1, -1 }, { 0, -1 }, { 1, 1 }, { -1, 1 }, { 1, -1 } },
|
||||
pieces,
|
||||
allowedToRecurse
|
||||
);
|
||||
allowedToRecurse);
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
@@ -13,10 +13,9 @@ public class Rook extends LongWalkers {
|
||||
@Override
|
||||
public List<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
return getMoves(
|
||||
new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}},
|
||||
new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } },
|
||||
pieces,
|
||||
allowedToRecurse
|
||||
);
|
||||
allowedToRecurse);
|
||||
}
|
||||
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
import java.awt.HeadlessException;
|
||||
import java.awt.event.ActionEvent;
|
||||
@@ -33,12 +33,12 @@ public class Schack {
|
||||
frame.setAlwaysOnTop(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
|
||||
final Board board = new Board();
|
||||
frame.setContentPane(board);
|
||||
frame.getContentPane().addMouseListener(board);
|
||||
|
||||
// Create menu
|
||||
// Create menu
|
||||
final JMenuBar menuBar = new JMenuBar();
|
||||
final JMenu gameMenu = new JMenu("Game");
|
||||
final JMenu connectMenu = new JMenu("Connect");
|
||||
@@ -81,9 +81,8 @@ public class Schack {
|
||||
});
|
||||
connectToOpponent.addActionListener((ActionEvent ae) -> {
|
||||
String opponentIP = JOptionPane.showInputDialog(null, "What's your opponents IP?");
|
||||
System.out.println("opponents ip: "+opponentIP);
|
||||
|
||||
|
||||
System.out.println("opponents ip: " + opponentIP);
|
||||
|
||||
});
|
||||
|
||||
// Add the menu stuff
|
@@ -1,4 +1,4 @@
|
||||
package schack;
|
||||
package com.billenius.schack;
|
||||
|
||||
/**
|
||||
*
|