diff --git a/.gitignore b/.gitignore index de367d6..a41222e 100644 --- a/.gitignore +++ b/.gitignore @@ -351,4 +351,5 @@ MigrationBackup/ # own out/ -.vscode/ \ No newline at end of file +.vscode/ +build/ \ No newline at end of file diff --git a/CMakePresets.json b/CMakePresets.json deleted file mode 100644 index f4bc98b..0000000 --- a/CMakePresets.json +++ /dev/null @@ -1,101 +0,0 @@ -{ - "version": 3, - "configurePresets": [ - { - "name": "windows-base", - "hidden": true, - "generator": "Ninja", - "binaryDir": "${sourceDir}/out/build/${presetName}", - "installDir": "${sourceDir}/out/install/${presetName}", - "cacheVariables": { - "CMAKE_C_COMPILER": "cl.exe", - "CMAKE_CXX_COMPILER": "cl.exe" - }, - "condition": { - "type": "equals", - "lhs": "${hostSystemName}", - "rhs": "Windows" - } - }, - { - "name": "x64-debug", - "displayName": "x64 Debug", - "inherits": "windows-base", - "architecture": { - "value": "x64", - "strategy": "external" - }, - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Debug" - } - }, - { - "name": "x64-release", - "displayName": "x64 Release", - "inherits": "x64-debug", - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Release" - } - }, - { - "name": "x86-debug", - "displayName": "x86 Debug", - "inherits": "windows-base", - "architecture": { - "value": "x86", - "strategy": "external" - }, - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Debug" - } - }, - { - "name": "x86-release", - "displayName": "x86 Release", - "inherits": "x86-debug", - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Release" - } - }, - { - "name": "linux-debug", - "displayName": "Linux Debug", - "generator": "Ninja", - "binaryDir": "${sourceDir}/out/build/${presetName}", - "installDir": "${sourceDir}/out/install/${presetName}", - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Debug" - }, - "condition": { - "type": "equals", - "lhs": "${hostSystemName}", - "rhs": "Linux" - }, - "vendor": { - "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { - "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" - } - } - }, - { - "name": "macos-debug", - "displayName": "macOS Debug", - "generator": "Ninja", - "binaryDir": "${sourceDir}/out/build/${presetName}", - "installDir": "${sourceDir}/out/install/${presetName}", - "cacheVariables": { - "CMAKE_BUILD_TYPE": "Debug" - }, - "condition": { - "type": "equals", - "lhs": "${hostSystemName}", - "rhs": "Darwin" - }, - "vendor": { - "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": { - "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}" - } - } - } - ] -} diff --git a/FC-Chess/CMakeLists.txt b/FC-Chess/CMakeLists.txt index a6486b3..1df0288 100644 --- a/FC-Chess/CMakeLists.txt +++ b/FC-Chess/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required (VERSION 3.8) # Add source to this project's executable. -add_executable (FC-Chess "main.cpp" "fc-chess.cpp" "graphics.cpp") +add_executable (FC-Chess "main.cpp" "FC-Chess.cpp" "tui.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET FC-Chess PROPERTY CXX_STANDARD 20) diff --git a/FC-Chess/FC-Chess.cpp b/FC-Chess/FC-Chess.cpp index 8c5cb6b..b93c987 100644 --- a/FC-Chess/FC-Chess.cpp +++ b/FC-Chess/FC-Chess.cpp @@ -1,42 +1,136 @@ -#include "fc-chess.h" +#include "FC-Chess.h" -void FCC::Chess::mainLoop() +FCC::Chess::Chess() { - FCC::ChessTUI tui; + // initialize user interface + tui.startup(); + //start game + FCC::Chess::mainMenu(); +} + +void FCC::Chess::mainMenu() +{ + /* TO DO + - Add options + - call FCC::Chess::GameLoop when game begins */ +} + +void FCC::Chess::gameLoop() +{ + // initialize board + FCC::Chess::startupGame(); + + // main loop + while(!gameEnded) + { + /* To Do: user input */ + tui.mainLoop(); + } +} + +void FCC::Chess::startupGame() +{ // preparing board: colors for (int8_t i = 0; i < 8; i++) { for (int j = 0; i < 2; i++) { - posColor[i][j] = (uint8_t)(PiecesColors::BLACK); + posColor[i][j] = PiecesColors::BLACK; } for (int j = 6; i < 8; i++) { - posColor[i][j] = (uint8_t)(PiecesColors::WHITE); + posColor[i][j] = PiecesColors::WHITE; } for (int j = 2; i < 7; i++) { - posColor[i][j] = (uint8_t)(PiecesColors::NONE); + posColor[i][j] = PiecesColors::NONE; } } // preparing board: pieces for (int8_t i = 0; i < 8; i++) { - posPieces[1][i] = (uint8_t)(Pieces::PAWN); - posPieces[6][i] = (uint8_t)(Pieces::PAWN); + posPieces[1][i] = Pieces::PAWN; + posPieces[6][i] = Pieces::PAWN; } for (int8_t i = 0; i < 2; i++) { - posPieces[i][0] = (uint8_t)(Pieces::ROOK); - posPieces[i][7] = (uint8_t)(Pieces::ROOK); - posPieces[i][1] = (uint8_t)(Pieces::KNIGHT); - posPieces[i][6] = (uint8_t)(Pieces::KNIGHT); - posPieces[i][2] = (uint8_t)(Pieces::BISHOP); - posPieces[i][5] = (uint8_t)(Pieces::BISHOP); - posPieces[i][3] = (uint8_t)(Pieces::KING); - posPieces[i][4] = (uint8_t)(Pieces::QUEEN); + posPieces[i][0] = Pieces::ROOK; + posPieces[i][7] = Pieces::ROOK; + posPieces[i][1] = Pieces::KNIGHT; + posPieces[i][6] = Pieces::KNIGHT; + posPieces[i][2] = Pieces::BISHOP; + posPieces[i][5] = Pieces::BISHOP; + posPieces[i][3] = Pieces::KING; + posPieces[i][4] = Pieces::QUEEN; + } +} + +bool FCC::Chess::checkIsEnemyOnField(uint8_t* x, uint8_t* y) +{ + // Check if the selected piece/field unequals the target piece/field => is true then + if (posColor[*x][*y] != posColor[selX][selY]) + { + return true; + } + return false; +} + +bool FCC::Chess::checkPosInBounds(uint8_t* x, uint8_t* y) +{ + // check if X and Y values are below 8. No negative values have to be checked since the var type is unsigned int + if (*x <= 7 && *y <= 7) + { + return true; + } + return false; +} + +void FCC::Chess::checkAllowedMoves() +{ + // reset all allowed positions + for (uint8_t i = 0; i <= 7; i++) + { + for (uint8_t j = 0; j <= 7; j++) + { + posPossible[i][j] = false; + } } - // main loop - while(!gameEnded) { - - tui.mainLoop(); + // check piece and then check possible moves accordingly + switch (FCC::Chess::posPieces[FCC::Chess::selX][FCC::Chess::selY]) { + case FCC::Pieces::PAWN: + break; + case FCC::Pieces::ROOK: + break; + default: + tui.errOutput(&lang.errNoPieceOnPosition); + break; } - - -} \ No newline at end of file +} + +void FCC::Chess::checkMovesHorizontal() +{ + for (uint8_t i = selX; i <= 7; i++) + { + if (checkIsEnemyOnField(&i, &selY)) break; + posPossible[i][selY] = true; + } + for (uint8_t i = selX; i >= 0; i--) + { + if (checkIsEnemyOnField(&i, &selY)) break; + posPossible[i][selY] = true; + } +} + +void FCC::Chess::checkMovesVertical() +{ + for (uint8_t i = selY; i <= 7; i++) + { + if (checkIsEnemyOnField(&selX, &i)) break; + posPossible[selX][i] = true; + } + for (uint8_t i = selY; i >= 0; i--) + { + if (checkIsEnemyOnField(&selX, &i)) break; + posPossible[selX][i] = true; + } +} + +//void FCC::Chess::set_lang(FCC::Languages lang) { +// m_lang = lang; +//} diff --git a/FC-Chess/FC-Chess.h b/FC-Chess/FC-Chess.h index e2452b8..206d31a 100644 --- a/FC-Chess/FC-Chess.h +++ b/FC-Chess/FC-Chess.h @@ -1,7 +1,7 @@ #pragma once -#include - -#include "graphics.h" +//#include +// #include "tui.h" +#include "lang.h" namespace FCC { @@ -20,21 +20,71 @@ namespace FCC BLACK = 10, WHITE = 20 }; - + // classes and funcs + class TUI + { + /* Why is FCC::TUI here and not in tui.h, so another file? Well, that's quite easy. MSVC doesn't know how to access code from other files, apparently. + Error code is that class FCC::TUI wouldn't be a member of namespace FCC - which is a wrong statement. It is a member of FCC. + So yeah, thanks Microsoft.*/ + + public: + // constructor and destructor + TUI() = default; + virtual ~TUI() = default; + + // funcs + void startup(); + void mainLoop(); + void testLoop(); + void errOutput(std::string* errMessage); + }; + class Chess { public: // constructor and destructor - Chess() = default; - ~Chess() = default; + Chess(); + virtual ~Chess() = default; // vars - uint8_t posPieces[8][8], posColor[8][8], posPossible[8][8]; + FCC::Pieces posPieces[8][8]; + FCC::PiecesColors posColor[8][8]; + bool posPossible[8][8]; + uint8_t selX, selY, tarX, tarY; //tar = target, sel = selected, pos = possible // funcs - void mainLoop(); + void mainMenu(); + void startupGame(); + void gameLoop(); + // void checkHorizontalMovementAllowed(); + // void checkVerticalMovementAllowed(); + // void checkDiagonalMovementAllowed(); + //void set_lang(FCC::Languages lang); + private: + // vars bool gameEnded; + + // texts for tui and gui + FCC::Lang lang; + FCC::TUI tui; + //FCC::ChessLang chessLangObj; + //FCC::Languages m_lang{ FCC::Languages::ENGLISH }; + + // funcs + void input(); + bool checkIsEnemyOnField(uint8_t* x, uint8_t* y); + bool checkPosInBounds(uint8_t* x, uint8_t* y); + void checkAllowedMoves(); + void checkMovesHorizontal(); + void checkMovesVertical(); + void checkMovesDiagonal(); + void checkMovesKnight(); + void checkMovesKing(); + void checkMovesPawn(); + void checkPosEqualsSelPos(uint8_t* x, uint8_t* y); + void resetFieldColors(); + void resetAllowedMovement(); }; } diff --git a/FC-Chess/_unused/lang.cpp b/FC-Chess/_unused/lang.cpp new file mode 100644 index 0000000..44714f6 --- /dev/null +++ b/FC-Chess/_unused/lang.cpp @@ -0,0 +1,6 @@ +#include "lang.h" + +void FCC::ChessLang::setLanguage(uint8_t setLangTo) +{ + language = setLangTo; +} \ No newline at end of file diff --git a/FC-Chess/_unused/lang.h b/FC-Chess/_unused/lang.h new file mode 100644 index 0000000..d71b7c3 --- /dev/null +++ b/FC-Chess/_unused/lang.h @@ -0,0 +1,22 @@ +#pragma once +#include +//#include "FC-Chess.h" +#include + +namespace FCC { + enum class Languages { + ENGLISH, + GERMAN + }; +} +namespace FCC::ChessLang +{ + inline std::string testString(FCC::Languages lang) { + switch (lang) { + case FCC::Languages::ENGLISH: + return "Hello World"; + case FCC::Languages::GERMAN: + return "Hallo Welt"; + } + } +} \ No newline at end of file diff --git a/FC-Chess/graphics.cpp b/FC-Chess/graphics.cpp deleted file mode 100644 index 9955f23..0000000 --- a/FC-Chess/graphics.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "graphics.h" - -void FCC::ChessTUI::mainLoop() { - -} - -void FCC::ChessTUI::testLoop() { - std::cout << "\u2654" << std::endl; - std::cout << "♔" << std::endl; - //std::cout << L"♔"; -} \ No newline at end of file diff --git a/FC-Chess/graphics.h b/FC-Chess/graphics.h deleted file mode 100644 index 9a5b3da..0000000 --- a/FC-Chess/graphics.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include - -#include "FC-Chess.h" - -namespace FCC -{ - class ChessTUI - { - public: - void mainLoop(); - void testLoop(); - }; -} \ No newline at end of file diff --git a/FC-Chess/lang.h b/FC-Chess/lang.h new file mode 100644 index 0000000..970dc01 --- /dev/null +++ b/FC-Chess/lang.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include + +namespace FCC +{ + class Lang + { + public: + // texts shown in gui/tui + std::string menuButtonPlay = "Start Game!"; + std::string menuButtonSettings = "Settings"; + std::string menuButtonCredits = "Credits"; + std::string menuButtonExitGame = "Exit to Desktop"; + std::string error = "An error occoured. The game will exit now."; + std::string gameForbiddenMove = "This move is not allowed."; + std::string credits = "FoxCreation Chess by SinusFox 2022.\nContact: admiralemser@gmail.com\nhttps://github.com/AdmiralEmser/FC-Chess"; + std::string backButton = "Back <--"; + + // error texts + std::string errNoPieceOnPosition = "Error: Empty position."; + }; +} \ No newline at end of file diff --git a/FC-Chess/main.cpp b/FC-Chess/main.cpp index b699344..f3d8dde 100644 --- a/FC-Chess/main.cpp +++ b/FC-Chess/main.cpp @@ -1,17 +1,8 @@ -// FC-Chess.cpp : Defines the entry point for the application. -// - -#include "fc-chess.h" - -//using namespace std; +#include "FC-Chess.h" int main() { + // starting the game FCC::Chess gm; - gm.mainLoop(); - - FCC::ChessTUI test; - test.testLoop(); - return 0; } diff --git a/FC-Chess/tui.cpp b/FC-Chess/tui.cpp new file mode 100644 index 0000000..a548192 --- /dev/null +++ b/FC-Chess/tui.cpp @@ -0,0 +1,24 @@ +// #include "tui.h" +#include "FC-Chess.h" + +void FCC::TUI::startup() +{ + +} + +void FCC::TUI::mainLoop() +{ + +} + +void FCC::TUI::testLoop() +{ + std::cout << "\u2654" << std::endl; + std::cout << "♔" << std::endl; + //std::cout << L"♔"; +} + +void FCC::TUI::errOutput(std::string* errMessage) +{ + std::cout << *errMessage; +}