Båda rör fortfarande samma färg och det repaintas bara varannat drag

This commit is contained in:
Love 2022-12-04 22:29:33 +01:00
parent 54dd294433
commit 36d539ff4c
No known key found for this signature in database
GPG Key ID: A3C10DC241C8FA9F
5 changed files with 60 additions and 26 deletions

View File

@ -5,10 +5,15 @@ import java.awt.Dimension;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
@ -34,8 +39,24 @@ import com.formdev.flatlaf.FlatLightLaf;
public class Schack {
final JFrame frame;
// Förlåt mig fader för jag kommer synda
public final static Map<String, BufferedImage> pieceIcons = new HashMap<>();
/**
* Ladda in samtliga pjäsbilder från paketet img
*
* @throws IOException ifall det inte finns någon bild pjäsen
*/
public void loadAllPieceIcons() throws IOException {
for (String color : new String[] { "Black", "White" })
for (String piece : new String[] { "Bishop", "Horse", "King", "Pawn", "Queen", "Rook" }) {
InputStream is = getClass().getResourceAsStream("/com/billenius/img/" + color + piece + ".png");
pieceIcons.put(color + piece, ImageIO.read(is));
}
}
public Schack() throws IOException {
loadAllPieceIcons();
// Set theme
try {
if (UIManager.getSystemLookAndFeelClassName()

View File

@ -33,7 +33,7 @@ public abstract class Board extends JPanel implements MouseListener {
private final Color moveableColor = new Color(255, 191, 0);
short turnCount = 0;
protected boolean whitesTurn = true;
private DefaultListModel<Move> moveList;
protected DefaultListModel<Move> moveList;
public Board(DefaultListModel<Move> listModel) throws IOException {
this.pieces = getPieces();
@ -133,7 +133,6 @@ public abstract class Board extends JPanel implements MouseListener {
}
Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
moveList.addElement(move);
makeMove(move);
} else {
previouslyClickedPoint = new Point(clickedCoordinate);

View File

@ -74,7 +74,7 @@ public final class NetworkBoard extends Board {
"What's the IP of your opponent?",
"Schack: Networking",
JOptionPane.QUESTION_MESSAGE);
this.socket = new Socket(ip, 1339);
this.socket = new Socket("localhost", 1339);
myColor = false;
// Get input/output stream
@ -88,7 +88,10 @@ public final class NetworkBoard extends Board {
@Override
protected void makeMove(Move move) {
try {
moveList.addElement(move);
move.movedPiece.move(pieces, move.to);
getParent().repaint();
outputStream.writeObject(move);
SchackState state = getSchackState();
@ -113,13 +116,31 @@ public final class NetworkBoard extends Board {
}
move = (Move) inputStream.readObject();
move.movedPiece.move(pieces, move.to);
moveList.addElement(move);
System.out.println(move);
pieces[move.from.x][move.from.y].move(pieces, move.to);
getParent().repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
// Hitta våran lokala pjäs brädet
protected Piece getLocalFromRemote(Piece remotePiece) {
for (Piece[] row : pieces)
for (Piece localPiece : row) {
if (localPiece == null)
continue;
if (localPiece.equals(remotePiece))
return remotePiece;
}
return null;
}
@Override
protected void toDoIfNoPreviousPieceSelected(Piece selectedPiece) {
if (selectedPiece != null && selectedPiece.isWhite() == myColor)

View File

@ -17,7 +17,8 @@ public final class SameBoard extends Board {
}
@Override
protected void makeMove(Move move) {
protected void makeMove(Move move) {
moveList.addElement(move);
move.movedPiece.move(pieces, move.to);
turnCount++;
whitesTurn = !whitesTurn;

View File

@ -10,6 +10,7 @@ import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import com.billenius.schack.Schack;
import com.billenius.schack.boards.Board;
public abstract class Piece implements Serializable {
@ -27,10 +28,6 @@ public abstract class Piece implements Serializable {
* SPECIAL RULÖES APPLY TO THE KING, (ITS GOOD TO BE THE KING:)
*/
public boolean supremeRuler = false;
/**
* Bild av pjäsen som ritas ut bärdet
*/
protected BufferedImage icon;
/**
* Nödvändigt för rockad
@ -40,27 +37,12 @@ public abstract class Piece implements Serializable {
public Piece(boolean white, Point startingPosition) throws IOException {
this.isWhite = white;
this.position = startingPosition;
setPieceIcon();
}
public Piece(boolean white) {
this.isWhite = white;
}
/**
* Ladda in pjäsbild från paketet img
*
* @param className
* @throws IOException ifall det inte finns någon bild pjäsen
*/
private void setPieceIcon() throws IOException {
String className = this.getClass().getSimpleName();
String colorName = this.isWhite() ? "White" : "Black";
String fileName = colorName + className + ".png";
InputStream is = getClass().getResourceAsStream("/com/billenius/img/" + fileName);
icon = ImageIO.read(is);
}
/**
* Ger tillbaks alla ställen pjäsen kan till
*
@ -89,8 +71,9 @@ public abstract class Piece implements Serializable {
* @param g2
*/
public void draw(Graphics2D g2) {
g2.drawImage(
icon,
getIcon(),
position.x * Board.SIZE_OF_TILE,
position.y * Board.SIZE_OF_TILE,
null);
@ -212,6 +195,13 @@ public abstract class Piece implements Serializable {
return this.getClass().getSimpleName() + "{" + "position=" + position + ", isWhite=" + isWhite + '}';
}
public boolean equals(Piece otherPiece) {
return getClass().getName().equals(otherPiece.getClass().getName())
&& isWhite() == otherPiece.isWhite()
&& supremeRuler == otherPiece.supremeRuler && moved == otherPiece.moved
&& position.equals(otherPiece.position);
}
/**
*
* @return true ifall pjäsen är vit
@ -230,7 +220,9 @@ public abstract class Piece implements Serializable {
}
public BufferedImage getIcon() {
return this.icon;
String className = this.getClass().getSimpleName();
String colorName = this.isWhite() ? "White" : "Black";
return Schack.pieceIcons.get(colorName + className);
}
}