From 9c0acb89b98ccea49e9c5e522b39490c8cb28c0e Mon Sep 17 00:00:00 2001 From: AdmiralEmser Date: Sun, 9 Oct 2022 17:13:41 +0200 Subject: [PATCH 1/5] added: first text templates, first checks for piece movement, Information: There is no tui.h since MSVC doesn't find the code in there... for some unexplainable reason... Check FC-Chess.h -> FCC::TUI for further information. --- .gitignore | 3 +- CMakePresets.json | 101 -------------------- FC-Chess/CMakeLists.txt | 2 +- FC-Chess/FC-Chess.cpp | 65 +++++++++++-- FC-Chess/FC-Chess.h | 51 ++++++++-- FC-Chess/_unused_and_old/_graphics.h | 20 ++++ FC-Chess/{ => _unused_and_old}/graphics.cpp | 12 ++- FC-Chess/_unused_and_old/graphics.h | 18 ++++ FC-Chess/_unused_and_old/lang.cpp | 6 ++ FC-Chess/_unused_and_old/lang.h | 22 +++++ FC-Chess/_unused_and_old/tui.h | 18 ++++ FC-Chess/graphics.h | 14 --- FC-Chess/lang.h | 20 ++++ FC-Chess/main.cpp | 8 +- FC-Chess/tui.cpp | 19 ++++ 15 files changed, 241 insertions(+), 138 deletions(-) delete mode 100644 CMakePresets.json create mode 100644 FC-Chess/_unused_and_old/_graphics.h rename FC-Chess/{ => _unused_and_old}/graphics.cpp (55%) create mode 100644 FC-Chess/_unused_and_old/graphics.h create mode 100644 FC-Chess/_unused_and_old/lang.cpp create mode 100644 FC-Chess/_unused_and_old/lang.h create mode 100644 FC-Chess/_unused_and_old/tui.h delete mode 100644 FC-Chess/graphics.h create mode 100644 FC-Chess/lang.h create mode 100644 FC-Chess/tui.cpp 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..b803252 100644 --- a/FC-Chess/FC-Chess.cpp +++ b/FC-Chess/FC-Chess.cpp @@ -1,9 +1,29 @@ -#include "fc-chess.h" +#include "FC-Chess.h" + +FCC::Chess::Chess() +{ + + //start game + FCC::Chess::startup(); + FCC::Chess::mainLoop(); +} void FCC::Chess::mainLoop() { - FCC::ChessTUI tui; + // initialize user interface + + // tui.startup(); + // main loop + while(!gameEnded) + { + /*MISSING: user input*/ + // tui.mainLoop(); + } +} + +void FCC::Chess::startup() +{ // preparing board: colors for (int8_t i = 0; i < 8; i++) { for (int j = 0; i < 2; i++) { @@ -32,11 +52,40 @@ void FCC::Chess::mainLoop() posPieces[i][4] = (uint8_t)(Pieces::QUEEN); } - // main loop - while(!gameEnded) { + // set language to English (default) + // m_lang = FCC::Languages::ENGLISH; +} - tui.mainLoop(); +bool FCC::Chess::checkIsEnemyOnField() +{ + // Check if the selected piece/field unequals the target piece/field => is true then + if (posColor[selX][selY] != posColor[tarX][tarY]) + { + return true; } - - -} \ No newline at end of file + return false; +} + +bool FCC::Chess::checkSelectedPosInBounds() +{ + // check if X and Y values are below 8. No negative values have to be checked since the var type is unsigned int + if (selX <= 7 && selX <= 7) + { + return true; + } + return false; +} + +bool FCC::Chess::checkTargetPosInBounds() +{ + // check if X and Y values are below 8. No negative values have to be checked since the var type is unsigned int + if (tarX <= 7 && tarX <= 7) + { + return true; + } + return false; +} + +//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..b48835a 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,58 @@ 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(); + }; + 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]; + uint8_t posPieces[8][8], posColor[8][8], posPossible[8][8], selX, selY, tarX, tarY; //tar = target, sel = selected, pos = possible // funcs + void startup(); void mainLoop(); + 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(); + bool checkSelectedPosInBounds(); + bool checkTargetPosInBounds(); + void resetFieldColors(); }; } diff --git a/FC-Chess/_unused_and_old/_graphics.h b/FC-Chess/_unused_and_old/_graphics.h new file mode 100644 index 0000000..d02d9e7 --- /dev/null +++ b/FC-Chess/_unused_and_old/_graphics.h @@ -0,0 +1,20 @@ +#pragma once +#include + +#include "FC-Chess.h" + +namespace FCC +{ + class TUI + { + public: + // constructor and destructor + TUI() = default; + virtual ~TUI() = default; + + // funcs + void startup(); + void mainLoop(); + void testLoop(); + }; +} diff --git a/FC-Chess/graphics.cpp b/FC-Chess/_unused_and_old/graphics.cpp similarity index 55% rename from FC-Chess/graphics.cpp rename to FC-Chess/_unused_and_old/graphics.cpp index 9955f23..f0add7f 100644 --- a/FC-Chess/graphics.cpp +++ b/FC-Chess/_unused_and_old/graphics.cpp @@ -1,10 +1,18 @@ #include "graphics.h" +// #include "FC-Chess.h" -void FCC::ChessTUI::mainLoop() { +void FCC::TUI::startup() +{ + +} + +void FCC::TUI::mainLoop() +{ } -void FCC::ChessTUI::testLoop() { +void FCC::TUI::testLoop() +{ std::cout << "\u2654" << std::endl; std::cout << "♔" << std::endl; //std::cout << L"♔"; diff --git a/FC-Chess/_unused_and_old/graphics.h b/FC-Chess/_unused_and_old/graphics.h new file mode 100644 index 0000000..c7142a6 --- /dev/null +++ b/FC-Chess/_unused_and_old/graphics.h @@ -0,0 +1,18 @@ +#pragma once +#include "FC-Chess.h" + +namespace FCC +{ + class TUI + { + public: + // constructor and destructor + TUI() = default; + virtual ~TUI() = default; + + // funcs + void startup(); + void mainLoop(); + void testLoop(); + }; +} \ No newline at end of file diff --git a/FC-Chess/_unused_and_old/lang.cpp b/FC-Chess/_unused_and_old/lang.cpp new file mode 100644 index 0000000..44714f6 --- /dev/null +++ b/FC-Chess/_unused_and_old/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_and_old/lang.h b/FC-Chess/_unused_and_old/lang.h new file mode 100644 index 0000000..d71b7c3 --- /dev/null +++ b/FC-Chess/_unused_and_old/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/_unused_and_old/tui.h b/FC-Chess/_unused_and_old/tui.h new file mode 100644 index 0000000..c7142a6 --- /dev/null +++ b/FC-Chess/_unused_and_old/tui.h @@ -0,0 +1,18 @@ +#pragma once +#include "FC-Chess.h" + +namespace FCC +{ + class TUI + { + public: + // constructor and destructor + TUI() = default; + virtual ~TUI() = default; + + // funcs + void startup(); + void mainLoop(); + void testLoop(); + }; +} \ 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..26945a9 --- /dev/null +++ b/FC-Chess/lang.h @@ -0,0 +1,20 @@ +#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 <--"; + }; +} \ No newline at end of file diff --git a/FC-Chess/main.cpp b/FC-Chess/main.cpp index b699344..c41f80f 100644 --- a/FC-Chess/main.cpp +++ b/FC-Chess/main.cpp @@ -1,17 +1,17 @@ // FC-Chess.cpp : Defines the entry point for the application. // -#include "fc-chess.h" +#include "FC-Chess.h" //using namespace std; int main() { FCC::Chess gm; - gm.mainLoop(); + - FCC::ChessTUI test; - test.testLoop(); + // FCC::TUI test; + // test.testLoop(); return 0; } diff --git a/FC-Chess/tui.cpp b/FC-Chess/tui.cpp new file mode 100644 index 0000000..1c78e81 --- /dev/null +++ b/FC-Chess/tui.cpp @@ -0,0 +1,19 @@ +// #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"♔"; +} \ No newline at end of file From 6d3492dbf9e4a4a66450675c71ed87803a78f841 Mon Sep 17 00:00:00 2001 From: AdmiralEmser Date: Sun, 9 Oct 2022 17:16:58 +0200 Subject: [PATCH 2/5] fixing minor issues in FC-Chess.cpp (stuff I commented out for testing) --- FC-Chess/FC-Chess.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/FC-Chess/FC-Chess.cpp b/FC-Chess/FC-Chess.cpp index b803252..6c8988d 100644 --- a/FC-Chess/FC-Chess.cpp +++ b/FC-Chess/FC-Chess.cpp @@ -11,14 +11,13 @@ FCC::Chess::Chess() void FCC::Chess::mainLoop() { // initialize user interface - - // tui.startup(); + tui.startup(); // main loop while(!gameEnded) { /*MISSING: user input*/ - // tui.mainLoop(); + tui.mainLoop(); } } From 544952c63ea17ed534e1a2a5ef0172092e790349 Mon Sep 17 00:00:00 2001 From: AdmiralEmser Date: Sun, 9 Oct 2022 17:19:00 +0200 Subject: [PATCH 3/5] removed duplicate code and renamed folder --- .../{_unused_and_old => _unused}/lang.cpp | 0 FC-Chess/{_unused_and_old => _unused}/lang.h | 0 FC-Chess/_unused_and_old/_graphics.h | 20 ------------------- FC-Chess/_unused_and_old/graphics.cpp | 19 ------------------ FC-Chess/_unused_and_old/graphics.h | 18 ----------------- FC-Chess/_unused_and_old/tui.h | 18 ----------------- 6 files changed, 75 deletions(-) rename FC-Chess/{_unused_and_old => _unused}/lang.cpp (100%) rename FC-Chess/{_unused_and_old => _unused}/lang.h (100%) delete mode 100644 FC-Chess/_unused_and_old/_graphics.h delete mode 100644 FC-Chess/_unused_and_old/graphics.cpp delete mode 100644 FC-Chess/_unused_and_old/graphics.h delete mode 100644 FC-Chess/_unused_and_old/tui.h diff --git a/FC-Chess/_unused_and_old/lang.cpp b/FC-Chess/_unused/lang.cpp similarity index 100% rename from FC-Chess/_unused_and_old/lang.cpp rename to FC-Chess/_unused/lang.cpp diff --git a/FC-Chess/_unused_and_old/lang.h b/FC-Chess/_unused/lang.h similarity index 100% rename from FC-Chess/_unused_and_old/lang.h rename to FC-Chess/_unused/lang.h diff --git a/FC-Chess/_unused_and_old/_graphics.h b/FC-Chess/_unused_and_old/_graphics.h deleted file mode 100644 index d02d9e7..0000000 --- a/FC-Chess/_unused_and_old/_graphics.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include - -#include "FC-Chess.h" - -namespace FCC -{ - class TUI - { - public: - // constructor and destructor - TUI() = default; - virtual ~TUI() = default; - - // funcs - void startup(); - void mainLoop(); - void testLoop(); - }; -} diff --git a/FC-Chess/_unused_and_old/graphics.cpp b/FC-Chess/_unused_and_old/graphics.cpp deleted file mode 100644 index f0add7f..0000000 --- a/FC-Chess/_unused_and_old/graphics.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "graphics.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"♔"; -} \ No newline at end of file diff --git a/FC-Chess/_unused_and_old/graphics.h b/FC-Chess/_unused_and_old/graphics.h deleted file mode 100644 index c7142a6..0000000 --- a/FC-Chess/_unused_and_old/graphics.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "FC-Chess.h" - -namespace FCC -{ - class TUI - { - public: - // constructor and destructor - TUI() = default; - virtual ~TUI() = default; - - // funcs - void startup(); - void mainLoop(); - void testLoop(); - }; -} \ No newline at end of file diff --git a/FC-Chess/_unused_and_old/tui.h b/FC-Chess/_unused_and_old/tui.h deleted file mode 100644 index c7142a6..0000000 --- a/FC-Chess/_unused_and_old/tui.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "FC-Chess.h" - -namespace FCC -{ - class TUI - { - public: - // constructor and destructor - TUI() = default; - virtual ~TUI() = default; - - // funcs - void startup(); - void mainLoop(); - void testLoop(); - }; -} \ No newline at end of file From 3b565e4d8cdcbba7b88377cd4ce0387da3bac682 Mon Sep 17 00:00:00 2001 From: AdmiralEmser Date: Sun, 9 Oct 2022 17:31:39 +0200 Subject: [PATCH 4/5] restructuring game startup --- FC-Chess/FC-Chess.cpp | 31 ++++++++++++++++++------------- FC-Chess/FC-Chess.h | 5 +++-- FC-Chess/main.cpp | 13 ++----------- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/FC-Chess/FC-Chess.cpp b/FC-Chess/FC-Chess.cpp index 6c8988d..c0ca0d5 100644 --- a/FC-Chess/FC-Chess.cpp +++ b/FC-Chess/FC-Chess.cpp @@ -1,27 +1,35 @@ #include "FC-Chess.h" FCC::Chess::Chess() -{ - - //start game - FCC::Chess::startup(); - FCC::Chess::mainLoop(); -} - -void FCC::Chess::mainLoop() { // 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) { - /*MISSING: user input*/ + /* To Do: user input */ tui.mainLoop(); } } -void FCC::Chess::startup() +void FCC::Chess::startupGame() { // preparing board: colors for (int8_t i = 0; i < 8; i++) { @@ -50,9 +58,6 @@ void FCC::Chess::startup() posPieces[i][3] = (uint8_t)(Pieces::KING); posPieces[i][4] = (uint8_t)(Pieces::QUEEN); } - - // set language to English (default) - // m_lang = FCC::Languages::ENGLISH; } bool FCC::Chess::checkIsEnemyOnField() diff --git a/FC-Chess/FC-Chess.h b/FC-Chess/FC-Chess.h index b48835a..8441648 100644 --- a/FC-Chess/FC-Chess.h +++ b/FC-Chess/FC-Chess.h @@ -50,8 +50,9 @@ namespace FCC uint8_t posPieces[8][8], posColor[8][8], posPossible[8][8], selX, selY, tarX, tarY; //tar = target, sel = selected, pos = possible // funcs - void startup(); - void mainLoop(); + void mainMenu(); + void startupGame(); + void gameLoop(); void checkHorizontalMovementAllowed(); void checkVerticalMovementAllowed(); void checkDiagonalMovementAllowed(); diff --git a/FC-Chess/main.cpp b/FC-Chess/main.cpp index c41f80f..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; - - - // FCC::TUI test; - // test.testLoop(); - return 0; } From b1319c76201e06aec44e4af14eb141ac5e26d36f Mon Sep 17 00:00:00 2001 From: AdmiralEmser Date: Sun, 9 Oct 2022 21:36:07 +0200 Subject: [PATCH 5/5] added checks for possible moves horizontal and vertical; changed some var types --- FC-Chess/FC-Chess.cpp | 85 ++++++++++++++++++++++++++++++++----------- FC-Chess/FC-Chess.h | 26 +++++++++---- FC-Chess/lang.h | 3 ++ FC-Chess/tui.cpp | 7 +++- 4 files changed, 91 insertions(+), 30 deletions(-) diff --git a/FC-Chess/FC-Chess.cpp b/FC-Chess/FC-Chess.cpp index c0ca0d5..b93c987 100644 --- a/FC-Chess/FC-Chess.cpp +++ b/FC-Chess/FC-Chess.cpp @@ -34,60 +34,101 @@ 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() +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[selX][selY] != posColor[tarX][tarY]) + if (posColor[*x][*y] != posColor[selX][selY]) { return true; } return false; } -bool FCC::Chess::checkSelectedPosInBounds() +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 (selX <= 7 && selX <= 7) + if (*x <= 7 && *y <= 7) { return true; } return false; } -bool FCC::Chess::checkTargetPosInBounds() +void FCC::Chess::checkAllowedMoves() { - // check if X and Y values are below 8. No negative values have to be checked since the var type is unsigned int - if (tarX <= 7 && tarX <= 7) + // reset all allowed positions + for (uint8_t i = 0; i <= 7; i++) { - return true; + for (uint8_t j = 0; j <= 7; j++) + { + posPossible[i][j] = false; + } + } + + // 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; + } +} + +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; } - return false; } //void FCC::Chess::set_lang(FCC::Languages lang) { diff --git a/FC-Chess/FC-Chess.h b/FC-Chess/FC-Chess.h index 8441648..206d31a 100644 --- a/FC-Chess/FC-Chess.h +++ b/FC-Chess/FC-Chess.h @@ -37,6 +37,7 @@ namespace FCC void startup(); void mainLoop(); void testLoop(); + void errOutput(std::string* errMessage); }; class Chess @@ -47,15 +48,18 @@ namespace FCC virtual ~Chess() = default; // vars - uint8_t posPieces[8][8], posColor[8][8], posPossible[8][8], selX, selY, tarX, tarY; //tar = target, sel = selected, pos = possible + 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 mainMenu(); void startupGame(); void gameLoop(); - void checkHorizontalMovementAllowed(); - void checkVerticalMovementAllowed(); - void checkDiagonalMovementAllowed(); + // void checkHorizontalMovementAllowed(); + // void checkVerticalMovementAllowed(); + // void checkDiagonalMovementAllowed(); //void set_lang(FCC::Languages lang); private: @@ -70,9 +74,17 @@ namespace FCC // funcs void input(); - bool checkIsEnemyOnField(); - bool checkSelectedPosInBounds(); - bool checkTargetPosInBounds(); + 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/lang.h b/FC-Chess/lang.h index 26945a9..970dc01 100644 --- a/FC-Chess/lang.h +++ b/FC-Chess/lang.h @@ -16,5 +16,8 @@ namespace FCC 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/tui.cpp b/FC-Chess/tui.cpp index 1c78e81..a548192 100644 --- a/FC-Chess/tui.cpp +++ b/FC-Chess/tui.cpp @@ -16,4 +16,9 @@ void FCC::TUI::testLoop() std::cout << "\u2654" << std::endl; std::cout << "♔" << std::endl; //std::cout << L"♔"; -} \ No newline at end of file +} + +void FCC::TUI::errOutput(std::string* errMessage) +{ + std::cout << *errMessage; +}