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

This commit is contained in:
2022-12-04 22:29:33 +01:00
parent 54dd294433
commit 36d539ff4c
5 changed files with 60 additions and 26 deletions

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 på 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;