Compare commits

...

2 Commits

Author SHA1 Message Date
Sebastian Brosch
074cbef775 added graphical chessboard 2025-01-04 15:48:04 +01:00
Sebastian Brosch
c7cb4b3fcb output of chessboard as text 2025-01-04 14:35:28 +01:00
8 changed files with 313 additions and 10 deletions

View File

@@ -17,7 +17,7 @@ ChessPiece* Chessboard::GetChessPiece(ChessPiecePosition* position) {
void Chessboard::UpdateChessPieces() {
for (auto const& field : chessboard) {
field.second->UpdateNextPositions(this);
std::cout << "calculated moves of " << field.second->GetAlgebraicNotationSymbol() << " at position " << field.second->GetPosition().ToString() << ": " << field.second->GetNextPositions().size() << std::endl;
//std::cout << "calculated moves of " << field.second->GetAlgebraicNotationSymbol() << " at position " << field.second->GetPosition().ToString() << ": " << field.second->GetNextPositions().size() << std::endl;
}
}
@@ -52,14 +52,15 @@ std::pair<ChessPiecePosition, ChessPiecePosition> Chessboard::GetLastMove() {
}
bool Chessboard::IsCheck() {
return false;
}
bool Chessboard::IsCheckmate() {
return false;
}
bool Chessboard::IsStalemate() {
// Der König steht nicht im Schach oder Schachmatt.
// Der König hat aber auch keine validen Züge mehr.
return false;
}

View File

@@ -0,0 +1 @@
#include "../Chessboard/ChessboardVisualizer.hpp"

View File

@@ -0,0 +1,13 @@
#ifndef ChessboardVisualizer_H
#define ChessboardVisualizer_H
#include "../ChessPieces/ChessPiece.hpp"
#include "../Chessboard/Chessboard.hpp"
class ChessboardVisualizer {
public:
ChessboardVisualizer() = default;
virtual void Draw(Chessboard chessboard) = 0;
};
#endif

View File

@@ -0,0 +1,165 @@
#include "../Chessboard/ChessboardVisualizerGraphic.hpp"
#include "../ChessPieces/ChessPiecePosition.hpp"
void ChessboardVisualizerGraphic::SetConsoleColor(Colors foreground, Colors background) {
const std::string SEQ_FOREGROUND = "\x1b[3";
const std::string SEQ_FOREGROUND_LIGHT = "\x1b[9";
const std::string SEQ_BACKGROUND = "\x1b[4";
const std::string SEQ_BACKGROUND_LIGHT = "\x1b[10";
// set foreground color
switch (foreground) {
case BLACK:
case RED:
case GREEN:
case YELLOW:
case BLUE:
case MAGENTA:
case CYAN:
case WHITE:
std::cout << SEQ_FOREGROUND << foreground << "m";
break;
case LIGHT_BLACK:
case LIGHT_RED:
case LIGHT_GREEN:
case LIGHT_YELLOW:
case LIGHT_BLUE:
case LIGHT_MAGENTA:
case LIGHT_CYAN:
case LIGHT_WHITE:
std::cout << SEQ_FOREGROUND_LIGHT << foreground << "m";
break;
case DEFAULT:
default:
std::cout << SEQ_FOREGROUND << foreground << "m";
break;
}
// set background color
switch (background) {
case BLACK:
case RED:
case GREEN:
case YELLOW:
case BLUE:
case MAGENTA:
case CYAN:
case WHITE:
std::cout << SEQ_BACKGROUND << background << "m";
break;
case LIGHT_BLACK:
case LIGHT_RED:
case LIGHT_GREEN:
case LIGHT_YELLOW:
case LIGHT_BLUE:
case LIGHT_MAGENTA:
case LIGHT_CYAN:
case LIGHT_WHITE:
std::cout << SEQ_BACKGROUND_LIGHT << background << "m";
break;
case DEFAULT:
default:
std::cout << SEQ_BACKGROUND << background << "m";
break;
}
}
void ChessboardVisualizerGraphic::Draw(Chessboard chessboard) {
std::cout << "\033[2J"; // clear the console
std::cout << std::endl;
for (int rank = 8; rank >= 1; rank--) {
if (rank == 8) {
for (int i = 0; i < 8; i++) {
if (i == 0) {
std::cout << " " << " " << " ";
std::cout << "\u2554";
}
std::cout << "\u2550" << "\u2550" << "\u2550" << "\u2550" << "\u2550" << "\u2550" << "\u2550";
if (i == 7) {
std::cout << "\u2557";
}
}
std::cout << std::endl;
}
for (int i = 0; i < 3; i++) {
for (char file = 'A'; file <= 'H'; file++) {
if (i == 1) {
if (file == 'A') {
std::cout << " " << rank << " ";
std::cout << "\u2551";
}
} else {
if (file == 'A') {
std::cout << " " << " " << " ";
std::cout << "\u2551";
}
}
if (file % 2 == (rank % 2)) {
this->SetConsoleColor(BLACK, WHITE);
if (i == 1) {
ChessPiecePosition* position = new ChessPiecePosition(file, rank);
if (chessboard.IsEmptyField(position)) {
std::cout << " ";
} else {
ChessPiece* piece = chessboard.GetChessPiece(position);
std::cout << " " << piece->GetCharUnicode() << " ";
}
} else {
std::cout << " ";
}
} else {
this->SetConsoleColor(WHITE, BLACK);
if (i == 1) {
ChessPiecePosition* position = new ChessPiecePosition(file, rank);
if (chessboard.IsEmptyField(position)) {
std::cout << " ";
} else {
ChessPiece* piece = chessboard.GetChessPiece(position);
std::cout << " " << piece->GetCharUnicode() << " ";
}
} else {
std::cout << " ";
}
}
if (file == 'H') {
this->SetConsoleColor(DEFAULT, DEFAULT);
std::cout << "\u2551";
}
}
this->SetConsoleColor(DEFAULT, DEFAULT);
std::cout << std::endl;
}
if (rank == 1) {
for (int i = 0; i < 8; i++) {
if (i == 0) {
std::cout << " " << " " << " ";
std::cout << "\u255A";
}
std::cout << "\u2550" << "\u2550" << "\u2550" << "\u2550" << "\u2550" << "\u2550" << "\u2550";
if (i == 7) {
std::cout << "\u255D";
}
}
std::cout << std::endl;
for (char file = 'A'; file <= 'H'; file++) {
if (file == 'A') {
std::cout << " ";
std::cout << " ";
}
std::cout << " " << file << " ";
if (file == 'H') {
std::cout << " ";
}
}
std::cout << std::endl;
}
}
this->SetConsoleColor(DEFAULT, DEFAULT);
std::cout << std::endl;
}

View File

@@ -0,0 +1,21 @@
#ifndef ChessboardVisualizerGraphic_H
#define ChessboardVisualizerGraphic_H
#include "../Chessboard/ChessboardVisualizer.hpp"
enum Colors {
BLACK = 0, RED = 1, GREEN = 2, YELLOW = 3, BLUE = 4, MAGENTA = 5, CYAN = 6, WHITE = 7,
LIGHT_BLACK = 10, LIGHT_RED = 11, LIGHT_GREEN = 12, LIGHT_YELLOW = 13, LIGHT_BLUE = 14,
LIGHT_MAGENTA = 15, LIGHT_CYAN = 16, LIGHT_WHITE = 17, DEFAULT = 9
};
class ChessboardVisualizerGraphic : public ChessboardVisualizer {
private:
void SetConsoleColor(Colors foreground, Colors background);
public:
ChessboardVisualizerGraphic() = default;
void Draw(Chessboard chessboard);
};
#endif

View File

@@ -0,0 +1,88 @@
#include "../Chessboard/ChessboardVisualizerText.hpp"
#include "../ChessPieces/ChessPiecePosition.hpp"
void ChessboardVisualizerText::Draw(Chessboard chessboard) {
std::cout << "\033[2J"; // clear the console
std::cout << std::endl;
for (int rank = 8; rank >= 1; rank--) {
if (rank == 8) {
for (int i = 0; i < 8; i++) {
if (i == 0) {
std::cout << " " << " " << " ";
std::cout << "\u250F";
} else {
std::cout << "\u2533";
}
std::cout << "\u2501" << "\u2501" << "\u2501";
if (i == 7) {
std::cout << "\u2513";
}
}
std::cout << std::endl;
}
for (char file = 'A'; file <= 'H'; file++) {
if (file == 'A') { // Beginn der Zeile
std::cout << " " << rank << " ";
std::cout << "\u2503";
}
ChessPiecePosition* position = new ChessPiecePosition(file, rank);
if (chessboard.IsEmptyField(position)) {
std::cout << " " << " " << " ";
} else {
ChessPiece* piece = chessboard.GetChessPiece(position);
std::cout << " " << piece->GetCharUnicode() << " ";
}
std::cout << "\u2503";
}
std::cout << std::endl;
if (rank != 1) {
for (int i = 0; i < 8; i++) {
if (i == 0) {
std::cout << " " << " " << " ";
std::cout << "\u2523";
} else {
std::cout << "\u254B";
}
std::cout << "\u2501" << "\u2501" << "\u2501";
if (i == 7) {
std::cout << "\u252B";
}
}
std::cout << std::endl;
} else {
for (int i = 0; i < 8; i++) {
if (i == 0) {
std::cout << " " << " " << " ";
std::cout << "\u2517";
} else {
std::cout << "\u253B";
}
std::cout << "\u2501" << "\u2501" << "\u2501";
if (i == 7) {
std::cout << "\u251B";
}
}
std::cout << std::endl;
for (char file = 'A'; file <= 'H'; file++) {
if (file == 'A') {
std::cout << " " << " " << " ";
std::cout << " ";
} else {
std::cout << " ";
}
std::cout << " " << file << " ";
if (file == 'H') {
std::cout << " ";
}
}
std::cout << std::endl;
}
}
std::cout << std::endl;
}

View File

@@ -0,0 +1,15 @@
#ifndef ChessboardVisualizerText_H
#define ChessboardVisualizerText_H
#include "../Chessboard/ChessboardVisualizer.hpp"
class ChessboardVisualizerText : public ChessboardVisualizer {
private:
void Clear();
public:
ChessboardVisualizerText() = default;
void Draw(Chessboard chessboard);
};
#endif

View File

@@ -9,6 +9,8 @@
#include "ChessPieces/ChessPiece.hpp"
#include "Chessboard/Chessboard.hpp"
#include "ChessPieces/ChessPiecePosition.hpp"
#include "Chessboard/ChessboardVisualizerText.hpp"
#include "Chessboard/ChessboardVisualizerGraphic.hpp"
int main(int argc, char* argv[]) {
SetConsoleOutputCP(CP_UTF8);
@@ -107,23 +109,20 @@ int main(int argc, char* argv[]) {
chessboard.SetChessPiece(new Pawn{ChessPieceColor::White, ChessPiecePosition{'G', 2}});
chessboard.SetChessPiece(new Pawn{ChessPieceColor::White, ChessPiecePosition{'H', 2}});
// ChessPiece* aa = chessboard.GetChessPiece(ChessPiecePosition('A', 1));
// std::cout << "Test: " << aa->GetAlgebraicNotationSymbol() << std::endl;
// std::cout << aa->GetColorName() << std::endl;
// std::cout << "First Move? - " << chessboard.GetChessPiece(ChessPiecePosition('A', 1))->IsFirstMove() << std::endl;
// Jetzt kann man Moves machen. Nach jedem Move müssen die ChessPieces aktualisiert werden.
chessboard.UpdateChessPieces();
//ChessboardVisualizerText* visualizer = new ChessboardVisualizerText();
ChessboardVisualizerGraphic* visualizer = new ChessboardVisualizerGraphic();
while (true) {
visualizer->Draw(chessboard);
std::string move;
std::cout << "Move: ";
std::cin >> move;
chessboard.MoveChessPiece(move);
}
// chessboard.SetChessPiece(rook1);
// ChessPiece aa = chessboard.GetChessPiece(rook1.GetPosition());