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 "Utils.cpp"
#include "Chesspiece.cpp"
#include "ChessUtils.cpp"
#include "Pawn.h"
#include <iostream>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
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 = "\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 "";
}
std::string Chessboard::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() {
display += " " + horizontal + " " + topLeft;
void Chessboard::generateTopLine() {
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) {
display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) display += topIntersection;
this->display += " " + Chessboard::getChessIcon(chessboard[row][col]) + " " + vertical;
}
display += topRight + "\n";
}
this->display += "\n";
void generatePlayingField(const std::vector<std::vector<char>>& chessboard) {
char desc = 56;
for (int row = 0; row < boardSize; ++row) {
display += " ";
display += desc--;
display += " " + vertical;
// Horizontale Trennlinie (außer nach der letzten Zeile)
if (row < boardSize - 1) {
this->display += " " + horizontal + " " + vertical;
for (int col = 0; col < boardSize; ++col) {
display += " " + getChessIcon(chessboard[row][col]) + " " + vertical;
}
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 += horizontal + horizontal + horizontal;
if (col < boardSize - 1) this->display += middleIntersection;
}
this->display += vertical + "\n";
}
}
}
void generateBottomLine() {
char desc = 65;
display += " " + horizontal + " " + bottomLeft;
for (int col = 0; col < boardSize; ++col) {
display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) display += bottomIntersection;
}
display += bottomRight + "\n";
display += " " + vertical;
for (int col = 0; col < boardSize; ++col) {
display += " ";
display += (desc++);
display += " " + vertical;
}
void Chessboard::generateBottomLine() {
char desc = 65;
this->display += " " + horizontal + " " + bottomLeft;
for (int col = 0; col < boardSize; ++col) {
this->display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) this->display += bottomIntersection;
}
public:
/* fields */
std::string display;
int boardSize = 8;
bool getTurnOrder() {
return this->turnOrder;
this->display += bottomRight + "\n";
this->display += " " + vertical;
for (int col = 0; col < boardSize; ++col) {
this->display += " ";
this->display += (desc++);
this->display += " " + vertical;
}
}
void setTurnOrder(bool turnOrder) {
this->turnOrder = turnOrder;
}
/* methods */
bool Chessboard::getTurnOrder() { return this->turnOrder; }
void Chessboard::setTurnOrder(bool turnOrder) { this->turnOrder = turnOrder; }
/* methods */
void setBoard(std::vector<std::vector<char>> board) {
this->currentBoard = board;
}
std::vector<std::vector<char>> Chessboard::getBoard() { return this->currentBoard; }
void Chessboard::setBoard(std::vector<std::vector<char>> board) { this->currentBoard = board; }
std::vector<std::vector<char>> getBoard() {
return this->currentBoard;
}
std::vector<std::vector<char>> Chessboard::getStartBoard() { return this->startBoard; }
std::vector<std::vector<char>> getStartBoard() {
return this->startBoard;
}
std::vector<std::vector<char>> Chessboard::getEmptyBoard() { return this->emptyBoard; }
std::vector<std::vector<char>> getEmptyBoard() {
return this->emptyBoard;
}
void Chessboard::draw () {
Chessboard::draw(getStartBoard());
}
void draw () {
draw(getStartBoard());
}
void Chessboard::draw (const std::vector<std::vector<char>>& chessboard) {
// Obere Rahmenlinie
Chessboard::generateTopLine();
void draw (const std::vector<std::vector<char>>& chessboard) {
// Obere Rahmenlinie
generateTopLine();
// Schachbrett mit vertikalen Linien
Chessboard::generatePlayingField(chessboard);
// Schachbrett mit vertikalen Linien
generatePlayingField(chessboard);
// Untere Rahmenlinie
Chessboard::generateBottomLine();
// Untere Rahmenlinie
generateBottomLine();
Chessboard::setBoard(chessboard);
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:
// 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
// add move to history queue
int getCorrectPiece(char pieceIdentifier) {
bool isWhite = getTurnOrder();
std::vector<std::string> splitMove;
static const std::unordered_map<char, int> pieceMap = {
{'P', 200},
{'K', 205},
{'Q', 204},
{'N', 202},
{'R', 201},
{'B', 203}
};
int piece = pieceMap.find(pieceIdentifier)->second;
if (isWhite) {
return piece - 100;
}
return piece;
if (move.find('#') != std::string::npos) {
// 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
}
std::pair<int, int> convertPosition(const std::string &pos) {
static const std::unordered_map<int, int> fileConvertion = {
{0, 7},
{1, 6},
{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);
// Standard move
if (move.find('-')) {
splitMove = ChessUtils::split(move, '-');
} else if (move.find('x')) {
splitMove = ChessUtils::split(move, 'x');
}
//
void move(std::string move) {
// ToDo:
// add move to history queue
std::pair oldCoords = ChessUtils::convertPosition(splitMove[0]);
std::pair newCoords = ChessUtils::convertPosition(splitMove[1]);
std::vector<std::string> splitMove;
std::vector<std::vector<char>> board = getBoard();
if (move.find('#') != std::string::npos) {
// 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
}
board[oldCoords.first][oldCoords.second] = getEmptyBoard()[oldCoords.first][oldCoords.second];
board[newCoords.first][newCoords.second] = getCorrectPiece(splitMove[0][0]);
// Standard move
if (move.find('-')) {
splitMove = Utils::split(move, '-');
} else if (move.find('x')) {
splitMove = Utils::split(move, 'x');
}
Chessboard::draw(board);
std::pair oldCoords = convertPosition(splitMove[0]);
std::pair newCoords = convertPosition(splitMove[1]);
// 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
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);
// 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
//
}
};
// This method loads a save state
void Chessboard::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
#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

View File

@ -2,29 +2,24 @@
// Created by hamac on 18.12.2024.
//
#include <string>
#include <vector>
#include "Chesspiece.h"
class Chesspiece {
private:
/* fields */
char color;
char identifier;
std::string position;
std::vector<std::string> positions;
Chesspiece::Chesspiece(char color, char identifier)
: color(color), identifier(identifier) {}
/* methods */
virtual std::vector<std::string> calcAvaibleMoves() {}
std::pair<int, int> Chesspiece::getPosition() { return this->position; };
void Chesspiece::setPosition(std::pair<int, int> position) {
this->position = position;
calcAvaibleMoves();
}
public:
std::string getPosition() { return this->position; };
void setPosition(std::string position) {
this->position = position;
calcAvaibleMoves();
}
char Chesspiece::getColor() { return color; }
void Chesspiece::setColor(char color) { color = color; }
char getColor() { return this->color; }
void setColor(char color) { this->color = color; }
std::vector<std::string> Chesspiece::getPositions() { return positions; }
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
2. Multiplayer
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 "Chessboard.cpp"
#include "Pawn.cpp"
int main() {
SetConsoleOutputCP(CP_UTF8);
//Pawn pawn('w', {1,6});
Chessboard chessboard;
chessboard.draw();
//chessboard.move("Pb2-b3");
chessboard.init();
/*chessboard.draw();
chessboard.move("Pb2-b3");*/
return 0;
}

BIN
main.exe

Binary file not shown.