From 390ebb597e8f27cda1ac08bfbe9eaf9aedc0cf98 Mon Sep 17 00:00:00 2001 From: Fabian Hamacher Date: Thu, 19 Dec 2024 00:05:13 +0100 Subject: [PATCH] Create parent class for pieces and change board source from int to char --- Chessboard.cpp | 108 +++++++++++++++++++++++++------------------------ Chesspiece.cpp | 24 ++++++++++- Chesspieces.h | 27 +++++++++++++ main.cpp | 7 +--- 4 files changed, 106 insertions(+), 60 deletions(-) create mode 100644 Chesspieces.h diff --git a/Chessboard.cpp b/Chessboard.cpp index 21f4065..7b53f1f 100644 --- a/Chessboard.cpp +++ b/Chessboard.cpp @@ -46,29 +46,29 @@ class Chessboard { const std::string blackPawn = "\u265F"; /* class fields */ - std::vector> currentBoard; + std::vector> currentBoard; // Starting formatting - std::vector> 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} + std::vector> 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> 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} + std::vector> 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 @@ -79,37 +79,26 @@ class Chessboard { /* methods */ // Method returns unicode for chess icon depending on the identifier - std::string getChessIcon(int identifier) { + std::string getChessIcon(char identifier) { switch (identifier) { - 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; + 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 ""; } } - static const std::unordered_map fileConvertion = { - {0, 7}, - {1, 6}, - {2, 5}, - {3, 4}, - {4, 3}, - {5, 2}, - {6, 1}, - {7, 0} - }; - void generateTopLine() { display += " " + horizontal + " " + topLeft; for (int col = 0; col < boardSize; ++col) { @@ -119,7 +108,7 @@ class Chessboard { display += topRight + "\n"; } - void generatePlayingField(const std::vector>& chessboard) { + void generatePlayingField(const std::vector>& chessboard) { char desc = 56; for (int row = 0; row < boardSize; ++row) { display += " "; @@ -172,19 +161,19 @@ class Chessboard { } /* methods */ - void setBoard(std::vector> board) { + void setBoard(std::vector> board) { this->currentBoard = board; } - std::vector> getBoard() { + std::vector> getBoard() { return this->currentBoard; } - std::vector> getStartBoard() { + std::vector> getStartBoard() { return this->startBoard; } - std::vector> getEmptyBoard() { + std::vector> getEmptyBoard() { return this->emptyBoard; } @@ -192,7 +181,7 @@ class Chessboard { draw(getStartBoard()); } - void draw (const std::vector>& chessboard) { + void draw (const std::vector>& chessboard) { // Obere Rahmenlinie generateTopLine(); @@ -238,6 +227,17 @@ class Chessboard { } std::pair convertPosition(const std::string &pos) { + static const std::unordered_map 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); @@ -266,10 +266,12 @@ class Chessboard { splitMove = Utils::split(move, 'x'); } - std::pair oldCoords= convertPosition(splitMove[0]); + std::pair oldCoords = convertPosition(splitMove[0]); std::pair newCoords = convertPosition(splitMove[1]); - std::vector> board = getBoard(); + std::vector> board = getBoard(); + + getCorrectPiece(board[oldCoords.first][oldCoords.second]); board[oldCoords.first][oldCoords.second] = getEmptyBoard()[oldCoords.first][oldCoords.second]; board[newCoords.first][newCoords.second] = getCorrectPiece(splitMove[0][0]); diff --git a/Chesspiece.cpp b/Chesspiece.cpp index 57d6a6a..b4a1664 100644 --- a/Chesspiece.cpp +++ b/Chesspiece.cpp @@ -2,7 +2,29 @@ // Created by hamac on 18.12.2024. // +#include +#include + class Chesspiece { + private: + /* fields */ + char color; + char identifier; + std::string position; + std::vector positions; + /* methods */ + virtual std::vector calcAvaibleMoves() {} -} + public: + std::string getPosition() { return this->position; }; + void setPosition(std::string position) { + this->position = position; + calcAvaibleMoves(); + } + + char getColor() { return this->color; } + void setColor(char color) { this->color = color; } + + std::vector getPositions() { return this->positions; } +}; diff --git a/Chesspieces.h b/Chesspieces.h new file mode 100644 index 0000000..75c8350 --- /dev/null +++ b/Chesspieces.h @@ -0,0 +1,27 @@ +// +// Created by hamac on 18.12.2024. +// + +#include +#include + +#ifndef CHESSPIECES_H +#define CHESSPIECES_H + +#endif //CHESSPIECES_H + +class Chesspiece { +private: + char color; + std::string position; + virtual std::vector calcAvaibleMoves() {} + +public: + std::string getPosition(); + void setPosition(std::string position); + + char getColor(); + void setColor(char color); + + std::vector getPositions(); +}; diff --git a/main.cpp b/main.cpp index 24d6ac9..8e78b79 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,6 @@ // Grundsätzlich erstmal nur zum Testen -#include -#include #include -#include -#include -#include #include "Chessboard.cpp" int main() { @@ -13,7 +8,7 @@ int main() { Chessboard chessboard; chessboard.draw(); - chessboard.move("Pb2-b3"); + //chessboard.move("Pb2-b3"); return 0; }