mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 12:50:10 +01:00
list istället för arraylist
This commit is contained in:
parent
e8581663c7
commit
ae692d1428
@ -2,7 +2,7 @@ package schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Bishop extends LongWalkers {
|
||||
|
||||
@ -11,7 +11,7 @@ public class Bishop extends LongWalkers {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
public List<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
return getMoves(
|
||||
new int[][]{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}},
|
||||
pieces,
|
||||
|
@ -9,6 +9,7 @@ import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
@ -16,7 +17,7 @@ public class Board extends JPanel implements MouseListener {
|
||||
|
||||
public static final int SIZE_OF_TILE = 100;
|
||||
private Piece[][] pieces = new Piece[8][8];
|
||||
private ArrayList<Point> validMovesToDraw = new ArrayList<>();
|
||||
private List<Point> validMovesToDraw = new ArrayList<>();
|
||||
private Point previouslyClickedPoint = new Point();
|
||||
private final Color moveableColor = new Color(255, 191, 0);
|
||||
short turnCount = 0;
|
||||
@ -161,8 +162,8 @@ public class Board extends JPanel implements MouseListener {
|
||||
* @return SCHACK, SCHACKMATT, PATT, NORMAL
|
||||
*/
|
||||
private SchackState getSchackState() {
|
||||
ArrayList<Point> allValidMoves = getMoves(whitesTurn);
|
||||
ArrayList<Point> opposingAttacks = getAttacks(!whitesTurn);
|
||||
List<Point> allValidMoves = getMoves(whitesTurn);
|
||||
List<Point> opposingAttacks = getAttacks(!whitesTurn);
|
||||
boolean weCanMove = !allValidMoves.isEmpty();
|
||||
|
||||
boolean inSchack = false;
|
||||
@ -188,8 +189,8 @@ public class Board extends JPanel implements MouseListener {
|
||||
return SchackState.NORMAL;
|
||||
}
|
||||
|
||||
private ArrayList<Point> getMoves(boolean whiteMovesAreWanted) {
|
||||
ArrayList<Point> allValidMoves = new ArrayList<>();
|
||||
private List<Point> getMoves(boolean whiteMovesAreWanted) {
|
||||
List<Point> allValidMoves = new ArrayList<>();
|
||||
for (Piece[] pieceArr : pieces) {
|
||||
for (Piece piece : pieceArr) {
|
||||
if (piece == null || whiteMovesAreWanted != piece.isWhite()) {
|
||||
@ -201,8 +202,8 @@ public class Board extends JPanel implements MouseListener {
|
||||
return allValidMoves;
|
||||
}
|
||||
|
||||
public ArrayList<Point> getAttacks(boolean whiteAttacksAreWanted) {
|
||||
ArrayList attacks = new ArrayList();
|
||||
public List<Point> getAttacks(boolean whiteAttacksAreWanted) {
|
||||
List attacks = new ArrayList();
|
||||
for (Piece[] pieceArr : pieces) {
|
||||
for (Piece piece : pieceArr) {
|
||||
if (piece == null || whiteAttacksAreWanted != piece.isWhite()) {
|
||||
|
@ -3,6 +3,7 @@ package schack;
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Horse extends Piece {
|
||||
|
||||
@ -11,8 +12,8 @@ public class Horse extends Piece {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
ArrayList<Point> movable = new ArrayList<>();
|
||||
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}) {
|
||||
|
@ -3,6 +3,7 @@ package schack;
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class King extends Piece {
|
||||
|
||||
@ -17,8 +18,8 @@ public final class King extends Piece {
|
||||
* @param pieces
|
||||
* @return
|
||||
*/
|
||||
private ArrayList<Point> getCastlingIfPossible(Piece[][] pieces) {
|
||||
ArrayList<Point> possibleCastling = new ArrayList<>();
|
||||
private List<Point> getCastlingIfPossible(Piece[][] pieces) {
|
||||
List<Point> possibleCastling = new ArrayList<>();
|
||||
if (this.isMoved()) {
|
||||
return possibleCastling;
|
||||
}
|
||||
@ -78,8 +79,8 @@ public final class King extends Piece {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
ArrayList<Point> movable = new ArrayList<>();
|
||||
public List<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
List<Point> movable = new ArrayList<>();
|
||||
|
||||
for (int loopX = -1; loopX < 2; loopX++) {
|
||||
for (int loopY = -1; loopY < 2; loopY++) {
|
||||
|
@ -3,6 +3,7 @@ package schack;
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class LongWalkers extends Piece {
|
||||
|
||||
@ -21,8 +22,8 @@ public abstract class LongWalkers extends Piece {
|
||||
* @param allowedToRecurse
|
||||
* @return
|
||||
*/
|
||||
ArrayList<Point> getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) {
|
||||
ArrayList<Point> movable = new ArrayList<>();
|
||||
List<Point> getMoves(int[][] directions, Piece[][] pieces, boolean allowedToRecurse) {
|
||||
List<Point> movable = new ArrayList<>();
|
||||
|
||||
for (int[] xy : directions) {
|
||||
int loopX = this.position.x, loopY = this.position.y;
|
||||
|
@ -4,6 +4,7 @@ import java.awt.HeadlessException;
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
public class Pawn extends Piece {
|
||||
@ -21,8 +22,8 @@ public class Pawn extends Piece {
|
||||
* @return Alla lämpliga attackMoves
|
||||
*/
|
||||
@Override
|
||||
public ArrayList<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
|
||||
ArrayList<Point> movable = new ArrayList<>();
|
||||
public List<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
|
||||
List<Point> movable = new ArrayList<>();
|
||||
|
||||
// Kolla ifall vi kan ta någon
|
||||
for (int pawnX : new int[]{-1, 1}) {
|
||||
@ -42,8 +43,8 @@ public class Pawn extends Piece {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
ArrayList<Point> movable = new ArrayList<>();
|
||||
public List<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
List<Point> movable = new ArrayList<>();
|
||||
|
||||
// Om bonden har gått en gång, får gå 1 steg, annars 2
|
||||
final int upTo = this.isMoved() ? 1 : 2;
|
||||
@ -75,8 +76,8 @@ public class Pawn extends Piece {
|
||||
* @param movable
|
||||
* @param pieces
|
||||
*/
|
||||
private ArrayList<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) {
|
||||
ArrayList<Point> movable = new ArrayList();
|
||||
private List<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) {
|
||||
List<Point> movable = new ArrayList();
|
||||
// Se till att vi inte är utanför brädet
|
||||
if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0) {
|
||||
return movable;
|
||||
@ -92,7 +93,7 @@ public class Pawn extends Piece {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean allowedToRecurse) {
|
||||
protected boolean addMovesIfCan(Point pos, List movable, Piece[][] pieces, boolean allowedToRecurse) {
|
||||
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
|
||||
return false;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public abstract class Piece {
|
||||
@ -18,7 +19,7 @@ public abstract class Piece {
|
||||
/**
|
||||
* Sant ifall pjäsens färg är vit, falskt ifall den är svart
|
||||
*/
|
||||
private boolean isWhite;
|
||||
private final boolean isWhite;
|
||||
/**
|
||||
* SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)
|
||||
*/
|
||||
@ -64,7 +65,7 @@ public abstract class Piece {
|
||||
* @param allowedToRecurse
|
||||
* @return
|
||||
*/
|
||||
public abstract ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse);
|
||||
public abstract List<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse);
|
||||
|
||||
/**
|
||||
* Ger tillbaks alla ställen pjäsen kan attackera
|
||||
@ -74,7 +75,7 @@ public abstract class Piece {
|
||||
* kolla ifall det är något i möjliga attackrutor ifall
|
||||
* @return Alla lämpliga attackMoves
|
||||
*/
|
||||
public ArrayList<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
|
||||
public List<Point> validAttacks(Piece[][] pieces, boolean shouldNotCareIfAttackSpaceIsEmptyOrNot) {
|
||||
return validMoves(pieces, false);
|
||||
}
|
||||
|
||||
@ -121,7 +122,7 @@ public abstract class Piece {
|
||||
*
|
||||
* @return true ifall man inte kan gå längre i denna riktning
|
||||
*/
|
||||
protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean allowedToRecurse) {
|
||||
protected boolean addMovesIfCan(Point pos, List<Point> movable, Piece[][] pieces, boolean allowedToRecurse) {
|
||||
// Ifall vi är utanför brädet ge tillbaka false
|
||||
if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) {
|
||||
return false;
|
||||
@ -186,7 +187,7 @@ public abstract class Piece {
|
||||
* @return true ifall det är schack
|
||||
*/
|
||||
protected boolean isInSchack(Piece[][] pieces) {
|
||||
ArrayList<Point> enemyAttacks = new ArrayList<>();
|
||||
List<Point> enemyAttacks = new ArrayList<>();
|
||||
|
||||
// Fråga alla pjäser vart de kan gå/ta
|
||||
for (Piece[] pieceArr : pieces) {
|
||||
|
@ -2,7 +2,7 @@ package schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Queen extends LongWalkers {
|
||||
|
||||
@ -11,7 +11,7 @@ public class Queen extends LongWalkers {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
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}},
|
||||
pieces,
|
||||
|
@ -2,7 +2,7 @@ package schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Rook extends LongWalkers {
|
||||
|
||||
@ -11,7 +11,7 @@ public class Rook extends LongWalkers {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
public List<Point> validMoves(Piece[][] pieces, boolean allowedToRecurse) {
|
||||
return getMoves(
|
||||
new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}},
|
||||
pieces,
|
||||
|
@ -1,9 +1,11 @@
|
||||
package schack;
|
||||
|
||||
import java.awt.HeadlessException;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuBar;
|
||||
@ -74,7 +76,7 @@ public class Schack {
|
||||
String ip = Inet4Address.getLocalHost().toString();
|
||||
JOptionPane.showMessageDialog(null, "IP: " + ip);
|
||||
|
||||
} catch (Exception e) {
|
||||
} catch (HeadlessException | UnknownHostException e) {
|
||||
}
|
||||
});
|
||||
connectToOpponent.addActionListener((ActionEvent ae) -> {
|
||||
|
Loading…
x
Reference in New Issue
Block a user