194 lines
5.4 KiB
C++
194 lines
5.4 KiB
C++
|
//
|
||
|
// Created by hamac on 18.12.2024.
|
||
|
//
|
||
|
|
||
|
#include "Chessboard.h"
|
||
|
#include "Utils.cpp"
|
||
|
#include <iostream>
|
||
|
#include <string>
|
||
|
#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<int>> 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}
|
||
|
};
|
||
|
|
||
|
/* 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;
|
||
|
default: return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void generateTopLine() {
|
||
|
display += topLeft;
|
||
|
for (int col = 0; col < boardSize; ++col) {
|
||
|
display += horizontal + horizontal + horizontal;
|
||
|
if (col < boardSize - 1) display += topIntersection;
|
||
|
}
|
||
|
display += topRight + "\n";
|
||
|
}
|
||
|
|
||
|
void generatePlayingField(const std::vector<std::vector<int>>& chessboard) {
|
||
|
for (int row = 0; row < boardSize; ++row) {
|
||
|
display += vertical;
|
||
|
for (int col = 0; col < boardSize; ++col) {
|
||
|
display += " " + getChessIcon(chessboard[row][col]) + " " + vertical;
|
||
|
}
|
||
|
display += "\n";
|
||
|
|
||
|
// Horizontale Trennlinie (außer nach der letzten Zeile)
|
||
|
if (row < boardSize - 1) {
|
||
|
display += vertical;
|
||
|
for (int col = 0; col < boardSize; ++col) {
|
||
|
display += horizontal + horizontal + horizontal;
|
||
|
if (col < boardSize - 1) display += middleIntersection;
|
||
|
}
|
||
|
display += vertical + "\n";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void generateBottomLine() {
|
||
|
display += bottomLeft;
|
||
|
for (int col = 0; col < boardSize; ++col) {
|
||
|
display += horizontal + horizontal + horizontal;
|
||
|
if (col < boardSize - 1) display += bottomIntersection;
|
||
|
}
|
||
|
display += bottomRight + "\n";
|
||
|
}
|
||
|
|
||
|
public:
|
||
|
/* fields */
|
||
|
std::string display;
|
||
|
int boardSize = 8;
|
||
|
|
||
|
/* methods */
|
||
|
void setBoard(std::vector<std::vector<int>> board) {
|
||
|
this->currentBoard = board;
|
||
|
}
|
||
|
|
||
|
std::vector<std::vector<int>> getCurrentBoard() {
|
||
|
return this->currentBoard;
|
||
|
}
|
||
|
|
||
|
std::vector<std::vector<int>> getStartBoard() {
|
||
|
return this->startBoard;
|
||
|
}
|
||
|
|
||
|
void draw () {
|
||
|
draw(getStartBoard());
|
||
|
}
|
||
|
|
||
|
void draw (const std::vector<std::vector<int>>& chessboard) {
|
||
|
// Obere Rahmenlinie
|
||
|
generateTopLine();
|
||
|
|
||
|
// Schachbrett mit vertikalen Linien
|
||
|
generatePlayingField(chessboard);
|
||
|
|
||
|
// Untere Rahmenlinie
|
||
|
generateBottomLine();
|
||
|
|
||
|
std::cout << display << std::endl;
|
||
|
}
|
||
|
|
||
|
//
|
||
|
void move(std::string move) {
|
||
|
|
||
|
|
||
|
// 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
|
||
|
// en passond: write square where pawn lands
|
||
|
|
||
|
// capture: x
|
||
|
// check: +
|
||
|
// checkmate: #
|
||
|
// draw/stalemate: 1/2-1/2
|
||
|
|
||
|
}
|
||
|
|
||
|
// This method saves the current board state
|
||
|
void saveBoard(Chessboard& chessboard) {
|
||
|
|
||
|
}
|
||
|
|
||
|
// This method loads a save state
|
||
|
void loadBoard(int saveState) {
|
||
|
// readJSONFile
|
||
|
//
|
||
|
}
|
||
|
};
|