Move draw method to new class file; Add Utils class
This commit is contained in:
parent
41803c5ef3
commit
95bc2a3f3c
193
Chessboard.cpp
Normal file
193
Chessboard.cpp
Normal file
@ -0,0 +1,193 @@
|
||||
//
|
||||
// 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
|
||||
//
|
||||
}
|
||||
};
|
8
Chessboard.h
Normal file
8
Chessboard.h
Normal file
@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by hamac on 18.12.2024.
|
||||
//
|
||||
|
||||
#ifndef CHESSBOARD_H
|
||||
#define CHESSBOARD_H
|
||||
|
||||
#endif //CHESSBOARD_H
|
8
Chesspiece.cpp
Normal file
8
Chesspiece.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
//
|
||||
// Created by hamac on 18.12.2024.
|
||||
//
|
||||
|
||||
class Chesspiece {
|
||||
|
||||
|
||||
}
|
60
OldCode.txt
Normal file
60
OldCode.txt
Normal file
@ -0,0 +1,60 @@
|
||||
void firstVersion() {
|
||||
const std::string white = "\u2B1C";
|
||||
const std::string black = "\u2B1B";
|
||||
|
||||
std::string brett[8][8];
|
||||
|
||||
std::string field;
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
if (y % 2 == 0) {
|
||||
field = (x % 2 == 0) ? "\u2B1C" : "\u2B1B";
|
||||
}
|
||||
else {
|
||||
field = (x % 2 == 0) ? "⬛" : "⬜";
|
||||
}
|
||||
std::cout << field;
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void secondVersion() {
|
||||
//wchar_t t = 0x25FF;
|
||||
|
||||
// Horizontale Linie u2500
|
||||
// Vertikale Linie u2502
|
||||
// Top Right Corner u250C
|
||||
// Top Left Corner u2510
|
||||
// Bottom Right Corner u2514
|
||||
// Bottom Left Corner u2518
|
||||
|
||||
std::string topRightCorner = "\u2554";
|
||||
std::string topLeftCorner = "\u2557";
|
||||
std::string bottomLeftCorner = "\u255A";
|
||||
std::string bottomRightCorner = "\u255D";
|
||||
std::string horizontalLine = "\u2550\u2550";
|
||||
std::string verticalLine = "\u2551";
|
||||
std::string crossSuc = "\u256C";
|
||||
std::string leftSide = "\u2560";
|
||||
std::string rightSide = "\u2563";
|
||||
std::string topSide = "\u2566";
|
||||
std::string bottomSide = "\u2569";
|
||||
|
||||
std::string firstLine = "\u2554\u2550\u2566";
|
||||
std::string line;
|
||||
|
||||
for (int row = 0; row < 9; ++row) {
|
||||
for (int col = 0; col < 8; ++col) {
|
||||
if (row == 0 && col > 0) line += topSide + horizontalLine;
|
||||
if (row == 8 && col > 0) line += bottomSide + horizontalLine;
|
||||
if (col == 0 && row < 8 && row > 0) line += leftSide + horizontalLine;
|
||||
if (row > 0 && row < 8 && col > 0) line += crossSuc + horizontalLine;
|
||||
if (col == 7 && row < 8 && row > 0) line += rightSide;
|
||||
if (row == 0 && col == 0) line += topRightCorner + horizontalLine;
|
||||
if (row == 8 && col == 0) line += bottomLeftCorner + horizontalLine;
|
||||
if (row == 0 && col == 7) line += topLeftCorner + "\n" + verticalLine;
|
||||
if (row == 8 && col == 7) line += bottomRightCorner;
|
||||
}
|
||||
line += "\n";
|
||||
}
|
19
Utils.cpp
Normal file
19
Utils.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by hamac on 18.12.2024.
|
||||
//
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
class Utils {
|
||||
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;
|
||||
}
|
||||
}
|
267
main.cpp
267
main.cpp
@ -1,277 +1,18 @@
|
||||
// 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() {
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
|
||||
// Zeichen für die Rahmen und das Schachbrett
|
||||
const std::string topLeft = "┌", topRight = "┐", bottomLeft = "└", bottomRight = "┘";
|
||||
const std::string horizontal = "─", vertical = "│";
|
||||
const std::string topIntersection = "┬", bottomIntersection = "┴", middleIntersection = "┼";
|
||||
|
||||
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";
|
||||
|
||||
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";
|
||||
|
||||
const int boardSize = 8;
|
||||
|
||||
int brett[8][8] = {
|
||||
{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}
|
||||
};
|
||||
|
||||
// Obere Rahmenlinie
|
||||
std::cout << topLeft;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
std::cout << horizontal << horizontal << horizontal;
|
||||
if (col < boardSize - 1) std::cout << topIntersection;
|
||||
}
|
||||
std::cout << topRight << "\n";
|
||||
|
||||
// Schachbrett mit vertikalen Linien
|
||||
for (int row = 0; row < boardSize; ++row) {
|
||||
for (int subRow = 0; subRow < 1; ++subRow) {
|
||||
std::cout << vertical;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
switch (brett[row][col]) {
|
||||
case 1: std::cout << " " << whiteSquare << " ";
|
||||
break;
|
||||
case 101: std::cout << " " << whiteRook << " ";
|
||||
break;
|
||||
case 102: std::cout << " " << whiteKnight << " ";
|
||||
break;
|
||||
case 103: std::cout << " " << whiteBischop << " ";
|
||||
break;
|
||||
case 104: std::cout << " " << whiteQueen << " ";
|
||||
break;
|
||||
case 105: std::cout << " " << whiteKing << " ";
|
||||
break;
|
||||
case 100: std::cout << " " << whitePawn << " ";
|
||||
break;
|
||||
case 2: std::cout << " " << blackSquare << " ";
|
||||
break;
|
||||
case 201: std::cout << " " << blackRook << " ";
|
||||
break;
|
||||
case 202: std::cout << " " << blackKnight << " ";
|
||||
break;
|
||||
case 203: std::cout << " " << blackBischop << " ";
|
||||
break;
|
||||
case 204: std::cout << " " << blackQueen << " ";
|
||||
break;
|
||||
case 205: std::cout << " " << blackKing << " ";
|
||||
break;
|
||||
case 200: std::cout << " " << blackPawn << " ";
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
/*if ((row + col) % 2 == 0) {
|
||||
std::cout << " " << whiteSquare << " ";
|
||||
}
|
||||
else {
|
||||
std::cout << " " << blackSquare << " ";
|
||||
}*/
|
||||
std::cout << vertical;
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
// Horizontale Trennlinie (außer nach der letzten Zeile)
|
||||
if (row < boardSize - 1) {
|
||||
std::cout << vertical;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
std::cout << horizontal << horizontal << horizontal;
|
||||
if (col < boardSize - 1) std::cout << middleIntersection;
|
||||
}
|
||||
std::cout << vertical << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Untere Rahmenlinie
|
||||
std::cout << bottomLeft;
|
||||
for (int col = 0; col < boardSize; ++col) {
|
||||
std::cout << horizontal << horizontal << horizontal;
|
||||
if (col < boardSize - 1) std::cout << bottomIntersection;
|
||||
}
|
||||
std::cout << bottomRight << "\n";
|
||||
Chessboard chessboard;
|
||||
chessboard.draw();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void givenBoard() {
|
||||
int brett[8][8] = {
|
||||
{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}
|
||||
};
|
||||
}
|
||||
|
||||
/*int main() {
|
||||
//std::setlocale(LC_ALL, "");
|
||||
|
||||
SetConsoleOutputCP(CP_UTF8);
|
||||
|
||||
const std::string white = "\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";
|
||||
|
||||
const std::string black = "\u2B1B";
|
||||
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";
|
||||
|
||||
int brett[8][8] = {
|
||||
{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::string field;
|
||||
for (auto& row : brett) {
|
||||
for (int col : row) {
|
||||
switch (col) {
|
||||
case 1: field += white;
|
||||
break;
|
||||
case 101: field += whiteRook;
|
||||
break;
|
||||
case 102: field += whiteKnight;
|
||||
break;
|
||||
case 103: field += whiteBischop;
|
||||
break;
|
||||
case 104: field += whiteQueen;
|
||||
break;
|
||||
case 105: field += whiteKing;
|
||||
break;
|
||||
case 100: field += whitePawn;
|
||||
break;
|
||||
case 2: field += black;
|
||||
break;
|
||||
case 201: field += blackRook;
|
||||
break;
|
||||
case 202: field += blackKnight;
|
||||
break;
|
||||
case 203: field += blackBischop;
|
||||
break;
|
||||
case 204: field += blackQueen;
|
||||
break;
|
||||
case 205: field += blackKing;
|
||||
break;
|
||||
case 200: field += blackPawn;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
field += "\n";
|
||||
}
|
||||
|
||||
std::cout << field << std::endl;
|
||||
|
||||
//std::wcout << L"\u2500" << std::endl;
|
||||
//std::cout << field << std::endl;
|
||||
return 0;
|
||||
}*/
|
||||
|
||||
|
||||
void firstVersion() {
|
||||
const std::string white = "\u2B1C";
|
||||
const std::string black = "\u2B1B";
|
||||
|
||||
std::string brett[8][8];
|
||||
|
||||
std::string field;
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
if (y % 2 == 0) {
|
||||
field = (x % 2 == 0) ? "\u2B1C" : "\u2B1B";
|
||||
}
|
||||
else {
|
||||
field = (x % 2 == 0) ? "⬛" : "⬜";
|
||||
}
|
||||
std::cout << field;
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void secondVersion() {
|
||||
//wchar_t t = 0x25FF;
|
||||
|
||||
// Horizontale Linie u2500
|
||||
// Vertikale Linie u2502
|
||||
// Top Right Corner u250C
|
||||
// Top Left Corner u2510
|
||||
// Bottom Right Corner u2514
|
||||
// Bottom Left Corner u2518
|
||||
|
||||
std::string topRightCorner = "\u2554";
|
||||
std::string topLeftCorner = "\u2557";
|
||||
std::string bottomLeftCorner = "\u255A";
|
||||
std::string bottomRightCorner = "\u255D";
|
||||
std::string horizontalLine = "\u2550\u2550";
|
||||
std::string verticalLine = "\u2551";
|
||||
std::string crossSuc = "\u256C";
|
||||
std::string leftSide = "\u2560";
|
||||
std::string rightSide = "\u2563";
|
||||
std::string topSide = "\u2566";
|
||||
std::string bottomSide = "\u2569";
|
||||
|
||||
std::string firstLine = "\u2554\u2550\u2566";
|
||||
std::string line;
|
||||
|
||||
for (int row = 0; row < 9; ++row) {
|
||||
for (int col = 0; col < 8; ++col) {
|
||||
if (row == 0 && col > 0) line += topSide + horizontalLine;
|
||||
if (row == 8 && col > 0) line += bottomSide + horizontalLine;
|
||||
if (col == 0 && row < 8 && row > 0) line += leftSide + horizontalLine;
|
||||
if (row > 0 && row < 8 && col > 0) line += crossSuc + horizontalLine;
|
||||
if (col == 7 && row < 8 && row > 0) line += rightSide;
|
||||
if (row == 0 && col == 0) line += topRightCorner + horizontalLine;
|
||||
if (row == 8 && col == 0) line += bottomLeftCorner + horizontalLine;
|
||||
if (row == 0 && col == 7) line += topLeftCorner + "\n" + verticalLine;
|
||||
if (row == 8 && col == 7) line += bottomRightCorner;
|
||||
}
|
||||
line += "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
34
saveStats.json
Normal file
34
saveStats.json
Normal file
@ -0,0 +1,34 @@
|
||||
{
|
||||
"saves": [
|
||||
[
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[]
|
||||
],
|
||||
[
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[]
|
||||
],
|
||||
[
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[],
|
||||
[]
|
||||
]
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user