Create parent class for pieces and change board source from int to char

This commit is contained in:
Fabian Hamacher 2024-12-19 00:05:13 +01:00
parent e26a7e4927
commit 390ebb597e
4 changed files with 106 additions and 60 deletions

View File

@ -46,29 +46,29 @@ class Chessboard {
const std::string blackPawn = "\u265F";
/* class fields */
std::vector<std::vector<int>> currentBoard;
std::vector<std::vector<char>> currentBoard;
// 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}
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<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}
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
@ -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<int, int> 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<std::vector<int>>& chessboard) {
void generatePlayingField(const std::vector<std::vector<char>>& chessboard) {
char desc = 56;
for (int row = 0; row < boardSize; ++row) {
display += " ";
@ -172,19 +161,19 @@ class Chessboard {
}
/* methods */
void setBoard(std::vector<std::vector<int>> board) {
void setBoard(std::vector<std::vector<char>> board) {
this->currentBoard = board;
}
std::vector<std::vector<int>> getBoard() {
std::vector<std::vector<char>> getBoard() {
return this->currentBoard;
}
std::vector<std::vector<int>> getStartBoard() {
std::vector<std::vector<char>> getStartBoard() {
return this->startBoard;
}
std::vector<std::vector<int>> getEmptyBoard() {
std::vector<std::vector<char>> getEmptyBoard() {
return this->emptyBoard;
}
@ -192,7 +181,7 @@ class Chessboard {
draw(getStartBoard());
}
void draw (const std::vector<std::vector<int>>& chessboard) {
void draw (const std::vector<std::vector<char>>& chessboard) {
// Obere Rahmenlinie
generateTopLine();
@ -238,6 +227,17 @@ class Chessboard {
}
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);
@ -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<std::vector<int>> board = getBoard();
std::vector<std::vector<char>> 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]);

View File

@ -2,7 +2,29 @@
// Created by hamac on 18.12.2024.
//
#include <string>
#include <vector>
class Chesspiece {
private:
/* fields */
char color;
char identifier;
std::string position;
std::vector<std::string> positions;
/* methods */
virtual std::vector<std::string> 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<std::string> getPositions() { return this->positions; }
};

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,11 +1,6 @@
// Grundsätzlich erstmal nur zum Testen
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <locale>
#include <string>
#include <iomanip>
#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;
}