Compare commits

..

No commits in common. "dc31b31f4b2ebb7ff7c1339aa59542ce8444d266" and "390ebb597e8f27cda1ac08bfbe9eaf9aedc0cf98" have entirely different histories.

15 changed files with 339 additions and 684 deletions

View File

@ -1,25 +0,0 @@
//
// 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);
}

View File

@ -1,34 +0,0 @@
//
// 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,262 +3,312 @@
// //
#include "Chessboard.h" #include "Chessboard.h"
#include "Chesspiece.cpp" #include "Utils.cpp"
#include "ChessUtils.cpp"
#include "Pawn.h"
#include <iostream> #include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
std::string Chessboard::getChessIcon(char identifier) { class Chessboard {
switch (identifier) { private:
case 'x': return blackSquare; /* Constants */
case 'r': return blackRook; // Chars to display board
case 'n': return blackKnight; // Corners
case 'b': return blackBischop; const std::string topLeft = "";
case 'q': return blackQueen; const std::string topRight = "";
case 'k': return blackKing; const std::string bottomLeft = "";
case 'p': return blackPawn; const std::string bottomRight = "";
case 'w': return whiteSquare;
case 'R': return whiteRook; // Line chars
case 'N': return whiteKnight; const std::string horizontal = "";
case 'B': return whiteBischop; const std::string vertical = "";
case 'Q': return whiteQueen; const std::string topIntersection = "";
case 'K': return whiteKing; const std::string bottomIntersection = "";
case 'P': return whitePawn; const std::string middleIntersection = "";
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 Chessboard::generateTopLine() { void generateTopLine() {
this->display += " " + horizontal + " " + topLeft; 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) {
this->display += " " + Chessboard::getChessIcon(chessboard[row][col]) + " " + vertical; display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) display += topIntersection;
} }
this->display += "\n"; display += topRight + "\n";
}
// Horizontale Trennlinie (außer nach der letzten Zeile) void generatePlayingField(const std::vector<std::vector<char>>& chessboard) {
if (row < boardSize - 1) { char desc = 56;
this->display += " " + horizontal + " " + vertical; for (int row = 0; row < boardSize; ++row) {
display += " ";
display += desc--;
display += " " + vertical;
for (int col = 0; col < boardSize; ++col) { for (int col = 0; col < boardSize; ++col) {
this->display += horizontal + horizontal + horizontal; display += " " + getChessIcon(chessboard[row][col]) + " " + vertical;
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 Chessboard::generateBottomLine() { void generateBottomLine() {
char desc = 65; char desc = 65;
this->display += " " + horizontal + " " + bottomLeft; display += " " + horizontal + " " + bottomLeft;
for (int col = 0; col < boardSize; ++col) { for (int col = 0; col < boardSize; ++col) {
this->display += horizontal + horizontal + horizontal; display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) this->display += bottomIntersection; if (col < boardSize - 1) display += bottomIntersection;
}
display += bottomRight + "\n";
display += " " + vertical;
for (int col = 0; col < boardSize; ++col) {
display += " ";
display += (desc++);
display += " " + vertical;
}
} }
this->display += bottomRight + "\n";
this->display += " " + vertical; public:
for (int col = 0; col < boardSize; ++col) { /* fields */
this->display += " "; std::string display;
this->display += (desc++); int boardSize = 8;
this->display += " " + vertical;
bool getTurnOrder() {
return this->turnOrder;
} }
}
/* methods */ void setTurnOrder(bool turnOrder) {
bool Chessboard::getTurnOrder() { return this->turnOrder; } this->turnOrder = turnOrder;
void Chessboard::setTurnOrder(bool turnOrder) { this->turnOrder = turnOrder; } }
std::vector<std::vector<char>> Chessboard::getBoard() { return this->currentBoard; } /* methods */
void Chessboard::setBoard(std::vector<std::vector<char>> board) { this->currentBoard = board; } void setBoard(std::vector<std::vector<char>> board) {
this->currentBoard = board;
}
std::vector<std::vector<char>> Chessboard::getStartBoard() { return this->startBoard; } std::vector<std::vector<char>> getBoard() {
return this->currentBoard;
}
std::vector<std::vector<char>> Chessboard::getEmptyBoard() { return this->emptyBoard; } std::vector<std::vector<char>> getStartBoard() {
return this->startBoard;
}
void Chessboard::draw () { std::vector<std::vector<char>> getEmptyBoard() {
Chessboard::draw(getStartBoard()); return this->emptyBoard;
} }
void Chessboard::draw (const std::vector<std::vector<char>>& chessboard) { void draw () {
// Obere Rahmenlinie draw(getStartBoard());
Chessboard::generateTopLine(); }
// Schachbrett mit vertikalen Linien void draw (const std::vector<std::vector<char>>& chessboard) {
Chessboard::generatePlayingField(chessboard); // Obere Rahmenlinie
generateTopLine();
// Untere Rahmenlinie // Schachbrett mit vertikalen Linien
Chessboard::generateBottomLine(); generatePlayingField(chessboard);
Chessboard::setBoard(chessboard); // Untere Rahmenlinie
generateBottomLine();
std::cout << this->display << std::endl; setBoard(chessboard);
}
// ToDo: std::cout << display << std::endl;
// 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:
// add move to history queue // 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
std::vector<std::string> splitMove; int getCorrectPiece(char pieceIdentifier) {
bool isWhite = getTurnOrder();
if (move.find('#') != std::string::npos) { static const std::unordered_map<char, int> pieceMap = {
// Schachmate {'P', 200},
// Finish game {'K', 205},
} else if (move.rfind("0-0", 0) == 0) { // Kleine Rochade {'Q', 204},
// Check for Rochade {'N', 202},
} else if (move.rfind("0-0-0", 0) == 0) { {'R', 201},
// Große Rochade {'B', 203}
};
int piece = pieceMap.find(pieceIdentifier)->second;
if (isWhite) {
return piece - 100;
}
return piece;
} }
// Standard move std::pair<int, int> convertPosition(const std::string &pos) {
if (move.find('-')) { static const std::unordered_map<int, int> fileConvertion = {
splitMove = ChessUtils::split(move, '-'); {0, 7},
} else if (move.find('x')) { {1, 6},
splitMove = ChessUtils::split(move, 'x'); {2, 5},
{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]); //
std::pair newCoords = ChessUtils::convertPosition(splitMove[1]); void move(std::string move) {
// ToDo:
// add move to history queue
std::vector<std::vector<char>> board = getBoard(); std::vector<std::string> splitMove;
board[oldCoords.first][oldCoords.second] = getEmptyBoard()[oldCoords.first][oldCoords.second]; if (move.find('#') != std::string::npos) {
board[newCoords.first][newCoords.second] = getCorrectPiece(splitMove[0][0]); // Schachmate
// 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
}
Chessboard::draw(board); // Standard move
if (move.find('-')) {
splitMove = Utils::split(move, '-');
} else if (move.find('x')) {
splitMove = Utils::split(move, 'x');
}
// Notation std::pair oldCoords = convertPosition(splitMove[0]);
// Start with current position - dash - new position std::pair newCoords = convertPosition(splitMove[1]);
// 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 std::vector<std::vector<char>> board = getBoard();
// check: +
// checkmate: #
// draw/stalemate: 1/2-1/2
}
// This method saves the current board state getCorrectPiece(board[oldCoords.first][oldCoords.second]);
void Chessboard::saveBoard(Chessboard& chessboard) {
} board[oldCoords.first][oldCoords.second] = getEmptyBoard()[oldCoords.first][oldCoords.second];
board[newCoords.first][newCoords.second] = getCorrectPiece(splitMove[0][0]);
// This method loads a save state draw(board);
void Chessboard::loadBoard(int saveState) {
// readJSONFile // Notation
// // 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,143 +1,8 @@
// //
// Created by hamac on 20.12.2024. // Created by hamac on 18.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,24 +2,29 @@
// Created by hamac on 18.12.2024. // Created by hamac on 18.12.2024.
// //
#include "Chesspiece.h" #include <string>
#include <vector>
Chesspiece::Chesspiece(char color, char identifier) class Chesspiece {
: color(color), identifier(identifier) {} private:
/* fields */
char color;
char identifier;
std::string position;
std::vector<std::string> positions;
std::pair<int, int> Chesspiece::getPosition() { return this->position; }; /* methods */
void Chesspiece::setPosition(std::pair<int, int> position) { virtual std::vector<std::string> calcAvaibleMoves() {}
this->position = position;
calcAvaibleMoves();
}
char Chesspiece::getColor() { return color; } public:
void Chesspiece::setColor(char color) { color = color; } std::string getPosition() { return this->position; };
void setPosition(std::string position) {
this->position = position;
calcAvaibleMoves();
}
std::vector<std::string> Chesspiece::getPositions() { return positions; } char getColor() { return this->color; }
void setColor(char color) { this->color = color; }
bool Chesspiece::checkNotation(std::string notation) { std::vector<std::string> getPositions() { return this->positions; }
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);
}

View File

@ -1,45 +0,0 @@
//
// 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

27
Chesspieces.h Normal file
View File

@ -0,0 +1,27 @@
//
// 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();
};

View File

@ -1,96 +0,0 @@
#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;
}
}

View File

@ -1,38 +0,0 @@
//
// 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

View File

@ -1,27 +0,0 @@
//
// 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
View File

@ -1,28 +0,0 @@
//
// 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,19 +55,3 @@
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
-

20
Utils.cpp Normal file
View File

@ -0,0 +1,20 @@
//
// 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,16 +2,13 @@
#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.init(); chessboard.draw();
/*chessboard.draw(); //chessboard.move("Pb2-b3");
chessboard.move("Pb2-b3");*/
return 0; return 0;
} }

BIN
main.exe

Binary file not shown.