diff options
author | Elwin Hammer <elwinhammer@gmail.com> | 2024-05-29 21:41:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-29 21:41:24 +0200 |
commit | 1f78927e2e399a504368fb9b407de12d06dddcb5 (patch) | |
tree | f80ba30274ca75704075610a39fc28930f7ac4fa /puzzle/neo/console-neopuzzle | |
parent | d7616546dd5e8ba35c2b1b1ece736bca60e0b990 (diff) | |
parent | 8894d20ff0d1c1dde69879a21e756e01bcfa5262 (diff) |
Merge pull request #11 from lonkaars/masterprot/software-puzzle
Bring software-puzzle up-to-date
Diffstat (limited to 'puzzle/neo/console-neopuzzle')
-rw-r--r-- | puzzle/neo/console-neopuzzle/neo.cpp | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/puzzle/neo/console-neopuzzle/neo.cpp b/puzzle/neo/console-neopuzzle/neo.cpp new file mode 100644 index 0000000..56d90f7 --- /dev/null +++ b/puzzle/neo/console-neopuzzle/neo.cpp @@ -0,0 +1,100 @@ +#include <iostream> +#include <array> + +#define MATRIX_SIZE 8 + +enum NeoState { + NEO_UNINITIALIZED, + NEO_PLAYING, + NEO_SOLVED +}; + +// Simulate the 8x8 LED matrix with a 2D array +std::array<std::array<bool, MATRIX_SIZE>, MATRIX_SIZE> neoMatrix; + +NeoState neoState = NEO_UNINITIALIZED; + +// Helper function to toggle LEDs if within bounds +void toggleIfValid(int x, int y) { + if (x >= 0 && x < MATRIX_SIZE && y >= 0 && y < MATRIX_SIZE) { + neoMatrix[x][y] = !neoMatrix[x][y]; + } +} + +void initializeNeoMatrix() { + // The initial pattern from the Appendix A example (assuming red is 'true'/on and white is 'false'/off) + std::array<std::array<bool, MATRIX_SIZE>, MATRIX_SIZE> initialPattern = {{ + {false, true, false, true, false, true, false, true}, + {true, false, true, false, true, false, true, false}, + {false, true, false, true, false, true, false, true}, + {true, false, true, false, true, false, true, false}, + {false, true, false, true, false, true, false, true}, + {true, false, true, false, true, false, true, false}, + {false, true, false, true, false, true, false, true}, + {true, false, true, false, true, false, true, false} + }}; + + for (int i = 0; i < MATRIX_SIZE; i++) { + for (int j = 0; j < MATRIX_SIZE; j++) { + neoMatrix[i][j] = initialPattern[i][j]; + } + } + + neoState = NEO_PLAYING; +} + + +void printNeoMatrix() { + // Print the matrix state to the console + for (int i = 0; i < MATRIX_SIZE; i++) { + for (int j = 0; j < MATRIX_SIZE; j++) { + std::cout << (neoMatrix[i][j] ? 1 : 0) << " "; + } + std::cout << std::endl; + } +} + +void toggleAdjacentLEDs(int x, int y) { + // Toggle the LED at (x, y) and adjacent LEDs + toggleIfValid(x, y); // Center + toggleIfValid(x - 1, y); // Up + toggleIfValid(x + 1, y); // Down + toggleIfValid(x, y - 1); // Left + toggleIfValid(x, y + 1); // Right +} + + +bool isNeoPuzzleSolved() { + for (int i = 0; i < MATRIX_SIZE; i++) { + for (int j = 0; j < MATRIX_SIZE; j++) { + if (neoMatrix[i][j]) return false; // If any LED is on, puzzle is not solved + } + } + return true; +} + +/// Integration needed +int main() { + initializeNeoMatrix(); + printNeoMatrix(); + + while (neoState != NEO_SOLVED) { + int x, y; + std::cout << "Enter the coordinates of the button pressed (x y): "; + std::cin >> x >> y; + + if (x >= 0 && x < MATRIX_SIZE && y >= 0 && y < MATRIX_SIZE) { + toggleAdjacentLEDs(x, y); + printNeoMatrix(); + + if (isNeoPuzzleSolved()) { + neoState = NEO_SOLVED; + std::cout << "The NeoTrellis puzzle is solved!\n"; + } + } else { + std::cout << "Invalid coordinates. Please enter values between 0 and " << MATRIX_SIZE - 1 << ".\n"; + } + } + + return 0; +} |