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