Add first iteration of primitiv working move method
This commit is contained in:
parent
95bc2a3f3c
commit
e26a7e4927
173
Chessboard.cpp
173
Chessboard.cpp
@ -5,7 +5,9 @@
|
||||
#include "Chessboard.h"
|
||||
#include "Utils.cpp"
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
class Chessboard {
|
||||
@ -48,40 +50,68 @@ class Chessboard {
|
||||
|
||||
// Starting formatting
|
||||
std::vector<std::vector<int>> startBoard = {
|
||||
{201, 202, 203, 204, 205, 203, 202, 201},
|
||||
{200, 200, 200, 200, 200, 200, 200, 200},
|
||||
{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},
|
||||
{100, 100, 100, 100, 100, 100, 100, 100},
|
||||
{101, 102, 103, 104, 105, 103, 102, 101}
|
||||
{201, 202, 203, 204, 205, 203, 202, 201},
|
||||
{200, 200, 200, 200, 200, 200, 200, 200},
|
||||
{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},
|
||||
{100, 100, 100, 100, 100, 100, 100, 100},
|
||||
{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 */
|
||||
// Method returns unicode for chess icon depending on the identifier
|
||||
std::string getChessIcon(int identifier) {
|
||||
switch (identifier) {
|
||||
case 1: return whiteSquare;
|
||||
case 101: return whiteRook;
|
||||
case 102: return whiteKnight;
|
||||
case 103: return whiteBischop;
|
||||
case 104: return whiteQueen;
|
||||
case 105: return whiteKing;
|
||||
case 100: return whitePawn;
|
||||
case 2: return blackSquare;
|
||||
case 201: return blackRook;
|
||||
case 202: return blackKnight;
|
||||
case 203: return blackBischop;
|
||||
case 204: return blackQueen;
|
||||
case 205: return blackKing;
|
||||
case 200: return blackPawn;
|
||||
case 1: return blackSquare;
|
||||
case 101: return blackRook;
|
||||
case 102: return blackKnight;
|
||||
case 103: return blackBischop;
|
||||
case 104: return blackQueen;
|
||||
case 105: return blackKing;
|
||||
case 100: return blackPawn;
|
||||
case 2: return whiteSquare;
|
||||
case 201: return whiteRook;
|
||||
case 202: return whiteKnight;
|
||||
case 203: return whiteBischop;
|
||||
case 204: return whiteQueen;
|
||||
case 205: return whiteKing;
|
||||
case 200: return whitePawn;
|
||||
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() {
|
||||
display += topLeft;
|
||||
display += " " + horizontal + " " + topLeft;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
display += horizontal + horizontal + horizontal;
|
||||
if (col < boardSize - 1) display += topIntersection;
|
||||
@ -90,8 +120,11 @@ class Chessboard {
|
||||
}
|
||||
|
||||
void generatePlayingField(const std::vector<std::vector<int>>& chessboard) {
|
||||
char desc = 56;
|
||||
for (int row = 0; row < boardSize; ++row) {
|
||||
display += vertical;
|
||||
display += " ";
|
||||
display += desc--;
|
||||
display += " " + vertical;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
display += " " + getChessIcon(chessboard[row][col]) + " " + vertical;
|
||||
}
|
||||
@ -99,7 +132,7 @@ class Chessboard {
|
||||
|
||||
// Horizontale Trennlinie (außer nach der letzten Zeile)
|
||||
if (row < boardSize - 1) {
|
||||
display += vertical;
|
||||
display += " " + horizontal + " " + vertical;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
display += horizontal + horizontal + horizontal;
|
||||
if (col < boardSize - 1) display += middleIntersection;
|
||||
@ -110,12 +143,19 @@ class Chessboard {
|
||||
}
|
||||
|
||||
void generateBottomLine() {
|
||||
display += bottomLeft;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
@ -123,12 +163,20 @@ class Chessboard {
|
||||
std::string display;
|
||||
int boardSize = 8;
|
||||
|
||||
bool getTurnOrder() {
|
||||
return this->turnOrder;
|
||||
}
|
||||
|
||||
void setTurnOrder(bool turnOrder) {
|
||||
this->turnOrder = turnOrder;
|
||||
}
|
||||
|
||||
/* methods */
|
||||
void setBoard(std::vector<std::vector<int>> board) {
|
||||
this->currentBoard = board;
|
||||
}
|
||||
|
||||
std::vector<std::vector<int>> getCurrentBoard() {
|
||||
std::vector<std::vector<int>> getBoard() {
|
||||
return this->currentBoard;
|
||||
}
|
||||
|
||||
@ -136,6 +184,10 @@ class Chessboard {
|
||||
return this->startBoard;
|
||||
}
|
||||
|
||||
std::vector<std::vector<int>> getEmptyBoard() {
|
||||
return this->emptyBoard;
|
||||
}
|
||||
|
||||
void draw () {
|
||||
draw(getStartBoard());
|
||||
}
|
||||
@ -150,12 +202,79 @@ class Chessboard {
|
||||
// Untere Rahmenlinie
|
||||
generateBottomLine();
|
||||
|
||||
setBoard(chessboard);
|
||||
|
||||
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) {
|
||||
// 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
|
||||
// Start with current position - dash - new position
|
||||
|
19
Utils.cpp
19
Utils.cpp
@ -7,13 +7,14 @@
|
||||
#include <vector>
|
||||
|
||||
class Utils {
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
1
main.cpp
1
main.cpp
@ -13,6 +13,7 @@ int main() {
|
||||
|
||||
Chessboard chessboard;
|
||||
chessboard.draw();
|
||||
chessboard.move("Pb2-b3");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user