Multiple changes committed:
- Add .clang-format - Rename map.d/* to maps/* - Refactor maze.c
This commit is contained in:
225
.clang-format
Normal file
225
.clang-format
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
---
|
||||||
|
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
|
||||||
|
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: 80
|
||||||
|
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: 2
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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
|
||||||
|
SpaceInEmptyParentheses: false
|
||||||
|
SpacesBeforeTrailingComments: 1
|
||||||
|
SpacesInAngles: Never
|
||||||
|
SpacesInConditionalStatement: false
|
||||||
|
SpacesInContainerLiterals: true
|
||||||
|
SpacesInCStyleCastParentheses: false
|
||||||
|
SpacesInLineCommentPrefix:
|
||||||
|
Minimum: 1
|
||||||
|
Maximum: -1
|
||||||
|
SpacesInParentheses: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
Standard: Latest
|
||||||
|
StatementAttributeLikeMacros:
|
||||||
|
- Q_EMIT
|
||||||
|
StatementMacros:
|
||||||
|
- Q_UNUSED
|
||||||
|
- QT_REQUIRE_VERSION
|
||||||
|
TabWidth: 8
|
||||||
|
UseTab: Never
|
||||||
|
WhitespaceSensitiveMacros:
|
||||||
|
- BOOST_PP_STRINGIZE
|
||||||
|
- CF_SWIFT_NAME
|
||||||
|
- NS_SWIFT_NAME
|
||||||
|
- PP_STRINGIZE
|
||||||
|
- STRINGIZE
|
||||||
|
...
|
||||||
|
|
||||||
438
maze.c
438
maze.c
@@ -15,296 +15,314 @@
|
|||||||
#define UP 3
|
#define UP 3
|
||||||
|
|
||||||
typedef struct termios tstate_t;
|
typedef struct termios tstate_t;
|
||||||
|
|
||||||
typedef uint8_t dir_t;
|
typedef uint8_t dir_t;
|
||||||
|
|
||||||
|
typedef struct entity_t {
|
||||||
|
size_t X;
|
||||||
|
size_t Y;
|
||||||
|
} entity_t;
|
||||||
|
|
||||||
typedef struct maze_t {
|
typedef struct maze_t {
|
||||||
size_t X_SIZE;
|
size_t SIZE_X;
|
||||||
size_t Y_SIZE;
|
size_t SIZE_Y;
|
||||||
size_t P_X_CORD;
|
entity_t *PLAYER;
|
||||||
size_t P_Y_CORD;
|
entity_t *START;
|
||||||
size_t S_X_CORD;
|
entity_t *FINISH;
|
||||||
size_t S_Y_CORD;
|
uint8_t **PNTR;
|
||||||
size_t F_X_CORD;
|
|
||||||
size_t F_Y_CORD;
|
|
||||||
uint8_t **PNTR;
|
|
||||||
} maze_t;
|
} maze_t;
|
||||||
|
|
||||||
tstate_t TSTATE;
|
tstate_t TSTATE;
|
||||||
|
size_t TERM_COL;
|
||||||
|
size_t TERM_ROW;
|
||||||
maze_t *MAZE;
|
maze_t *MAZE;
|
||||||
|
|
||||||
bool QUIT = false;
|
bool QUIT = false;
|
||||||
bool WINSTATE = false;
|
bool WINSTATE = false;
|
||||||
|
|
||||||
size_t TERM_COL;
|
|
||||||
size_t TERM_ROW;
|
|
||||||
|
|
||||||
void get_term_size() {
|
void get_term_size() {
|
||||||
struct winsize w;
|
struct winsize w;
|
||||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||||
TERM_ROW = w.ws_row;
|
TERM_ROW = w.ws_row;
|
||||||
TERM_COL = w.ws_col;
|
TERM_COL = w.ws_col;
|
||||||
}
|
}
|
||||||
|
|
||||||
void exit_on_inadequate_space() {
|
void term_too_small_handler() {
|
||||||
if (TERM_COL < 2 * MAZE->X_SIZE || TERM_ROW < MAZE->Y_SIZE) {
|
if (TERM_COL < 2 * MAZE->SIZE_X || TERM_ROW < MAZE->SIZE_Y) {
|
||||||
printf("Terminal is too small to display the whole maze.\n\
|
printf("Terminal is too small to display the whole maze.\n\
|
||||||
Either make the terminal window bigger, or use a smaller maze map.\n");
|
Either make the terminal window bigger, or use a smaller maze map.\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_maze_size(const char *path) {
|
void get_maze_size(const char *path) {
|
||||||
FILE *file;
|
FILE *file;
|
||||||
if (!(file = fopen(path, "r"))) {
|
if (!(file = fopen(path, "r"))) {
|
||||||
printf("Couldn't find map file. Check file path.\n");
|
printf("Couldn't find map file. Check file path.\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
char ch;
|
char ch;
|
||||||
size_t x_cnt = 0;
|
size_t x_cnt = 0;
|
||||||
size_t y_cnt = 0;
|
size_t y_cnt = 0;
|
||||||
bool cnt_x = true;
|
bool cnt_x = true;
|
||||||
while ((ch = getc(file)) != EOF) {
|
while ((ch = getc(file)) != EOF) {
|
||||||
if (ch == '\n') {
|
if (ch == '\n') {
|
||||||
++y_cnt;
|
++y_cnt;
|
||||||
cnt_x = false;
|
cnt_x = false;
|
||||||
} else if (cnt_x) {
|
} else if (cnt_x) {
|
||||||
++x_cnt;
|
++x_cnt;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
MAZE->X_SIZE = x_cnt;
|
MAZE->SIZE_X = x_cnt;
|
||||||
MAZE->Y_SIZE = y_cnt;
|
MAZE->SIZE_Y = y_cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define restore_cursor() printf("\033[u")
|
#define restore_cursor() printf("\033[u")
|
||||||
#define save_cursor() printf("\033[s")
|
#define save_cursor() printf("\033[s")
|
||||||
#define zero_cursor() printf("\033[0;0H"); fflush(stdout)
|
#define zero_cursor() \
|
||||||
|
printf("\033[0;0H"); \
|
||||||
|
fflush(stdout)
|
||||||
|
|
||||||
void move(dir_t dir) {
|
void move(dir_t dir) {
|
||||||
switch (dir) {
|
switch (dir) {
|
||||||
case 1:
|
case 1:
|
||||||
if (MAZE->P_X_CORD != 0 && MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD - 1] != 1) {
|
if (MAZE->PLAYER->X != 0 &&
|
||||||
if (MAZE->P_X_CORD == MAZE->S_X_CORD && MAZE->P_Y_CORD == MAZE->S_Y_CORD) {
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X - 1] != 1) {
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 2;
|
if (MAZE->PLAYER->X == MAZE->START->X &&
|
||||||
} else {
|
MAZE->PLAYER->Y == MAZE->START->Y) {
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 0;
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 2;
|
||||||
}
|
} else {
|
||||||
--MAZE->P_X_CORD;
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 0;
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 4;
|
}
|
||||||
}
|
--MAZE->PLAYER->X;
|
||||||
break;
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 4;
|
||||||
case 2:
|
|
||||||
if (MAZE->P_Y_CORD != MAZE->Y_SIZE - 1 && MAZE->PNTR[MAZE->P_Y_CORD + 1][MAZE->P_X_CORD] != 1) {
|
|
||||||
if (MAZE->P_X_CORD == MAZE->S_X_CORD && MAZE->P_Y_CORD == MAZE->S_Y_CORD) {
|
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 2;
|
|
||||||
} else {
|
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 0;
|
|
||||||
}
|
|
||||||
++MAZE->P_Y_CORD;
|
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 4;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
if (MAZE->P_Y_CORD != 0 && MAZE->PNTR[MAZE->P_Y_CORD - 1][MAZE->P_X_CORD] != 1) {
|
|
||||||
if (MAZE->P_X_CORD == MAZE->S_X_CORD && MAZE->P_Y_CORD == MAZE->S_Y_CORD) {
|
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 2;
|
|
||||||
} else {
|
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 0;
|
|
||||||
}
|
|
||||||
--MAZE->P_Y_CORD;
|
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 4;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
if (MAZE->P_X_CORD != MAZE->X_SIZE - 1 && MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD + 1] != 1) {
|
|
||||||
if (MAZE->P_X_CORD == MAZE->S_X_CORD && MAZE->P_Y_CORD == MAZE->S_Y_CORD) {
|
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 2;
|
|
||||||
} else {
|
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 0;
|
|
||||||
}
|
|
||||||
++MAZE->P_X_CORD;
|
|
||||||
MAZE->PNTR[MAZE->P_Y_CORD][MAZE->P_X_CORD] = 4;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
if (MAZE->PLAYER->Y != MAZE->SIZE_Y - 1 &&
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y + 1][MAZE->PLAYER->X] != 1) {
|
||||||
|
if (MAZE->PLAYER->X == MAZE->START->X &&
|
||||||
|
MAZE->PLAYER->Y == MAZE->START->Y) {
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 2;
|
||||||
|
} else {
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 0;
|
||||||
|
}
|
||||||
|
++MAZE->PLAYER->Y;
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 4;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
if (MAZE->PLAYER->Y != 0 &&
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y - 1][MAZE->PLAYER->X] != 1) {
|
||||||
|
if (MAZE->PLAYER->X == MAZE->START->X &&
|
||||||
|
MAZE->PLAYER->Y == MAZE->START->Y) {
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 2;
|
||||||
|
} else {
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 0;
|
||||||
|
}
|
||||||
|
--MAZE->PLAYER->Y;
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 4;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
if (MAZE->PLAYER->X != MAZE->SIZE_X - 1 &&
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X + 1] != 1) {
|
||||||
|
if (MAZE->PLAYER->X == MAZE->START->X &&
|
||||||
|
MAZE->PLAYER->Y == MAZE->START->Y) {
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 2;
|
||||||
|
} else {
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 0;
|
||||||
|
}
|
||||||
|
++MAZE->PLAYER->X;
|
||||||
|
MAZE->PNTR[MAZE->PLAYER->Y][MAZE->PLAYER->X] = 4;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_map() {
|
void free_map() {
|
||||||
for (size_t y_cord = 0; y_cord < MAZE->Y_SIZE; ++y_cord) {
|
for (size_t y_cord = 0; y_cord < MAZE->SIZE_Y; ++y_cord) {
|
||||||
free(MAZE->PNTR[y_cord]);
|
free(MAZE->PNTR[y_cord]);
|
||||||
}
|
}
|
||||||
free(MAZE->PNTR);
|
free(MAZE->PNTR);
|
||||||
free(MAZE);
|
free(MAZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void alloc_maze_buffer(const char *path) {
|
void alloc_maze_buffer(const char *path) {
|
||||||
MAZE = (maze_t *)malloc(sizeof(maze_t));
|
MAZE = (maze_t *)malloc(sizeof(maze_t));
|
||||||
get_maze_size(path);
|
MAZE->PLAYER = (entity_t *)malloc(sizeof(entity_t));
|
||||||
MAZE->PNTR = (uint8_t **)malloc(MAZE->Y_SIZE * sizeof(uint8_t *));
|
MAZE->START = (entity_t *)malloc(sizeof(entity_t));
|
||||||
for (size_t y_cord = 0; y_cord < MAZE->Y_SIZE; ++y_cord) {
|
MAZE->FINISH = (entity_t *)malloc(sizeof(entity_t));
|
||||||
MAZE->PNTR[y_cord] = (uint8_t *)malloc(MAZE->X_SIZE * sizeof(uint8_t));
|
get_maze_size(path);
|
||||||
}
|
MAZE->PNTR = (uint8_t **)malloc(MAZE->SIZE_Y * sizeof(uint8_t *));
|
||||||
|
for (size_t y_cord = 0; y_cord < MAZE->SIZE_Y; ++y_cord) {
|
||||||
|
MAZE->PNTR[y_cord] = (uint8_t *)malloc(MAZE->SIZE_X * sizeof(uint8_t));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_maze_to_buffer(const char *path) {
|
void load_maze_to_buffer(const char *path) {
|
||||||
FILE *file;
|
FILE *file;
|
||||||
if (!(file = fopen(path, "r"))) {
|
if (!(file = fopen(path, "r"))) {
|
||||||
printf("Couldn't find map file. Check file path.\n");
|
printf("Couldn't find map file. Check file path.\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t y_cord = 0;
|
size_t y_cord = 0;
|
||||||
size_t x_cord = 0;
|
size_t x_cord = 0;
|
||||||
|
|
||||||
char cur;
|
char cur;
|
||||||
while (EOF != (cur = getc(file))) {
|
while (EOF != (cur = getc(file))) {
|
||||||
if ('\n' == cur) {
|
if ('\n' == cur) {
|
||||||
x_cord = 0;
|
x_cord = 0;
|
||||||
++y_cord;
|
++y_cord;
|
||||||
continue;
|
continue;
|
||||||
} else if ('4' == cur) {
|
} else if ('4' == cur) {
|
||||||
MAZE->P_X_CORD = x_cord;
|
MAZE->PLAYER->X = x_cord;
|
||||||
MAZE->P_Y_CORD = y_cord;
|
MAZE->PLAYER->Y = y_cord;
|
||||||
MAZE->S_X_CORD = x_cord;
|
MAZE->START->X = x_cord;
|
||||||
MAZE->S_Y_CORD = y_cord;
|
MAZE->START->Y = y_cord;
|
||||||
} else if ('3' == cur) {
|
} else if ('3' == cur) {
|
||||||
MAZE->F_X_CORD = x_cord;
|
MAZE->FINISH->X = x_cord;
|
||||||
MAZE->F_Y_CORD = y_cord;
|
MAZE->FINISH->Y = y_cord;
|
||||||
}
|
|
||||||
MAZE->PNTR[y_cord][x_cord] = (uint8_t)(cur - (char)'0');
|
|
||||||
++x_cord;
|
|
||||||
}
|
}
|
||||||
|
MAZE->PNTR[y_cord][x_cord] = (uint8_t)(cur - (char)'0');
|
||||||
|
++x_cord;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print() {
|
void print() {
|
||||||
const char sprites[] = " H*XO";
|
const char sprites[] = " H*XO";
|
||||||
save_cursor();
|
save_cursor();
|
||||||
for (size_t y_cord = 0; y_cord < MAZE->Y_SIZE; ++y_cord) {
|
for (size_t y_cord = 0; y_cord < MAZE->SIZE_Y; ++y_cord) {
|
||||||
for (size_t x_cord = 0; x_cord < MAZE->X_SIZE; ++x_cord) {
|
for (size_t x_cord = 0; x_cord < MAZE->SIZE_X; ++x_cord) {
|
||||||
putchar(sprites[MAZE->PNTR[y_cord][x_cord]]);
|
putchar(sprites[MAZE->PNTR[y_cord][x_cord]]);
|
||||||
if (x_cord < MAZE->X_SIZE - 1) {
|
if (x_cord < MAZE->SIZE_X - 1) {
|
||||||
putchar(' ');
|
putchar(' ');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (y_cord < MAZE->Y_SIZE - 1) {
|
|
||||||
putchar('\n');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
restore_cursor();
|
if (y_cord < MAZE->SIZE_Y - 1) {
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
restore_cursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_term_def();
|
void set_term_def();
|
||||||
void set_term_raw();
|
void set_term_raw();
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
zero_cursor();
|
zero_cursor();
|
||||||
save_cursor();
|
save_cursor();
|
||||||
for (size_t y_cord = 0; y_cord < TERM_ROW; ++y_cord) {
|
for (size_t y_cord = 0; y_cord < TERM_ROW; ++y_cord) {
|
||||||
for (size_t x_cord = 0; x_cord < TERM_COL; ++x_cord) {
|
for (size_t x_cord = 0; x_cord < TERM_COL; ++x_cord) {
|
||||||
putchar(' ');
|
putchar(' ');
|
||||||
}
|
|
||||||
if (y_cord < MAZE->Y_SIZE - 1) {
|
|
||||||
putchar('\n');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
restore_cursor();
|
if (y_cord < MAZE->SIZE_Y - 1) {
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
restore_cursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_win() {
|
void check_win() {
|
||||||
if (MAZE->P_Y_CORD == MAZE->F_Y_CORD && MAZE->P_X_CORD == MAZE->F_X_CORD) {
|
if (MAZE->PLAYER->Y == MAZE->FINISH->Y &&
|
||||||
WINSTATE = true;
|
MAZE->PLAYER->X == MAZE->FINISH->X) {
|
||||||
}
|
WINSTATE = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads keyboard input
|
// Reads keyboard input
|
||||||
void *capture() {
|
void *capture() {
|
||||||
set_term_raw(); // local function: Enable Raw Mode
|
set_term_raw(); // local function: Enable Raw Mode
|
||||||
|
|
||||||
char ch;
|
char ch;
|
||||||
while ((ch = getchar()) != 'q') {
|
while ((ch = getchar()) != 'q') {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'w':
|
case 'w':
|
||||||
move(UP);
|
move(UP);
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
move(LEFT);
|
move(LEFT);
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
move(DOWN);
|
move(DOWN);
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
move(RIGHT);
|
move(RIGHT);
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
check_win();
|
|
||||||
if (WINSTATE) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QUIT = true;
|
check_win();
|
||||||
|
if (WINSTATE) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
QUIT = true;
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *update() {
|
void *update() {
|
||||||
zero_cursor();
|
zero_cursor();
|
||||||
|
clear();
|
||||||
|
while (!QUIT) { // When ESC is not pressed
|
||||||
|
print();
|
||||||
|
usleep(41667);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (WINSTATE) {
|
||||||
clear();
|
clear();
|
||||||
while (!QUIT) { // When ESC is not pressed
|
printf("Congratulations! You have won the game.\n");
|
||||||
print();
|
} else {
|
||||||
usleep(41667);
|
clear();
|
||||||
}
|
printf("Keyboard interrupt! Quitting now...\n");
|
||||||
|
}
|
||||||
|
|
||||||
if (WINSTATE) {
|
return EXIT_SUCCESS;
|
||||||
clear();
|
|
||||||
printf("Congratulations! You have won the game.\n");
|
|
||||||
} else {
|
|
||||||
clear();
|
|
||||||
printf("Keyboard interrupt! Quitting now...\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_term_raw() {
|
void set_term_raw() {
|
||||||
struct termios raw;
|
struct termios raw;
|
||||||
tcgetattr(STDIN_FILENO, &raw); // Save the state of the terminal to struct raw
|
tcgetattr(STDIN_FILENO, &raw); // Save the state of the terminal to struct raw
|
||||||
// STDIN_FILENO is from <stdlib.h>
|
// STDIN_FILENO is from <stdlib.h>
|
||||||
// tcgetattr() from <termios.h>
|
// tcgetattr() from <termios.h>
|
||||||
|
|
||||||
tcgetattr(STDIN_FILENO, &TSTATE);
|
tcgetattr(STDIN_FILENO, &TSTATE);
|
||||||
|
|
||||||
atexit(&set_term_def); // Revert to canonical mode when exiting the program
|
atexit(&set_term_def); // Revert to canonical mode when exiting the program
|
||||||
// atext() from <stdlib.h>
|
// atext() from <stdlib.h>
|
||||||
raw.c_lflag &= ~(ECHO | ICANON); // Turn off canonical mode
|
raw.c_lflag &=
|
||||||
// Turn off ECHO mode so that keyboard is not
|
~(ECHO | ICANON); // Turn off canonical mode
|
||||||
// printing to terminal
|
// Turn off ECHO mode so that keyboard is not
|
||||||
// ICANON and ECHO is bitflag. ~ is binary NOT operator
|
// printing to terminal
|
||||||
|
// ICANON and ECHO is bitflag. ~ is binary NOT operator
|
||||||
|
|
||||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); // Set the terminal to be in raw mode
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); // Set the terminal to be in raw
|
||||||
// tcsetattr() from <termios.h>
|
// mode tcsetattr() from <termios.h>
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_term_def() {
|
void set_term_def() {
|
||||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &TSTATE); // Set terminal to TSTATE state
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &TSTATE); // Set terminal to TSTATE state
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc > 1 && argc < 3) {
|
if (argc > 1 && argc < 3) {
|
||||||
get_term_size();
|
get_term_size();
|
||||||
alloc_maze_buffer(argv[1]);
|
alloc_maze_buffer(argv[1]);
|
||||||
exit_on_inadequate_space();
|
term_too_small_handler();
|
||||||
load_maze_to_buffer(argv[1]);
|
load_maze_to_buffer(argv[1]);
|
||||||
|
|
||||||
pthread_t update_pid, capture_pid;
|
pthread_t update_pid, capture_pid;
|
||||||
|
|
||||||
pthread_create(&update_pid, NULL, update, NULL);
|
pthread_create(&update_pid, NULL, update, NULL);
|
||||||
pthread_create(&capture_pid, NULL, capture, NULL);
|
pthread_create(&capture_pid, NULL, capture, NULL);
|
||||||
|
|
||||||
pthread_join(update_pid, NULL);
|
pthread_join(update_pid, NULL);
|
||||||
pthread_join(capture_pid, NULL);
|
pthread_join(capture_pid, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user