mirror of
https://github.com/lov3b/Schack.git
synced 2024-11-10 07:00:11 +01:00
Ändra tillbaka till pjäs vet sin ikon. Ändra tillbaka tilld det överlägset snyggare sättet där varje pjäs vet sin egna ikon. Anledningen till att det byttes mot att istället ha en statisk map med samtliga ikoner, var därför att man inte kan skicka ikoner över nätverk. Detta är nu löst med att man endast skickar kordinater över nätverk som sedan görs om till en kordinat med pjäs.
This commit is contained in:
parent
ee85052295
commit
bf9d170040
24
src/main/java/com/billenius/schack/BasicMove.java
Normal file
24
src/main/java/com/billenius/schack/BasicMove.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package com.billenius.schack;
|
||||||
|
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class BasicMove implements Serializable {
|
||||||
|
public Point from;
|
||||||
|
public Point to;
|
||||||
|
|
||||||
|
public BasicMove(Point from, Point to) {
|
||||||
|
this.from = from;
|
||||||
|
this.to = to;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasicMove(Move m) {
|
||||||
|
this.from = m.from;
|
||||||
|
this.to = m.to;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return (from.x + 1) + ":" + (from.y + 1)
|
||||||
|
+ " " + '\u27F6' + " " + (to.x + 1) + ":" + (to.y + 1);
|
||||||
|
}
|
||||||
|
}
|
@ -1,25 +1,20 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import com.billenius.schack.pieces.Piece;
|
import com.billenius.schack.pieces.Piece;
|
||||||
|
|
||||||
public class Move implements Serializable {
|
public class Move extends BasicMove {
|
||||||
public Piece movedPiece;
|
public Piece movedPiece;
|
||||||
public Point from;
|
|
||||||
public Point to;
|
|
||||||
public String color;
|
|
||||||
|
|
||||||
public Move(Piece movedPiece, Point from, Point to) {
|
public Move(Piece piece, Point from, Point to) {
|
||||||
this.from = from;
|
super(from, to);
|
||||||
this.to = to;
|
this.movedPiece = piece;
|
||||||
this.color = movedPiece.isWhite() ? "White" : "Black";
|
|
||||||
this.movedPiece = movedPiece;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public Move(Piece piece, BasicMove basicMove) {
|
||||||
return (from.x + 1) + ":" + (from.y + 1)
|
super(basicMove.from, basicMove.to);
|
||||||
+ " " + '\u27F6' + " " + (to.x + 1) + ":" + (to.y + 1);
|
this.movedPiece = piece;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,21 +42,9 @@ public class Schack {
|
|||||||
// Förlåt mig fader för jag kommer synda
|
// Förlåt mig fader för jag kommer synda
|
||||||
public final static Map<String, BufferedImage> pieceIcons = new HashMap<>();
|
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 på 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 {
|
public Schack() throws IOException {
|
||||||
loadAllPieceIcons();
|
|
||||||
// Set theme
|
// Set theme
|
||||||
try {
|
try {
|
||||||
if (UIManager.getSystemLookAndFeelClassName()
|
if (UIManager.getSystemLookAndFeelClassName()
|
||||||
|
@ -7,6 +7,7 @@ import java.io.ObjectOutputStream;
|
|||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
@ -19,6 +20,7 @@ import javax.swing.JLabel;
|
|||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
import com.billenius.schack.BasicMove;
|
||||||
import com.billenius.schack.Move;
|
import com.billenius.schack.Move;
|
||||||
import com.billenius.schack.SchackState;
|
import com.billenius.schack.SchackState;
|
||||||
import com.billenius.schack.pieces.Piece;
|
import com.billenius.schack.pieces.Piece;
|
||||||
@ -79,7 +81,9 @@ public final class NetworkBoard extends Board implements Runnable {
|
|||||||
"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);
|
if (ip.equals(""))
|
||||||
|
ip = "localhost";
|
||||||
|
this.socket = new Socket(ip, 1339);
|
||||||
myColor = false; // SVART
|
myColor = false; // SVART
|
||||||
|
|
||||||
// Get input/output stream
|
// Get input/output stream
|
||||||
@ -101,7 +105,7 @@ public final class NetworkBoard extends Board implements Runnable {
|
|||||||
System.out.println("repainting");
|
System.out.println("repainting");
|
||||||
getParent().repaint();
|
getParent().repaint();
|
||||||
System.out.println("Sending move to opponent");
|
System.out.println("Sending move to opponent");
|
||||||
outputStream.writeObject(move);
|
outputStream.writeObject(new BasicMove(move));
|
||||||
System.out.println("Move sent");
|
System.out.println("Move sent");
|
||||||
|
|
||||||
SchackState state = getSchackState();
|
SchackState state = getSchackState();
|
||||||
@ -130,20 +134,6 @@ public final class NetworkBoard extends Board implements Runnable {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
@Override
|
||||||
protected void toDoIfNoPreviousPieceSelected(Piece selectedPiece) {
|
protected void toDoIfNoPreviousPieceSelected(Piece selectedPiece) {
|
||||||
if (selectedPiece != null && selectedPiece.isWhite() == myColor)
|
if (selectedPiece != null && selectedPiece.isWhite() == myColor)
|
||||||
@ -154,17 +144,18 @@ public final class NetworkBoard extends Board implements Runnable {
|
|||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
Move move = (Move) inputStream.readObject();
|
BasicMove basicMove = (BasicMove) inputStream.readObject();
|
||||||
|
Move move = new Move(pieces[basicMove.from.x][basicMove.from.y], basicMove);
|
||||||
System.out.println("Got move");
|
System.out.println("Got move");
|
||||||
moveList.addElement(move);
|
moveList.addElement(move);
|
||||||
turnCount++;
|
turnCount++;
|
||||||
|
|
||||||
System.out.println("Moving piece");
|
System.out.println("Moving piece");
|
||||||
pieces[move.from.x][move.from.y].move(pieces, move.to);
|
move.movedPiece.move(pieces, move.to);
|
||||||
System.out.println("Repainting");
|
System.out.println("Repainting");
|
||||||
getParent().repaint();
|
getParent().repaint();
|
||||||
}
|
}
|
||||||
} catch (EOFException eof) {
|
} catch (EOFException | SocketException e) {
|
||||||
JOptionPane.showMessageDialog(this, "Lost connection to opponent");
|
JOptionPane.showMessageDialog(this, "Lost connection to opponent");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -5,15 +5,13 @@ import java.awt.Point;
|
|||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import com.billenius.schack.Schack;
|
|
||||||
import com.billenius.schack.boards.Board;
|
import com.billenius.schack.boards.Board;
|
||||||
|
|
||||||
public abstract class Piece implements Serializable {
|
public abstract class Piece {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Variabel som alltid bör vara samma värde som pjäsen är i brädes av
|
* Variabel som alltid bör vara samma värde som pjäsen är i brädes av
|
||||||
@ -33,10 +31,12 @@ public abstract class Piece implements Serializable {
|
|||||||
* Nödvändigt för rockad
|
* Nödvändigt för rockad
|
||||||
*/
|
*/
|
||||||
protected boolean moved = false;
|
protected boolean moved = false;
|
||||||
|
protected BufferedImage pieceIcon;
|
||||||
|
|
||||||
public Piece(boolean white, Point startingPosition) throws IOException {
|
public Piece(boolean white, Point startingPosition) throws IOException {
|
||||||
this.isWhite = white;
|
this.isWhite = white;
|
||||||
this.position = startingPosition;
|
this.position = startingPosition;
|
||||||
|
this.pieceIcon = getPieceIcon();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Piece(boolean white) {
|
public Piece(boolean white) {
|
||||||
@ -65,6 +65,13 @@ public abstract class Piece implements Serializable {
|
|||||||
return validMoves(pieces, false);
|
return validMoves(pieces, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private BufferedImage getPieceIcon() throws IOException {
|
||||||
|
String colorName = isWhite() ? "White" : "Black";
|
||||||
|
String path = "/com/billenius/img/" + colorName + getClass().getSimpleName() + ".png";
|
||||||
|
InputStream inputStream = getClass().getResourceAsStream(path);
|
||||||
|
return ImageIO.read(inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ritar ut pjäsen baserat på den ihågkommna positionen
|
* Ritar ut pjäsen baserat på den ihågkommna positionen
|
||||||
*
|
*
|
||||||
@ -220,9 +227,7 @@ public abstract class Piece implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public BufferedImage getIcon() {
|
public BufferedImage getIcon() {
|
||||||
String className = this.getClass().getSimpleName();
|
return this.pieceIcon;
|
||||||
String colorName = this.isWhite() ? "White" : "Black";
|
|
||||||
return Schack.pieceIcons.get(colorName + className);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user