mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 21:00:11 +01:00
Merge branch 'logged'
This commit is contained in:
commit
376bed38b0
@ -10,9 +10,19 @@ import java.awt.event.MouseListener;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
|
import com.billenius.schack.pieces.Bishop;
|
||||||
|
import com.billenius.schack.pieces.Horse;
|
||||||
|
import com.billenius.schack.pieces.King;
|
||||||
|
import com.billenius.schack.pieces.Pawn;
|
||||||
|
import com.billenius.schack.pieces.Piece;
|
||||||
|
import com.billenius.schack.pieces.Queen;
|
||||||
|
import com.billenius.schack.pieces.Rook;
|
||||||
|
|
||||||
public class Board extends JPanel implements MouseListener {
|
public class Board extends JPanel implements MouseListener {
|
||||||
|
|
||||||
public static final int SIZE_OF_TILE = 100;
|
public static final int SIZE_OF_TILE = 100;
|
||||||
@ -22,10 +32,12 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
private final Color moveableColor = new Color(255, 191, 0);
|
private final Color moveableColor = new Color(255, 191, 0);
|
||||||
short turnCount = 0;
|
short turnCount = 0;
|
||||||
private boolean whitesTurn = true;
|
private boolean whitesTurn = true;
|
||||||
|
private DefaultListModel<Move> moveList;
|
||||||
|
|
||||||
public Board() throws IOException {
|
public Board(DefaultListModel<Move> listModel) throws IOException {
|
||||||
this.pieces = getPieces();
|
this.pieces = getPieces();
|
||||||
setPreferredSize(new Dimension(800, 800));
|
setPreferredSize(new Dimension(800, 800));
|
||||||
|
this.moveList = listModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -103,20 +115,8 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void makeMove(Move move) {
|
||||||
public void mousePressed(MouseEvent mouseEvent) {
|
move.movedPiece.move(pieces, move.to);
|
||||||
final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
|
|
||||||
final int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
|
|
||||||
final Point clickedCoordinate = new Point(mouseCoordinateX, mouseCoordinateY);
|
|
||||||
|
|
||||||
// Ifall vi har tryckt på en pjäs och sedan ska gå dit
|
|
||||||
if (validMovesToDraw.contains(clickedCoordinate)) {
|
|
||||||
Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
|
|
||||||
if (selectedPiece == null) {
|
|
||||||
validMovesToDraw.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
selectedPiece.move(pieces, clickedCoordinate);
|
|
||||||
turnCount++;
|
turnCount++;
|
||||||
whitesTurn = !whitesTurn;
|
whitesTurn = !whitesTurn;
|
||||||
|
|
||||||
@ -141,6 +141,25 @@ public class Board extends JPanel implements MouseListener {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mousePressed(MouseEvent mouseEvent) {
|
||||||
|
final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
|
||||||
|
final int mouseCoordinateY = (int) (mouseEvent.getY() / SIZE_OF_TILE);
|
||||||
|
final Point clickedCoordinate = new Point(mouseCoordinateX, mouseCoordinateY);
|
||||||
|
|
||||||
|
// Ifall vi har tryckt på en pjäs och sedan ska gå dit
|
||||||
|
if (validMovesToDraw.contains(clickedCoordinate)) {
|
||||||
|
Piece selectedPiece = pieces[previouslyClickedPoint.x][previouslyClickedPoint.y];
|
||||||
|
if (selectedPiece == null) {
|
||||||
|
validMovesToDraw.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
|
||||||
|
moveList.addElement(move);
|
||||||
|
makeMove(move);
|
||||||
} else {
|
} else {
|
||||||
previouslyClickedPoint = new Point(clickedCoordinate);
|
previouslyClickedPoint = new Point(clickedCoordinate);
|
||||||
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear
|
validMovesToDraw = new ArrayList<>(); // Snabbare än .clear
|
||||||
|
24
src/main/java/com/billenius/schack/Move.java
Normal file
24
src/main/java/com/billenius/schack/Move.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package com.billenius.schack;
|
||||||
|
|
||||||
|
import java.awt.Point;
|
||||||
|
|
||||||
|
import com.billenius.schack.pieces.Piece;
|
||||||
|
|
||||||
|
public class Move {
|
||||||
|
public Piece movedPiece;
|
||||||
|
public Point from;
|
||||||
|
public Point to;
|
||||||
|
public String color;
|
||||||
|
|
||||||
|
public Move(Piece movedPiece, Point from, Point to) {
|
||||||
|
this.from = from;
|
||||||
|
this.to = to;
|
||||||
|
this.color = movedPiece.isWhite() ? "White" : "Black";
|
||||||
|
this.movedPiece = movedPiece;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return (from.x + 1) + ":" + (from.y + 1)
|
||||||
|
+ " " + '\u27F6' + " " + (to.x + 1) + ":" + (to.y + 1);
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/com/billenius/schack/PieceRenderer.java
Normal file
25
src/main/java/com/billenius/schack/PieceRenderer.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package com.billenius.schack;
|
||||||
|
|
||||||
|
import java.awt.Component;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JList;
|
||||||
|
import javax.swing.ListCellRenderer;
|
||||||
|
|
||||||
|
public class PieceRenderer extends JLabel implements ListCellRenderer<Move> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Component getListCellRendererComponent(JList<? extends Move> list, Move move, int index,
|
||||||
|
boolean isSelected,
|
||||||
|
boolean cellHasFocus) {
|
||||||
|
BufferedImage image = move.movedPiece.getIcon();
|
||||||
|
ImageIcon ii = new ImageIcon(image.getScaledInstance(40, 40, java.awt.Image.SCALE_SMOOTH));
|
||||||
|
|
||||||
|
setIcon(ii);
|
||||||
|
setText(move.toString());
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +1,29 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack;
|
||||||
|
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.Font;
|
||||||
import java.awt.HeadlessException;
|
import java.awt.HeadlessException;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.NetworkInterface;
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JLabel;
|
||||||
|
import javax.swing.JList;
|
||||||
import javax.swing.JMenu;
|
import javax.swing.JMenu;
|
||||||
import javax.swing.JMenuBar;
|
import javax.swing.JMenuBar;
|
||||||
import javax.swing.JMenuItem;
|
import javax.swing.JMenuItem;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.JSplitPane;
|
||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
|
||||||
|
import com.billenius.schack.Move;
|
||||||
|
import com.billenius.schack.PieceRenderer;
|
||||||
import com.formdev.flatlaf.FlatLightLaf;
|
import com.formdev.flatlaf.FlatLightLaf;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,11 +48,35 @@ public class Schack {
|
|||||||
frame = new JFrame();
|
frame = new JFrame();
|
||||||
frame.setTitle("Schack");
|
frame.setTitle("Schack");
|
||||||
frame.setAlwaysOnTop(false);
|
frame.setAlwaysOnTop(false);
|
||||||
frame.setResizable(false);
|
frame.setResizable(true);
|
||||||
|
|
||||||
|
DefaultListModel<Move> listModel = new DefaultListModel<>();
|
||||||
|
|
||||||
// Might throw an IOException if the icon of the Pieces isn't embedded correctly
|
// Might throw an IOException if the icon of the Pieces isn't embedded correctly
|
||||||
final Board board = new Board();
|
final Board board = new Board(listModel);
|
||||||
frame.setContentPane(board);
|
|
||||||
|
// Logger
|
||||||
|
final JList<Move> jlist = new JList<>(listModel);
|
||||||
|
jlist.setVisible(true);
|
||||||
|
jlist.setCellRenderer(new PieceRenderer());
|
||||||
|
JScrollPane scrollPane = new JScrollPane(jlist);
|
||||||
|
// scrollPane.setPreferredSize(new Dimension(120, 700));
|
||||||
|
JPanel logPanel = new JPanel(new BorderLayout());
|
||||||
|
logPanel.setPreferredSize(new Dimension(120, 800));
|
||||||
|
|
||||||
|
JLabel infoText = new JLabel("Moves");
|
||||||
|
infoText.setFont(new Font("Cantarell", Font.PLAIN, 18));
|
||||||
|
|
||||||
|
infoText.setHorizontalAlignment(JLabel.CENTER);
|
||||||
|
logPanel.add(infoText, BorderLayout.NORTH);
|
||||||
|
logPanel.add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
|
||||||
|
splitPane.setDividerLocation(800);
|
||||||
|
splitPane.setLeftComponent(board);
|
||||||
|
splitPane.setRightComponent(logPanel);
|
||||||
|
|
||||||
|
frame.setContentPane(splitPane);
|
||||||
frame.getContentPane().addMouseListener(board);
|
frame.getContentPane().addMouseListener(board);
|
||||||
|
|
||||||
// Create menu
|
// Create menu
|
||||||
@ -88,7 +123,6 @@ public class Schack {
|
|||||||
connectToOpponent.addActionListener((ActionEvent ae) -> {
|
connectToOpponent.addActionListener((ActionEvent ae) -> {
|
||||||
String opponentIP = JOptionPane.showInputDialog(null, "What's your opponents IP?");
|
String opponentIP = JOptionPane.showInputDialog(null, "What's your opponents IP?");
|
||||||
System.out.println("opponents ip: " + opponentIP);
|
System.out.println("opponents ip: " + opponentIP);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add the menu stuff
|
// Add the menu stuff
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack.pieces;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack.pieces;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack.pieces;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack.pieces;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack.pieces;
|
||||||
|
|
||||||
import java.awt.HeadlessException;
|
import java.awt.HeadlessException;
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -78,9 +78,9 @@ public class Pawn extends Piece {
|
|||||||
private List<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) {
|
private List<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) {
|
||||||
List<Point> movable = new ArrayList<>();
|
List<Point> movable = new ArrayList<>();
|
||||||
// Se till att vi inte är utanför brädet
|
// Se till att vi inte är utanför brädet
|
||||||
if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0) {
|
if (pos.x >= pieces.length || pos.x < 0 || pos.y >= pieces[0].length || pos.y < 0)
|
||||||
return movable;
|
return movable;
|
||||||
}
|
|
||||||
final Piece potentialEnemy = pieces[pos.x][pos.y];
|
final Piece potentialEnemy = pieces[pos.x][pos.y];
|
||||||
// Ifall det är tomt här, gör ingenting
|
// Ifall det är tomt här, gör ingenting
|
||||||
if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) {
|
if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) {
|
||||||
@ -93,9 +93,8 @@ public class Pawn extends Piece {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean addMovesIfCan(Point pos, List<Point> movable, Piece[][] pieces, boolean allowedToRecurse) {
|
protected boolean addMovesIfCan(Point pos, List<Point> movable, Piece[][] pieces, boolean allowedToRecurse) {
|
||||||
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
|
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7)
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
Piece pieceToCheck = pieces[pos.x][pos.y];
|
Piece pieceToCheck = pieces[pos.x][pos.y];
|
||||||
if (pieceToCheck == null) {
|
if (pieceToCheck == null) {
|
||||||
@ -114,10 +113,9 @@ public class Pawn extends Piece {
|
|||||||
|
|
||||||
// Check if the pawn has moved to the end and should be transformed
|
// Check if the pawn has moved to the end and should be transformed
|
||||||
if (this.position.y == 0 && this.isWhite()
|
if (this.position.y == 0 && this.isWhite()
|
||||||
|| this.position.y == 7 && !this.isWhite()) {
|
|| this.position.y == 7 && !this.isWhite())
|
||||||
transform(pieces);
|
transform(pieces);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void transform(Piece[][] pieces) throws HeadlessException {
|
private void transform(Piece[][] pieces) throws HeadlessException {
|
||||||
String[] transformations = { "Queen", "Rook", "Bishop", "Horse" };
|
String[] transformations = { "Queen", "Rook", "Bishop", "Horse" };
|
@ -1,4 +1,4 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack.pieces;
|
||||||
|
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -9,6 +9,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import com.billenius.schack.Board;
|
||||||
|
|
||||||
public abstract class Piece {
|
public abstract class Piece {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -226,4 +228,8 @@ public abstract class Piece {
|
|||||||
return moved;
|
return moved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BufferedImage getIcon() {
|
||||||
|
return this.icon;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack.pieces;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
|||||||
package com.billenius.schack;
|
package com.billenius.schack.pieces;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
Loading…
x
Reference in New Issue
Block a user