From b1fc616425b92acf6a142bcf09cc5dc99d2523c8 Mon Sep 17 00:00:00 2001 From: Arkaprabha Chakraborty Date: Sat, 3 Feb 2024 12:41:24 +0530 Subject: [PATCH] Initial commit --- .clang-format | 236 +++++++++++++++++++++++++++++++++++++ maps/21x21.maze | 21 ++++ maps/33x33.maze | 33 ++++++ maps/49x97.maze | 49 ++++++++ maps/65x65.maze | 65 +++++++++++ maze.cxx | 302 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 706 insertions(+) create mode 100644 .clang-format create mode 100644 maps/21x21.maze create mode 100644 maps/33x33.maze create mode 100644 maps/49x97.maze create mode 100644 maps/65x65.maze create mode 100644 maze.cxx diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b18f1ff --- /dev/null +++ b/.clang-format @@ -0,0 +1,236 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCaseColons: false +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAfterAttributes: Never +BreakAfterJavaFieldAnnotations: false +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Attach +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 120 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +KeepEmptyLinesAtEOF: false +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +PPIndentWidth: -1 +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RemoveParentheses: Leave +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + InCStyleCasts: false + InConditionalStatements: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +... + diff --git a/maps/21x21.maze b/maps/21x21.maze new file mode 100644 index 0000000..9079a08 --- /dev/null +++ b/maps/21x21.maze @@ -0,0 +1,21 @@ +111111111111111111111 +401000100000001000001 +101011111110101110111 +100000000000100010001 +111110111111111110111 +100010101000101010001 +101010101011101010101 +101010000010101000101 +101110111110101110101 +100010000000000000101 +111010111111111110111 +100000000010001000101 +111110111110101010101 +101010000000101010001 +101011101111111111101 +100000100010100000101 +101011111110101111101 +101010000000100000001 +101111101110101110111 +100000001000000010003 +111111111111111111111 diff --git a/maps/33x33.maze b/maps/33x33.maze new file mode 100644 index 0000000..cb42bbd --- /dev/null +++ b/maps/33x33.maze @@ -0,0 +1,33 @@ +111111111111111111111111111111111 +400000001000000010001000000000001 +111011111110111011101010101110111 +101000101010101010001010101000001 +101011101010101010111010111110111 +100000100000100000100010100000001 +111010111110111110101111111110111 +100010100010000010001010101000101 +101011101111111111101010101011101 +101010000000100000000010000000101 +101110111010111110101111101011101 +100000101010100000100010001000001 +111010101111101010111011111111111 +100010001000001010100000000000001 +101011101010111110101011111110101 +101010100010001000101000001000101 +101010111011101111111111111111111 +101000100010001010001000000000001 +111111111011111011101110111010111 +100000100010101010000000001010001 +101110101010101011111111111110111 +100010101000100000101010001000001 +111010111010111010101010111010111 +100010001010100010001000100010001 +111011111110111110111110101010111 +100000101000000010000010001010101 +101111101010111110111110111110101 +100010000010000000100000001000001 +101111111011111110111111101111101 +100010000000100000100010000000101 +101011101011101010101010101111101 +101000001010001010001000100000101 +111111111111111111111111111111131 diff --git a/maps/49x97.maze b/maps/49x97.maze new file mode 100644 index 0000000..8cdf165 --- /dev/null +++ b/maps/49x97.maze @@ -0,0 +1,49 @@ +4011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 +0010001000001010000010101000100000100000100000001010000010001000001000000010000010101000100000101 +1010111110111010111110101010101111101110111011111010101111101111101011111010111010101011111010101 +1000001000000010100010000010000010001000000000000010100010000000001000001000101010101000101010001 +1011101110111110101111111110101011111010101110111011101111111111101110111010101010101110101011101 +1000100000001010000010101000101010100010101010001000000000100000000010100010100000101000000010101 +1010111111101010111110101011111110101010111011101010111011101011111111101111101110101111111110101 +1010001010000000101010001000101000001010001000001010100000101000100000000000101000001010000000001 +1110111011111111101010111011101111111011111111101110101111111010111110111111101111111011101011101 +1000101000001000100000100010000000001010000000000010101010000010100010101010000010000000001000101 +1110101110101011111011101010111110111111111011101011111010101011101011101011101111101011111111101 +1010001000101010100000101000001000101010000000101000001010101000101010000010000010101010101010001 +1011101111101010101111111110111111101011101111111110111011101111111011101111111010111110101010111 +1000101010100000100010000000001000001000101000100000000010000000000000001010100010000010000000001 +1011101010111010111011111111101010101110101110101011111010111110101010111010101111101110111010101 +1000000000100010100000100000000010100000100000101000100000000010101010100010000000001000001010101 +1010111010101111101010111111101111111010111110111110111111101110101111101011111110111011111111101 +1010101010000010101010101010100000100010000000000010000000100010101010001000100000000000101010001 +1011101111101110111010101010101111111110111111101011111110101111111010111111101011111011101010111 +1010001010001000000010101000001010101010001000001010100010100010001000001000001000001000100010001 +1011101011111011111011101011101010101010111111111110101110111111101011101011101010111011111010101 +1010001000000010101000100010000010000010100010001010000010100000101010000010001010001000000010101 +1011101011101010101011101111101011111011101110111011101110101111101110111110111110111110111111111 +1000000010001010100010000010101010000000101010000000000010000000101010100010001010001000000000001 +1011111111111010111110101110101011101110101010111011111011101110101011101010111010111110111011111 +1010100000000000100010101010001010000010001000100010000000001010000010001010100000100000001010001 +1110101011111011101011101010111010111111111110111111101011101011111010101111111011111110111011101 +1000001000100000001010001010001000100000001000101000101010001000000000100010000000001000001000001 +1111111110111011101110101010111110111011111011101011101010101110111011111110101111101110111110101 +1000100000001000100010100010001010101010001000001000001010101000001000001000101000101010100000101 +1010101111111011101111101111111011101011101011101110111110111011111111101111111110111011111010111 +1010000000101010100000101000100000100000001010101000001000100000001000000000100010101000101010001 +1010111110101010111111101010101111101110111010111111111111111111111010101111101110101010101110111 +1010100000001010000000000010101000001000001000001000000000000010100010101010101000100010100010101 +1111111010101111111110111011101010101110111111101110101111101110101110101010101110111011101110101 +1010001010100010001000101000100010101010000000000010101010000000101010100010001000100010100000001 +1011101110101111101010101110101110101011101111111010111010111010111010111110101010111010101011101 +1000001010101000101010101010100010101000001010101010101010101010000010000010101010000010101010001 +1110101010111011101011101011111111111111111010101010101011101110101111111111101010111010101011101 +1000100010000000001000101000100000001000000000000000101010000000100000001000101010001000101000101 +1010101110111110101011101011101010111011111011101110101011111111101110111110101110111010111011111 +1010100010001000100010000010001010101010000000100010000010100000000010001000000000100010101000001 +1011111110111110111111111011101011101110111111111110111110101010101111101110111011111111101011111 +1000000000001000000000001000101010000010001000100010101010001010101000000010001000000000001010101 +1011111011101011111010101011111010111010111011101010101010111111111010111011101111111011101010101 +1000001010001000001010100000100000001010000000101010000010100010000010100000000010100010000010101 +1111101110101110101111111110111111101111101011111010101111111010111010101111111110101111111010101 +1000001000100010100010000000000000000000101000100000100000100000100010100000000000101000000000000 +1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111103 diff --git a/maps/65x65.maze b/maps/65x65.maze new file mode 100644 index 0000000..bb5eaea --- /dev/null +++ b/maps/65x65.maze @@ -0,0 +1,65 @@ +40111111111111111111111111111111111111111111111111111111111111111 +00001000101000000000001000000000100010001000000010100010100000001 +10111110101110111111101011111011111011101011101010111010111110111 +10000010100010000000100010001010100000000010101010000010001000101 +11101010101010101011111010111110101011111110101010111011101110101 +10001000101000101010100000001000101000101000101000001000000000001 +11111010111110111110111110101010111011101110111110111110111011111 +10100010100000100000001000101010100000100000100000100000001000101 +10101110101110111011111110101110101110111011111011101110111111101 +10101010101000000010100010100010101010100000101010101010000000001 +10101011111011111010101011101110111011101110101010111011101111111 +10000000000000100000101000101000000000101000101010000000100000001 +11111111101011111011101110101110111111111010101111111010111110111 +10100010001010100000000010101000101010000010001010000010000000001 +10101111101010111110111011111110101011101010111011111110101111101 +10000000101010001000100000000010000010101010001000000000101000001 +10111110111111101010111111101111101010111010101010111111111111101 +10001000000000001010101000001000001000100010101010000000000010001 +10101111101110111011101110111110101011111011111011111111111111101 +10101010000010000010000000100000101010000000100000000000000010001 +11111010111010111111111110101111111111101110111011101111111111101 +10001000100010000010001010001000101000000010101010000010000010001 +10101011101011111011101010111110101111101110101011101111101011101 +10100010001000101010100010000000101000100010100010000010101010101 +11111110111111101110111011111010101110101011101110111010101110101 +10001000101010000010100000000010000000101010001000100000100000101 +10111010101010101110101111111111101010101111101011111010101010111 +10101010101000100000001000001000001010100010001000101010101010001 +10101011101011101011111111101010101011111011101010101110101110111 +10000000100010001000100010000010101000000000001010001010001000001 +10101010101110111111111010101111111110111111101011111011101011111 +10101010101000000010101010101000100010100000001010001000001000001 +10101011101011111110101011101010101110111111101010101011101111111 +10101000101000100010101000000010100000001010001010100010101010101 +10111111111010101110101110101111101111111010111011111010111010101 +10000010101010101000100010101000100010000010101010000000100000001 +11101110101110101010111010111010111111111011101110111010101011101 +10001000001000101010100010100010100010101000100000001010001010001 +11101110111110101011111010101111101010101010111111111011111111101 +10000010001000001000100000000000001010001010100010001000000010101 +10101110101010111010101011101110111110101110111010101110111110111 +10101010101010001010001010100010100000100010100000101000000000001 +11101011101010111111111110111110101111101110111010101010111111101 +10001000000010101000100010100000000000101000001010101010100010001 +10111110111010101011111010101110111111101110101110111011101010111 +10001000001010001000101010001000101000000000101000000000101010001 +10111010101011111110101011111011101110111111111111111110111011101 +10100010101000001010000010100000001000000010000010100010101000001 +11101011101011111011101010101011101011101111111010111010101011111 +10101010001000100010001010001010101010100010000000000010001000101 +10101110101110111010101011111010111010101111101110101111111010101 +10001000101010001000101000100000100010100000000010100000001010001 +11111111101011101011101110101111111110101111101111111011101111101 +10100010000010000010000010100010100010101010101000000000101000001 +10111011111111111111101111111010101110101010101111111110111110111 +10001010000010100010001000000010101000001000100000001000001000101 +11101010101110111010111010111110101111101011101111111111111011101 +10101000101000100000001010101000000000001000100000100010000000001 +10101110111010101010111111101110111110111011111111101011101110111 +10000000000010101010000010001000001000000010001010001010001000101 +11101111111010101110111110111110101011101010111011101010111011101 +10001010100010101000100000000000101010001000000000101010001010101 +10111010111110101011101010111110111011101111101110111010101110101 +10000010000000001010001010100000100010000010001000100000100000000 +11111111111111111111111111111111111111111111111111111111111111103 diff --git a/maze.cxx b/maze.cxx new file mode 100644 index 0000000..9ef65d1 --- /dev/null +++ b/maze.cxx @@ -0,0 +1,302 @@ +#include +#include +#include +#include +#include + +#include +#include +#include + +class Entity { + public: + size_t xIdx; + size_t yIdx; + + enum class Move { + Up, + Down, + Left, + Right, + }; + + Entity(size_t x, size_t y) : xIdx(x), yIdx(y) {} +}; + +class Maze { + public: + size_t rowSize; + size_t colSize; + std::unique_ptr player; + std::unique_ptr start; + std::unique_ptr end; + std::vector> pntr; + + Maze(size_t rowSize, size_t colSize) : rowSize(rowSize), colSize(colSize) { + pntr.resize(colSize, std::vector(rowSize, 0)); + } +}; + +class Game { + private: + struct termios term_state; + size_t termColSize; + size_t termRowSize; + std::unique_ptr maze; + bool quitGame; + bool winGame; + + public: + Game() : quitGame(false), winGame(false) {} + + void getTermSize() { + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + termRowSize = w.ws_row; + termColSize = w.ws_col; + } + + void termTooSmallHandler() { + if (termColSize < 2 * maze->rowSize || termRowSize < maze->colSize) { + std::cout << "Terminal is too small to display the whole maze.\n" + "Either make the terminal window bigger, or use a smaller maze map.\n"; + exit(0); + } + } + + void getMazeSize(const char *path) { + FILE *file; + if (!(file = fopen(path, "r"))) { + std::cout << "Couldn't find map file. Check file path.\n"; + exit(0); + } + + char ch; + size_t x_cnt = 0; + size_t y_cnt = 0; + bool cnt_x = true; + while ((ch = getc(file)) != EOF) { + if (ch == '\n') { + ++y_cnt; + cnt_x = false; + } else if (cnt_x) { + ++x_cnt; + } + } + + maze->rowSize = x_cnt; + maze->colSize = y_cnt; + } + + void restoreCursor() { std::cout << "\033[u"; } + + void saveCursor() { std::cout << "\033[s"; } + + void zeroCursor() { std::cout << "\033[0;0H" << std::flush; } + + void move(Entity::Move move) { + if (move == Entity::Move::Left && maze->player->xIdx != 0 && + maze->pntr[maze->player->yIdx][maze->player->xIdx - 1] != 1) { + if (maze->player->xIdx == maze->start->xIdx && maze->player->yIdx == maze->start->yIdx) { + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 2; + } else { + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 0; + } + --maze->player->xIdx; + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 4; + } + if (move == Entity::Move::Down && maze->player->yIdx != maze->colSize - 1 && + maze->pntr[maze->player->yIdx + 1][maze->player->xIdx] != 1) { + if (maze->player->xIdx == maze->start->xIdx && maze->player->yIdx == maze->start->yIdx) { + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 2; + } else { + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 0; + } + ++maze->player->yIdx; + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 4; + } + if (move == Entity::Move::Up && maze->player->yIdx != 0 && + maze->pntr[maze->player->yIdx - 1][maze->player->xIdx] != 1) { + if (maze->player->xIdx == maze->start->xIdx && maze->player->yIdx == maze->start->yIdx) { + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 2; + } else { + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 0; + } + --maze->player->yIdx; + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 4; + } + if (move == Entity::Move::Right && maze->player->xIdx != maze->rowSize - 1 && + maze->pntr[maze->player->yIdx][maze->player->xIdx + 1] != 1) { + if (maze->player->xIdx == maze->start->xIdx && maze->player->yIdx == maze->start->yIdx) { + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 2; + } else { + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 0; + } + ++maze->player->xIdx; + maze->pntr[maze->player->yIdx][maze->player->xIdx] = 4; + } + } + + void freeMap() { + // Free memory for maze and entities + maze.reset(); + } + + void allocMazeBuffer(const char *path) { + maze = std::make_unique(0, 0); + maze->player = std::make_unique(0, 0); + maze->start = std::make_unique(0, 0); + maze->end = std::make_unique(0, 0); + getMazeSize(path); + + maze->pntr.resize(maze->colSize, std::vector(maze->rowSize, 0)); + } + + void loadMazeToBuffer(const char *path) { + FILE *file; + if (!(file = fopen(path, "r"))) { + std::cout << "Couldn't find map file. Check file path.\n"; + exit(0); + } + + size_t y_cord = 0; + size_t x_cord = 0; + + char cur; + while (EOF != (cur = getc(file))) { + if ('\n' == cur) { + x_cord = 0; + ++y_cord; + continue; + } else if ('4' == cur) { + maze->player->xIdx = x_cord; + maze->player->yIdx = y_cord; + maze->start->xIdx = x_cord; + maze->start->yIdx = y_cord; + } else if ('3' == cur) { + maze->end->xIdx = x_cord; + maze->end->yIdx = y_cord; + } + maze->pntr[y_cord][x_cord] = (uint32_t)(cur - '0'); + ++x_cord; + } + } + + void print() { + const char sprites[] = " H*XO"; + saveCursor(); + for (size_t y_cord = 0; y_cord < maze->colSize; ++y_cord) { + for (size_t x_cord = 0; x_cord < maze->rowSize; ++x_cord) { + std::cout << sprites[maze->pntr[y_cord][x_cord]]; + if (x_cord < maze->rowSize - 1) { + std::cout << ' '; + } + } + if (y_cord < maze->colSize - 1) { + std::cout << '\n'; + } + } + restoreCursor(); + } + + void setTermDef() { tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_state); } + + void setTermRaw() { + struct termios raw; + tcgetattr(STDIN_FILENO, &raw); + tcgetattr(STDIN_FILENO, &term_state); + //// atexit(&Game::setTermDef); + + raw.c_lflag &= ~(ECHO | ICANON); + tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); + } + + void clear() { + zeroCursor(); + saveCursor(); + for (size_t y_cord = 0; y_cord < termRowSize; ++y_cord) { + for (size_t x_cord = 0; x_cord < termColSize; ++x_cord) { + std::cout << ' '; + } + if (y_cord < maze->colSize - 1) { + std::cout << '\n'; + } + } + restoreCursor(); + } + + void checkWin() { + if (maze->player->yIdx == maze->end->yIdx && maze->player->xIdx == maze->end->xIdx) { + winGame = true; + } + } + + void capture() { + setTermRaw(); + + char ch; + while ((ch = getchar()) != 'q') { + switch (ch) { + case 'w': + move(Entity::Move::Up); + break; + case 'a': + move(Entity::Move::Left); + break; + case 's': + move(Entity::Move::Down); + break; + case 'd': + move(Entity::Move::Right); + break; + } + + checkWin(); + if (winGame) { + break; + } + } + + quitGame = true; + } + + void update() { + zeroCursor(); + clear(); + while (!quitGame) { + print(); + std::this_thread::sleep_for(std::chrono::microseconds(41667)); + } + + if (winGame) { + clear(); + zeroCursor(); + std::cout << "Congratulations! You have won the game.\n"; + } else { + clear(); + zeroCursor(); + std::cout << "Keyboard interrupt! Quitting now...\n"; + } + } + + void startGame(int argc, char *argv[]) { + if (argc > 1 && argc < 3) { + getTermSize(); + allocMazeBuffer(argv[1]); + termTooSmallHandler(); + loadMazeToBuffer(argv[1]); + + std::thread updateThread(&Game::update, this); + std::thread captureThread(&Game::capture, this); + + updateThread.join(); + captureThread.join(); + } + } +}; + +int main(int argc, char *argv[]) { + Game game; + game.startGame(argc, argv); + return 0; +}