Compare commits

...

5 Commits

15 changed files with 684 additions and 339 deletions

25
ChessUtils.cpp Normal file
View File

@ -0,0 +1,25 @@
//
// Created by hamac on 18.12.2024.
//
#include <string>
#include <sstream>
#include <vector>
#include <unordered_map>
#include "ChessUtils.h"
std::vector<std::string> ChessUtils::split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
std::pair<int, int> ChessUtils::convertPosition(const std::string &pos) {
int y = pos[pos.size()-2] - 'a';
int x = ChessUtils::fileConvertion.find((pos[pos.size()-1] - '0')-1)->second;
return std::make_pair(x, y);
}

34
ChessUtils.h Normal file
View File

@ -0,0 +1,34 @@
//
// Created by hamac on 20.12.2024.
//
#ifndef CHESSUTILS_H
#define CHESSUTILS_H
//
// Created by hamac on 18.12.2024.
//
#include <string>
#include <sstream>
#include <vector>
#include <unordered_map>
class ChessUtils {
public:
inline static const std::unordered_map<int, int> fileConvertion = {
{0, 7},
{1, 6},
{2, 5},
{3, 4},
{4, 3},
{5, 2},
{6, 1},
{7, 0}
};
static std::vector<std::string> split(const std::string &s, char delim);
static std::pair<int, int> convertPosition(const std::string &pos);
};
#endif //CHESSUTILS_H

View File

@ -3,312 +3,262 @@
// //
#include "Chessboard.h" #include "Chessboard.h"
#include "Utils.cpp" #include "Chesspiece.cpp"
#include "ChessUtils.cpp"
#include "Pawn.h"
#include <iostream> #include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
class Chessboard { std::string Chessboard::getChessIcon(char identifier) {
private: switch (identifier) {
/* Constants */ case 'x': return blackSquare;
// Chars to display board case 'r': return blackRook;
// Corners case 'n': return blackKnight;
const std::string topLeft = ""; case 'b': return blackBischop;
const std::string topRight = ""; case 'q': return blackQueen;
const std::string bottomLeft = ""; case 'k': return blackKing;
const std::string bottomRight = ""; case 'p': return blackPawn;
case 'w': return whiteSquare;
// Line chars case 'R': return whiteRook;
const std::string horizontal = ""; case 'N': return whiteKnight;
const std::string vertical = ""; case 'B': return whiteBischop;
const std::string topIntersection = ""; case 'Q': return whiteQueen;
const std::string bottomIntersection = ""; case 'K': return whiteKing;
const std::string middleIntersection = ""; case 'P': return whitePawn;
default: return "";
// White pieces
const std::string whiteSquare = "\u25A1";
const std::string whiteKing = "\u2654";
const std::string whiteQueen = "\u2655";
const std::string whiteRook = "\u2656";
const std::string whiteBischop = "\u2657";
const std::string whiteKnight = "\u2658";
const std::string whitePawn = "\u2659";
// Black pieces
const std::string blackSquare = "\u25A0";
const std::string blackKing = "\u265A";
const std::string blackQueen = "\u265B";
const std::string blackRook = "\u265C";
const std::string blackBischop = "\u265D";
const std::string blackKnight = "\u265E";
const std::string blackPawn = "\u265F";
/* class fields */
std::vector<std::vector<char>> currentBoard;
// Starting formatting
std::vector<std::vector<char>> startBoard = {
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
};
std::vector<std::vector<char>> emptyBoard = {
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'}
};
// Saves turn order; true = white and false = black
bool turnOrder = true;
// ToDo: Max Size definieren?
std::queue<std::string> queue;
/* methods */
// Method returns unicode for chess icon depending on the identifier
std::string getChessIcon(char identifier) {
switch (identifier) {
case 'x': return blackSquare;
case 'r': return blackRook;
case 'n': return blackKnight;
case 'b': return blackBischop;
case 'q': return blackQueen;
case 'k': return blackKing;
case 'p': return blackPawn;
case 'w': return whiteSquare;
case 'R': return whiteRook;
case 'N': return whiteKnight;
case 'B': return whiteBischop;
case 'Q': return whiteQueen;
case 'K': return whiteKing;
case 'P': return whitePawn;
default: return "";
}
} }
}
void generateTopLine() { void Chessboard::generateTopLine() {
display += " " + horizontal + " " + topLeft; this->display += " " + horizontal + " " + topLeft;
for (int col = 0; col < boardSize; ++col) {
this->display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) this->display += topIntersection;
}
this->display += topRight + "\n";
}
void Chessboard::generatePlayingField(const std::vector<std::vector<char>>& chessboard) {
char desc = 56;
for (int row = 0; row < boardSize; ++row) {
this->display += " ";
this->display += desc--;
this->display += " " + vertical;
for (int col = 0; col < boardSize; ++col) { for (int col = 0; col < boardSize; ++col) {
display += horizontal + horizontal + horizontal; this->display += " " + Chessboard::getChessIcon(chessboard[row][col]) + " " + vertical;
if (col < boardSize - 1) display += topIntersection;
} }
display += topRight + "\n"; this->display += "\n";
}
void generatePlayingField(const std::vector<std::vector<char>>& chessboard) { // Horizontale Trennlinie (außer nach der letzten Zeile)
char desc = 56; if (row < boardSize - 1) {
for (int row = 0; row < boardSize; ++row) { this->display += " " + horizontal + " " + vertical;
display += " ";
display += desc--;
display += " " + vertical;
for (int col = 0; col < boardSize; ++col) { for (int col = 0; col < boardSize; ++col) {
display += " " + getChessIcon(chessboard[row][col]) + " " + vertical; this->display += horizontal + horizontal + horizontal;
} if (col < boardSize - 1) this->display += middleIntersection;
display += "\n";
// Horizontale Trennlinie (außer nach der letzten Zeile)
if (row < boardSize - 1) {
display += " " + horizontal + " " + vertical;
for (int col = 0; col < boardSize; ++col) {
display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) display += middleIntersection;
}
display += vertical + "\n";
} }
this->display += vertical + "\n";
} }
} }
}
void generateBottomLine() { void Chessboard::generateBottomLine() {
char desc = 65; char desc = 65;
display += " " + horizontal + " " + bottomLeft; this->display += " " + horizontal + " " + bottomLeft;
for (int col = 0; col < boardSize; ++col) { for (int col = 0; col < boardSize; ++col) {
display += horizontal + horizontal + horizontal; this->display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) display += bottomIntersection; if (col < boardSize - 1) this->display += bottomIntersection;
}
display += bottomRight + "\n";
display += " " + vertical;
for (int col = 0; col < boardSize; ++col) {
display += " ";
display += (desc++);
display += " " + vertical;
}
} }
this->display += bottomRight + "\n";
public: this->display += " " + vertical;
/* fields */ for (int col = 0; col < boardSize; ++col) {
std::string display; this->display += " ";
int boardSize = 8; this->display += (desc++);
this->display += " " + vertical;
bool getTurnOrder() {
return this->turnOrder;
} }
}
void setTurnOrder(bool turnOrder) { /* methods */
this->turnOrder = turnOrder; bool Chessboard::getTurnOrder() { return this->turnOrder; }
} void Chessboard::setTurnOrder(bool turnOrder) { this->turnOrder = turnOrder; }
/* methods */ std::vector<std::vector<char>> Chessboard::getBoard() { return this->currentBoard; }
void setBoard(std::vector<std::vector<char>> board) { void Chessboard::setBoard(std::vector<std::vector<char>> board) { this->currentBoard = board; }
this->currentBoard = board;
}
std::vector<std::vector<char>> getBoard() { std::vector<std::vector<char>> Chessboard::getStartBoard() { return this->startBoard; }
return this->currentBoard;
}
std::vector<std::vector<char>> getStartBoard() { std::vector<std::vector<char>> Chessboard::getEmptyBoard() { return this->emptyBoard; }
return this->startBoard;
}
std::vector<std::vector<char>> getEmptyBoard() { void Chessboard::draw () {
return this->emptyBoard; Chessboard::draw(getStartBoard());
} }
void draw () { void Chessboard::draw (const std::vector<std::vector<char>>& chessboard) {
draw(getStartBoard()); // Obere Rahmenlinie
} Chessboard::generateTopLine();
void draw (const std::vector<std::vector<char>>& chessboard) { // Schachbrett mit vertikalen Linien
// Obere Rahmenlinie Chessboard::generatePlayingField(chessboard);
generateTopLine();
// Schachbrett mit vertikalen Linien // Untere Rahmenlinie
generatePlayingField(chessboard); Chessboard::generateBottomLine();
// Untere Rahmenlinie Chessboard::setBoard(chessboard);
generateBottomLine();
setBoard(chessboard); std::cout << this->display << std::endl;
}
std::cout << display << std::endl; // ToDo:
} // Methode um mögliche Züge anzuzeigen
// Rochade
// Mate
// Schachmate
// En Passond
// Restlichen moves
// Angabe mit Menü Optionen als Zahlen
// Figuren wählen mit Schachnotation
void Chessboard::init() {
Chessboard::initPieces();
}
void Chessboard::initPieces() {
/*playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,0)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,1)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,2)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,3)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,4)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,5)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,6)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,7)));
playingPieces['w'].push_back(std::make_unique<Rook>('w', std::make_pair(7,0)));
playingPieces['w'].push_back(std::make_unique<Knight>('w', std::make_pair(7,1)));
playingPieces['w'].push_back(std::make_unique<Bischop>('w', std::make_pair(7,2)));
playingPieces['w'].push_back(std::make_unique<Queen>('w', std::make_pair(7,3)));
playingPieces['w'].push_back(std::make_unique<King>('w', std::make_pair(7,4)));
playingPieces['w'].push_back(std::make_unique<Bischop>('w', std::make_pair(7,5)));
playingPieces['w'].push_back(std::make_unique<Knight>('w', std::make_pair(7,6)));
playingPieces['w'].push_back(std::make_unique<Rooke>('w', std::make_pair(7,7)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(1,0)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,1)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,2)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,3)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,4)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,5)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,6)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,7)));
playingPieces['b'].push_back(std::make_unique<Rook>('b', std::make_pair(0,0)));
playingPieces['b'].push_back(std::make_unique<Knight>('b', std::make_pair(0,1)));
playingPieces['b'].push_back(std::make_unique<Bischop>('b', std::make_pair(0,2)));
playingPieces['b'].push_back(std::make_unique<Queen>('b', std::make_pair(0,3)));
playingPieces['b'].push_back(std::make_unique<King>('b', std::make_pair(0,4)));
playingPieces['b'].push_back(std::make_unique<Bischop>('b', std::make_pair(0,5)));
playingPieces['b'].push_back(std::make_unique<Knight>('b', std::make_pair(0,6)));
playingPieces['b'].push_back(std::make_unique<Rook>('b', std::make_pair(0,7)));*/
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,0)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,1)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,2)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,3)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,4)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,5)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,6)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(6,7)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(7,0)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(7,1)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(7,2)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(7,3)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(7,4)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(7,5)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(7,6)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(7,7)));
playingPieces['w'].push_back(std::make_unique<Pawn>('w', std::make_pair(1,0)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,1)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,2)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,3)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,4)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,5)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,6)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(1,7)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(0,0)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(0,1)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(0,2)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(0,3)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(0,4)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(0,5)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(0,6)));
playingPieces['b'].push_back(std::make_unique<Pawn>('b', std::make_pair(0,7)));
}
void Chessboard::move() {
}
char Chessboard::getCorrectPiece(char pieceIdentifier) {
bool isWhite = Chessboard::getTurnOrder();
if (!isWhite) return pieceIdentifier + 32;
return pieceIdentifier;
}
//
void Chessboard::move(std::string move) {
// ToDo: // ToDo:
// Methode um mögliche Züge anzuzeigen // add move to history queue
// Rochade
// Mate
// Schachmate
// En Passond
// Restlichen moves
// Angabe mit Menü Optionen als Zahlen
// Figuren wählen mit Schachnotation
int getCorrectPiece(char pieceIdentifier) { std::vector<std::string> splitMove;
bool isWhite = getTurnOrder();
static const std::unordered_map<char, int> pieceMap = { if (move.find('#') != std::string::npos) {
{'P', 200}, // Schachmate
{'K', 205}, // Finish game
{'Q', 204}, } else if (move.rfind("0-0", 0) == 0) { // Kleine Rochade
{'N', 202}, // Check for Rochade
{'R', 201}, } else if (move.rfind("0-0-0", 0) == 0) {
{'B', 203} // Große Rochade
};
int piece = pieceMap.find(pieceIdentifier)->second;
if (isWhite) {
return piece - 100;
}
return piece;
} }
std::pair<int, int> convertPosition(const std::string &pos) { // Standard move
static const std::unordered_map<int, int> fileConvertion = { if (move.find('-')) {
{0, 7}, splitMove = ChessUtils::split(move, '-');
{1, 6}, } else if (move.find('x')) {
{2, 5}, splitMove = ChessUtils::split(move, 'x');
{3, 4},
{4, 3},
{5, 2},
{6, 1},
{7, 0}
};
int y = pos[pos.size()-2] - 'a';
int x = fileConvertion.find((pos[pos.size()-1] - '0')-1)->second;
return std::make_pair(x, y);
} }
// std::pair oldCoords = ChessUtils::convertPosition(splitMove[0]);
void move(std::string move) { std::pair newCoords = ChessUtils::convertPosition(splitMove[1]);
// ToDo:
// add move to history queue
std::vector<std::string> splitMove; std::vector<std::vector<char>> board = getBoard();
if (move.find('#') != std::string::npos) { board[oldCoords.first][oldCoords.second] = getEmptyBoard()[oldCoords.first][oldCoords.second];
// Schachmate board[newCoords.first][newCoords.second] = getCorrectPiece(splitMove[0][0]);
// Finish game
} else if (move.rfind("0-0", 0) == 0) { // Kleine Rochade
// Check for Rochade
} else if (move.rfind("0-0-0", 0) == 0) {
// Große Rochade
}
// Standard move Chessboard::draw(board);
if (move.find('-')) {
splitMove = Utils::split(move, '-');
} else if (move.find('x')) {
splitMove = Utils::split(move, 'x');
}
std::pair oldCoords = convertPosition(splitMove[0]); // Notation
std::pair newCoords = convertPosition(splitMove[1]); // Start with current position - dash - new position
// eg.: b1-c3
// Letter first than number
// eg.: a5
// Pawn: e4 or p e4 (pawn)
// R for rook
// N for Knight
// K for King
// B for Bischop
// Q for Queen
// Special:
// 0-0 short castle
// 0-0-0 long castle
// en passond: write square where pawn lands
std::vector<std::vector<char>> board = getBoard(); // capture: x
// check: +
// checkmate: #
// draw/stalemate: 1/2-1/2
}
getCorrectPiece(board[oldCoords.first][oldCoords.second]); // This method saves the current board state
void Chessboard::saveBoard(Chessboard& chessboard) {
board[oldCoords.first][oldCoords.second] = getEmptyBoard()[oldCoords.first][oldCoords.second]; }
board[newCoords.first][newCoords.second] = getCorrectPiece(splitMove[0][0]);
draw(board); // This method loads a save state
void Chessboard::loadBoard(int saveState) {
// Notation // readJSONFile
// Start with current position - dash - new position //
// eg.: b1-c3 }
// Letter first than number
// eg.: a5
// Pawn: e4 or p e4 (pawn)
// R for rook
// N for Knight
// K for King
// B for Bischop
// Q for Queen
// Special:
// 0-0 short castle
// 0-0-0 long castle
// en passond: write square where pawn lands
// capture: x
// check: +
// checkmate: #
// draw/stalemate: 1/2-1/2
}
// This method saves the current board state
void saveBoard(Chessboard& chessboard) {
}
// This method loads a save state
void loadBoard(int saveState) {
// readJSONFile
//
}
};

View File

@ -1,8 +1,143 @@
// //
// Created by hamac on 18.12.2024. // Created by hamac on 20.12.2024.
// //
#ifndef CHESSBOARD_H #ifndef CHESSBOARD_H
#define CHESSBOARD_H #define CHESSBOARD_H
#include "Chesspiece.h"
#include <queue>
#include <string>
#include <vector>
#include <unordered_map>
class Chessboard {
private:
/* Constants */
// Chars to display board
// Corners
const std::string topLeft = "";
const std::string topRight = "";
const std::string bottomLeft = "";
const std::string bottomRight = "";
// Line chars
const std::string horizontal = "";
const std::string vertical = "";
const std::string topIntersection = "";
const std::string bottomIntersection = "";
const std::string middleIntersection = "";
// White pieces
const std::string whiteSquare = "";
const std::string whiteKing = "";
const std::string whiteQueen = "";
const std::string whiteRook = "";
const std::string whiteBischop = "";
const std::string whiteKnight = "";
const std::string whitePawn = "";
// Black pieces
const std::string blackSquare = "";
const std::string blackKing = "";
const std::string blackQueen = "";
const std::string blackRook = "";
const std::string blackBischop = "";
const std::string blackKnight = "";
const std::string blackPawn = "";
/* class fields */
std::vector<std::vector<char>> currentBoard;
std::unordered_map<char, std::vector<std::unique_ptr<Chesspiece>>> playingPieces;
// Starting formatting
std::vector<std::vector<char>> startBoard = {
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'},
};
// ToDo: Mathematisch sicherlich weg rationalisierbar
std::vector<std::vector<char>> emptyBoard = {
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'},
{'w', 'x', 'w', 'x' ,'w', 'x', 'w', 'x'},
{'x', 'w', 'x', 'w', 'x', 'w', 'x', 'w'}
};
// Saves turn order; true = white and false = black
bool turnOrder = true;
// ToDo: Max Size definieren?
std::queue<std::string> queue;
/* methods */
// Method returns unicode for chess icon depending on the identifier
std::string getChessIcon(char identifier);
void generateTopLine();
void generatePlayingField(const std::vector<std::vector<char>>& chessboard);
void generateBottomLine();
public:
/* fields */
std::string display;
int boardSize = 8;
/* methods */
bool getTurnOrder();
void setTurnOrder(bool turnOrder);
std::vector<std::vector<char>> getBoard();
void setBoard(std::vector<std::vector<char>> board);
std::vector<std::vector<char>> getStartBoard();
std::vector<std::vector<char>> getEmptyBoard();
void draw ();
void draw (const std::vector<std::vector<char>>& chessboard);
// ToDo:
// Methode um mögliche Züge anzuzeigen
// Rochade
// Mate
// Schachmate
// En Passond
// Restlichen moves
// Angabe mit Menü Optionen als Zahlen
// Figuren wählen mit Schachnotation
void init();
void initPieces();
void move();
char getCorrectPiece(char pieceIdentifier);
//
void move(std::string move);
// This method saves the current board state
void saveBoard(Chessboard& chessboard);
// This method loads a save state
void loadBoard(int saveState);
};
#endif //CHESSBOARD_H #endif //CHESSBOARD_H

View File

@ -2,29 +2,24 @@
// Created by hamac on 18.12.2024. // Created by hamac on 18.12.2024.
// //
#include <string> #include "Chesspiece.h"
#include <vector>
class Chesspiece { Chesspiece::Chesspiece(char color, char identifier)
private: : color(color), identifier(identifier) {}
/* fields */
char color;
char identifier;
std::string position;
std::vector<std::string> positions;
/* methods */ std::pair<int, int> Chesspiece::getPosition() { return this->position; };
virtual std::vector<std::string> calcAvaibleMoves() {} void Chesspiece::setPosition(std::pair<int, int> position) {
this->position = position;
calcAvaibleMoves();
}
public: char Chesspiece::getColor() { return color; }
std::string getPosition() { return this->position; }; void Chesspiece::setColor(char color) { color = color; }
void setPosition(std::string position) {
this->position = position;
calcAvaibleMoves();
}
char getColor() { return this->color; } std::vector<std::string> Chesspiece::getPositions() { return positions; }
void setColor(char color) { this->color = color; }
std::vector<std::string> getPositions() { return this->positions; } bool Chesspiece::checkNotation(std::string notation) {
}; std::regex pattern("R^([KQRNBP]?)([a-h][1-8])[-x]([a-h][1-8])([+#]?)$");
std::smatch match;
return (std::regex_search(notation, match, pattern) ? true : false);
}

45
Chesspiece.h Normal file
View File

@ -0,0 +1,45 @@
//
// Created by hamac on 19.12.2024.
//
#ifndef CHESSPIECE_H
#define CHESSPIECE_H
//
// Created by hamac on 18.12.2024.
//
#include <string>
#include <vector>
#include <regex>
class Chesspiece {
private:
/* fields */
// ToDo: von char auf enum Color {W, B} umstellen
char color;
char identifier;
std::pair<int, int> position;
std::vector<std::string> positions;
/* methods */
virtual std::vector<std::string> calcAvaibleMoves() = 0;
public:
Chesspiece(char color, char identifier);
virtual ~Chesspiece() = default;
std::pair<int, int> getPosition();
void setPosition(std::pair<int, int> position);
char getColor();
void setColor(char color);
std::vector<std::string> getPositions();
bool checkNotation(std::string notation);
};
#endif //CHESSPIECE_H

View File

@ -1,27 +0,0 @@
//
// Created by hamac on 18.12.2024.
//
#include <string>
#include <vector>
#ifndef CHESSPIECES_H
#define CHESSPIECES_H
#endif //CHESSPIECES_H
class Chesspiece {
private:
char color;
std::string position;
virtual std::vector<std::string> calcAvaibleMoves() {}
public:
std::string getPosition();
void setPosition(std::string position);
char getColor();
void setColor(char color);
std::vector<std::string> getPositions();
};

96
MoveList.cpp Normal file
View File

@ -0,0 +1,96 @@
#include "MoveList.h"
MoveList::MoveList() {
this->head->next = nullptr;
}
MoveList::~MoveList() {
Node *current = head;
std::cout << "Destructor: " << std::endl;
while (current != nullptr) {
Node *nextNode = current->next;
std::cout << " delete on " << current->data.first << " " << current->data.second << std::endl;
delete current;
current = nextNode;
}
}
MoveList &display() {
Node *currentNode = this->head;
while (currentNode->next != nullptr) {
currentNode = currentNode->next;
std::cout << "[" << currentNode->data.first << " " << currentNode->data.second << " | " << currentNode->next << ((currentNode->next != nullptr) ? "] ---> " : "] ");
}
std::cout << std::endl;
return *this;
}
MoveList &append(const std::pair<int, int> &data) {
if(this->head->next == nullptr) {
Node *node = new Node;
node->data = data;
this->head->next = node;
} else {
Node *newNode = new Node;
newNode->data = data;
newNode->next = nullptr;
Node *currentNode = this->head;
while (currentNode->next != nullptr) {
currentNode = currentNode->next;
}
currentNode->next = newNode;
}
return *this;
}
MoveList &insert(const std::pair<int, int> &data) {
Node *newNode = new Node;
newNode->data = data;
newNode->next = this->head->next;
this->head->next = newNode;
return *this;
}
Node* MoveList::search(const std::pair<int, int> &data) const {
Node *currentNode = this->head;
do {
if (currentNode->data == data) {
return currentNode;
}
} while(currentNode = currentNode->next);
return nullptr;
}
MoveList &remove(const std::pair<int, int> &data) {
Node *currentNode = this->head;
do {
if (currentNode->next->data == data) {
Node *temp = currentNode->next;
currentNode->next = currentNode->next->next;
delete temp;
break;
}
} while((currentNode = currentNode->next) && (currentNode->next != nullptr));
return *this;
}
void MoveList::removeTail(const std::pair<int, int> &data) {
Node *found = search(data);
if(found != nullptr) {
found->next = nullptr;
}
}

38
MoveList.h Normal file
View File

@ -0,0 +1,38 @@
//
// Created by hamac on 20.12.2024.
//
#ifndef MOVELIST_H
#define MOVELIST_H
#include <iostream>
struct Node {
std::pair<int, int> data;
Node *next;
};
class MoveList {
private:
Node *head;
public:
MoveList();
~MoveList();
MoveList &display();
MoveList &append(const std::pair<int, int> &data);
MoveList &insert(const std::pair<int, int> &data);
Node* search(const std::pair<int, int> &data) const;
MoveList &remove(const std::pair<int, int> &data);
void removeTail(const std::pair<int, int> &data);
};
#endif //MOVELIST_H

27
Pawn.cpp Normal file
View File

@ -0,0 +1,27 @@
//
// Created by hamac on 19.12.2024.
//
#include "Chesspiece.h"
#include "Pawn.h"
#include <iostream>
/* methods */
std::vector<std::string> Pawn::calcAvaibleMoves() {
std::pair<int, int> pos = Chesspiece::getPosition();
std::vector<std::string> moves;
if (this->firstMove) {
//moves.
}
std::cout << "Hello World!\n" << std::endl;
return moves;
}
Pawn::Pawn(char color, std::pair<int, int> position): Chesspiece(color, 'P') {
setPosition(position);
setColor(color);
}

28
Pawn.h Normal file
View File

@ -0,0 +1,28 @@
//
// Created by hamac on 20.12.2024.
//
#ifndef PAWN_H
#define PAWN_H
//
// Created by hamac on 19.12.2024.
//
#include <string>
#include <vector>
#include "Chesspiece.h"
class Pawn: public Chesspiece {
private:
/* fields */
bool firstMove = true;
bool movedTwoFields = false;
/* methods */
std::vector<std::string> calcAvaibleMoves() override ;
public:
Pawn(char color, std::pair<int, int> position);
};
#endif //PAWN_H

View File

@ -55,3 +55,19 @@
1. Bedienung per Maus 1. Bedienung per Maus
2. Multiplayer 2. Multiplayer
3. Historie der Spielzüge 3. Historie der Spielzüge
# Gedankensammlung
- Initialisierung:
- Brett zeichnen
- Figuren mit Zugmöglichkeiten ermitteln
- Züge kalkulieren
- Objekt mit allen Figuren erstellen -> alle Figuren initialisieren
- Kalkulation der Züge
- Anhand der aktuellen Position
- Mit einbeziehen, dass Zug König nicht in Mate versetzt (verboten)
- Sonder Züge, wie En passant, Rochade (Lang, Kurz), Bauern zwei Feld, beachten
- Prüfen, ob andere Figuren im Weg stehen
- Array von Linked Lists
- Array Index steht für Richtung von Bewegungen (diagonalen, L, Gerade)
- alles droppen, was nach pos mit Figur kommt
-

View File

@ -1,20 +0,0 @@
//
// Created by hamac on 18.12.2024.
//
#include <string>
#include <sstream>
#include <vector>
class Utils {
public:
static std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
};

View File

@ -2,13 +2,16 @@
#include <windows.h> #include <windows.h>
#include "Chessboard.cpp" #include "Chessboard.cpp"
#include "Pawn.cpp"
int main() { int main() {
SetConsoleOutputCP(CP_UTF8); SetConsoleOutputCP(CP_UTF8);
//Pawn pawn('w', {1,6});
Chessboard chessboard; Chessboard chessboard;
chessboard.draw(); chessboard.init();
//chessboard.move("Pb2-b3"); /*chessboard.draw();
chessboard.move("Pb2-b3");*/
return 0; return 0;
} }

BIN
main.exe

Binary file not shown.