TurboSchach/Chessboard.cpp

269 lines
11 KiB
C++
Raw Permalink Normal View History

//
// Created by hamac on 18.12.2024.
//
#include "Chessboard.h"
#include "Chesspiece.cpp"
#include "ChessUtils.cpp"
#include "Pawn.h"
#include <iostream>
std::string Chessboard::getChessIcon(char identifier) {
switch (identifier) {
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 "";
}
}
void Chessboard::generateTopLine() {
this->display += " " + horizontal + " " + topLeft;
for (int col = 0; col < boardSize; ++col) {
this->display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) this->display += topIntersection;
}
this->display += topRight + "\n";
}
void Chessboard::generatePlayingField(const std::vector<std::vector<char>>& chessboard) {
char desc = 56;
for (int row = 0; row < boardSize; ++row) {
this->display += " ";
this->display += desc--;
this->display += " " + vertical;
for (int col = 0; col < boardSize; ++col) {
this->display += " " + Chessboard::getChessIcon(chessboard[row][col]) + " " + vertical;
}
this->display += "\n";
// Horizontale Trennlinie (außer nach der letzten Zeile)
if (row < boardSize - 1) {
this->display += " " + horizontal + " " + vertical;
for (int col = 0; col < boardSize; ++col) {
this->display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) this->display += middleIntersection;
}
this->display += vertical + "\n";
}
}
}
void Chessboard::generateBottomLine() {
char desc = 65;
this->display += " " + horizontal + " " + bottomLeft;
for (int col = 0; col < boardSize; ++col) {
this->display += horizontal + horizontal + horizontal;
if (col < boardSize - 1) this->display += bottomIntersection;
}
this->display += bottomRight + "\n";
this->display += " " + vertical;
for (int col = 0; col < boardSize; ++col) {
this->display += " ";
this->display += (desc++);
this->display += " " + vertical;
}
}
/* methods */
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());
}
void Chessboard::draw (const std::vector<std::vector<char>>& chessboard) {
// Obere Rahmenlinie
Chessboard::generateTopLine();
// Schachbrett mit vertikalen Linien
Chessboard::generatePlayingField(chessboard);
// Untere Rahmenlinie
Chessboard::generateBottomLine();
Chessboard::setBoard(chessboard);
std::cout << this->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
void Chessboard::init() {
Chessboard::initPieces();
}
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)));
}
void Chessboard::move() {
}
char Chessboard::getCorrectPiece(char pieceIdentifier) {
bool isWhite = Chessboard::getTurnOrder();
if (!isWhite) return pieceIdentifier + 32;
return pieceIdentifier;
}
//
void Chessboard::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 = ChessUtils::split(move, '-');
} else if (move.find('x')) {
splitMove = ChessUtils::split(move, 'x');
}
std::pair oldCoords = ChessUtils::convertPosition(splitMove[0]);
std::pair newCoords = ChessUtils::convertPosition(splitMove[1]);
std::vector<std::vector<char>> board = getBoard();
board[oldCoords.first][oldCoords.second] = getEmptyBoard()[oldCoords.first][oldCoords.second];
board[newCoords.first][newCoords.second] = getCorrectPiece(splitMove[0][0]);
2024-12-20 10:19:33 +00:00
//e4-e5
Chessboard::draw(board);
// Notation
// Start with current position - dash - new position
// eg.: b1-c3
// Letter first than number
// eg.: a5
// Pawn: e4 or p e4 (pawn)
// R for rook
// N for Knight
// K for King
// B for Bischop
// Q for Queen
// Special:
// 0-0 short castle
// 0-0-0 long castle
2024-12-20 10:19:33 +00:00
// en passant: write square where pawn lands
// Umwandlung: g8=R,Q,N,B (Wo gehst du hin = Was wirst du)
// capture: x
// check: +
// checkmate: #
// draw/stalemate: 1/2-1/2
2024-12-20 10:19:33 +00:00
}
// This method saves the current board state
void Chessboard::saveBoard(Chessboard& chessboard) {
}
// This method loads a save state
void Chessboard::loadBoard(int saveState) {
// readJSONFile
//
}