varje spelare går med separat lag, nu är det bara repaint

This commit is contained in:
Love 2022-12-04 22:59:31 +01:00
parent 36d539ff4c
commit 38d4cfa0a3
No known key found for this signature in database
GPG Key ID: A3C10DC241C8FA9F
2 changed files with 15 additions and 12 deletions

View File

@ -95,6 +95,10 @@ public abstract class Board extends JPanel implements MouseListener {
g2.fillOval(validMove.x * SIZE_OF_TILE, validMove.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE); g2.fillOval(validMove.x * SIZE_OF_TILE, validMove.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE);
} }
if (turnCount > 0) {
boolean b = true;
}
// Måla alla pjäser // Måla alla pjäser
for (Piece[] pieceArr : pieces) for (Piece[] pieceArr : pieces)
for (Piece piece : pieceArr) { for (Piece piece : pieceArr) {
@ -143,11 +147,6 @@ public abstract class Board extends JPanel implements MouseListener {
if (!validMovesToDraw.contains(clickedCoordinate)) { if (!validMovesToDraw.contains(clickedCoordinate)) {
Piece selectedPiece = pieces[clickedCoordinate.x][clickedCoordinate.y]; Piece selectedPiece = pieces[clickedCoordinate.x][clickedCoordinate.y];
toDoIfNoPreviousPieceSelected(selectedPiece); toDoIfNoPreviousPieceSelected(selectedPiece);
if (selectedPiece != null && selectedPiece.isWhite() == whitesTurn)
validMovesToDraw.addAll(selectedPiece.validMoves(pieces, true));
else
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear
} else } else
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear validMovesToDraw = new ArrayList<>(); // Snabbare än .clear

View File

@ -41,16 +41,17 @@ public final class NetworkBoard extends Board {
options, options,
options[0]); options[0]);
// Nu ska vi bli en Server
if (choosenOption == 0) { if (choosenOption == 0) {
String ip = Inet4Address.getLocalHost().toString(); String ip = Inet4Address.getLocalHost().getHostAddress();
final JFrame frame = new JFrame("Schack: Networking"); final JFrame frame = new JFrame("Schack: Networking");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout()); JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(200, 200)); panel.setPreferredSize(new Dimension(600, 200));
JLabel loading = new JLabel( JLabel loading = new JLabel(
"Waiting for opponent to connect...\nYour ip is " + ip + " Listening on port 1339"); "<html>Waiting for opponent to connect...<br/>Listening on <i>" + ip + ":1339</i></html>");
loading.setFont(new Font("Cantarell", Font.PLAIN, 24)); loading.setFont(new Font("Cantarell", Font.PLAIN, 24));
loading.setHorizontalAlignment(JLabel.CENTER); loading.setHorizontalAlignment(JLabel.CENTER);
@ -61,7 +62,7 @@ public final class NetworkBoard extends Board {
serverSocket = new ServerSocket(1339); serverSocket = new ServerSocket(1339);
this.socket = serverSocket.accept(); this.socket = serverSocket.accept();
// myColor = new Random().nextBoolean(); // myColor = new Random().nextBoolean();
myColor = true; myColor = true; // VIT
frame.setVisible(false); frame.setVisible(false);
// Get input/output stream // Get input/output stream
@ -69,13 +70,15 @@ public final class NetworkBoard extends Board {
inputStream = new ObjectInputStream(socket.getInputStream()); inputStream = new ObjectInputStream(socket.getInputStream());
System.out.println("Getting outputstream"); System.out.println("Getting outputstream");
outputStream = new ObjectOutputStream(socket.getOutputStream()); outputStream = new ObjectOutputStream(socket.getOutputStream());
} else { }
// Nu ska vi bli en klient
else {
String ip = JOptionPane.showInputDialog(null, String ip = JOptionPane.showInputDialog(null,
"What's the IP of your opponent?", "What's the IP of your opponent?",
"Schack: Networking", "Schack: Networking",
JOptionPane.QUESTION_MESSAGE); JOptionPane.QUESTION_MESSAGE);
this.socket = new Socket("localhost", 1339); this.socket = new Socket("localhost", 1339);
myColor = false; myColor = false; // SVART
// Get input/output stream // Get input/output stream
System.out.println("Getting outputstream"); System.out.println("Getting outputstream");
@ -88,8 +91,8 @@ public final class NetworkBoard extends Board {
@Override @Override
protected void makeMove(Move move) { protected void makeMove(Move move) {
try { try {
turnCount++;
moveList.addElement(move); moveList.addElement(move);
move.movedPiece.move(pieces, move.to); move.movedPiece.move(pieces, move.to);
getParent().repaint(); getParent().repaint();
outputStream.writeObject(move); outputStream.writeObject(move);
@ -117,6 +120,7 @@ public final class NetworkBoard extends Board {
move = (Move) inputStream.readObject(); move = (Move) inputStream.readObject();
moveList.addElement(move); moveList.addElement(move);
turnCount++;
System.out.println(move); System.out.println(move);
pieces[move.from.x][move.from.y].move(pieces, move.to); pieces[move.from.x][move.from.y].move(pieces, move.to);