Compare commits
5 Commits
390ebb597e
...
dc31b31f4b
Author | SHA1 | Date | |
---|---|---|---|
dc31b31f4b | |||
046ffe6607 | |||
94a86ede09 | |||
1a931462e2 | |||
b90a60e877 |
25
ChessUtils.cpp
Normal file
25
ChessUtils.cpp
Normal 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
34
ChessUtils.h
Normal 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
|
310
Chessboard.cpp
310
Chessboard.cpp
@ -3,83 +3,12 @@
|
||||
//
|
||||
|
||||
#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) {
|
||||
std::string Chessboard::getChessIcon(char identifier) {
|
||||
switch (identifier) {
|
||||
case 'x': return blackSquare;
|
||||
case 'r': return blackRook;
|
||||
@ -99,101 +28,82 @@ class Chessboard {
|
||||
}
|
||||
}
|
||||
|
||||
void generateTopLine() {
|
||||
display += " " + horizontal + " " + topLeft;
|
||||
void Chessboard::generateTopLine() {
|
||||
this->display += " " + horizontal + " " + topLeft;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
display += horizontal + horizontal + horizontal;
|
||||
if (col < boardSize - 1) display += topIntersection;
|
||||
this->display += horizontal + horizontal + horizontal;
|
||||
if (col < boardSize - 1) this->display += topIntersection;
|
||||
}
|
||||
display += topRight + "\n";
|
||||
this->display += topRight + "\n";
|
||||
}
|
||||
|
||||
void generatePlayingField(const std::vector<std::vector<char>>& chessboard) {
|
||||
void Chessboard::generatePlayingField(const std::vector<std::vector<char>>& chessboard) {
|
||||
char desc = 56;
|
||||
for (int row = 0; row < boardSize; ++row) {
|
||||
display += " ";
|
||||
display += desc--;
|
||||
display += " " + vertical;
|
||||
this->display += " ";
|
||||
this->display += desc--;
|
||||
this->display += " " + vertical;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
display += " " + getChessIcon(chessboard[row][col]) + " " + vertical;
|
||||
this->display += " " + Chessboard::getChessIcon(chessboard[row][col]) + " " + vertical;
|
||||
}
|
||||
display += "\n";
|
||||
this->display += "\n";
|
||||
|
||||
// Horizontale Trennlinie (außer nach der letzten Zeile)
|
||||
if (row < boardSize - 1) {
|
||||
display += " " + horizontal + " " + vertical;
|
||||
this->display += " " + horizontal + " " + vertical;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
display += horizontal + horizontal + horizontal;
|
||||
if (col < boardSize - 1) display += middleIntersection;
|
||||
this->display += horizontal + horizontal + horizontal;
|
||||
if (col < boardSize - 1) this->display += middleIntersection;
|
||||
}
|
||||
display += vertical + "\n";
|
||||
this->display += vertical + "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void generateBottomLine() {
|
||||
void Chessboard::generateBottomLine() {
|
||||
char desc = 65;
|
||||
display += " " + horizontal + " " + bottomLeft;
|
||||
this->display += " " + horizontal + " " + bottomLeft;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
display += horizontal + horizontal + horizontal;
|
||||
if (col < boardSize - 1) display += bottomIntersection;
|
||||
this->display += horizontal + horizontal + horizontal;
|
||||
if (col < boardSize - 1) this->display += bottomIntersection;
|
||||
}
|
||||
display += bottomRight + "\n";
|
||||
display += " " + vertical;
|
||||
this->display += bottomRight + "\n";
|
||||
this->display += " " + vertical;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
display += " ";
|
||||
display += (desc++);
|
||||
display += " " + vertical;
|
||||
this->display += " ";
|
||||
this->display += (desc++);
|
||||
this->display += " " + vertical;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
/* fields */
|
||||
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<char>> board) {
|
||||
this->currentBoard = board;
|
||||
bool Chessboard::getTurnOrder() { return this->turnOrder; }
|
||||
void Chessboard::setTurnOrder(bool turnOrder) { this->turnOrder = turnOrder; }
|
||||
|
||||
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>> Chessboard::getStartBoard() { return this->startBoard; }
|
||||
|
||||
std::vector<std::vector<char>> Chessboard::getEmptyBoard() { return this->emptyBoard; }
|
||||
|
||||
void Chessboard::draw () {
|
||||
Chessboard::draw(getStartBoard());
|
||||
}
|
||||
|
||||
std::vector<std::vector<char>> getBoard() {
|
||||
return this->currentBoard;
|
||||
}
|
||||
|
||||
std::vector<std::vector<char>> getStartBoard() {
|
||||
return this->startBoard;
|
||||
}
|
||||
|
||||
std::vector<std::vector<char>> getEmptyBoard() {
|
||||
return this->emptyBoard;
|
||||
}
|
||||
|
||||
void draw () {
|
||||
draw(getStartBoard());
|
||||
}
|
||||
|
||||
void draw (const std::vector<std::vector<char>>& chessboard) {
|
||||
void Chessboard::draw (const std::vector<std::vector<char>>& chessboard) {
|
||||
// Obere Rahmenlinie
|
||||
generateTopLine();
|
||||
Chessboard::generateTopLine();
|
||||
|
||||
// Schachbrett mit vertikalen Linien
|
||||
generatePlayingField(chessboard);
|
||||
Chessboard::generatePlayingField(chessboard);
|
||||
|
||||
// Untere Rahmenlinie
|
||||
generateBottomLine();
|
||||
Chessboard::generateBottomLine();
|
||||
|
||||
setBoard(chessboard);
|
||||
Chessboard::setBoard(chessboard);
|
||||
|
||||
std::cout << display << std::endl;
|
||||
std::cout << this->display << std::endl;
|
||||
}
|
||||
|
||||
// ToDo:
|
||||
@ -206,45 +116,89 @@ class Chessboard {
|
||||
// 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;
|
||||
void Chessboard::init() {
|
||||
Chessboard::initPieces();
|
||||
}
|
||||
|
||||
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}
|
||||
};
|
||||
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)));
|
||||
}
|
||||
|
||||
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 Chessboard::move() {
|
||||
|
||||
}
|
||||
|
||||
char Chessboard::getCorrectPiece(char pieceIdentifier) {
|
||||
bool isWhite = Chessboard::getTurnOrder();
|
||||
if (!isWhite) return pieceIdentifier + 32;
|
||||
return pieceIdentifier;
|
||||
}
|
||||
|
||||
//
|
||||
void move(std::string move) {
|
||||
void Chessboard::move(std::string move) {
|
||||
// ToDo:
|
||||
// add move to history queue
|
||||
|
||||
@ -261,22 +215,20 @@ class Chessboard {
|
||||
|
||||
// Standard move
|
||||
if (move.find('-')) {
|
||||
splitMove = Utils::split(move, '-');
|
||||
splitMove = ChessUtils::split(move, '-');
|
||||
} else if (move.find('x')) {
|
||||
splitMove = Utils::split(move, 'x');
|
||||
splitMove = ChessUtils::split(move, 'x');
|
||||
}
|
||||
|
||||
std::pair oldCoords = convertPosition(splitMove[0]);
|
||||
std::pair newCoords = convertPosition(splitMove[1]);
|
||||
std::pair oldCoords = ChessUtils::convertPosition(splitMove[0]);
|
||||
std::pair newCoords = ChessUtils::convertPosition(splitMove[1]);
|
||||
|
||||
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]);
|
||||
|
||||
draw(board);
|
||||
Chessboard::draw(board);
|
||||
|
||||
// Notation
|
||||
// Start with current position - dash - new position
|
||||
@ -298,17 +250,15 @@ class Chessboard {
|
||||
// check: +
|
||||
// checkmate: #
|
||||
// draw/stalemate: 1/2-1/2
|
||||
|
||||
}
|
||||
|
||||
// This method saves the current board state
|
||||
void saveBoard(Chessboard& chessboard) {
|
||||
void Chessboard::saveBoard(Chessboard& chessboard) {
|
||||
|
||||
}
|
||||
|
||||
// This method loads a save state
|
||||
void loadBoard(int saveState) {
|
||||
void Chessboard::loadBoard(int saveState) {
|
||||
// readJSONFile
|
||||
//
|
||||
}
|
||||
};
|
||||
|
137
Chessboard.h
137
Chessboard.h
@ -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
|
||||
|
@ -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() {}
|
||||
|
||||
public:
|
||||
std::string getPosition() { return this->position; };
|
||||
void setPosition(std::string position) {
|
||||
std::pair<int, int> Chesspiece::getPosition() { return this->position; };
|
||||
void Chesspiece::setPosition(std::pair<int, int> position) {
|
||||
this->position = position;
|
||||
calcAvaibleMoves();
|
||||
}
|
||||
|
||||
char getColor() { return this->color; }
|
||||
void setColor(char color) { this->color = color; }
|
||||
char Chesspiece::getColor() { return color; }
|
||||
void Chesspiece::setColor(char color) { color = color; }
|
||||
|
||||
std::vector<std::string> getPositions() { return this->positions; }
|
||||
};
|
||||
std::vector<std::string> Chesspiece::getPositions() { return 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
45
Chesspiece.h
Normal 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
|
@ -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
96
MoveList.cpp
Normal 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
38
MoveList.h
Normal 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
27
Pawn.cpp
Normal 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
28
Pawn.h
Normal 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
|
16
README.md
16
README.md
@ -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
|
||||
-
|
||||
|
20
Utils.cpp
20
Utils.cpp
@ -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;
|
||||
}
|
||||
};
|
7
main.cpp
7
main.cpp
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user