commit
46a2ff98f2
19
.classpath
19
.classpath
|
|
@ -36,7 +36,22 @@
|
|||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="lib" path="com.jgoodies.common_1.8.1.v20240327-0800.jar"/>
|
||||
<classpathentry kind="lib" path="com.jgoodies.forms_1.9.0.v20240327-0800.jar"/>
|
||||
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
||||
|
|
|
|||
13
.project
13
.project
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Schach</name>
|
||||
<name>SchachMVN</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
|
|
@ -20,4 +20,15 @@
|
|||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1750157105125</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
package de.mannheim.th.chess.controller;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import com.github.bhlangonijr.chesslib.move.Move;
|
||||
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
import de.mannheim.th.chess.ui.SpielFrame.BoardMode;
|
||||
|
||||
public class ButtonMovePieceListener implements ActionListener {
|
||||
private SpielFrame sf;
|
||||
private Game game;
|
||||
private Move mv;
|
||||
|
||||
public ButtonMovePieceListener(SpielFrame sf, Game game, Move mv) {
|
||||
this.sf = sf;
|
||||
this.game = game;
|
||||
this.mv = mv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (game.isPromotionMove(this.mv))
|
||||
game.doPromotionMove(this.sf.showPromotion(), mv.getFrom(), mv.getTo());
|
||||
else
|
||||
this.game.playMove(this.mv);
|
||||
|
||||
if (this.game.isDraw()) {
|
||||
this.game.stopClock();
|
||||
this.sf.setBoardMode(BoardMode.finished);
|
||||
this.sf.showDraw();
|
||||
} else if (this.game.isMate()) {
|
||||
this.game.stopClock();
|
||||
this.sf.setBoardMode(BoardMode.finished);
|
||||
this.sf.showWin(game.getActivePlayer());
|
||||
}
|
||||
this.sf.setBoardMode(BoardMode.normal);
|
||||
this.sf.setCursor(null);
|
||||
this.sf.erstelleBrett();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package de.mannheim.th.chess.controller;
|
||||
|
||||
import java.awt.Cursor;
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import com.github.bhlangonijr.chesslib.Square;
|
||||
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
|
||||
public class ButtonSelectPieceListener implements ActionListener {
|
||||
private SpielFrame sf;
|
||||
private Square selectedSquare;
|
||||
|
||||
public ButtonSelectPieceListener(SpielFrame sf, Square sq) {
|
||||
this.sf = sf;
|
||||
this.selectedSquare = sq;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
sf.setBoardMode(SpielFrame.BoardMode.pieceSelected);
|
||||
sf.setSelectedSquare(this.selectedSquare);
|
||||
|
||||
String symbolChoosed = sf.getBelegung().get(e.getSource());
|
||||
|
||||
// setzt cursor auf spielfigur für die animation
|
||||
String pfad = "src/main/resources/" + (int) symbolChoosed.toCharArray()[2] + ".png";
|
||||
|
||||
// Bild laden und Cursor im gesamten Frame setzen
|
||||
Image image = Toolkit.getDefaultToolkit().getImage(pfad);
|
||||
Image scaled = image.getScaledInstance(32, 32, Image.SCALE_SMOOTH);
|
||||
Cursor figurCursor = Toolkit.getDefaultToolkit().createCustomCursor(scaled, new Point(0, 0),
|
||||
"figurCursor");
|
||||
sf.setCursor(figurCursor);
|
||||
|
||||
sf.erstelleBrett();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package de.mannheim.th.chess.controller;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import de.mannheim.th.chess.ui.SpielFrame;
|
||||
import de.mannheim.th.chess.ui.SpielFrame.BoardMode;
|
||||
|
||||
public class ButtonToNormalListener implements ActionListener {
|
||||
private SpielFrame sf;
|
||||
|
||||
public ButtonToNormalListener(SpielFrame sf) {
|
||||
this.sf = sf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
this.sf.setBoardMode(BoardMode.normal);
|
||||
this.sf.setSelectedSquare(null);
|
||||
this.sf.setCursor(null);
|
||||
this.sf.erstelleBrett();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -4,9 +4,13 @@ import java.util.List;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
import com.github.bhlangonijr.chesslib.Board;
|
||||
import com.github.bhlangonijr.chesslib.Piece;
|
||||
import com.github.bhlangonijr.chesslib.Rank;
|
||||
import com.github.bhlangonijr.chesslib.Side;
|
||||
import com.github.bhlangonijr.chesslib.Square;
|
||||
import com.github.bhlangonijr.chesslib.move.Move;
|
||||
import com.github.bhlangonijr.chesslib.move.MoveList;
|
||||
import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
|
||||
|
||||
import de.mannheim.th.chess.utl.Clock;
|
||||
|
||||
|
|
@ -17,8 +21,7 @@ import de.mannheim.th.chess.utl.Clock;
|
|||
public class Game {
|
||||
|
||||
private Board board;
|
||||
private Clock clockPlayer1;
|
||||
private Clock clockPlayer2;
|
||||
private Clock clock;
|
||||
|
||||
private MoveList movelist;
|
||||
|
||||
|
|
@ -30,8 +33,8 @@ public class Game {
|
|||
|
||||
this.movelist = new MoveList();
|
||||
|
||||
// this.clockPlayer1 = new Clock();
|
||||
// this.clockPlayer2 = new Clock();
|
||||
clock = new Clock("blitz");
|
||||
clock.start();
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -76,6 +79,7 @@ public class Game {
|
|||
public void playMove(Move move) {
|
||||
this.board.doMove(move);
|
||||
this.movelist.add(move);
|
||||
clock.pressClock();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -88,6 +92,22 @@ public class Game {
|
|||
Move move = new Move(origin, desination);
|
||||
this.board.doMove(move);
|
||||
this.movelist.add(move);
|
||||
|
||||
}
|
||||
|
||||
public boolean isMate() {
|
||||
return board.isMated();
|
||||
}
|
||||
|
||||
public boolean isDraw() {
|
||||
return board.isDraw();
|
||||
}
|
||||
|
||||
public int getActivePlayer() {
|
||||
if (board.getSideToMove() == Side.WHITE) {
|
||||
return 1;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -104,7 +124,82 @@ public class Game {
|
|||
|
||||
}
|
||||
|
||||
public void stopClock() {
|
||||
clock.endGame();
|
||||
}
|
||||
|
||||
public boolean isPromotionMove(Move move) {
|
||||
return ((move.getTo().getRank().equals(Rank.RANK_8) || move.getTo().getRank().equals(Rank.RANK_1)) &&
|
||||
(board.getPiece(move.getFrom()) == Piece.BLACK_PAWN || board.getPiece(move.getFrom()) == Piece.WHITE_PAWN));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a list of all legal moveable squares from the current board state.
|
||||
*
|
||||
* @return a List of Square objects representing all legal moveable squares.
|
||||
*/
|
||||
public List<Square> getAllLegalMoveableSquares() {
|
||||
return this.board.legalMoves().stream()
|
||||
.map(move -> move.getFrom())
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a list of legal moveable squares for a given square.
|
||||
*
|
||||
* @param square the Square from which to retrieve legal moveable squares
|
||||
* @return a List of Square objects representing the legal moveable squares
|
||||
* from the specified square.
|
||||
*/
|
||||
public List<Square> getLegalMoveableSquares(Square square) {
|
||||
return this.board.legalMoves().stream()
|
||||
.filter(move -> move.getFrom() == square)
|
||||
.map(move -> move.getTo())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public void doPromotionMove(int piece, Square origin, Square destination) {
|
||||
System.out.println(piece);
|
||||
Piece promotedTo;
|
||||
switch (piece) {
|
||||
case 7:
|
||||
promotedTo = Piece.BLACK_KNIGHT;
|
||||
break;
|
||||
case 4:
|
||||
promotedTo = Piece.BLACK_QUEEN;
|
||||
break;
|
||||
case 5:
|
||||
promotedTo = Piece.BLACK_ROOK;
|
||||
break;
|
||||
case 6:
|
||||
promotedTo = Piece.BLACK_BISHOP;
|
||||
break;
|
||||
case 3:
|
||||
promotedTo = Piece.WHITE_KNIGHT;
|
||||
break;
|
||||
case 0:
|
||||
promotedTo = Piece.WHITE_QUEEN;
|
||||
break;
|
||||
case 1:
|
||||
promotedTo = Piece.WHITE_ROOK;
|
||||
break;
|
||||
case 2:
|
||||
promotedTo = Piece.WHITE_BISHOP;
|
||||
break;
|
||||
default:
|
||||
promotedTo = Piece.WHITE_QUEEN;
|
||||
}
|
||||
Move promotionMove = new Move(origin, destination, promotedTo);
|
||||
playMove(promotionMove);
|
||||
}
|
||||
|
||||
public String toFEN() {
|
||||
board.toString();
|
||||
return board.getFen();
|
||||
}
|
||||
|
||||
public Square getSelectedSquare() {
|
||||
return this.getSelectedSquare();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,11 @@
|
|||
package de.mannheim.th.chess.ui;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
|
|
@ -20,10 +16,8 @@ import javax.swing.BoxLayout;
|
|||
import javax.swing.JButton;
|
||||
import javax.swing.JFileChooser;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Component;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Color;
|
||||
|
||||
public class MainFrame extends JFrame {
|
||||
|
|
@ -33,22 +27,6 @@ public class MainFrame extends JFrame {
|
|||
private static final long serialVersionUID = 1L;
|
||||
private JPanel contentPane;
|
||||
|
||||
/**
|
||||
* Launch the application.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
MainFrame frame = new MainFrame();
|
||||
frame.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -3,13 +3,15 @@ package de.mannheim.th.chess.ui;
|
|||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import com.github.bhlangonijr.chesslib.Board;
|
||||
import com.github.bhlangonijr.chesslib.File;
|
||||
import com.github.bhlangonijr.chesslib.Rank;
|
||||
import com.github.bhlangonijr.chesslib.Piece;
|
||||
import com.github.bhlangonijr.chesslib.Square;
|
||||
import com.github.bhlangonijr.chesslib.move.Move;
|
||||
|
||||
import de.mannheim.th.chess.App;
|
||||
import de.mannheim.th.chess.domain.Game;
|
||||
import de.mannheim.th.chess.controller.ButtonMovePieceListener;
|
||||
import de.mannheim.th.chess.controller.ButtonSelectPieceListener;
|
||||
import de.mannheim.th.chess.controller.ButtonToNormalListener;
|
||||
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.Font;
|
||||
|
|
@ -17,56 +19,34 @@ import java.awt.Font;
|
|||
import javax.swing.BorderFactory;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JDialog;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JSplitPane;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Cursor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.Image;
|
||||
import java.awt.Point;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class SpielFrame extends JFrame {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(App.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private JPanel contentPane;
|
||||
private ArrayList<JButton> buttons = new ArrayList<>();
|
||||
private List<Integer> clickableButtons = new ArrayList<>();
|
||||
private HashMap<JButton, Integer> positions = new HashMap<>();
|
||||
private HashMap<JButton, String> belegungen = new HashMap<>();
|
||||
private HashMap<JButton, Color> farben = new HashMap<>();
|
||||
private JPanel panelLinks, panelRechts;
|
||||
private JPanel panelLinks, panelRechts, contentPane;
|
||||
private Game game;
|
||||
private String symbolChoosed;
|
||||
private JButton buttonChoosed;
|
||||
private boolean playerWhite = true;
|
||||
private boolean moveFinished = false;
|
||||
|
||||
/**
|
||||
* Launch the application. Die Main-Methode für den WindowBuilder.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
SpielFrame frame = new SpielFrame();
|
||||
frame.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
private BoardMode mode;
|
||||
private Square selectedSquare;
|
||||
|
||||
public enum BoardMode {
|
||||
normal, pieceSelected, finished
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -75,13 +55,14 @@ public class SpielFrame extends JFrame {
|
|||
public SpielFrame() {
|
||||
|
||||
game = new Game();
|
||||
mode = BoardMode.normal;
|
||||
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 1920, 1080);
|
||||
setTitle("Schach");
|
||||
setAlwaysOnTop(true);
|
||||
|
||||
JPanel contentPane = new JPanel();
|
||||
contentPane = new JPanel();
|
||||
contentPane.setLayout(new BorderLayout());
|
||||
setContentPane(contentPane);
|
||||
|
||||
|
|
@ -104,174 +85,62 @@ public class SpielFrame extends JFrame {
|
|||
setVisible(true);
|
||||
}
|
||||
|
||||
public void setBoardMode(BoardMode bm) {
|
||||
this.mode = bm;
|
||||
}
|
||||
|
||||
public void setSelectedSquare(Square sq) {
|
||||
this.selectedSquare = sq;
|
||||
}
|
||||
|
||||
public HashMap<JButton, String> getBelegung() {
|
||||
return this.belegungen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt alle Buttons und fügt sie dem Frame hinzu.
|
||||
*/
|
||||
private void erstelleBrett() {
|
||||
for (int i = 0; i < 64; i++) {
|
||||
JButton b = new JButton();
|
||||
b.setFocusPainted(false);
|
||||
b.setFont(new Font("Arial", Font.PLAIN, 30));
|
||||
public void erstelleBrett() {
|
||||
|
||||
if ((i / 8 + i % 8) % 2 == 0) {
|
||||
logger.info("Helles Feld erstellt." + i);
|
||||
b.setBackground(new Color(90, 90, 90));
|
||||
} else {
|
||||
logger.info("Dunkles Feld erstellt." + i);
|
||||
b.setBackground(new Color(65, 65, 65));
|
||||
}
|
||||
|
||||
b.setForeground(Color.WHITE);
|
||||
b.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
|
||||
b.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
// setzt alle Roten Felder zurück
|
||||
for (JButton b : farben.keySet()) {
|
||||
|
||||
if (b.getBackground().equals(new Color(230, 100, 100))) {
|
||||
|
||||
b.setBackground(farben.get(b));
|
||||
}
|
||||
}
|
||||
|
||||
// wenn weiß dran
|
||||
if (playerWhite) {
|
||||
|
||||
// wenn gerade Figur ausgewählt wird...
|
||||
buttonChoosed = (JButton) e.getSource();
|
||||
symbolChoosed = belegungen.get(buttonChoosed);
|
||||
|
||||
// setzt cursor auf spielfigur für die animation
|
||||
String pfad = "src/main/resources/" + (int) symbolChoosed.toCharArray()[2] + ".png";
|
||||
|
||||
// Bild laden und Cursor im gesamten Frame setzen
|
||||
Image image = Toolkit.getDefaultToolkit().getImage(pfad);
|
||||
Image scaled = image.getScaledInstance(32, 32, Image.SCALE_SMOOTH);
|
||||
Cursor figurCursor = Toolkit.getDefaultToolkit().createCustomCursor(scaled, new Point(0, 0),
|
||||
"figurCursor");
|
||||
setCursor(figurCursor);
|
||||
|
||||
// filtert möglichen Züge heraus
|
||||
int position = positions.get(buttonChoosed);
|
||||
|
||||
clickableButtons = game
|
||||
.getLegalMoves(
|
||||
Square.encode(Rank.allRanks[7 - position / 8], File.allFiles[position % 8]))
|
||||
.stream().peek(System.out::println).map(m -> m.getTo()).peek(System.out::println)
|
||||
.map(s -> 56 - s.getRank().ordinal() * 8 + s.getFile().ordinal())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// filtert mögliche Züge und nicht mögliche Züge in eine Map aus Listen
|
||||
Map<Boolean, List<JButton>> buttonsSeperated = buttons.stream()
|
||||
.collect(Collectors.partitioningBy(b -> clickableButtons.contains(buttons.indexOf(b))));
|
||||
|
||||
for (Boolean list : buttonsSeperated.keySet()) {
|
||||
|
||||
if (list) {
|
||||
// alle möglichen felder rot markieren
|
||||
for (JButton b : positions.keySet()) {
|
||||
// wenn button ein möglicher zug ist
|
||||
if (clickableButtons.contains(positions.get(b))) {
|
||||
farben.put(b, b.getBackground()); // damit sich gemerkt werden kann welches feld welche farbe vorher
|
||||
// hatte
|
||||
b.setBackground(new Color(230, 100, 100));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// den rest der buttons ausser die möglichen züge deaktivieren
|
||||
List<JButton> andere = buttonsSeperated.get(list);
|
||||
|
||||
for (JButton b : andere) {
|
||||
if (!belegungen.get(b).split("-")[0].equals("w")) {
|
||||
b.setEnabled(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// alle weisen squares deaktivieren, damit dannach klar ist wer dran ist
|
||||
for (JButton b : belegungen.keySet()) {
|
||||
if (belegungen.get(b).split("-")[0].equals("b")) {
|
||||
b.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// wenn gerade Figur ausgewählt wird...
|
||||
buttonChoosed = (JButton) e.getSource();
|
||||
symbolChoosed = belegungen.get(buttonChoosed);
|
||||
// System.out.println(symbolChoosed+" wurde gewählt.");
|
||||
// setzt cursor auf spielfigur für die animation
|
||||
String pfad = "src/main/resources/" + (int) symbolChoosed.toCharArray()[2] + ".png";
|
||||
|
||||
// Bild laden und Cursor im gesamten Frame setzen
|
||||
Image image = Toolkit.getDefaultToolkit().getImage(pfad);
|
||||
Image scaled = image.getScaledInstance(32, 32, Image.SCALE_SMOOTH);
|
||||
Cursor figurCursor = Toolkit.getDefaultToolkit().createCustomCursor(scaled, new Point(0, 0),
|
||||
"figurCursor");
|
||||
setCursor(figurCursor);
|
||||
|
||||
// filtert möglichen Züge heraus
|
||||
int position = positions.get(buttonChoosed);
|
||||
List<Integer> clickableButtons = new ArrayList<>();
|
||||
clickableButtons = game
|
||||
.getLegalMoves(
|
||||
Square.encode(Rank.allRanks[7 - position / 8], File.allFiles[position % 8]))
|
||||
.stream().peek(System.out::println).map(m -> m.getTo()).peek(System.out::println)
|
||||
.map(s -> 56 - s.getRank().ordinal() * 8 + s.getFile().ordinal())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (JButton b : positions.keySet()) {
|
||||
// wenn button ein möglicher zug ist
|
||||
if (clickableButtons.contains(positions.get(b))) {
|
||||
b.setBackground(new Color(230, 100, 100));
|
||||
}
|
||||
}
|
||||
|
||||
// alle schwarzen squares deaktivieren, damit dannach klar ist wer dran ist
|
||||
for (JButton b : belegungen.keySet()) {
|
||||
if (belegungen.get(b).split("-")[0].equals("w")) {
|
||||
b.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// alle anderen Buttons nicht ckickbar zu machen
|
||||
|
||||
// Button Icon zurücksetzen
|
||||
|
||||
// Buttonposition merken (in MoveListe oder so)
|
||||
|
||||
// wenn Button platzierd werden soll...
|
||||
|
||||
// neuen Button in Moveliste eintragen
|
||||
|
||||
// Icon ändern
|
||||
|
||||
// Modus auf auswählen setzen und spielerwechsel markieren
|
||||
|
||||
// spielerwechsel
|
||||
if (moveFinished)
|
||||
playerWhite = !playerWhite;
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
panelLinks.add(b);
|
||||
buttons.add(b);
|
||||
positions.put(b, i);
|
||||
|
||||
}
|
||||
this.clearButtons();
|
||||
this.setDefaultBackground();
|
||||
this.setButtonsActions();
|
||||
|
||||
ladeBrett();
|
||||
|
||||
panelLinks.revalidate();
|
||||
panelLinks.repaint();
|
||||
|
||||
// // Bild laden und Cursor im gesamten Frame setzen
|
||||
// Image image = Toolkit.getDefaultToolkit().getImage(pfad);
|
||||
// Image scaled = image.getScaledInstance(32, 32, Image.SCALE_SMOOTH);
|
||||
// Cursor figurCursor = Toolkit.getDefaultToolkit().createCustomCursor(scaled,
|
||||
// new Point(0, 0),
|
||||
// "figurCursor");
|
||||
// setCursor(figurCursor);
|
||||
|
||||
// }else
|
||||
// {
|
||||
//
|
||||
// // wenn gerade Figur ausgewählt wird...
|
||||
// buttonChoosed = (JButton) e.getSource();
|
||||
// symbolChoosed = belegungen.get(buttonChoosed);
|
||||
// // System.out.println(symbolChoosed+" wurde gewählt.");
|
||||
// // setzt cursor auf spielfigur für die animation
|
||||
// String pfad = "src/main/resources/" + (int) symbolChoosed.toCharArray()[2] +
|
||||
// ".png";
|
||||
//
|
||||
// // Bild laden und Cursor im gesamten Frame setzen
|
||||
// Image image = Toolkit.getDefaultToolkit().getImage(pfad);
|
||||
// Image scaled = image.getScaledInstance(32, 32, Image.SCALE_SMOOTH);
|
||||
// Cursor figurCursor = Toolkit.getDefaultToolkit().createCustomCursor(scaled,
|
||||
// new Point(0, 0),
|
||||
// "figurCursor");
|
||||
// setCursor(figurCursor);
|
||||
}
|
||||
|
||||
private int mirrowedGrid(int i) {
|
||||
return 63 - (((i / 8) * 8) + (7 - i % 8));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -287,7 +156,8 @@ public class SpielFrame extends JFrame {
|
|||
int leerfelder = Character.getNumericValue(fen[j]);
|
||||
for (int k = 0; k < leerfelder; k++) {
|
||||
belegungen.put(buttons.get(i), "n-n");
|
||||
buttons.get(i).setEnabled(false); // erstmal deaktivieren, weil leere Felder nicht ckickbar sein sollten.
|
||||
// buttons.get(i).setEnabled(false); // erstmal deaktivieren, weil leere Felder
|
||||
// nicht ckickbar sein sollten.
|
||||
i++;
|
||||
}
|
||||
continue;
|
||||
|
|
@ -295,12 +165,159 @@ public class SpielFrame extends JFrame {
|
|||
belegungen.put(buttons.get(i), "w-" + fen[j]);
|
||||
} else if (fen[j] >= 97 && fen[j] <= 122) { // ein Kleinbuchstabe, also
|
||||
belegungen.put(buttons.get(i), "b-" + fen[j]);
|
||||
buttons.get(i).setEnabled(false); // erstmal deaktivieren, damit weiß beginnen kann
|
||||
// buttons.get(i).setEnabled(false); // erstmal deaktivieren, damit weiß
|
||||
// beginnen kann
|
||||
}
|
||||
buttons.get(i).setIcon(new ImageIcon("src/main/resources/" + (int) fen[j] + ".png"));
|
||||
buttons.get(i).setDisabledIcon(new ImageIcon("src/main/resources/" + (int) fen[j] + ".png"));
|
||||
|
||||
i++;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the existing buttons from the button list, panellinks and fills them
|
||||
* with new blank ones.
|
||||
*/
|
||||
private void clearButtons() {
|
||||
buttons.clear();
|
||||
panelLinks.removeAll();
|
||||
|
||||
for (int i = 0; i < 64; i++) {
|
||||
JButton b = new JButton();
|
||||
|
||||
b.setEnabled(false);
|
||||
|
||||
// style
|
||||
b.setFocusPainted(false);
|
||||
b.setFont(new Font("Arial", Font.PLAIN, 30));
|
||||
b.setForeground(Color.WHITE);
|
||||
b.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
|
||||
b.setName(i + "");
|
||||
|
||||
buttons.add(b);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default background color for the buttons in the grid.
|
||||
*/
|
||||
private void setDefaultBackground() {
|
||||
for (int i = 0; i < 64; i++) {
|
||||
JButton b = buttons.get(i);
|
||||
if ((i / 8 + i % 8) % 2 == 0) {
|
||||
logger.info("Helles Feld erstellt." + i);
|
||||
b.setBackground(new Color(90, 90, 90));
|
||||
} else {
|
||||
logger.info("Dunkles Feld erstellt." + i);
|
||||
b.setBackground(new Color(65, 65, 65));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Switches the button actions depending on the boardmode
|
||||
*/
|
||||
private void setButtonsActions() {
|
||||
|
||||
List<Square> selectables;
|
||||
|
||||
switch (this.mode) {
|
||||
case BoardMode.normal:
|
||||
selectables = game.getAllLegalMoveableSquares();
|
||||
|
||||
for (Square square : selectables) {
|
||||
JButton b = buttons.get(mirrowedGrid(square.ordinal()));
|
||||
b.setEnabled(true);
|
||||
// b.setBackground(Color.green);
|
||||
b.addActionListener(new ButtonSelectPieceListener(this, square));
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case BoardMode.pieceSelected:
|
||||
|
||||
JButton s = buttons.get(mirrowedGrid(selectedSquare.ordinal()));
|
||||
s.setEnabled(true);
|
||||
s.setBackground(new Color(165, 42, 42));
|
||||
s.addActionListener(new ButtonToNormalListener(this)); // cancel action
|
||||
|
||||
selectables = game.getLegalMoveableSquares(selectedSquare);
|
||||
|
||||
for (Square square : selectables) {
|
||||
JButton b = buttons.get(mirrowedGrid(square.ordinal()));
|
||||
final Move move = new Move(selectedSquare, square);
|
||||
b.setEnabled(true);
|
||||
b.setBackground(new Color(230, 100, 100));
|
||||
b.addActionListener(new ButtonMovePieceListener(this, this.game, move));
|
||||
}
|
||||
break;
|
||||
case finished:
|
||||
clearButtons();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
for (JButton b : buttons) {
|
||||
panelLinks.add(b);
|
||||
}
|
||||
}
|
||||
|
||||
public void showDraw() {
|
||||
JFrame frame = new JFrame("Result");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(300, 150);
|
||||
frame.setLayout(null);
|
||||
|
||||
// JLabel jl = new JLabel(String.format("%d - %d", player / 2, player % 2));
|
||||
// jl.setBounds(50, 30, 200, 25);
|
||||
// jl.setFont(new Font("Tahoma", Font.BOLD, 20));
|
||||
// frame.add(jl);
|
||||
// frame.setVisible(true);
|
||||
}
|
||||
|
||||
public int showPromotion() {
|
||||
final int[] result = { -1 };
|
||||
|
||||
JDialog dialog = new JDialog(this, "Wähle eine Figur", true);
|
||||
dialog.setLayout(new GridLayout(2, 2));
|
||||
dialog.setSize(300, 200);
|
||||
|
||||
int[] pictures = { 81, 82, 66, 78, 113, 114, 98, 110 };
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int index = (game.getActivePlayer() - 1) * 4 + i;
|
||||
JButton jb = new JButton();
|
||||
jb.setIcon(new ImageIcon("src/main/resources/" + pictures[index] + ".png"));
|
||||
int selectedPiece = index;
|
||||
jb.addActionListener(e -> {
|
||||
System.out.println("Test");
|
||||
result[0] = selectedPiece;
|
||||
dialog.dispose();
|
||||
});
|
||||
dialog.add(jb);
|
||||
}
|
||||
|
||||
dialog.setLocationRelativeTo(null);
|
||||
dialog.setVisible(true);
|
||||
|
||||
return result[0];
|
||||
}
|
||||
|
||||
public void showWin(int player) {
|
||||
JFrame frame = new JFrame("Result");
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.setSize(300, 150);
|
||||
frame.setLayout(null);
|
||||
|
||||
JLabel jl = new JLabel(String.format("%d - %d", player / 2, player % 2));
|
||||
jl.setBounds(50, 30, 200, 25);
|
||||
jl.setFont(new Font("Tahoma", Font.BOLD, 20));
|
||||
frame.add(jl);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
|
|
@ -2,6 +2,7 @@ package de.mannheim.th.chess.domain;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
|
@ -24,4 +25,31 @@ public class GameTest {
|
|||
assertEquals("a2a4", list.getLast().toString());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLegalMoveableSquaresTest() {
|
||||
Game game = new Game();
|
||||
Square square = Square.A2;
|
||||
|
||||
List<Square> controllList = Arrays.asList(Square.A3, Square.A4);
|
||||
assertEquals(controllList, game.getLegalMoveableSquares(square));
|
||||
|
||||
game = new Game("k7/8/8/8/8/8/8/K6N w - - 0 1");
|
||||
square = Square.H1;
|
||||
controllList = Arrays.asList(Square.F2, Square.G3);
|
||||
assertEquals(controllList, game.getLegalMoveableSquares(square));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllLegalMoveableSquaresTest() {
|
||||
Game game = new Game();
|
||||
|
||||
List<Square> controllList = Arrays.asList(Square.A2, Square.B2, Square.C2, Square.D2, Square.E2, Square.F2,
|
||||
Square.G2, Square.H2, Square.B1, Square.G1);
|
||||
assertEquals(controllList, game.getAllLegalMoveableSquares());
|
||||
|
||||
game = new Game("k7/8/8/8/8/8/8/K6N w - - 0 1");
|
||||
controllList = Arrays.asList(Square.H1, Square.A1);
|
||||
assertEquals(controllList, game.getAllLegalMoveableSquares());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue