diff --git a/schach/pom.xml b/schach/pom.xml index af8a653..7956515 100644 --- a/schach/pom.xml +++ b/schach/pom.xml @@ -1,7 +1,8 @@ - + 4.0.0 de.hs-mannheim.informatik.schach @@ -10,7 +11,6 @@ schach A simple schach. - http://www.example.com @@ -19,16 +19,31 @@ 8 + + + jitpack.io + https://jitpack.io + + + + + + com.github.bhlangonijr + chesslib + 1.3.4 + + junit junit 3.8.1 + test - + maven-clean-plugin @@ -42,7 +57,6 @@ maven-project-info-reports-plugin 3.6.1 - maven-resources-plugin 3.3.1 diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/main/Main.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/main/Main.java index 4967926..0ca22cf 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/main/Main.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/main/Main.java @@ -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(); + } } diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java index ecb125e..e213948 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java @@ -1,5 +1,90 @@ package de.hs_mannheim.informatik.chess.model; -public class ChessEngine { +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 getLegalDestinations(String from) { + List 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" + } + }