diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..297ee4f --- /dev/null +++ b/.clang-format @@ -0,0 +1,246 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: 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 +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true +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 +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Leave +BreakAfterJavaFieldAnnotations: false +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Attach +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 88 +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: false +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 +PenaltyBreakScopeResolution: 500 +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 +SkipMacroDefinitionBody: false +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 + AfterPlacementOperator: true + 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/.clang-tidy b/.clang-tidy index 5c44609..7756ffa 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,7 +1,22 @@ # clang-tidy configuration for Go-Board-Game # Keep checks at tool defaults while treating all warnings as errors. +# clang-analyzer-* : enable static analysis passes for deeper bug finding +# clang-diagnostic-* : surface compiler-style warnings (unused variables, conversions, etc.) +# clang-diagnostic-unused-const-variable : warn on unused 'const' variables +# google-build-using-namespace : reject global using-namespace directives +# modernize-deprecated-headers : prefer C++ standard headers over legacy C ones +# modernize-loop-convert : prefer range-based for loops over traditional loops +# modernize-use-stdarray : prefer 'std::array' over C-style arrays +# modernize-use-using : prefer 'using' type alias declarations over 'typedef's +# readability-magic-numbers : discourage use of magic numbers in code Checks: > - clang-diagnostic-*, clang-analyzer-*, - google-build-using-namespace + clang-diagnostic-*, + clang-diagnostic-unused-const-variable, + google-build-using-namespace, + modernize-deprecated-headers, + modernize-loop-convert, + modernize-use-stdarray, + modernize-use-using + WarningsAsErrors: '*' diff --git a/CMakeLists.txt b/CMakeLists.txt index 51ca1b6..3f83888 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,9 @@ -cmake_minimum_required(VERSION 3.10) # Set minimum cmake version (what's set in this version of image version) -project(main) # set project name -set(CMAKE_CXX_STANDARD 14) # Enable c++14 standard +cmake_minimum_required(VERSION 3.20) # C++20 features require newer CMake tooling +project(main LANGUAGES CXX) # set project name and language + +set(CMAKE_CXX_STANDARD 20) # Enable C++20 standard +set(CMAKE_CXX_STANDARD_REQUIRED ON) # Fail configuration if the compiler can't do C++20 +set(CMAKE_CXX_EXTENSIONS OFF) # Use -std=c++20, not compiler-specific gnu++20 set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Generate compile_commands.json for tooling set(SOURCE_FILES main.cpp) # Add main.cpp file of project root directory as source file diff --git a/README.md b/README.md index 7eb76e4..ebb840d 100644 --- a/README.md +++ b/README.md @@ -80,8 +80,8 @@ Below are the keyboard explanations: 7 : Toggle between smooth and flat shading 8 : Toggle texture -o,O : Toggle teapot rotation along the y-axis -p,P : Toggle teapot rotation along the x-axis +o,O : Toggle board rotation along the y-axis +p,P : Toggle board rotation along the x-axis r,R : Activate "reset board option" y,Y : Confirm "reset board option" n,N : Cancel "reset board option" diff --git a/main.cpp b/main.cpp index 883e56a..d3886d9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,22 +1,24 @@ // OLD INCLUDES/USINGS -//#include -//#include -//#include -//#include - -//using namespace std; - -// NEW INCLUDES/USINGS +// #include +// #include +// #include +// Standard library +#include +#include +#include +#include -//#pragma comment(lib, "DevIL.lib") -//#pragma comment(lib, "ILU.lib") -//#pragma comment(lib, "ILUT.lib") +// For Sleep in Windows +// #include +// For Sleep in Linux +#include +// #pragma comment(lib, "ILUT.lib") #include -//For Sleep in Windows -//#include -//For Sleep in Linux +// For Sleep in Windows +// #include +// For Sleep in Linux #include #include @@ -30,75 +32,81 @@ #include #include -ILuint il[3] = { 0, 1, 2 }; -GLuint texture[3] = { 0, 1, 2 }; -#include -#include -#include -#include - +ILuint il[3] = {0, 1, 2}; +GLuint texture[3] = {0, 1, 2}; // For the queue -#include #include +#include #include #define GL_CLAMP_TO_EDGE 0x812F -typedef std::queue INTQUEUE; // Window settings -int windowID; // Glut window ID (for display) +int windowID; // Glut window ID (for display) - // Camera methods +// Camera methods void mouse(int button, int state, int x, int y); void motion(int x, int y); // Camera settings bool updateCamZPos = false; -int lastX = 0; -int lastY = 0; +int lastX = 0; +int lastY = 0; const float ZOOM_SCALE = 0.01; -GLdouble camXPos = 0.0; -GLdouble camYPos = 0.0; -GLdouble camZPos = -1.5; +constexpr int BOARD_SIZE = 19; +constexpr int BOARD_CENTER = BOARD_SIZE / 2; -const GLdouble CAMERA_FOVY = 60.0; -const GLdouble NEAR_CLIP = 0.1; -const GLdouble FAR_CLIP = 1000.0; +// Global Constants +// Camera initial position +constexpr GLdouble INITIAL_CAM_X = 0.0; +constexpr GLdouble INITIAL_CAM_Y = 0.0; +constexpr GLdouble INITIAL_CAM_Z = -1.5; +constexpr float PI = 3.14159265358979323846F; // Pi constant for trigonometric helpers + +struct Camera { + GLdouble x; + GLdouble y; + GLdouble z; +}; -// global variables int wireframe = 0; int lighting = 1; int material = 1; -int pauseTeapot_y = 1; -int pauseTeapot_x = 1; +int pauseBoardRotation_y = 1; +int pauseBoardRotation_x = 1; int pauseLighting = 0; int depthTest = 1; int cullFace = 1; int smooth = 1; int enabletexture = 0; -float angle_y = 0; -float angle_x = 45; -float angle2 = 0; // Useless + +constexpr int INITIAL_BOARD_ANGLE_X = 45; +constexpr int INITIAL_BOARD_ANGLE_Y = 0; +float angle_x = INITIAL_BOARD_ANGLE_X; +float angle_y = INITIAL_BOARD_ANGLE_Y; float translatelight = 0; +Camera camera{INITIAL_CAM_X, INITIAL_CAM_Y, INITIAL_CAM_Z}; + // Behind the Scenes variables int placex = 0; int placey = 0; int col = 1; float t = 0; -float inc = 0.002; // Depicts how fast time increments. +constexpr float TIME_INCREMENT = 0.002; // Depicts how fast time increments. +constexpr int DEFAULT_SLEEP_TIME = 3000; // Default sleep time in microseconds. - // Behind the Scenes -int board_status[19][19]; -int liberties_status[19][19]; +// Behind the Scenes +std::array, BOARD_SIZE> board_status{}; +std::array, BOARD_SIZE> liberties_status{}; int restart_option = 0; -INTQUEUE rm_queue; +std::queue rm_queue; std::list> L; -//float rm_array[1083]; // Holds the objects that will be removed. -//int p = 0; // Always points to the last index of rm_array. +// float rm_array[1083]; // Holds the objects that will be removed. +// int p = 0; // Always points to the last index of rm_array. // declaration, forward void display(); @@ -106,8 +114,8 @@ void keyboard(unsigned char, int, int); void init(); void idle(); void SetImage(); -void DrawSphere(int color); -void DrawUnitCube(int color); +void DrawSphere(int); +void DrawUnitCube(int); void ApplyTransformations(float indx, float indy, float z); void ApplyTransformationGeneral(float indx, float indy, float z); @@ -120,618 +128,583 @@ void remove_block(int x, int y, int piece); void jump_off(int x0, int y0, int color); //! Program entry point -int main(int argc, char** argv) { - - // initialize GLUT - glutInit(&argc, argv); - // set visual - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); - // set physical window properties - glutInitWindowSize(512, 512); - // position window on the screen - glutInitWindowPosition(100, 100); - // create window - windowID = glutCreateWindow("hello"); - - ilInit(); - iluInit(); - ilutInit(); - ilutRenderer(ILUT_OPENGL); - - ilGenImages(2, il); - glGenTextures(2, texture); - - // initialize OpenGL - init(); - - // register rendering mainloop - glutKeyboardFunc(keyboard); - glutDisplayFunc(display); - glutIdleFunc(idle); - - glutMouseFunc(mouse); // Call mouse whenever mouse button pressed - glutMotionFunc(motion); // Call motion whenever mouse moves while button pressed - // let's rock ... - glutMainLoop(); - - return 0; +int main(int argc, char **argv) { + const int DEFAULT_WINDOW_WIDTH = 512; + const int DEFAULT_WINDOW_HEIGHT = 512; + const int DEFAULT_WINDOW_X = 100; + const int DEFAULT_WINDOW_Y = 100; + const char *WINDOW_TITLE = "Go Board Game"; + // initialize GLUT + glutInit(&argc, argv); + // set visual + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); + // set physical window properties + glutInitWindowSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT); + // position window on the screen + glutInitWindowPosition(DEFAULT_WINDOW_X, DEFAULT_WINDOW_Y); + // create window + windowID = glutCreateWindow(WINDOW_TITLE); + + ilInit(); + iluInit(); + ilutInit(); + ilutRenderer(ILUT_OPENGL); + + ilGenImages(2, il); + glGenTextures(2, texture); + + // initialize OpenGL + init(); + + // register rendering mainloop + glutKeyboardFunc(keyboard); + glutDisplayFunc(display); + glutIdleFunc(idle); + + glutMouseFunc(mouse); // Call mouse whenever mouse button pressed + glutMotionFunc(motion); // Call motion whenever mouse moves while button + // pressed let's rock ... + glutMainLoop(); + + return 0; } -void keyboard(unsigned char key, int x, int y) -{ - /* - - 1 : Toggle specularity - 2 : Toggle lighting - 3 : Toggle wireframe - 4 : Toggle light animation - 5 : Toggle depth testing - 6 : Toggle culling (try disabling both depth testing and culling) - 7 : Toggle between smooth and flat shading - 8 : Toggle texture - - o,O : Toggle teapot rotation along the y-axis - p,P : Toggle teapot rotation along the x-axis - r,R : Activate "reset board option" - y,Y : Confirm "reset board option" - n,N : Cancel "reset board option" - v,V : Set color to white (This makes testing a bit faster) - b,B : Set color to black (This makes testing a bit faster) - - wasd : Move a piece around the board - enter : Place a piece on the board - - ON RIGHT CLICK AND HOLD MOUSE: - slide left : move camera left - slide right : move camera right - slide up : zoom camera in - slide down : zoom camera out - */ - - switch (key) - { - case '1': - // Enable/disable wireframe mode - wireframe = !wireframe; - - if (wireframe) { - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - } - else { - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - } - - break; - - case '3': - material = !material; - - break; - - case '5': - depthTest = !depthTest; - - if (depthTest) { - glEnable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - } - else { - glDisable(GL_DEPTH_TEST); - } - - break; - - case '6': - cullFace = !cullFace; - - if (cullFace) { - glEnable(GL_CULL_FACE); - } - else { - glDisable(GL_CULL_FACE); - } - - break; - - case '7': - smooth = !smooth; - - if (smooth) { - glShadeModel(GL_SMOOTH); - } - else { - glShadeModel(GL_FLAT); - } - - break; - - case '8': - enabletexture = !enabletexture; - - if (enabletexture) { - glEnable(GL_TEXTURE_2D); - } - else { - glDisable(GL_TEXTURE_2D); - } - break; - - case 'o': - case 'O': - std::cout << "PRESSED O\n"; - pauseTeapot_y = !pauseTeapot_y; - - break; - - case 'p': - case 'P': - std::cout << "PRESSED P\n"; - pauseTeapot_x = !pauseTeapot_x; - - break; - - case 'a': - case 'A': - if (placex > -9) { placex -= 1; } - - break; - - case 'w': - case 'W': - if (placey < 9) { placey += 1; } - - break; - case 's': - case 'S': - if (placey > -9) { placey -= 1; } - - break; - case 'd': - case 'D': - if (placex < 9) { placex += 1; } - - break; - - case 13: - if (board_status[placex + 9][placey + 9] != 0) - { - std::cout << "YOU CAN'T PLACE A PIECE HERE BECAUSE THERE ALREADY IS A PIECE HERE!!!\n"; - } - else - { - std::cout << "ENTER KEY PRESSED!!!\n"; - make_move(placex + 9, placey + 9, col); - if (col == 1) { col = 2; } - else { col = 1; } - } - break; - case 'r': - case 'R': - std::cout << "You pressed 'r', the restart button. Press 'y' to confirm restart. Press 'n' to cancel.\n"; - restart_option = 1; - break; - case 'n': - case 'N': - if (restart_option) { - std::cout << "You pressed 'n'. Restart option cancelled.\n"; - restart_option = 0; - } - break; - case 'y': - case 'Y': - if (restart_option) { - std::cout << "You pressed 'y'. The game has been restarted.\n"; - init_board(); - col = 1; - } - break; - - case 'b': - case 'B': - col = 2; break; - case 'v': - case 'V': - col = 1; break; - - } +void keyboard(unsigned char key, [[maybe_unused]] int x, [[maybe_unused]] int y) { + /* + + 1 : Toggle specularity + 2 : Toggle lighting + 3 : Toggle wireframe + 4 : Toggle light animation + 5 : Toggle depth testing + 6 : Toggle culling (try disabling both depth testing and culling) + 7 : Toggle between smooth and flat shading + 8 : Toggle texture + + o,O : Toggle board rotation along the y-axis + p,P : Toggle board rotation along the x-axis + r,R : Activate "reset board option" + y,Y : Confirm "reset board option" + n,N : Cancel "reset board option" + v,V : Set color to white (This makes testing a bit faster) + b,B : Set color to black (This makes testing a bit faster) + + wasd : Move a piece around the board + enter : Place a piece on the board + + ON RIGHT CLICK AND HOLD MOUSE: + slide left : move camera left + slide right : move camera right + slide up : zoom camera in + slide down : zoom camera out + */ + + switch (key) { + case '1': + // Enable/disable wireframe mode + wireframe = !wireframe; + + if (wireframe) { + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + } else { + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + } + + break; + + case '3': + material = !material; + + break; + + case '5': + depthTest = !depthTest; + + if (depthTest) { + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + } else { + glDisable(GL_DEPTH_TEST); + } + + break; + + case '6': + cullFace = !cullFace; + + if (cullFace) { + glEnable(GL_CULL_FACE); + } else { + glDisable(GL_CULL_FACE); + } + + break; + + case '7': + smooth = !smooth; + + if (smooth) { + glShadeModel(GL_SMOOTH); + } else { + glShadeModel(GL_FLAT); + } + + break; + + case '8': + enabletexture = !enabletexture; + + if (enabletexture) { + glEnable(GL_TEXTURE_2D); + } else { + glDisable(GL_TEXTURE_2D); + } + break; + + case 'o': + case 'O': + std::cout << "PRESSED O\n"; + pauseBoardRotation_y = !pauseBoardRotation_y; + + break; + + case 'p': + case 'P': + std::cout << "PRESSED P\n"; + pauseBoardRotation_x = !pauseBoardRotation_x; + + break; + + case 'a': + case 'A': + if (placex > -BOARD_CENTER) { + placex -= 1; + } + + break; + + case 'w': + case 'W': + if (placey < BOARD_CENTER) { + placey += 1; + } + + break; + case 's': + case 'S': + if (placey > -BOARD_CENTER) { + placey -= 1; + } + + break; + case 'd': + case 'D': + if (placex < BOARD_CENTER) { + placex += 1; + } + + break; + + case '\r': + if (board_status[placex + BOARD_CENTER][placey + BOARD_CENTER] != 0) { + std::cout << "YOU CAN'T PLACE A PIECE HERE BECAUSE THERE ALREADY IS A " + "PIECE HERE!!!\n"; + } else { + std::cout << "ENTER KEY PRESSED!!!\n"; + make_move(placex + BOARD_CENTER, placey + BOARD_CENTER, col); + if (col == 1) { + col = 2; + } else { + col = 1; + } + } + break; + case 'r': + case 'R': + std::cout << "You pressed 'r', the restart button. Press 'y' to confirm " + "restart. Press 'n' to cancel.\n"; + restart_option = 1; + break; + case 'n': + case 'N': + if (restart_option) { + std::cout << "You pressed 'n'. Restart option cancelled.\n"; + restart_option = 0; + } + break; + case 'y': + case 'Y': + if (restart_option) { + std::cout << "You pressed 'y'. The game has been restarted.\n"; + init_board(); + col = 1; + } + break; + + case 'b': + case 'B': + col = 2; + break; + case 'v': + case 'V': + col = 1; + break; + } } // Handles mouse motion events while a button is pressed -void motion(int x, int y) -{ - // If the RMB is pressed and dragged then zoom in / out - if (updateCamZPos) - { - // Update camera z position - camZPos += (y - lastY) * ZOOM_SCALE; - camXPos += (x - lastX) * ZOOM_SCALE; - lastX = x; - lastY = y; - - // Redraw the scene from updated camera position - glutSetWindow(windowID); - glutPostRedisplay(); - } +void motion(int x, int y) { + // If the RMB is pressed and dragged then zoom in / out + if (updateCamZPos) { + // Update camera position while the mouse is dragged + camera.z += (y - lastY) * ZOOM_SCALE; + camera.x += (x - lastX) * ZOOM_SCALE; + lastX = x; + lastY = y; + + // Redraw the scene from updated camera position + glutSetWindow(windowID); + glutPostRedisplay(); + } } // Handles mouse button pressed / released events -void mouse(int button, int state, int x, int y) -{ - // If the RMB is pressed and dragged then zoom in / out - if (button == GLUT_RIGHT_BUTTON) - { - if (state == GLUT_DOWN) - { - lastX = x; - lastY = y; - updateCamZPos = true; - } - else - { - updateCamZPos = false; - } - } +void mouse(int button, int state, int x, int y) { + // If the RMB is pressed and dragged then zoom in / out + if (button == GLUT_RIGHT_BUTTON) { + if (state == GLUT_DOWN) { + lastX = x; + lastY = y; + updateCamZPos = true; + } else { + updateCamZPos = false; + } + } } -void printMatrix() -{ - float matrixData[16]; - - glGetFloatv(GL_MODELVIEW_MATRIX, matrixData); - - std::cout << std::endl; - - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - std::cout << matrixData[i + 4 * j] << ", "; - } - - std::cout << std::endl; - } +void idle() { + const float BOARD_ROTATION_STEP = 0.5f; + const float ANGLE_LIMIT = 360.0f; + if (!pauseBoardRotation_y) { + angle_y += BOARD_ROTATION_STEP; + if (angle_y > ANGLE_LIMIT) { + angle_y -= ANGLE_LIMIT; + } + } + + if (!pauseBoardRotation_x) { + angle_x += BOARD_ROTATION_STEP; + if (angle_x > ANGLE_LIMIT) { + angle_x -= ANGLE_LIMIT; + } + } + + /* + if (!pauseLighting) { + translatelight += BOARD_ROTATION_STEP; + + if (translatelight > 360) { + translatelight -= 360; + } + } + */ + + glutPostRedisplay(); } -void idle() -{ - - if (!pauseTeapot_y) { - angle_y = angle_y + 0.5; - - if (angle_y > 360) { - angle_y = angle_y - 360; - } - } - - if (!pauseTeapot_x) { - angle_x = angle_x + 0.5; +void lightingFunc() { + // Specify light position + GLfloat lightposition[4] = {0.0, 0.0, -0.5, 1.0}; + glLightfv(GL_LIGHT0, GL_POSITION, lightposition); - if (angle_x > 360) { - angle_x = angle_x - 360; - } - } + // Specify diffuse component. Diffuse component is white. + // GLfloat lightdiffuse[4] = {0.0, 0.0, 0.0, 1.0}; + GLfloat lightstrength[4] = {2.0, 2.0, 2.0, 2.0}; + glLightfv(GL_LIGHT0, GL_DIFFUSE, lightstrength); - angle2 = angle2 + 0.5; + // Specify specular component. Specular component is green. + // TODO: Decide on desired specular component -- IF NEEDED + // GLfloat lightspecular[4] = {0.0, 0.0, 0.0, 1.0}; + // GLfloat lightspecular[4] = { 0.0, 2.0, 0.0, 1.0 }; + glLightfv(GL_LIGHT0, GL_SPECULAR, lightstrength); - if (angle2 > 360) { - angle2 = angle2 - 360; - } + GLfloat spread = 90 - 90 * t; + glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, &spread); - /* - if (!pauseLighting) { - translatelight = translatelight + 0.5; - - if (translatelight > 360) { - translatelight = translatelight - 360; - } - } - */ - - glutPostRedisplay(); + glEnable(GL_LIGHT0); } +void materialFunc() { + // Add a diffuse component to our board. The diffuse reflection constant + // is white (white light source produces white reflection). + GLfloat materialdiffuse[4] = {0.5, 0.5, 1.0, 1.0}; + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materialdiffuse); -void lightingFunc() -{ - - // Specify light position - GLfloat lightposition[4] = { 0.0, 0.0, -0.5, 1.0 }; - glLightfv(GL_LIGHT0, GL_POSITION, lightposition); + // Add a specular component to our board. The specular reflection constant + // is white (white light source produces white reflection). + GLfloat materialspecular[4] = {1.0, 1.0, 1.0, 1.0}; + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialspecular); - // Specify diffuse component. Diffuse component is white. - //GLfloat lightdiffuse[4] = {0.0, 0.0, 0.0, 1.0}; - GLfloat lightstrength[4] = { 2.0, 2.0, 2.0, 2.0 }; - glLightfv(GL_LIGHT0, GL_DIFFUSE, lightstrength); - - // Specify specular component. Specular component is green. - //GLfloat lightspecular[4] = {0.0, 0.0, 0.0, 1.0}; - GLfloat lightspecular[4] = { 0.0, 2.0, 0.0, 1.0 }; - glLightfv(GL_LIGHT0, GL_SPECULAR, lightstrength); - - GLfloat spread = 90 - 90 * t; - glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, &spread); - - glEnable(GL_LIGHT0); + // Defines the shinyness (the exponent to the phong model). + GLfloat materialshininess[1] = {100.0}; + glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, materialshininess); } -void materialFunc() -{ - // Add a diffuse component to our teapot. The diffuse reflection constant - // is white (white light source produces white reflection). - GLfloat materialdiffuse[4] = { 0.5, 0.5, 1.0, 1.0 }; - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, materialdiffuse); - - // Add a specular component to our teapot. The specular reflection constant - // is white (white light source produces white reflection). - GLfloat materialspecular[4] = { 1.0, 1.0, 1.0, 1.0 }; - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialspecular); - - // Defines the shinyness (the exponent to the phong model). - GLfloat materialshininess[1] = { 100.0 }; - glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, materialshininess); +void DrawSphere(int color) { + float colorNone[4] = {0.0, 0.0, 0.0, 0.0}; + + if (color == 0) { + float colorRed[4] = {1.0, 0.0, 0.0, 0.0}; + glColor4fv(colorRed); + glMaterialfv(GL_FRONT, GL_DIFFUSE, colorRed); + glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + } + + if (color == 1) { + float colorWhite[4] = {1.0, 1.0, 1.0, 1.0}; + glColor4fv(colorWhite); + glMaterialfv(GL_FRONT, GL_DIFFUSE, colorWhite); + glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + } + + if (color == 2) { + float colorBlack[4] = {0.0, 0.0, 0.0, 0.0}; + glColor4fv(colorBlack); + glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlack); + glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + } + + if (color == 3) { + float colorGreen[4] = {0.0, 0.0, 1.0, 0.0}; + glColor4fv(colorGreen); + glMaterialfv(GL_FRONT, GL_DIFFUSE, colorGreen); + glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + } + + if (color == 4) { + float colorBlue[4] = {0.0, 1.0, 0.0, 0.0}; + glColor4fv(colorBlue); + glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlue); + glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + } + + glBegin(GL_TRIANGLES); + GLUquadricObj *quadratic; + quadratic = gluNewQuadric(); // Create A Pointer To The Quadric Object + gluQuadricNormals(quadratic, GLU_SMOOTH); // Create Smooth Normals + gluQuadricTexture(quadratic, GL_TRUE); // Create Texture Coords + + gluSphere(quadratic, 1.0f, 32, 32); + glEnd(); } - -void DrawSphere(int color) -{ - float colorNone[4] = { 0.0, 0.0, 0.0, 0.0 }; - - if (color == 0) - { - float colorRed[4] = { 1.0, 0.0, 0.0, 0.0 }; - glColor4fv(colorRed); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorRed); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); - } - - if (color == 1) - { - float colorWhite[4] = { 1.0, 1.0, 1.0, 1.0 }; - glColor4fv(colorWhite); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorWhite); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); - } - - if (color == 2) - { - float colorBlack[4] = { 0.0, 0.0, 0.0, 0.0 }; - glColor4fv(colorBlack); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlack); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); - } - - if (color == 3) - { - float colorGreen[4] = { 0.0, 0.0, 1.0, 0.0 }; - glColor4fv(colorGreen); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorGreen); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); - } - - if (color == 4) - { - float colorBlue[4] = { 0.0, 1.0, 0.0, 0.0 }; - glColor4fv(colorBlue); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlue); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); - } - - - - glBegin(GL_TRIANGLES); - GLUquadricObj *quadratic; - quadratic = gluNewQuadric(); // Create A Pointer To The Quadric Object - gluQuadricNormals(quadratic, GLU_SMOOTH); // Create Smooth Normals - gluQuadricTexture(quadratic, GL_TRUE); // Create Texture Coords - - gluSphere(quadratic, 1.0f, 32, 32); - glEnd(); +void DrawUnitCube(int color) { + if (color == 0) { + glBindTexture(GL_TEXTURE_2D, texture[0]); + } else if (color == 3) { + float colorNone[4] = {0.0, 0.0, 0.0, 0.0}; + float colorGreen[4] = {0.0, 0.0, 1.0, 0.0}; + glColor4fv(colorGreen); + glMaterialfv(GL_FRONT, GL_DIFFUSE, colorGreen); + glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + } else if (color == 4) { + float colorNone[4] = {0.0, 0.0, 0.0, 0.0}; + float colorBlue[4] = {0.0, 1.0, 0.0, 0.0}; + glColor4fv(colorBlue); + glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlue); + glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); + } + + glBegin(GL_QUADS); + + // Front Face + glNormal3f(0.0f, 0.0f, 2.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + // Back Face + glNormal3f(0.0f, 0.0f, -2.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(1.0f, -1.0f, -1.0f); + // Bottom Face + glNormal3f(0.0f, -2.0f, 0.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + // Right face + glNormal3f(2.0f, 0.0f, 0.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + // Left Face + glNormal3f(-2.0f, 0.0f, 0.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, -1.0f); + + if (color == 0) { + glEnd(); + + glBindTexture(GL_TEXTURE_2D, texture[1]); + glBegin(GL_QUADS); + } + + // Top Face + glNormal3f(0.0f, 2.0f, 0.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, -1.0f); + + glEnd(); + + usleep(DEFAULT_SLEEP_TIME); } -void DrawUnitCube(int color) -{ - if (color == 0) { glBindTexture(GL_TEXTURE_2D, texture[0]); } - else if (color == 3) - { - float colorNone[4] = { 0.0, 0.0, 0.0, 0.0 }; - float colorGreen[4] = { 0.0, 0.0, 1.0, 0.0 }; - glColor4fv(colorGreen); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorGreen); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); - } - else if (color == 4) - { - float colorNone[4] = { 0.0, 0.0, 0.0, 0.0 }; - float colorBlue[4] = { 0.0, 1.0, 0.0, 0.0 }; - glColor4fv(colorBlue); - glMaterialfv(GL_FRONT, GL_DIFFUSE, colorBlue); - glMaterialfv(GL_FRONT, GL_SPECULAR, colorNone); - } - - glBegin(GL_QUADS); - - // Front Face - glNormal3f(0.0f, 0.0f, 2.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); - // Back Face - glNormal3f(0.0f, 0.0f, -2.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); - // Bottom Face - glNormal3f(0.0f, -2.0f, 0.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, -1.0f, -1.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); - // Right face - glNormal3f(2.0f, 0.0f, 0.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, -1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); - // Left Face - glNormal3f(-2.0f, 0.0f, 0.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); - - if (color == 0) - { - glEnd(); - - glBindTexture(GL_TEXTURE_2D, texture[1]); - glBegin(GL_QUADS); - } - - // Top Face - glNormal3f(0.0f, 2.0f, 0.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, -1.0f); - - glEnd(); - - // Windows - //Sleep(3000); - // Linux - usleep(3000); - +void display() { + // clear screen to background color + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // draw a white quad + glColor4f(1.0, 1.0, 1.0, 1.0); + + // Push lighting bit; we'll pop it later so that the lighting state + // isn't saved onto the next frame. We do this so that we can disable + // lighting more easily. + glPushAttrib(GL_LIGHTING_BIT); + + glLoadIdentity(); + + // Specify camera transformation + glTranslatef(static_cast(camera.x), static_cast(camera.y), + static_cast(camera.z)); + + // Specify the lighting we'll be using for this app. Note that + // the lights can be transformed using the usual translate, rotate, + // and scale commands. + glPushMatrix(); + + //////////////////// MOVING LIGHT!!!!! //////////////////// + glTranslatef(2.0F * std::sin(translatelight * 2.0F * PI / 360.0F), 0.0F, 0.0F); + + if (lighting) { + lightingFunc(); + } + + glPopMatrix(); + + glTranslatef(0.0, 0.0, -2.0); // Move objects to viewing area. + glRotatef(angle_x, 1.0, 0.0, 0.0); // To allow rotation on the x-axis + glRotatef(angle_y, 0.0, 1.0, 0.0); // To allow rotation on the y-axis + glPushMatrix(); + + glScalef(1.0, 0.05, 1.0); // Flatten the Cube. + // It should look like a board now. + + // Specify material for the board we'll be drawing. + if (material) { + materialFunc(); + } + + DrawUnitCube(0); + + glPopMatrix(); + glPushMatrix(); + // Draw Beads + + glBindTexture(GL_TEXTURE_2D, 0); + + glScalef(0.0525, 0.0525, 0.0525); // Just the right size :) + glTranslatef(0.0, 1.0, 0.0); // The centre + + glPushMatrix(); + ApplyTransformations(placex, placey, 0); + DrawSphere(0); + glPopMatrix(); + + for (int i = 0; i < 19; i++) { + for (int j = 0; j < 19; j++) { + if (board_status[i][j] != 0) { + glPushMatrix(); + ApplyTransformations(i - 9, j - 9, 0); + DrawSphere(board_status[i][j]); + glPopMatrix(); + } + } + } + + // THE REMOVAL OF PIECE PORTION + if (!L.empty()) { + for (const auto &piece : L) { + jump_off(piece[0], piece[1], piece[2]); + } + t += (TIME_INCREMENT * L.size()); + } + if (t > 1) { + t = 0; + L.clear(); + } + + glPopMatrix(); + glPopAttrib(); + + // immediately process commands + glFlush(); + + // immediately call display loop again + glutSwapBuffers(); } -void display() -{ - // clear screen to background color - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - // draw a white quad - glColor4f(1.0, 1.0, 1.0, 1.0); - - // Push lighting bit; we'll pop it later so that the lighting state - // isn't saved onto the next frame. We do this so that we can disable - // lighting more easily. - glPushAttrib(GL_LIGHTING_BIT); - - glLoadIdentity(); - - // Specify camera transformation - glTranslatef(camXPos, camYPos, camZPos); - - // Specify the lighting we'll be using for this app. Note that - // the lights can be transformed using the usual translate, rotate, - // and scale commands. - glPushMatrix(); - - //////////////////// MOVING LIGHT!!!!! //////////////////// - glTranslatef(2 * sin(translatelight * 2 * 3.14 / 360), 0.0, 0.0); - - if (lighting) { - lightingFunc(); - } - - glPopMatrix(); - - - glTranslatef(0.0, 0.0, -2.0); // Move objects to viewing area. - glRotatef(angle_x, 1.0, 0.0, 0.0); // To allow rotation on the x-axis - glRotatef(angle_y, 0.0, 1.0, 0.0); // To allow rotation on the y-axis - glPushMatrix(); - - glScalef(1.0, 0.05, 1.0); // Flatten the Cube. - // It should look like a board now. - - // Specify material for the teapot we'll be drawing. - if (material) { - materialFunc(); - } - - DrawUnitCube(0); - - - glPopMatrix(); - glPushMatrix(); - // Draw Beads - - glBindTexture(GL_TEXTURE_2D, 0); - - glScalef(0.0525, 0.0525, 0.0525); // Just the right size :) - glTranslatef(0.0, 1.0, 0.0); // The centre - - - glPushMatrix(); - ApplyTransformations(placex, placey, 0); - DrawSphere(0); - glPopMatrix(); - - for (int i = 0; i < 19; i++) { - for (int j = 0; j < 19; j++) { - if (board_status[i][j] != 0) { - glPushMatrix(); - ApplyTransformations(i - 9, j - 9, 0); - DrawSphere(board_status[i][j]); - glPopMatrix(); - } - } - } - - // THE REMOVAL OF PIECE PORTION - if (!L.empty()) { - - for (std::list>::iterator i = L.begin(); i != L.end(); ++i) { - jump_off((*i)[0], (*i)[1], (*i)[2]); - } - t += (inc*L.size()); - } - if (t > 1) { - t = 0; - L.clear(); - } - - glPopMatrix(); - glPopAttrib(); - - // immediately process commands - glFlush(); - - // immediately call display loop again - glutSwapBuffers(); -} - -void ApplyTransformations(float indx, float indy, float z) -{ - int i; - if (indx < 0) - { - for (i = 0; i 18 || y < 0 || y > 18) { - return 0; - } - if (board_status[x][y] == 0) { - return 1; - } - if (board_status[x][y] != piece) { - return 0; - } - if (liberties_status[x][y] != -1) { - return liberties_status[x][y]; - } - int direcx = x - originx; - int direcy = y - originy; - if ((direcx >= 0 && check_liberties(x + 1, y, originx, originy, piece)) || - (direcx <= 0 && check_liberties(x - 1, y, originx, originy, piece)) || - (direcy >= 0 && check_liberties(x, y + 1, originx, originy, piece)) || - (direcy <= 0 && check_liberties(x, y - 1, originx, originy, piece))) { - liberties_status[x][y] = 1; - } - else { - liberties_status[x][y] = 0; - } - return liberties_status[x][y]; + if (x < 0 || x > 18 || y < 0 || y > 18) { + return 0; + } + if (board_status[x][y] == 0) { + return 1; + } + if (board_status[x][y] != piece) { + return 0; + } + if (liberties_status[x][y] != -1) { + return liberties_status[x][y]; + } + int direcx = x - originx; + int direcy = y - originy; + if ((direcx >= 0 && check_liberties(x + 1, y, originx, originy, piece)) || + (direcx <= 0 && check_liberties(x - 1, y, originx, originy, piece)) || + (direcy >= 0 && check_liberties(x, y + 1, originx, originy, piece)) || + (direcy <= 0 && check_liberties(x, y - 1, originx, originy, piece))) { + liberties_status[x][y] = 1; + } else { + liberties_status[x][y] = 0; + } + return liberties_status[x][y]; } void jump_off(int x0, int z0, int color) { - // For now, let's just assume they're all jumping to point (-4,0,-4) - - int x1 = -4; float xt = x0*(1 - t) + x1*t; - int y0 = 9; float yt = -y0*pow(t, 2) + y0*t + y0; - int z1 = -4; float zt = z0*(1 - t) + z1*t; - - glPushMatrix(); - ApplyTransformationGeneral((xt - 9), (yt - 9), -(zt - 9)); - DrawSphere(color); - - // Draw Thigh - glPushMatrix(); - if (t < 0.1) { glRotatef(-80 + 80 * sinf(t * 10 * M_PI), 1.0, 0.0, 1.0); } - else { glRotatef(-80, 1.0, 0.0, 1.0); } - glScalef(2.0 / 5.0, 1.0, 2.0 / 5.0); - glTranslatef(0.0, -1.0, 0.0); - DrawUnitCube(3); - glScalef(5.0 / 2.0, 1.0, 5.0 / 2.0); - - // Draw Shin - glPushMatrix(); - - glTranslatef(0, -0.8, 0); - if (t < 0.1) { glRotatef(90 - 90 * sinf(t * 10 * M_PI), 1.0, 0.0, 1.0); } - else { glRotatef(90, 1.0, 0.0, 1.0); } - glScalef(1.0 / 5.0, 1.0, 1.0 / 5.0); - glTranslatef(0, -1.0, 0); - DrawUnitCube(4); - glScalef(5.0, 1.0, 5.0); - - // Draw Ankle - glPushMatrix(); - glTranslatef(0, -1.0, 0); - if (t < 0.1) { glRotatef(-100 + 100 * sinf(t * 10 * M_PI), 1.0, 0.0, 1.0); } - else { glRotatef(-100, 1.0, 0.0, 1.0); } - glScalef(1.0 / 2.0, 2.0 / 3.0, 1.0 / 2.0); - glTranslatef(0, -1.0, 0); - DrawSphere(0); - glPopMatrix(); - glPopMatrix(); - glPopMatrix(); - glPopMatrix(); + // For now, let's just assume they're all jumping to point (-4,0,-4) + + int x1 = -4; + float xt = x0 * (1 - t) + x1 * t; + int y0 = 9; + float yt = -y0 * std::pow(t, 2.0F) + y0 * t + y0; + int z1 = -4; + float zt = z0 * (1 - t) + z1 * t; + + glPushMatrix(); + ApplyTransformationGeneral((xt - 9), (yt - 9), -(zt - 9)); + DrawSphere(color); + + // Draw Thigh + glPushMatrix(); + if (t < 0.1) { + glRotatef(-80 + 80 * std::sin(t * 10.0F * PI), 1.0, 0.0, 1.0); + } else { + glRotatef(-80, 1.0, 0.0, 1.0); + } + glScalef(2.0 / 5.0, 1.0, 2.0 / 5.0); + glTranslatef(0.0, -1.0, 0.0); + DrawUnitCube(3); + glScalef(5.0 / 2.0, 1.0, 5.0 / 2.0); + + // Draw Shin + glPushMatrix(); + + glTranslatef(0, -0.8, 0); + if (t < 0.1) { + glRotatef(90 - 90 * std::sin(t * 10.0F * PI), 1.0, 0.0, 1.0); + } else { + glRotatef(90, 1.0, 0.0, 1.0); + } + glScalef(1.0 / 5.0, 1.0, 1.0 / 5.0); + glTranslatef(0, -1.0, 0); + DrawUnitCube(4); + glScalef(5.0, 1.0, 5.0); + + // Draw Ankle + glPushMatrix(); + glTranslatef(0, -1.0, 0); + if (t < 0.1) { + glRotatef(-100 + 100 * std::sin(t * 10.0F * PI), 1.0, 0.0, 1.0); + } else { + glRotatef(-100, 1.0, 0.0, 1.0); + } + glScalef(1.0 / 2.0, 2.0 / 3.0, 1.0 / 2.0); + glTranslatef(0, -1.0, 0); + DrawSphere(0); + glPopMatrix(); + glPopMatrix(); + glPopMatrix(); + glPopMatrix(); } void remove_block(int x, int y, int piece) { - if (board_status[x][y] == piece) { - board_status[x][y] = 0; - std::cout << "Jump from "; std::cout << x; std::cout << y; std::cout << '\n'; - - // rm_queue.push(x); - // rm_queue.push(y); - // rm_queue.push(piece); - - std::vector rm_piece; - rm_piece.push_back(x); - rm_piece.push_back(y); - rm_piece.push_back(piece); - L.push_back(rm_piece); - - remove_block(x - 1, y, piece); - remove_block(x + 1, y, piece); - remove_block(x, y - 1, piece); - remove_block(x, y + 1, piece); - } - + if (board_status[x][y] == piece) { + board_status[x][y] = 0; + std::cout << "Jump from "; + std::cout << x; + std::cout << y; + std::cout << '\n'; + + // rm_queue.push(x); + // rm_queue.push(y); + // rm_queue.push(piece); + + std::vector rm_piece; + rm_piece.push_back(x); + rm_piece.push_back(y); + rm_piece.push_back(piece); + L.push_back(rm_piece); + + remove_block(x - 1, y, piece); + remove_block(x + 1, y, piece); + remove_block(x, y - 1, piece); + remove_block(x, y + 1, piece); + } } int make_move(int x, int y, int piece) { - board_status[x][y] = piece; - clear_liberties(); - int other; - if (piece == 1) { - other = 2; - } - else { - other = 1; - } - int has_liberties = 0; - if (x > 0 && board_status[x - 1][y] != piece) { - if (piece == 0) { - has_liberties = 1; - } - else if (!check_liberties(x - 1, y, x - 1, y, other)) { - has_liberties = 1; - remove_block(x - 1, y, other); - } - } - if (x < 18 && board_status[x + 1][y] != piece) { - if (piece == 0) { - has_liberties = 1; - } - else if (!check_liberties(x + 1, y, x + 1, y, other)) { - has_liberties = 1; - remove_block(x + 1, y, other); - } - } - if (y > 0 && board_status[x][y - 1] != piece) { - if (piece == 0) { - has_liberties = 1; - } - else if (!check_liberties(x, y - 1, x, y - 1, other)) { - has_liberties = 1; - remove_block(x, y - 1, other); - } - } - if (y < 18 && board_status[x][y + 1] != piece) { - if (piece == 0) { - has_liberties = 1; - } - else if (!check_liberties(x, y + 1, x, y + 1, other)) { - has_liberties = 1; - remove_block(x, y + 1, other); - - } - } - if (!has_liberties && !check_liberties(x, y, x, y, piece)) { - remove_block(x, y, piece); - return 0; - } - return 1; + board_status[x][y] = piece; + clear_liberties(); + int other; + if (piece == 1) { + other = 2; + } else { + other = 1; + } + int has_liberties = 0; + if (x > 0 && board_status[x - 1][y] != piece) { + if (piece == 0) { + has_liberties = 1; + } else if (!check_liberties(x - 1, y, x - 1, y, other)) { + has_liberties = 1; + remove_block(x - 1, y, other); + } + } + if (x < 18 && board_status[x + 1][y] != piece) { + if (piece == 0) { + has_liberties = 1; + } else if (!check_liberties(x + 1, y, x + 1, y, other)) { + has_liberties = 1; + remove_block(x + 1, y, other); + } + } + if (y > 0 && board_status[x][y - 1] != piece) { + if (piece == 0) { + has_liberties = 1; + } else if (!check_liberties(x, y - 1, x, y - 1, other)) { + has_liberties = 1; + remove_block(x, y - 1, other); + } + } + if (y < 18 && board_status[x][y + 1] != piece) { + if (piece == 0) { + has_liberties = 1; + } else if (!check_liberties(x, y + 1, x, y + 1, other)) { + has_liberties = 1; + remove_block(x, y + 1, other); + } + } + if (!has_liberties && !check_liberties(x, y, x, y, piece)) { + remove_block(x, y, piece); + return 0; + } + return 1; } -void init() -{ - // select clearing color - glClearColor(0.0, 0.0, 0.0, 0.0); +void init() { + // select clearing color + glClearColor(0.0, 0.0, 0.0, 0.0); - // initialize projection matrix - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); + // initialize projection matrix + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); - double fovY = 45, aspect = 1.0, zNear = 0.1, zFar = 100; + double fovY = 45, aspect = 1.0, zNear = 0.1, zFar = 100; - gluPerspective(fovY, // Field of view - aspect, // Aspect ratio (width / height) - zNear, // Near plane - zFar); // Far plane + gluPerspective(fovY, // Field of view + aspect, // Aspect ratio (width / height) + zNear, // Near plane + zFar); // Far plane - // initialize modelview matrix - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); + // initialize modelview matrix + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); - glEnable(GL_LIGHTING); // enable lighting - glEnable(GL_CULL_FACE); // enable backface culling - glEnable(GL_DEPTH_TEST); // enable depth testing - //glEnable(GL_TEXTURE_2D); // enable textures + glEnable(GL_LIGHTING); // enable lighting + glEnable(GL_CULL_FACE); // enable backface culling + glEnable(GL_DEPTH_TEST); // enable depth testing + // glEnable(GL_TEXTURE_2D); // enable textures - SetImages(); + SetImages(); - init_board(); // Create the "Board" + init_board(); // Create the "Board" } \ No newline at end of file diff --git a/scripts/install_dependencies.sh b/scripts/install_dependencies.sh index 95bcd95..410fa68 100755 --- a/scripts/install_dependencies.sh +++ b/scripts/install_dependencies.sh @@ -20,4 +20,5 @@ run_with_sudo apt-get install -y \ libdevil-dev \ cmake \ clang-tidy \ + clang-format \ cppcheck diff --git a/scripts/run_lint.sh b/scripts/run_lint.sh index 57a2011..077b5ba 100755 --- a/scripts/run_lint.sh +++ b/scripts/run_lint.sh @@ -6,7 +6,15 @@ REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" BUILD_DIR="${REPO_ROOT}/build" # Configure the build tree and (re)generate compile_commands.json used by the linters. -cmake -S "${REPO_ROOT}" -B "${BUILD_DIR}" -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON +# Enable comprehensive warnings so clang diagnostics (unused constants, etc.) surface during analysis. +cmake -S "${REPO_ROOT}" \ + -B "${BUILD_DIR}" \ + -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ + -DCMAKE_CXX_FLAGS="-Wall -Wextra -Wpedantic" + +# Style check with clang-format; fails if formatting differs from .clang-format. +clang-format --dry-run --Werror "${REPO_ROOT}/main.cpp" # Static analysis with clang-tidy; uses the compile database for accurate diagnostics. clang-tidy "${REPO_ROOT}/main.cpp" -p "${BUILD_DIR}"