Merge branch 'funkar-schack-menh-ja'

This commit is contained in:
loveb 2022-05-10 16:02:07 +02:00
commit cb3ccecade
9 changed files with 430 additions and 765 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,5 @@
build.xml.data.CRC32=8c4b7f48
build.xml.script.CRC32=3becf7c5
build.xml.stylesheet.CRC32=f85dc8f2@1.102.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=ab896e8c
nbproject/build-impl.xml.script.CRC32=69634283
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.101.0.48
nbproject/build-impl.xml.data.CRC32=8c4b7f48
nbproject/build-impl.xml.script.CRC32=e431c94e
nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48

View File

@ -18,11 +18,11 @@ 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 ArrayList<Point> validDebugMovesToDraw = new ArrayList<>();
private Point selectedPiece = new Point();
private Color moveableColor = new Color(255, 191, 0);
public static boolean turn = true;
public boolean developerMode = false;
short turnCount = 0;
boolean whitesTurn = true;
public Board() throws IOException {
@ -62,10 +62,6 @@ public class Board extends JPanel implements MouseListener {
Graphics2D g2 = (Graphics2D) g;
drawSquares(g2);
validDebugMovesToDraw.stream().filter(point -> point != null).forEach(point -> {
g2.setColor(Color.CYAN);
g2.fillRect(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE);
});
// måla alla ställen man kan ¨till
validMovesToDraw.stream().filter(point -> point != null)
.forEach(point -> {
@ -114,7 +110,49 @@ public class Board extends JPanel implements MouseListener {
try {
Piece p = pieces[selectedPiece.x][selectedPiece.y];
p.move(pieces, clicked);
turn = !turn;
turnCount++;
whitesTurn = !whitesTurn;
ArrayList<Point> allValidMoves = new ArrayList<>();
for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) {
if (piece == null || whitesTurn != piece.isWhite()) {
continue;
}
// Kolla ifall vi är samma färg som får röra sig
// Ifall en pjäs får röra sig sätt weCanMove till sant och sluta
allValidMoves.addAll(piece.validMoves(pieces, true));
}
}
ArrayList<Point> opposingAttacks = checkAttacks(!whitesTurn);
boolean weCanMove = allValidMoves.size() > 0;
boolean inSchack = false;
for (Point attack : opposingAttacks) {
Piece attacked = pieces[attack.x][attack.y];
if (attacked == null) {
continue;
}
if (attacked.supremeRuler) {
// Kolla ifall vi är i schackmatt
if (weCanMove) {
JOptionPane.showMessageDialog(this, "Du står i schack");
} else {
int choise = JOptionPane.showConfirmDialog(this, "Schackmatt\nVill du starta om?");
if (choise == JOptionPane.YES_OPTION) {
this.pieces = initPieces();
whitesTurn = true;
}
}
inSchack = true;
}
}
if (!inSchack && !weCanMove) {
JOptionPane.showMessageDialog(this, "Patt");
}
} catch (Exception e) {
validMovesToDraw.clear();
@ -132,23 +170,22 @@ public class Board extends JPanel implements MouseListener {
Piece selectedPiece = pieces[mouseCoordinateX][mouseCoordinateY];
// Kolla endast ifall vi kan röra pjäsen om det är samma färg som den tur vi är
if (selectedPiece.isWhite() == turn || developerMode) {
ArrayList<Point> attacks = checkAttacks(turn);
if (selectedPiece.isWhite() == whitesTurn || developerMode) {
ArrayList<Point> attacks = checkAttacks(whitesTurn);
ArrayList<Point> validMoves = selectedPiece.validMoves(pieces, true);
// Kolla ifall vi kan röra oss
// Loopa igenom allt
System.out.println("\n\n\n\n\n\n");
ArrayList<Point> allValidMoves = new ArrayList<>();
for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) {
if (piece == null || turn != piece.white) {
if (piece == null || whitesTurn != piece.isWhite) {
continue;
}
// Kolla ifall vi är samma färg som får röra sig
// Ifall en pjäs får röra sig sätt weCanMove till sant och sluta
allValidMoves.addAll(piece.validMoves(pieces, turn));
allValidMoves.addAll(piece.validMoves(pieces, whitesTurn));
}
}
@ -157,41 +194,8 @@ public class Board extends JPanel implements MouseListener {
//validMoves.removeAll(attacks);
}
allValidMoves.removeAll(attacks);
validDebugMovesToDraw = allValidMoves;
boolean weCanMove = allValidMoves.size() > 0;
boolean hasShowedMessageAboutSchack = false;
System.out.println("turn: " + (turn ? "white" : "black"));
System.out.println("All valid moves: " + allValidMoves);
System.out.println("WeCanMo´vsadadade: " + weCanMove);
ArrayList<Point> opposingAttacks = checkAttacks(!turn);
System.out.println("opposingAttacks: " + opposingAttacks);
System.out.println("attacks: " + attacks);
opposingAttacks.removeAll(allValidMoves);
//allValidMoves.removeAll(attacks);
// Kollar ifall kungen står i schack just nu
for (Point attack : opposingAttacks) {
Piece attacked = pieces[attack.x][attack.y];
if (attacked == null) {
continue;
}
if (attacked.supremeRuler) {
// Kolla ifall vi är i schackmatt
if (weCanMove) {
JOptionPane.showMessageDialog(this, "Du står i schack");
} else {
JOptionPane.showMessageDialog(this, "Schackmatt");
}
hasShowedMessageAboutSchack = true;
}
}
if (!hasShowedMessageAboutSchack && !weCanMove) {
JOptionPane.showMessageDialog(this, "Patt");
}
validMovesToDraw.addAll(validMoves);
}
} catch (Exception e) {
@ -210,7 +214,7 @@ public class Board extends JPanel implements MouseListener {
for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) {
// Ifall det är tomrum skippa
if (piece == null || preferedColor != piece.white) {
if (piece == null || preferedColor != piece.isWhite) {
continue;
}
// Lägg till alla attacker för respektive färg

View File

@ -15,44 +15,15 @@ public class Horse extends Piece {
public ArrayList<Point> validMoves(Piece[][] pieces, boolean isSelected) {
ArrayList<Point> movable = new ArrayList<>();
// TODO: Integrera
/*
for (int dx : new int[]{-2, -1, 1, 2}) {
for (int y = -1; y <= 1; y += 2) {
int dy = y * (3 - abs(dx));
for (int direction : new int[]{-1, 1}) {
int stepLength = (3 - abs(dx));
int dy = direction * stepLength;
Point potentialMove = new Point(this.position.x + dx, this.position.y + dy);
addMovesIfCan(potentialMove, movable, pieces, isSelected);
}
}
*/
// Postitioner att checka
Point[] positions = {
// Snett höger uppåt
new Point(this.position.x + 1, this.position.y - 2),
// Snett höger neråt
new Point(this.position.x + 1, this.position.y + 2),
// Långt höger åt sidan uppåt
new Point(this.position.x + 2, this.position.y - 1),
// Långt höger åt sidan neråt
new Point(this.position.x + 2, this.position.y + 1),
// Snett vänster uppåt
new Point(this.position.x - 1, this.position.y - 2),
// Snett vänster neråt
new Point(this.position.x - 1, this.position.y + 2),
// Långt vänster åt sidan uppåt
new Point(this.position.x - 2, this.position.y - 1),
// Långt vänster åt sidan neråt
new Point(this.position.x - 2, this.position.y + 1)
};
for (Point pos : positions) {
// Ifall en är blockerad ska vi inte sluta kolla
addMovesIfCan(pos, movable, pieces, isSelected);
}
return movable;
}
}

View File

@ -1,4 +1,3 @@
package schack;
import java.awt.Point;
@ -13,7 +12,7 @@ public final class King extends PieceKnownIfMoved {
}
private void addCastlingIfCan(Piece[][] pieces, ArrayList<Point> movable, Point toMove, Point selected) {
if (moved) {
if (isMoved()) {
return;
}
@ -27,23 +26,16 @@ public final class King extends PieceKnownIfMoved {
// Check att man bara kan göra rockad ifall tornet inte rört sig
Piece p = pieces[loopX][this.position.y];
if (p != null) {
try {
PieceKnownIfMoved PKIM = (PieceKnownIfMoved) p;
if (!PKIM.moved) {
if (!p.isMoved()) {
movable.add(new Point(2, this.position.y));
}
} catch (Exception e) {
}
}
}
// Kolla ifall det är tomt emellan kung och torn
if (pieces[loopX][this.position.y] != null) {
nothingInBetween = false;
}
}
// Höger
@ -52,8 +44,14 @@ public final class King extends PieceKnownIfMoved {
// Kolla ifall vi kollar tornet och inget är emellan
if (loopX == 7 && nothingInBetween) {
// Check att man bara kan göra rockad ifall tornet inte rört sig
Piece p = pieces[loopX][this.position.y];
if (p != null) {
if (!p.isMoved()) {
movable.add(new Point(6, this.position.y));
}
}
}
// Kolla ifall det är tomt emellan kung och torn
if (pieces[loopX][this.position.y] != null) {

View File

@ -17,12 +17,12 @@ public class Pawn extends PieceKnownIfMoved {
// Kolla ifall vi kan ta någon
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.white ? -1 : 1));
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) {
continue;
}
Piece piece = pieces[pos.x][pos.y];
if (piece == null || piece.white != piece.white) {
if (piece == null || piece.isWhite != piece.isWhite) {
movable.add(pos);
}
}
@ -39,10 +39,9 @@ public class Pawn extends PieceKnownIfMoved {
// Kolla om man kan rakt frak
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
Point pos = new Point(this.position.x, this.position.y + (this.white ? -pawnDY : pawnDY));
Point pos = new Point(this.position.x, this.position.y + (this.isWhite ? -pawnDY : pawnDY));
boolean shouldBreak = addMovesIfCan(pos, movable, pieces, isSelected);
if (shouldBreak) {
System.out.println("should brkje!");
break;
}
}
@ -50,10 +49,9 @@ public class Pawn extends PieceKnownIfMoved {
// Kolla ifall vi kan ta någon
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.white ? -1 : 1));
Point pos = new Point(this.position.x + pawnX, this.position.y + (this.isWhite ? -1 : 1));
addAttackMovesIfCan(pos, movable, pieces);
}
System.out.println("len of movable: " + movable.size());
return movable;
}
@ -78,23 +76,44 @@ public class Pawn extends PieceKnownIfMoved {
if (piece == null) {
return;
} else if (piece.isWhite() != this.isWhite()) {
tryToMoveAndCheckIfCheck(pieces, movable, pos);
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
}
}
@Override
protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean isSelected) {
protected boolean addMovesIfCan(Point pos, ArrayList movable, Piece[][] pieces, boolean isSelected) {
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
return false;
}
// Instead of checking index and null, try-catch
try {
// Ifall vi kollar utanför brädet kommer detta att faila
Piece p = pieces[pos.x][pos.y];
Piece pieceToCheck = pieces[pos.x][pos.y];
if (pieceToCheck != null) {
return true;
// Ifall pjäsen här har samma färg som oss, break
// Ifall det inte är någon pjäs här kommer det att ner till
// catch(NullPointerException) och lägger vi till detta drag i listan
// Ifall det är inte är en pjäs här, kasta ett NullPointerException
// Detta är för att vara lik super-implementationen som möjligt
if (p == null) {
throw new NullPointerException();
} else {
tryToMoveAndCheckIfCheck(pieces, movable, pos);
// Detta betyder att det finns en pjäs här
// Vi kan ta men inte längre.
return true;
}
} catch (NullPointerException npe) {
// This is an empty spot
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
} catch (IndexOutOfBoundsException ioobe) {
// This means that the player is at the edge
System.out.println(pos);
} catch (Exception e) {
// For good meassure
}
return false;
}
}
}

View File

@ -19,7 +19,7 @@ public abstract class Piece {
/**
* Sant ifall pjäsens färg är vit, falskt ifall den är svart
*/
public boolean white;
public boolean isWhite;
/**
* SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)
*/
@ -30,13 +30,13 @@ public abstract class Piece {
protected BufferedImage icon;
public Piece(boolean white, Point startingPosition) throws IOException {
this.white = white;
this.isWhite = white;
this.position = startingPosition;
setPieceIcon();
}
public Piece(boolean white) {
this.white = white;
this.isWhite = white;
}
public void setPosition(Point p) {
@ -51,7 +51,7 @@ public abstract class Piece {
*/
protected void setPieceIcon() throws IOException {
String className = this.getClass().getSimpleName();
String colorName = white ? "White" : "Black";
String colorName = isWhite ? "White" : "Black";
String fileName = colorName + className + ".png";
InputStream is = getClass().getResourceAsStream("/img/" + fileName);
icon = ImageIO.read(is);
@ -110,15 +110,6 @@ public abstract class Piece {
this.position = new Point(toMove);
}
/**
* Lägger till möjliga drag i movable ifall det går
*
* @param pos
* @param movable
* @param pieces
* @param isSelected
* @return true ifall det inte finns fler drag att lägga till
*/
protected boolean addMovesIfCan(Point pos, ArrayList<Point> movable, Piece[][] pieces, boolean isSelected) {
// Ifall vi är utanför brädet ge tillbaka false
if (pos.x > 7 || pos.x < 0 || pos.y > 7 || pos.y < 0) {
@ -132,7 +123,7 @@ public abstract class Piece {
if (!isSelected) {
movable.add(pos);
} else {
tryToMoveAndCheckIfCheck(pieces, movable, pos);
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
}
// Fortsätt att
return false;
@ -143,7 +134,7 @@ public abstract class Piece {
* längre Ifall det är samma färg som oss betyder det att vi inte kan
* lägga till den
*/
if (pieceToCheck.isWhite() != this.white) {
if (pieceToCheck.isWhite() != this.isWhite) {
/**
* Detta betyder att det är en motsatts pjäs här, vi kan ta men inte
* längre
@ -151,22 +142,15 @@ public abstract class Piece {
if (!isSelected) {
movable.add(pos);
} else {
tryToMoveAndCheckIfCheck(pieces, movable, pos);
movable.addAll(tryToMoveAndCheckIfCheck(pieces, pos));
}
}
return true;
}
/**
* Simulera ett drag och kolla ifall det är schack. Ifall det inte är schack
* lägg till draget i listan movable
*
* @param pieces
* @param movable Lista där allt kommer läggas till
* @param pos
*/
void tryToMoveAndCheckIfCheck(Piece[][] pieces, ArrayList movable, Point pos) {
ArrayList<Point> tryToMoveAndCheckIfCheck(Piece[][] pieces, Point pos) {
ArrayList<Point> movable = new ArrayList<>();
// Kom ihåg vart vi var
Point previousPosition = new Point(this.position);
@ -188,25 +172,41 @@ public abstract class Piece {
if (!inSchack) {
movable.add(pos);
}
return movable;
}
boolean checkIfSchack(Point pos, Piece[][] pieces) {
boolean ourColor = this.isWhite();
Piece selectedPiece = this;
ArrayList<Point> attacks = new ArrayList<>();
// Fråga alla pjäser vart de kan /ta
for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) {
if (piece != null && piece.isWhite() != this.isWhite()) {
Arrays.stream(pieces).forEach(array -> {
Arrays.stream(array).filter(piece -> piece != null && piece.isWhite() != this.isWhite()).forEach(piece -> {
attacks.addAll(piece.validAttacks(pieces));
}
}
});
});
/* for (Piece[] pieceArr : pieces) {
for (Piece piece : pieceArr) {
// Ifall det är tomrum skippa
if (piece == null) {
continue;
} else if (piece.isWhite() == ourColor) {
continue;
}
// Lägg till alla attacker för mostståndaren
attacks.addAll(piece.validAttacks(pieces));
}
}*/
// Kollar ifall kungen står i schack just nu
for (Point attack : attacks) {
Piece attacked = pieces[attack.x][attack.y];
if (attacked != null && attacked.supremeRuler) {
if (attacked == null) {
continue;
}
if (attacked.supremeRuler) {
return true;
}
}
@ -215,12 +215,21 @@ public abstract class Piece {
@Override
public String toString() {
return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + white + '}';
return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}';
// return "Piece{" + "position=" + position + ", isWhite=" + white + '}';
}
public boolean isWhite() {
return white;
return isWhite;
}
/**
* Kompabilitet med PieceKnownIfMoved
*
* @return false
*/
public boolean isMoved() {
return false;
}
}

View File

@ -2,7 +2,6 @@ package schack;
import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;
public abstract class PieceKnownIfMoved extends Piece {
@ -12,16 +11,13 @@ public abstract class PieceKnownIfMoved extends Piece {
super(isWhite, startingPosition);
}
public boolean isSeen(ArrayList<Piece> pieces) {
return true;
}
@Override
public void move(Piece[][] pieces, Point toMove) {
super.move(pieces, toMove);
moved = true;
}
@Override
public boolean isMoved() {
return moved;
}

View File

@ -51,7 +51,7 @@ public class Schack {
JMenuItem showLocalIP = new JMenuItem("Show IP");
JMenuItem askForRemi = new JMenuItem("Ask for remi");
JMenuItem surrender = new JMenuItem("Surrender");
JMenuItem developerMode = new JMenuItem("Toggle Developermode");
// Actions
connectToOpponent.addActionListener((ActionEvent ae) -> {
@ -71,9 +71,7 @@ public class Schack {
surrender.addActionListener((ActionEvent ae) -> {
System.out.println("I'M FRENCH! (TODO)");
});
developerMode.addActionListener(ae -> {
board.developerMode = !board.developerMode;
});
// Add the menu stuff
frame.setJMenuBar(menuBar);
@ -83,7 +81,6 @@ public class Schack {
connectMenu.add(showLocalIP);
gameMenu.add(askForRemi);
gameMenu.add(surrender);
gameMenu.add(developerMode);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);