mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 12:50:10 +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.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JOptionPane;
|
||||
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 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);
|
||||
short turnCount = 0;
|
||||
private boolean whitesTurn = true;
|
||||
private DefaultListModel<Move> moveList;
|
||||
|
||||
public Board() throws IOException {
|
||||
public Board(DefaultListModel<Move> listModel) throws IOException {
|
||||
this.pieces = getPieces();
|
||||
setPreferredSize(new Dimension(800, 800));
|
||||
this.moveList = listModel;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -103,6 +115,34 @@ public class Board extends JPanel implements MouseListener {
|
||||
}
|
||||
}
|
||||
|
||||
private void makeMove(Move move) {
|
||||
move.movedPiece.move(pieces, move.to);
|
||||
turnCount++;
|
||||
whitesTurn = !whitesTurn;
|
||||
|
||||
SchackState state = getSchackState();
|
||||
switch (state) {
|
||||
case SCHACK:
|
||||
JOptionPane.showMessageDialog(this, "Du står i schack");
|
||||
break;
|
||||
case SCHACKMATT:
|
||||
case PATT:
|
||||
String stateStr = state.toString();
|
||||
String msg = stateStr.charAt(0) + stateStr.substring(1, stateStr.length()).toLowerCase();
|
||||
int choice = JOptionPane.showConfirmDialog(this, msg + "\nVill du starta om?");
|
||||
|
||||
if (choice == JOptionPane.YES_OPTION)
|
||||
try {
|
||||
restartGame();
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent mouseEvent) {
|
||||
final int mouseCoordinateX = (int) (mouseEvent.getX() / SIZE_OF_TILE);
|
||||
@ -116,31 +156,10 @@ public class Board extends JPanel implements MouseListener {
|
||||
validMovesToDraw.clear();
|
||||
return;
|
||||
}
|
||||
selectedPiece.move(pieces, clickedCoordinate);
|
||||
turnCount++;
|
||||
whitesTurn = !whitesTurn;
|
||||
|
||||
SchackState state = getSchackState();
|
||||
switch (state) {
|
||||
case SCHACK:
|
||||
JOptionPane.showMessageDialog(this, "Du står i schack");
|
||||
break;
|
||||
case SCHACKMATT:
|
||||
case PATT:
|
||||
String stateStr = state.toString();
|
||||
String msg = stateStr.charAt(0) + stateStr.substring(1, stateStr.length()).toLowerCase();
|
||||
int choice = JOptionPane.showConfirmDialog(this, msg + "\nVill du starta om?");
|
||||
|
||||
if (choice == JOptionPane.YES_OPTION)
|
||||
try {
|
||||
restartGame();
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
Move move = new Move(selectedPiece, selectedPiece.position, clickedCoordinate);
|
||||
moveList.addElement(move);
|
||||
makeMove(move);
|
||||
} else {
|
||||
previouslyClickedPoint = new Point(clickedCoordinate);
|
||||
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;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.HeadlessException;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JMenu;
|
||||
import javax.swing.JMenuBar;
|
||||
import javax.swing.JMenuItem;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JSplitPane;
|
||||
import javax.swing.UIManager;
|
||||
import java.awt.BorderLayout;
|
||||
|
||||
import com.billenius.schack.Move;
|
||||
import com.billenius.schack.PieceRenderer;
|
||||
import com.formdev.flatlaf.FlatLightLaf;
|
||||
|
||||
/**
|
||||
@ -37,11 +48,35 @@ public class Schack {
|
||||
frame = new JFrame();
|
||||
frame.setTitle("Schack");
|
||||
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
|
||||
final Board board = new Board();
|
||||
frame.setContentPane(board);
|
||||
final Board board = new Board(listModel);
|
||||
|
||||
// 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);
|
||||
|
||||
// Create menu
|
||||
@ -88,7 +123,6 @@ public class Schack {
|
||||
connectToOpponent.addActionListener((ActionEvent ae) -> {
|
||||
String opponentIP = JOptionPane.showInputDialog(null, "What's your opponents IP?");
|
||||
System.out.println("opponents ip: " + opponentIP);
|
||||
|
||||
});
|
||||
|
||||
// Add the menu stuff
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.billenius.schack;
|
||||
package com.billenius.schack.pieces;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
||||
package com.billenius.schack;
|
||||
package com.billenius.schack.pieces;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
||||
package com.billenius.schack;
|
||||
package com.billenius.schack.pieces;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
||||
package com.billenius.schack;
|
||||
package com.billenius.schack.pieces;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
@ -1,4 +1,4 @@
|
||||
package com.billenius.schack;
|
||||
package com.billenius.schack.pieces;
|
||||
|
||||
import java.awt.HeadlessException;
|
||||
import java.awt.Point;
|
||||
@ -78,9 +78,9 @@ public class Pawn extends Piece {
|
||||
private List<Point> addAttackMovesIfCan(Point pos, Piece[][] pieces) {
|
||||
List<Point> movable = new ArrayList<>();
|
||||
// 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;
|
||||
}
|
||||
|
||||
final Piece potentialEnemy = pieces[pos.x][pos.y];
|
||||
// Ifall det är tomt här, gör ingenting
|
||||
if (potentialEnemy != null && potentialEnemy.isWhite() != this.isWhite()) {
|
||||
@ -93,9 +93,8 @@ public class Pawn extends Piece {
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
Piece pieceToCheck = pieces[pos.x][pos.y];
|
||||
if (pieceToCheck == null) {
|
||||
@ -114,9 +113,8 @@ public class Pawn extends Piece {
|
||||
|
||||
// Check if the pawn has moved to the end and should be transformed
|
||||
if (this.position.y == 0 && this.isWhite()
|
||||
|| this.position.y == 7 && !this.isWhite()) {
|
||||
|| this.position.y == 7 && !this.isWhite())
|
||||
transform(pieces);
|
||||
}
|
||||
}
|
||||
|
||||
private void transform(Piece[][] pieces) throws HeadlessException {
|
@ -1,4 +1,4 @@
|
||||
package com.billenius.schack;
|
||||
package com.billenius.schack.pieces;
|
||||
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
@ -9,6 +9,8 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import com.billenius.schack.Board;
|
||||
|
||||
public abstract class Piece {
|
||||
|
||||
/**
|
||||
@ -226,4 +228,8 @@ public abstract class Piece {
|
||||
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.io.IOException;
|
@ -1,4 +1,4 @@
|
||||
package com.billenius.schack;
|
||||
package com.billenius.schack.pieces;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
Loading…
x
Reference in New Issue
Block a user