Einleitung: Objektorientierte Modellierung der Schachfiguren
Die Spielmechanik der einzelnen Schachfiguren ist über eine allgemeine Klasse Piece sowie spezialisierte Klasse King, Queen, Rook, Knight, Bishop und Pawn abgebildet.
Im Zentrum jeder Schach-KI steht die Fähigkeit, die Regeln der einzelnen Figuren korrekt umzusetzen. In dieser Datei werden die sechs grundlegenden Schachfigurenklassen – Pawn, Rook, Knight, Bishop, Queen, King – zusammen mit einer gemeinsamen abstrakten Basisklasse Piece implementiert. Ziel ist es, jede Figur mit ihren bewegungsspezifischen Regeln, einer Bewertungsfunktion und einer strukturierten Schnittstelle zur Interaktion mit dem Spielbrett auszustatten.
Diese Klassen bilden die Grundlage für die Spiellogik, die Bewertung der Spielsituationen sowie für das spätere Mini-Max-gestützte Entscheidungsverhalten eures Schachcomputers.
Objektorientierter Aufbau
Alle Figuren erben von der Klasse Piece, welche gemeinsame Funktionalitäten kapselt:
Referenz zum aktuellen Brett und zur Position der Figur
Informationen über die Farbe (weiß oder schwarz)
Ein universelles Interface für Bewertung (
evaluate) und die Ermittlung gültiger Züge (get_valid_cells)
Jede abgeleitete Klasse (Pawn, Rook, Knight etc.) implementiert ihre eigene `get_reachable_cells()`-Methode, um die figurenspezifische Bewegungslogik zu definieren.
Intelligente Bewertung
Die evaluate()-Methode quantifiziert den Wert einer Figur auf dem Brett. Neben einem Basiswert (z. B. 1 für einen Bauern, 9 für eine Dame) berücksichtigt sie zusätzliche Heuristiken wie:
Mobilität: Wie viele Züge sind möglich?
Bedrohungspotential: Welche gegnerischen Figuren können geschlagen werden?
Taktische Vorteile wie Gabeln (mehrere Bedrohungen gleichzeitig)
Dadurch entsteht eine feinjustierte Bewertungsgrundlage für den Mini-Max-Algorithmus, die über bloße Materialzählung hinausgeht.
Gültige Züge und Regelkonformität
Während die get_reachable_cells()-Methoden ausschließlich die potenziellen Bewegungsmöglichkeiten der jeweiligen Figur auf Basis der Schachregeln berechnen, filtert get_valid_cells() diese Liste durch eine Sicherheitsprüfung: Nur Züge, die den eigenen König nicht ins Schach bringen, werden als gültig markiert. Diese Trennung erlaubt sowohl einfache Tests der Bewegung als auch regelkonformes Spielverhalten.
Fazit
Die modellierten Figurenklassen sind ein essenzielles Bindeglied zwischen statischer Brettlogik und dynamischer Spielentscheidung. Sie ermöglichen eine regelkonforme, bewertbare und erweiterbare Darstellung der Spielfiguren – und bilden damit den Kern eines funktionsfähigen Schachcomputers.
Reference - Piece
- class pieces.Piece(board, white)[source]
Base class for pieces on the board.
A piece holds a reference to the board, its color and its currently located cell. In this class, you need to implement two methods, the “evaluate()” method and the “get_valid_cells()” method.
- can_enter_cell(cell)[source]
Shortcut method to see if a cell on the board can be entered. Simply calls
piece_can_enter_cellfrom the current board class.- Parameters:
cell – The cell to check for. Must be a unpackable (row, col) type.
- Returns:
True if the provided cell can enter, False otherwise
- can_hit_on_cell(cell)[source]
Shortcut method to see if this piece can hit another piece on a cell. Simply calls
piece_can_hit_on_cellfrom the current board class.- Parameters:
cell – The cell to check for. Must be a unpackable (row, col) type.
- Returns:
True if the piece can hit on the provided cell, False otherwise
- evaluate()[source]
TODO Implement a meaningful numerical evaluation of this piece on the board. This evaluation happens independent of the color as later, values for white pieces will be added and values for black pieces will be substracted.
HINT Making this method independent of the pieces color is crucial to get a symmetric evaluation metric in the end.
The pure existance of this piece alone is worth some points. This will create an effect where the player with more pieces on the board will, in sum, get the most points assigned.
Think of other criteria that would make this piece more valuable, e.g. movability or whether this piece can hit other pieces. Value them accordingly.
- Returns:
Return numerical score between -infinity and +infinity. Greater values indicate better evaluation result (more favorable).
- get_valid_cells()[source]
TODO Return a list of valid cells this piece can move into.
- A cell is valid if
it is reachable. That is what the
get_reachable_cells()method is for andafter a move into this cell the own king is not (or no longer) in check.
HINT: Use the
get_reachable_cells()method of this piece to receive a list of reachable cells. Iterate through all of them and temporarily place the piece on this cell. Then check whether your own King (same color) is in check. Use theis_king_check_cached()method to test for checks. If there is no check after this move, add this cell to the list of valid cells. After every move, restore the original board configuration.To temporarily move a piece into a new cell, first store its old position (self.cell) in a local variable. The target cell might have another piece already placed on it. Use
get_cellto retrieve that piece (or None if there was none) and store it as well. Then callset_cellto place this piece on the target cell and test for any checks given. After this, restore the original configuration by placing this piece back into its old position (callset_cellagain) and place the previous piece also back into its cell.- Returns:
Return True
Reference - King
- class pieces.King(board, white)[source]
- get_reachable_cells()[source]
TODO Implement the movability mechanic for the king.
NOTE: Here you do not yet need to consider whether your own King would become checked after a move. This will be taken care of by the
is_king_checkandget_valid_cellsmethods.HINT: Kings can move horizontally, vertically and diagonally but only one piece at a time.
You can call
cell_is_valid_and_empty,can_hit_on_cellandcan_enter_cellto check for necessary conditions to implement the rook movability mechanics.- Returns:
A list of reachable cells this king could move into.
Reference - Queen
- class pieces.Queen(board, white)[source]
- get_reachable_cells()[source]
TODO Implement the movability mechanic for the queen.
NOTE: Here you do not yet need to consider whether your own King would become checked after a move. This will be taken care of by the
is_king_checkandget_valid_cellsmethods.HINT: Queens can move horizontally, vertically and diagonally an arbitrary amount of cells until blocked. They combine the movability of rooks and bishops.
You can call
cell_is_valid_and_empty,can_hit_on_cellandcan_enter_cellto check for necessary conditions to implement the rook movability mechanics.- Returns:
A list of reachable cells this queen could move into.
Reference - Rook
- class pieces.Rook(board, white)[source]
- get_reachable_cells()[source]
TODO Implement the movability mechanic for rooks.
NOTE: Here you do not yet need to consider whether your own King would become checked after a move. This will be taken care of by the
is_king_checkandget_valid_cellsmethods.HINT: Rooks can move only horizontally or vertically. They can move an arbitrary amount of cells until blocked by an own piece or an opposing piece (which they could hit and then being stopped).
You can call
cell_is_valid_and_empty,can_hit_on_cellandcan_enter_cellto check for necessary conditions to implement the rook movability mechanics.- Returns:
A list of reachable cells this rook could move into.
Reference - Knight
- class pieces.Knight(board, white)[source]
- get_reachable_cells()[source]
TODO Implement the movability mechanic for knights.
NOTE: Here you do not yet need to consider whether your own King would become checked after a move. This will be taken care of by the
is_king_checkandget_valid_cellsmethods.HINT: Knights can move in a special pattern. They can move two rows up or down and then one column left or right. Alternatively, they can move one row up or down and then two columns left or right. They are not blocked by pieces in between.
You can call
cell_is_valid_and_empty,can_hit_on_cellandcan_enter_cellto check for necessary conditions to implement the rook movability mechanics.- Returns:
A list of reachable cells this knight could move into.
Reference - Bishop
- class pieces.Bishop(board, white)[source]
- get_reachable_cells()[source]
TODO Implement the movability mechanic for bishop.
NOTE: Here you do not yet need to consider whether your own King would become checked after a move. This will be taken care of by the
is_king_checkandget_valid_cellsmethods.HINT: Bishops can move diagonally an arbitrary amount of cells until blocked.
You can call
cell_is_valid_and_empty,can_hit_on_cellandcan_enter_cellto check for necessary conditions to implement the rook movability mechanics.- Returns:
A list of reachable cells this bishop could move into.
Reference - Pawn
- class pieces.Pawn(board, white)[source]
- get_reachable_cells()[source]
TODO Implement the movability mechanik for pawns.
NOTE: Here you do not yet need to consider whether your own King would become checked after a move. This will be taken care of by the
is_king_checkandget_valid_cellsmethods.HINT: Pawns can move only forward (towards the opposing army). Depening of whether this piece is black of white, this means pawn can move only to higher or lower rows. Normally a pawn can only move one cell forward as long as the target cell is not occupied by any other piece. If the pawn is still on its starting row, it can also dash forward and move two pieces at once (as long as the path to that cell is not blocked). Pawns can only hit diagonally, meaning they can hit other pieces only the are one cell forward left or one cell forward right from them.
You can call
cell_is_valid_and_empty,can_hit_on_cellandcan_enter_cellto check for necessary conditions to implement the pawn movability mechanics.NOTE: For all you deep chess experts: Hitting en passant does not need to be implemented.
- Returns:
A list of reachable cells this pawn could move into.