Add first iteration of primitiv working move method

This commit is contained in:
Fabian Hamacher 2024-12-18 23:12:48 +01:00
parent 95bc2a3f3c
commit e26a7e4927
4 changed files with 157 additions and 36 deletions

View File

@ -5,7 +5,9 @@
#include "Chessboard.h" #include "Chessboard.h"
#include "Utils.cpp" #include "Utils.cpp"
#include <iostream> #include <iostream>
#include <queue>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
class Chessboard { class Chessboard {
@ -48,40 +50,68 @@ class Chessboard {
// Starting formatting // Starting formatting
std::vector<std::vector<int>> startBoard = { std::vector<std::vector<int>> startBoard = {
{201, 202, 203, 204, 205, 203, 202, 201}, {201, 202, 203, 204, 205, 203, 202, 201},
{200, 200, 200, 200, 200, 200, 200, 200}, {200, 200, 200, 200, 200, 200, 200, 200},
{1, 2, 1, 2, 1, 2, 1, 2}, {1, 2, 1, 2, 1, 2, 1, 2},
{2, 1, 2, 1, 2, 1, 2, 1}, {2, 1, 2, 1, 2, 1, 2, 1},
{1, 2, 1, 2, 1, 2, 1, 2}, {1, 2, 1, 2, 1, 2, 1, 2},
{2, 1, 2, 1, 2, 1, 2, 1}, {2, 1, 2, 1, 2, 1, 2, 1},
{100, 100, 100, 100, 100, 100, 100, 100}, {100, 100, 100, 100, 100, 100, 100, 100},
{101, 102, 103, 104, 105, 103, 102, 101} {101, 102, 103, 104, 105, 103, 102, 101}
}; };
std::vector<std::vector<int>> emptyBoard = {
{1, 2, 1, 2, 1, 2, 1, 2},
{2, 1, 2, 1, 2, 1, 2, 1},
{1, 2, 1, 2, 1, 2, 1, 2},
{2, 1, 2, 1, 2, 1, 2, 1},
{1, 2, 1, 2, 1, 2, 1, 2},
{2, 1, 2, 1, 2, 1, 2, 1},
{1, 2, 1, 2, 1, 2, 1, 2},
{2, 1, 2, 1, 2, 1, 2, 1}
};
// Saves turn order; true = white and false = black
bool turnOrder = true;
// ToDo: Max Size definieren?
std::queue<std::string> queue;
/* methods */ /* methods */
// Method returns unicode for chess icon depending on the identifier // Method returns unicode for chess icon depending on the identifier
std::string getChessIcon(int identifier) { std::string getChessIcon(int identifier) {
switch (identifier) { switch (identifier) {
case 1: return whiteSquare; case 1: return blackSquare;
case 101: return whiteRook; case 101: return blackRook;
case 102: return whiteKnight; case 102: return blackKnight;
case 103: return whiteBischop; case 103: return blackBischop;
case 104: return whiteQueen; case 104: return blackQueen;
case 105: return whiteKing; case 105: return blackKing;
case 100: return whitePawn; case 100: return blackPawn;
case 2: return blackSquare; case 2: return whiteSquare;
case 201: return blackRook; case 201: return whiteRook;
case 202: return blackKnight; case 202: return whiteKnight;
case 203: return blackBischop; case 203: return whiteBischop;
case 204: return blackQueen; case 204: return whiteQueen;
case 205: return blackKing; case 205: return whiteKing;
case 200: return blackPawn; case 200: return whitePawn;
default: return ""; default: return "";
} }
} }
static const std::unordered_map<int, int> fileConvertion = {
{0, 7},
{1, 6},
{2, 5},
{3, 4},
{4, 3},
{5, 2},
{6, 1},
{7, 0}
};
void generateTopLine() { void generateTopLine() {
display += topLeft; display += " " + horizontal + " " + topLeft;
for (int col = 0; col < boardSize; ++col) { for (int col = 0; col < boardSize; ++col) {
display += horizontal + horizontal + horizontal; display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) display += topIntersection; if (col < boardSize - 1) display += topIntersection;
@ -90,8 +120,11 @@ class Chessboard {
} }
void generatePlayingField(const std::vector<std::vector<int>>& chessboard) { void generatePlayingField(const std::vector<std::vector<int>>& chessboard) {
char desc = 56;
for (int row = 0; row < boardSize; ++row) { for (int row = 0; row < boardSize; ++row) {
display += 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; display += " " + getChessIcon(chessboard[row][col]) + " " + vertical;
} }
@ -99,7 +132,7 @@ class Chessboard {
// Horizontale Trennlinie (außer nach der letzten Zeile) // Horizontale Trennlinie (außer nach der letzten Zeile)
if (row < boardSize - 1) { if (row < boardSize - 1) {
display += vertical; display += " " + horizontal + " " + vertical;
for (int col = 0; col < boardSize; ++col) { for (int col = 0; col < boardSize; ++col) {
display += horizontal + horizontal + horizontal; display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) display += middleIntersection; if (col < boardSize - 1) display += middleIntersection;
@ -110,12 +143,19 @@ class Chessboard {
} }
void generateBottomLine() { void generateBottomLine() {
display += bottomLeft; char desc = 65;
display += " " + horizontal + " " + bottomLeft;
for (int col = 0; col < boardSize; ++col) { for (int col = 0; col < boardSize; ++col) {
display += horizontal + horizontal + horizontal; display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) display += bottomIntersection; if (col < boardSize - 1) display += bottomIntersection;
} }
display += bottomRight + "\n"; display += bottomRight + "\n";
display += " " + vertical;
for (int col = 0; col < boardSize; ++col) {
display += " ";
display += (desc++);
display += " " + vertical;
}
} }
public: public:
@ -123,12 +163,20 @@ class Chessboard {
std::string display; std::string display;
int boardSize = 8; int boardSize = 8;
bool getTurnOrder() {
return this->turnOrder;
}
void setTurnOrder(bool turnOrder) {
this->turnOrder = turnOrder;
}
/* methods */ /* methods */
void setBoard(std::vector<std::vector<int>> board) { void setBoard(std::vector<std::vector<int>> board) {
this->currentBoard = board; this->currentBoard = board;
} }
std::vector<std::vector<int>> getCurrentBoard() { std::vector<std::vector<int>> getBoard() {
return this->currentBoard; return this->currentBoard;
} }
@ -136,6 +184,10 @@ class Chessboard {
return this->startBoard; return this->startBoard;
} }
std::vector<std::vector<int>> getEmptyBoard() {
return this->emptyBoard;
}
void draw () { void draw () {
draw(getStartBoard()); draw(getStartBoard());
} }
@ -150,12 +202,79 @@ class Chessboard {
// Untere Rahmenlinie // Untere Rahmenlinie
generateBottomLine(); generateBottomLine();
setBoard(chessboard);
std::cout << 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
int getCorrectPiece(char pieceIdentifier) {
bool isWhite = getTurnOrder();
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;
}
std::pair<int, int> convertPosition(const std::string &pos) {
int y = pos[pos.size()-2] - 'a';
int x = fileConvertion.find((pos[pos.size()-1] - '0')-1)->second;
return std::make_pair(x, y);
}
// //
void move(std::string move) { void move(std::string move) {
// ToDo:
// add move to history queue
std::vector<std::string> splitMove;
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
}
// Standard move
if (move.find('-')) {
splitMove = Utils::split(move, '-');
} else if (move.find('x')) {
splitMove = Utils::split(move, 'x');
}
std::pair oldCoords= convertPosition(splitMove[0]);
std::pair newCoords = convertPosition(splitMove[1]);
std::vector<std::vector<int>> board = getBoard();
board[oldCoords.first][oldCoords.second] = getEmptyBoard()[oldCoords.first][oldCoords.second];
board[newCoords.first][newCoords.second] = getCorrectPiece(splitMove[0][0]);
draw(board);
// Notation // Notation
// Start with current position - dash - new position // Start with current position - dash - new position

View File

@ -7,13 +7,14 @@
#include <vector> #include <vector>
class Utils { class Utils {
static std::vector<std::string> split(const std::string &s, char delim) { public:
std::vector<std::string> elems; static std::vector<std::string> split(const std::string &s, char delim) {
std::stringstream ss(s); std::vector<std::string> elems;
std::string item; std::stringstream ss(s);
while (std::getline(ss, item, delim)) { std::string item;
elems.push_back(item); while (std::getline(ss, item, delim)) {
} elems.push_back(item);
return elems; }
return elems;
} }
} };

View File

@ -13,6 +13,7 @@ int main() {
Chessboard chessboard; Chessboard chessboard;
chessboard.draw(); chessboard.draw();
chessboard.move("Pb2-b3");
return 0; return 0;
} }

BIN
main.exe

Binary file not shown.