Legale felder die angeklickte Figur zu bewegen werden rot markiert
parent
fd8c025c49
commit
d2798b977b
|
|
@ -2,8 +2,12 @@ package de.mannheim.th.chess.domain;
|
||||||
|
|
||||||
import com.github.bhlangonijr.chesslib.Board;
|
import com.github.bhlangonijr.chesslib.Board;
|
||||||
import com.github.bhlangonijr.chesslib.Square;
|
import com.github.bhlangonijr.chesslib.Square;
|
||||||
|
import com.github.bhlangonijr.chesslib.move.Move;
|
||||||
|
import com.github.bhlangonijr.chesslib.move.MoveList;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import de.mannheim.th.chess.utl.Clock;
|
import de.mannheim.th.chess.utl.Clock;
|
||||||
|
|
||||||
|
|
@ -15,10 +19,11 @@ public class Game{
|
||||||
private Board board;
|
private Board board;
|
||||||
private Clock clock;
|
private Clock clock;
|
||||||
private ArrayList<String> moves;
|
private ArrayList<String> moves;
|
||||||
|
private ArrayList<Move> movelist = new ArrayList<>();
|
||||||
|
|
||||||
public Game() {
|
public Game() {
|
||||||
board = new Board();
|
board = new Board();
|
||||||
clock = new Clock();
|
clock = new Clock("blitz");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,4 +37,42 @@ public class Game{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plays the move on the board and adds it to the movelist
|
||||||
|
*
|
||||||
|
* @param move the move to be played
|
||||||
|
*/
|
||||||
|
public void playMove(Move move) {
|
||||||
|
this.board.doMove(move);
|
||||||
|
this.movelist.add(move);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plays the move on the board and adds it to the movelist
|
||||||
|
*
|
||||||
|
* @param origin The square from wich it moves from.
|
||||||
|
* @param desination The square where it will move to.
|
||||||
|
*/
|
||||||
|
public void playMove(Square origin, Square desination) {
|
||||||
|
Move move = new Move(origin, desination);
|
||||||
|
this.board.doMove(move);
|
||||||
|
this.movelist.add(move);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of legal moves originating from the specified square.
|
||||||
|
*
|
||||||
|
* @param square The square from which to retrieve legal moves.
|
||||||
|
*
|
||||||
|
* @return A list of legal moves that originate from the specified square.
|
||||||
|
*/
|
||||||
|
public List<Move> getLegalMoves(Square square) {
|
||||||
|
return this.board.legalMoves().stream()
|
||||||
|
.filter(move -> move.getFrom() == square)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -4,6 +4,9 @@ import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import com.github.bhlangonijr.chesslib.Board;
|
import com.github.bhlangonijr.chesslib.Board;
|
||||||
|
import com.github.bhlangonijr.chesslib.File;
|
||||||
|
import com.github.bhlangonijr.chesslib.Rank;
|
||||||
|
import com.github.bhlangonijr.chesslib.Square;
|
||||||
|
|
||||||
import de.mannheim.th.chess.App;
|
import de.mannheim.th.chess.App;
|
||||||
import de.mannheim.th.chess.domain.Game;
|
import de.mannheim.th.chess.domain.Game;
|
||||||
|
|
@ -34,7 +37,8 @@ public class SpielFrame extends JFrame {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
private ArrayList<JButton> buttons = new ArrayList<>();
|
private HashMap<Integer, JButton> buttons = new HashMap<>();
|
||||||
|
private HashMap<JButton, Integer> positions = new HashMap<>();
|
||||||
private HashMap<JButton, String> belegungen = new HashMap<>();
|
private HashMap<JButton, String> belegungen = new HashMap<>();
|
||||||
private JPanel panelLinks, panelRechts;
|
private JPanel panelLinks, panelRechts;
|
||||||
private Game game;
|
private Game game;
|
||||||
|
|
@ -126,6 +130,13 @@ public class SpielFrame extends JFrame {
|
||||||
//wenn richtiger spieler dran:
|
//wenn richtiger spieler dran:
|
||||||
|
|
||||||
setCursor(figurCursor);
|
setCursor(figurCursor);
|
||||||
|
int position = positions.get(clickedButton);
|
||||||
|
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())
|
||||||
|
.forEach(i -> buttons.get(i).setBackground(Color.RED));
|
||||||
|
|
||||||
//Button Icon zurücksetzen
|
//Button Icon zurücksetzen
|
||||||
|
|
||||||
|
|
@ -147,7 +158,8 @@ public class SpielFrame extends JFrame {
|
||||||
});
|
});
|
||||||
|
|
||||||
panelLinks.add(b);
|
panelLinks.add(b);
|
||||||
buttons.add(b);
|
buttons.put(i, b);
|
||||||
|
positions.put(b, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
game = new Game();
|
game = new Game();
|
||||||
|
|
@ -182,4 +194,6 @@ public class SpielFrame extends JFrame {
|
||||||
|
|
||||||
//System.out.println(belegungen.toString());
|
//System.out.println(belegungen.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue