Merge branch 'ChessEngine' into Main
commit
fe3a5dec5b
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
@ -10,7 +11,6 @@
|
|||
|
||||
<name>schach</name>
|
||||
<description>A simple schach.</description>
|
||||
<!-- FIXME change it to the project's website -->
|
||||
<url>http://www.example.com</url>
|
||||
|
||||
<properties>
|
||||
|
@ -19,16 +19,31 @@
|
|||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<!-- Schachlib (bhlangonijr) -->
|
||||
<dependency>
|
||||
<groupId>com.github.bhlangonijr</groupId>
|
||||
<artifactId>chesslib</artifactId>
|
||||
<version>1.3.4</version>
|
||||
</dependency>
|
||||
<!-- JUnit 3 (wie im Template, ggf. auf JUnit 4/5 upgraden für moderne Projekte) -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
|
@ -42,7 +57,6 @@
|
|||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.6.1</version>
|
||||
</plugin>
|
||||
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package de.hs_mannheim.informatik.chess.main;
|
||||
import de.hs_mannheim.informatik.chess.gui.Gui;
|
||||
|
||||
|
||||
public class Main{
|
||||
|
||||
public static void main( String[] args ){
|
||||
new Gui();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,90 @@
|
|||
package de.hs_mannheim.informatik.chess.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.github.bhlangonijr.chesslib.Board;
|
||||
import com.github.bhlangonijr.chesslib.Piece;
|
||||
import com.github.bhlangonijr.chesslib.Square;
|
||||
import com.github.bhlangonijr.chesslib.move.Move;
|
||||
|
||||
public class ChessEngine {
|
||||
private Board board;
|
||||
|
||||
public ChessEngine() {
|
||||
board = new Board();
|
||||
}
|
||||
|
||||
public boolean move(String from, String to) {
|
||||
Move move = new Move(Square.valueOf(from.toUpperCase()), Square.valueOf(to.toUpperCase()));
|
||||
if (board.legalMoves().contains(move)) {
|
||||
board.doMove(move);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getLegalDestinations(String from) {
|
||||
List<String> destinations = new ArrayList<>();
|
||||
Square fromSq = Square.valueOf(from.toUpperCase());
|
||||
for (Move move : board.legalMoves()) {
|
||||
if (move.getFrom().equals(fromSq)) {
|
||||
destinations.add(move.getTo().toString()); // z.B. "E4"
|
||||
}
|
||||
}
|
||||
return destinations;
|
||||
}
|
||||
|
||||
public String getPieceAt(String square) {
|
||||
Piece piece = board.getPiece(Square.valueOf(square.toUpperCase()));
|
||||
return piece.toString(); // z.B. "WHITE_PAWN"
|
||||
}
|
||||
|
||||
public String[][] getBoardUnicode() {
|
||||
String[][] unicodeBoard = new String[8][8];
|
||||
for (int rank = 8; rank >= 1; rank--) {
|
||||
for (int file = 0; file < 8; file++) {
|
||||
Square square = Square.valueOf("" + (char)('A' + file) + rank);
|
||||
Piece piece = board.getPiece(square);
|
||||
unicodeBoard[8-rank][file] = pieceToUnicode(piece);
|
||||
}
|
||||
}
|
||||
return unicodeBoard;
|
||||
}
|
||||
|
||||
private String pieceToUnicode(Piece piece) {
|
||||
switch (piece) {
|
||||
case WHITE_KING: return "♔";
|
||||
case WHITE_QUEEN: return "♕";
|
||||
case WHITE_ROOK: return "♖";
|
||||
case WHITE_BISHOP: return "♗";
|
||||
case WHITE_KNIGHT: return "♘";
|
||||
case WHITE_PAWN: return "♙";
|
||||
case BLACK_KING: return "♚";
|
||||
case BLACK_QUEEN: return "♛";
|
||||
case BLACK_ROOK: return "♜";
|
||||
case BLACK_BISHOP: return "♝";
|
||||
case BLACK_KNIGHT: return "♞";
|
||||
case BLACK_PAWN: return "♟";
|
||||
default: return " ";
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMated() {
|
||||
return board.isMated();
|
||||
}
|
||||
|
||||
public boolean isStalemate() {
|
||||
return board.isStaleMate();
|
||||
}
|
||||
|
||||
public boolean isDraw() {
|
||||
return board.isDraw();
|
||||
}
|
||||
|
||||
public String getCurrentPlayer() {
|
||||
return board.getSideToMove().toString(); // "WHITE" oder "BLACK"
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue