Compare commits

...

7 Commits

12 changed files with 558 additions and 31 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.vscode/
*.exe
*.txt
chess

View File

@@ -127,6 +127,54 @@ ChessPiecePosition ChessPiece::GetPosition() {
}
}
/**
* Gets all chess piece positions of the chess piece which are still protective for the king.
* Any position between the attacker chess piece and the current chess piece is protective for the king.
* The position is also protective for the king if the chess piece can beat the attacker chess piece.
* @param chessboard A pointer to the chessboard.
* @param positions A set of positions the chess piece can move next (also positions are not protective).
* @return A set of chess piece positions which are still protective for the king.
*/
std::set<ChessPiecePosition> ChessPiece::GetProtectivePositions(Chessboard* chessboard, std::set<ChessPiecePosition> positions) {
ChessPiece* chessPieceKing = chessboard->GetChessPieceKing(this->GetColor());
std::set<ChessPiecePosition> protectivePositions;
for (ChessPiece* chessPiece : chessboard->GetChessPieces()) {
if (chessPiece->GetColor() != this->GetColor()) {
std::set<ChessPiecePosition> positionsStay = chessPiece->GetNextPositions(chessboard, {});
std::set<ChessPiecePosition> positionsMove = chessPiece->GetNextPositions(chessboard, {this->GetPosition()});
if (positionsStay.count(chessPieceKing->GetPosition()) == 0 && positionsMove.count(chessPieceKing->GetPosition()) == 1) {
// the position is protective if the current chess piece can beat the attacker chess piece.
if (positions.count(chessPiece->GetPosition()) == 1) {
protectivePositions.insert(chessPiece->GetPosition());
}
// every position between the current chess piece and the attacker chess piece is also protective.
std::string step = ChessPiecePosition::GetStep(chessPiece->GetPosition(), chessPieceKing->GetPosition());
if (step.empty()) {
continue;
}
ChessPiecePosition currentPosition = chessPiece->GetPosition();
ChessPiecePosition* nextPosition = &currentPosition;
while (nextPosition = nextPosition->NextFromString(step)) {
if (nextPosition->ToString() == this->GetPosition().ToString()) {
break;
} else if (positions.count(*nextPosition) == 1) {
protectivePositions.insert(*nextPosition);
}
}
}
}
}
return protectivePositions;
}
/**
* Gets a set of all positions along the rank.
* @param chessboard A pointer to the chessboard.
@@ -300,7 +348,7 @@ void ChessPiece::SetPosition(ChessPiecePosition position) {
*/
void ChessPiece::UpdateNextPositions(Chessboard* chessboard) {
if (this->IsProtective(chessboard)) {
this->SetNextPositions({});
this->SetNextPositions(this->GetProtectivePositions(chessboard, this->GetNextPositions(chessboard)));
} else {
this->SetNextPositions(this->GetNextPositions(chessboard));
}

View File

@@ -14,6 +14,8 @@ class ChessPiece {
ChessPieceColor _color;
std::vector<ChessPiecePosition> _positions;
std::set<ChessPiecePosition> _positions_next;
std::set<ChessPiecePosition> GetProtectivePositions(Chessboard* chessboard, std::set<ChessPiecePosition> positions);
bool IsProtective(Chessboard* chessboard);
protected:
char _char = '\0';
@@ -46,7 +48,6 @@ class ChessPiece {
bool IsKing();
bool IsKnight();
bool IsPawn();
bool IsProtective(Chessboard* chessboard);
bool IsQueen();
bool IsRook();
void SetPosition(ChessPiecePosition position);

View File

@@ -0,0 +1,163 @@
#include "../ChessPieces/ChessPieceMove.hpp"
/**
* Creates a new move of a chess piece that uses the short algebraic notation.
* @param move The move that uses the short algebraic notation.
*/
ChessPieceMove::ChessPieceMove(std::string move) {
move = this->Normalize(move);
if (this->IsValidShortNotation(move)) {
this->_move = move;
this->ParseShortNotation();
} else {
throw std::invalid_argument("invalid move notation");
}
}
/**
* Status whether the move is a valid move that uses the short algebraic notation.
* @return Status whether the move is a valid move that uses the short algebraic notation.
*/
bool ChessPieceMove::IsValidShortNotation(std::string move) {
if (move.empty()) {
return std::regex_match(this->_move, this->_notation);
} else {
return std::regex_match(move, this->_notation);
}
}
/**
* Normalizes the move which, if valid, can then be parsed.
* @param move The move to be normalized.
* @return The normalized move which, if valid, can then be parsed.
*/
std::string ChessPieceMove::Normalize(std::string move) {
std::replace(move.begin(), move.end(), 'O', '0'); // castling: portable game notation to algebraic notation
std::replace(move.begin(), move.end(), 'S', 'N'); // german notation to english notation (knight)
std::replace(move.begin(), move.end(), 'L', 'B'); // german notation to english notation (bishop)
std::replace(move.begin(), move.end(), 'T', 'R'); // german notation to english notation (queen)
return move;
}
/**
* Parses a move that uses the short algebraic notation.
*/
void ChessPieceMove::ParseShortNotation() {
std::smatch parts;
std::regex_search(this->_move, parts, this->_notation);
this->_chessPiece = parts[1].str();
this->_fromPositionFile = parts[2].str();
this->_fromPositionRank = parts[3].str();
this->_capture = parts[4].str();
this->_toPositionFile = parts[5].str();
this->_toPositionRank = parts[6].str();
this->_promotion = parts[7].str();
this->_castling = parts[8].str();
this->_draw = parts[9].str();
this->_additional = parts[10].str();
}
/**
* Gets the chess piece char of the move.
* @return The chess piece char of the move.
*/
char ChessPieceMove::GetChessPieceChar() {
return this->_chessPiece.empty() ? 'P' : this->_chessPiece[0];
}
/**
* Gets the file part of the source position.
* @return The file part of the source position.
*/
char ChessPieceMove::GetFromPositionFile() {
return this->_fromPositionFile.empty() ? '\0' : this->_fromPositionFile[0];
}
/**
* Gets the rank part of the source position.
* @return The rank part of the source position.
*/
int ChessPieceMove::GetFromPositionRank() {
return this->_fromPositionRank.empty() ? 0 : stoi(this->_fromPositionRank);
}
/**
* Gets the chess piece character of the promotion.
* @return The chess piece character of the promotion.
*/
char ChessPieceMove::GetPromotionChessPieceChar() {
return this->_promotion.empty() ? '\0' : this->_promotion[this->_promotion.length() - 1];
}
/**
* Gets the file part of the target position.
* @return The file part of the target position.
*/
char ChessPieceMove::GetToPositionFile() {
return this->_toPositionFile.empty() ? '\0' : this->_toPositionFile[0];
}
/**
* Gets the rank part of the target position.
* @return The rank part of the target position.
*/
int ChessPieceMove::GetToPositionRank() {
return this->_toPositionRank.empty() ? 0 : stoi(this->_toPositionRank);
}
/**
* Status whether the move is a move with capture.
* @return Status whether the move is a move with capture.
*/
bool ChessPieceMove::IsCapture() {
return this->_capture == "x" ? true : false;
}
/**
* Status whether the move results in a check.
* @return Status whether the move results in a check.
*/
bool ChessPieceMove::IsCheck() {
return this->_additional == "+";
}
/**
* Status whether the move results in a checkmate.
* @return Status whether the move results in a checkmate.
*/
bool ChessPieceMove::IsCheckmate() {
return this->_additional == "++" || this->_additional == "#";
}
/**
* Status whether the move is a offer for a draw.
* @return Status whether the move is a offer for a draw.
*/
bool ChessPieceMove::IsDrawOffer() {
return this->_draw == "(=)";
}
/**
* Status whether the move is an "en passant" by pawn.
* @return Status whether the move is an "en passant" by pawn.
*/
bool ChessPieceMove::IsEnPassant() {
return this->_additional == "e.p.";
}
/**
* Status whether the move is the long castling (castling queenside).
* @return Status whether the move is the long castling (castling queenside).
*/
bool ChessPieceMove::IsLongCastling() {
return this->_castling == "0-0-0";
}
/**
* Status whether the move is the short castling (castling kingside).
* @return Status whether the move is the short castling (castling kingside).
*/
bool ChessPieceMove::IsShortCastling() {
return this->_castling == "0-0";
}

View File

@@ -0,0 +1,41 @@
#ifndef ChessPieceMove_H
#define ChessPieceMove_H
#include <regex>
class ChessPieceMove {
private:
std::string _chessPiece;
std::string _fromPositionFile;
std::string _fromPositionRank;
std::string _capture;
std::string _toPositionFile;
std::string _toPositionRank;
std::string _promotion;
std::string _castling;
std::string _draw;
std::string _additional;
std::string _move;
const std::regex _notation = std::regex("^(?:([RNBQKP]?)([a-h]?)([1-8]?)(x?)([a-h])([1-8])((?:[=])?[QRBN])?|(0-0(?:-0)?)|(\\(=\\)))([+#!?]| e.p.)*$");
std::string Normalize(std::string move);
void ParseShortNotation();
public:
ChessPieceMove(std::string move);
bool IsValidShortNotation(std::string move = "");
char GetChessPieceChar();
char GetFromPositionFile();
int GetFromPositionRank();
bool IsCapture();
char GetToPositionFile();
int GetToPositionRank();
char GetPromotionChessPieceChar();
bool IsShortCastling();
bool IsLongCastling();
bool IsDrawOffer();
bool IsCheck();
bool IsCheckmate();
bool IsEnPassant();
};
#endif

View File

@@ -56,18 +56,18 @@ std::string ChessPiecePosition::GetDifference(ChessPiecePosition fromPosition, C
} else {
std::string difference;
if (toPosition.GetFile() < fromPosition.GetFile()) {
difference.append(std::string(1, ChessPiecePosition::_MOVE_LEFT) + std::to_string(fromPosition.GetFile() - toPosition.GetFile()));
} else if (toPosition.GetFile() > fromPosition.GetFile()) {
difference.append(std::string(1, ChessPiecePosition::_MOVE_RIGHT) + std::to_string(toPosition.GetFile() - fromPosition.GetFile()));
}
if (toPosition.GetRank() < fromPosition.GetRank()) {
difference.append(std::string(1, ChessPiecePosition::_MOVE_BOTTOM) + std::to_string(fromPosition.GetRank() - toPosition.GetRank()));
} else if (toPosition.GetRank() > fromPosition.GetRank()) {
difference.append(std::string(1, ChessPiecePosition::_MOVE_TOP) + std::to_string(toPosition.GetRank() - fromPosition.GetRank()));
}
if (toPosition.GetFile() < fromPosition.GetFile()) {
difference.append(std::string(1, ChessPiecePosition::_MOVE_LEFT) + std::to_string(fromPosition.GetFile() - toPosition.GetFile()));
} else if (toPosition.GetFile() > fromPosition.GetFile()) {
difference.append(std::string(1, ChessPiecePosition::_MOVE_RIGHT) + std::to_string(toPosition.GetFile() - fromPosition.GetFile()));
}
return difference;
}
}

View File

@@ -1,4 +1,9 @@
#include "../Chessboard/Chessboard.hpp"
#include "../ChessPieces/Rook.hpp"
#include "../ChessPieces/Queen.hpp"
#include "../ChessPieces/Bishop.hpp"
#include "../ChessPieces/Knight.hpp"
#include "../ChessPieces/ChessPieceMove.hpp"
#include "../ChessPieces/ChessPiece.hpp"
void Chessboard::SetChessPiece(ChessPiece* chesspiece) {
@@ -13,26 +18,203 @@ ChessPiece* Chessboard::GetChessPiece(ChessPiecePosition* position) {
return this->chessboard.at(*position);
}
void Chessboard::RemoveChessPiece(ChessPiecePosition position) {
if (this->chessboard.count(position) == 1) {
delete this->chessboard[position];
this->chessboard.erase(position);
}
}
bool Chessboard::MoveCastling(ChessPieceColor color, bool shortCastling) {
ChessPiecePosition fromPositionKing = (color == ChessPieceColor::White) ? ChessPiecePosition("E1") : ChessPiecePosition("E8");
ChessPiecePosition fromPositionRook = (color == ChessPieceColor::White) ? ChessPiecePosition(shortCastling ? "H1" : "A1") : ChessPiecePosition(shortCastling ? "H8" : "A8");
ChessPiecePosition toPositionKing = (color == ChessPieceColor::White) ? ChessPiecePosition(shortCastling ? "G1" : "C1") : ChessPiecePosition(shortCastling ? "G8" : "C8");
ChessPiecePosition toPositionRook = (color == ChessPieceColor::White) ? ChessPiecePosition(shortCastling ? "F1" : "D1") : ChessPiecePosition(shortCastling ? "F8" : "D8");
ChessPiece* chessPieceRook = this->GetChessPiece(&fromPositionRook);
ChessPiece* chessPieceKing = this->GetChessPiece(&fromPositionKing);
if (chessPieceKing == nullptr || chessPieceRook == nullptr) {
return false;
}
if (chessPieceKing->IsKing() == false || chessPieceRook->IsRook() == false) {
return false;
}
if (chessPieceKing->IsFirstMove() == false || chessPieceRook->IsFirstMove() == false) {
return false;
}
std::string step = ChessPiecePosition::GetStep(chessPieceKing->GetPosition(), toPositionKing);
ChessPiecePosition currentPosition = chessPieceKing->GetPosition();
ChessPiecePosition* nextPosition = &currentPosition;
while (nextPosition = nextPosition->NextFromString(step)) {
if (this->IsEmptyField(nextPosition)) {
for (std::pair<ChessPiecePosition, ChessPiece*> field : this->chessboard) {
if (field.second->GetColor() != color) {
if (field.second->GetNextPositions().count(*nextPosition) == 0) {
continue;
} else {
return false;
}
}
}
if (nextPosition->ToString() == toPositionKing.ToString()) {
break;
}
} else {
return false;
}
}
this->chessboard[toPositionKing] = chessPieceKing;
this->chessboard.erase(chessPieceKing->GetPosition());
this->SetToHistory(chessPieceKing->GetPosition(), toPositionKing);
chessPieceKing->SetPosition(toPositionKing);
this->chessboard[toPositionRook] = chessPieceRook;
this->chessboard.erase(chessPieceRook->GetPosition());
this->SetToHistory(chessPieceRook->GetPosition(), toPositionRook);
chessPieceRook->SetPosition(toPositionRook);
return true;
}
void Chessboard::MoveChessPiece(std::string move) {
if (move.length() == 5) {
ChessPiecePosition* fromPosition = new ChessPiecePosition(move.substr(0, 2));
ChessPiecePosition* toPosition = new ChessPiecePosition(move.substr(3, 2));
ChessPieceMove chessPieceMove = ChessPieceMove(move);
ChessPiece* piece = this->GetChessPiece(fromPosition);
if (chessPieceMove.IsValidShortNotation() == false) {
return;
}
if (piece->GetColor() != this->GetCurrentPlayer()->GetColor()) {
return; // wrong player
if (chessPieceMove.IsShortCastling()) {
this->MoveCastling(this->GetCurrentPlayer()->GetColor(), true);
} else if (chessPieceMove.IsLongCastling()) {
this->MoveCastling(this->GetCurrentPlayer()->GetColor(), false);
} else if (chessPieceMove.IsDrawOffer()) {
// Remis
} else {
ChessPiece* chessPiece = nullptr;
ChessPiecePosition toChessPiecePosition = ChessPiecePosition(chessPieceMove.GetToPositionFile(), chessPieceMove.GetToPositionRank());
char fromPositionFile = chessPieceMove.GetFromPositionFile();
int fromPositionRank = chessPieceMove.GetFromPositionRank();
for (std::pair<ChessPiecePosition, ChessPiece*> field : this->chessboard) {
if (field.second->GetColor() == this->GetCurrentPlayer()->GetColor()) {
if (field.second->GetChar() == chessPieceMove.GetChessPieceChar() && field.second->GetNextPositions().count(toChessPiecePosition) == 1) {
if (fromPositionFile >= 'a' && fromPositionFile <= 'h') {
if (field.second->GetPosition().GetFile() == std::toupper(fromPositionFile)) {
chessPiece = field.second;
break;
} else {
continue;
}
}
if (fromPositionRank >= 1 && fromPositionRank <= 8) {
if (field.second->GetPosition().GetRank() == fromPositionRank) {
chessPiece = field.second;
break;
} else {
continue;
}
}
chessPiece = field.second;
break;
}
}
}
if (piece->GetNextPositions().count(*toPosition) == 1) {
this->chessboard[*toPosition] = this->chessboard[*fromPosition];
this->chessboard.erase(*fromPosition);
piece->SetPosition(*toPosition);
this->SetToHistory(*fromPosition, *toPosition);
} else {
return; // wrong or invalid move
if (chessPiece) {
if (this->IsEmptyField(&toChessPiecePosition) == false) {
this->RemoveChessPiece(toChessPiecePosition);
this->chessboard[toChessPiecePosition] = this->chessboard[chessPiece->GetPosition()];
this->chessboard.erase(chessPiece->GetPosition());
this->SetToHistory(chessPiece->GetPosition(), toChessPiecePosition);
chessPiece->SetPosition(toChessPiecePosition);
} else {
if (chessPiece->IsPawn()) {
if (chessPiece->GetColor() == ChessPieceColor::White) {
std::string difference = ChessPiecePosition::GetDifference(chessPiece->GetPosition(), toChessPiecePosition);
if (difference == "T1L1" || difference == "T1R1") {
ChessPiecePosition currentPosition = toChessPiecePosition;
ChessPiecePosition* nextPosition = &currentPosition;
if (nextPosition = nextPosition->NextBottom()) {
if (this->IsEmptyField(nextPosition) == false) {
ChessPiece* chessPieceRemove = this->GetChessPiece(nextPosition);
if (chessPieceRemove->GetColor() != chessPiece->GetColor() && chessPieceRemove->IsPawn()) {
this->chessboard[toChessPiecePosition] = this->chessboard[chessPiece->GetPosition()];
this->chessboard.erase(chessPiece->GetPosition());
this->SetToHistory(chessPiece->GetPosition(), toChessPiecePosition);
chessPiece->SetPosition(toChessPiecePosition);
this->RemoveChessPiece(chessPieceRemove->GetPosition());
}
}
}
} else {
this->chessboard[toChessPiecePosition] = this->chessboard[chessPiece->GetPosition()];
this->chessboard.erase(chessPiece->GetPosition());
this->SetToHistory(chessPiece->GetPosition(), toChessPiecePosition);
chessPiece->SetPosition(toChessPiecePosition);
}
} else if (chessPiece->GetColor() == ChessPieceColor::Black) {
std::string difference = ChessPiecePosition::GetDifference(chessPiece->GetPosition(), toChessPiecePosition);
if (difference == "B1L1" || difference == "B1R1") {
ChessPiecePosition currentPosition = toChessPiecePosition;
ChessPiecePosition* nextPosition = &currentPosition;
if (nextPosition = nextPosition->NextTop()) {
if (this->IsEmptyField(nextPosition) == false) {
ChessPiece* chessPieceRemove = this->GetChessPiece(nextPosition);
if (chessPieceRemove->GetColor() != chessPiece->GetColor() && chessPieceRemove->IsPawn()) {
this->chessboard[toChessPiecePosition] = this->chessboard[chessPiece->GetPosition()];
this->chessboard.erase(chessPiece->GetPosition());
this->SetToHistory(chessPiece->GetPosition(), toChessPiecePosition);
chessPiece->SetPosition(toChessPiecePosition);
this->RemoveChessPiece(chessPieceRemove->GetPosition());
}
}
}
} else {
this->chessboard[toChessPiecePosition] = this->chessboard[chessPiece->GetPosition()];
this->chessboard.erase(chessPiece->GetPosition());
this->SetToHistory(chessPiece->GetPosition(), toChessPiecePosition);
chessPiece->SetPosition(toChessPiecePosition);
}
}
} else {
this->chessboard[toChessPiecePosition] = this->chessboard[chessPiece->GetPosition()];
this->chessboard.erase(chessPiece->GetPosition());
this->SetToHistory(chessPiece->GetPosition(), toChessPiecePosition);
chessPiece->SetPosition(toChessPiecePosition);
}
}
if (chessPieceMove.GetPromotionChessPieceChar() != '\0') {
ChessPieceColor color = chessPiece->GetColor();
this->RemoveChessPiece(toChessPiecePosition);
switch (chessPieceMove.GetPromotionChessPieceChar()) {
case 'R':
this->chessboard[toChessPiecePosition] = new Rook(color, toChessPiecePosition);
break;
case 'B':
this->chessboard[toChessPiecePosition] = new Bishop(color, toChessPiecePosition);
break;
case 'N':
this->chessboard[toChessPiecePosition] = new Knight(color, toChessPiecePosition);
break;
case 'Q':
this->chessboard[toChessPiecePosition] = new Queen(color, toChessPiecePosition);
break;
}
}
}
}
@@ -108,18 +290,16 @@ bool Chessboard::IsCheckmate() {
}
}
if (deniable == false) {
return true;
if (deniable) {
continue;
}
// move between attacker and king
// this is not possible for pawn, knight and king because they beat on next field.
std::string step = ChessPiecePosition::GetStep(chessPieceOpponent->GetPosition(), chessPieceKing->GetPosition());
ChessPiecePosition currentPosition = chessPieceOpponent->GetPosition();
ChessPiecePosition* nextPosition = &currentPosition;
deniable = false;
while (nextPosition = nextPosition->NextFromString(step)) {
for (ChessPiece* chessPieceCurrent : this->GetChessPieces()) {
@@ -268,15 +448,31 @@ void Chessboard::UpdateChessPieces() {
*/
bool Chessboard::IsPositionUnderAttack(ChessPiecePosition position, ChessPieceColor color) {
for (std::pair<ChessPiecePosition, ChessPiece*> field : this->chessboard) {
if (field.second->GetColor() == color && field.second->GetNextPositions().count(position) == 1) {
if (field.second->IsPawn() == false && field.second->GetColor() == color && field.second->GetNextPositions().count(position) == 1) {
return true;
}
}
for (std::pair<ChessPiecePosition, ChessPiece*> field : this->chessboard) {
if (field.second->IsPawn() && field.second->GetColor() == color) {
std::string difference = ChessPiecePosition::GetDifference(field.second->GetPosition(), position);
if (color == ChessPieceColor::White && (difference == "T1L1" || difference == "T1R1")) {
return true;
} else if (color == ChessPieceColor::Black && (difference == "B1L1" || difference == "B1R1")) {
return true;
} else {
continue;
}
}
}
return false;
}
Chessboard::~Chessboard() {
delete this->players.first;
delete this->players.second;
for (std::pair<ChessPiecePosition, ChessPiece*> field : this->chessboard) {
delete field.second;
}

View File

@@ -4,6 +4,7 @@
#include <iostream>
#include <map>
#include <random>
#include <regex>
#include <string>
#include <utility>
#include <vector>
@@ -19,6 +20,7 @@ class Chessboard {
std::vector<std::pair<ChessPiecePosition, ChessPiecePosition>> history;
std::pair<Player*, Player*> players;
Player* currentPlayer;
void RemoveChessPiece(ChessPiecePosition position);
public:
~Chessboard();
@@ -28,6 +30,7 @@ class Chessboard {
std::vector<ChessPiece*> GetChessPieces();
void UpdateChessPieces();
void MoveChessPiece(std::string move);
bool MoveCastling(ChessPieceColor color, bool shortCastling);
void SetToHistory(ChessPiecePosition fromPosition, ChessPiecePosition toPosition);
int GetCountMoves();
std::pair<ChessPiecePosition, ChessPiecePosition> GetLastMove();

View File

@@ -92,16 +92,25 @@
## ToDo
- Speicherfunktion
- Schachnotation (Lange)
- Menü (ASCII Art of TurboSchach)
- Startmenü
- Spielmenü mit Historie, Kommand
- Plattformunabhängiges CMD clearing vor dem Zeichnen
- Spezialbewegung (Rochade, Umwandlung, En passant)
## Build
## Kompilieren und Ausführen
### Windows
```
g++ -o chess.exe main.cpp ChessPieces/*.cpp Chessboard/*.cpp Player/*.cpp
./chess.exe
```
### Linux
```
g++ -o chess main.cpp ChessPieces/*.cpp Chessboard/*.cpp Player/*.cpp
./chess
```
## Voraussetzungen

View File

@@ -1,4 +1,7 @@
#ifdef _WIN32
#include <windows.h>
#endif
#include <fstream>
#include "Player/Player.hpp"
#include "ChessPieces/Knight.hpp"
#include "ChessPieces/Bishop.hpp"
@@ -13,7 +16,9 @@
#include "Chessboard/ChessboardVisualizerGraphic.hpp"
int main(int argc, char* argv[]) {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
std::cout << "Spieler A: ";
std::string namePlayerA;
@@ -69,13 +74,68 @@ int main(int argc, char* argv[]) {
//ChessboardVisualizerText* visualizer = new ChessboardVisualizerText();
ChessboardVisualizerGraphic* visualizer = new ChessboardVisualizerGraphic();
visualizer->Draw(&chessboard);
// chessgames.com/1649097
// https://www.chessgames.com/perl/chessgame?gid=1649097
//
//1258478 - Stalemate
//std::ifstream input("C:\\Users\\Sebastian Brosch\\Desktop\\2767717.pgn");
// std::ifstream input("C:\\Users\\Sebastian Brosch\\Desktop\\1258478.pgn");
// //std::ifstream input("2767713.pgn");
// //std::ifstream input("1482706.pgn");
// //std::ifstream input("1693032.pgn");
// //std::ifstream input("1272702.pgn");
// //std::ifstream input("2446807.pgn");
// //std::ifstream input("1884783.pgn");
// std::regex moves_regex("([0-9]+.)(?:\n| )?([A-Za-z][A-Za-z0-9+#-=]+)(?:\n| )?([A-Za-z][A-Za-z0-9+#-]+)?");
// std::smatch moves_matches;
// std::string moves_content;
// for (std::string line; getline(input, line);) {
// if (line[0] == '[') {
// // Info
// } else {
// moves_content.append((moves_content == "") ? "" : " ").append(line);
// }
// }
// std::regex_search(moves_content, moves_matches, moves_regex);
// std::vector<std::string> moves;
// for(std::sregex_iterator i = std::sregex_iterator(moves_content.begin(), moves_content.end(), moves_regex); i != std::sregex_iterator(); ++i) {
// std::smatch m = *i;
// if (m[2].str() != "") {
// moves.push_back(m[2].str());
// }
// if (m[3].str() != "") {
// moves.push_back(m[3].str());
// }
// }
// int move_count = 0;
// while (chessboard.IsCheckmate() == false) {
// move_count++;
// std::string move = moves.front();
// moves.erase(moves.begin());
// chessboard.MoveChessPiece(move);
// visualizer->Draw(&chessboard);
// std::cout << "SPIELZUG: " << move << " | " << move_count << std::endl;
// if (moves.size() == 0) {
// break;
// }
// }
// std::cout << std::endl;
// std::cout << "Game is over! - " << ((chessboard.GetOpponentPlayer()->GetColor() == ChessPieceColor::White) ? "White" : "Black") << " has won the game!" << std::endl;
// return 0;
while (chessboard.IsCheckmate() == false) {
visualizer->Draw(&chessboard);
std::string move;
std::cout << "Move [" << ((chessboard.GetCurrentPlayer()->GetColor() == ChessPieceColor::White) ? "White" : "Black") << "] : ";
std::cin >> move;
chessboard.MoveChessPiece(move);
visualizer->Draw(&chessboard);
}
std::cout << "Game is over! - " << chessboard.GetOpponentPlayer()->GetName() << " has won the game!" << std::endl;

2
run.ps1 Normal file
View File

@@ -0,0 +1,2 @@
& "g++" -o chess main.cpp ChessPieces/*.cpp Chessboard/*.cpp Player/*.cpp
./chess.exe

3
run.sh Normal file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
g++ -o chess main.cpp ChessPieces/*.cpp Chessboard/*.cpp Player/*.cpp
./chess