Przeglądaj źródła

Squashed 'rubiks_cube_scrambler/' changes from 703719f16..7896fcd73

7896fcd73 bump versions and set bt trigger to use keys from ble remote
5e03b350e fix plugins, remove dupe
44b35460e categories part 2
23d8c5a41 categories part 1
5ba301902 more manifestos, xbox controller and videopoker ufbt fixes
622c5d1c7 Add Screenshots
1173c29d9 API 31 / unzip sources
REVERT: 703719f16 refactor: move the scramble generation
REVERT: 5e53c2b55 fix: code cleanup
REVERT: a151357f8 fix: use better types, code cleanup
REVERT: a0395aede fix: code cleanup, bug fix
REVERT: e1d91090a fix: Made the scramble making 10 times faster
REVERT: 61517a6d9 fix: better and faster scrambles, bug fixes
REVERT: c83430ff5 feat: more moves in the scrambles
REVERT: 85b8f9f1f fix: make the scramble centered
REVERT: e55cf2438 fix: made the scrambler making faster
REVERT: a792c369b chore: format file
REVERT: ceba2970e fix: center text, better logic
REVERT: fae3453b5 chore: format code
REVERT: 4abc565b7 fix: free the malloced memory after usage
REVERT: d5a51bd03 chore: credits to the creator of the scrambler :)
REVERT: 0af361e5c feat: the scrambler code

git-subtree-dir: rubiks_cube_scrambler
git-subtree-split: 7896fcd73b0a01656b1c36e3a2a646305aa27587
Willy-JL 2 lat temu
rodzic
commit
0a9a4d3d82
8 zmienionych plików z 146 dodań i 88 usunięć
  1. 21 0
      LICENSE
  2. 13 0
      README.md
  3. 6 2
      application.fam
  4. BIN
      img/1.png
  5. BIN
      img/2.png
  6. 39 56
      rubiks_cube_scrambler.c
  7. 64 28
      scrambler.c
  8. 3 2
      scrambler.h

+ 21 - 0
LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2022 RaZe
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 13 - 0
README.md

@@ -0,0 +1,13 @@
+# Rubik's Cube Scrambler FAP
+
+## Where to start?
+Install the .fap file and put it in your apps folder
+
+## What does what?
+The On/Off button toggles the vibration notification on and off. The "New" button generates a new scramble. The scramble letters correspond with the following moves: R = Right, L = Left, U = Up, D = Down, F = Front, B = Back. The number after the letter indicates how many times to turn that face. For example, R2 means to turn the right face twice. The ' symbol indicates a counter-clockwise turn. For example, R' means to turn the right face counter-clockwise once.
+
+<img src="assets/1.png">
+
+# A special thanks to Tanish for their c scrambler example 🙏
+https://github.com/TanishBhongade/RubiksCubeScrambler-C/
+

+ 6 - 2
application.fam

@@ -1,6 +1,6 @@
 # COMPILE ISTRUCTIONS:
 
-# Clean the code and remove old binaries/compilation artefact 
+# Clean the code and remove old binaries/compilation artefact
 # ./fbt -c fap_rubiks_cube_scrambler
 
 # Compile FAP
@@ -15,6 +15,10 @@ App(
     apptype=FlipperAppType.EXTERNAL,
     entry_point="rubiks_cube_scrambler_main",
     stack_size=1 * 1024,
-    fap_category="Misc",
+    fap_category="Games",
     fap_icon="cube.png",
+    fap_author="@RaZeSloth",
+    fap_weburl="https://github.com/RaZeSloth/flipperzero-rubiks-cube-scrambler",
+    fap_version="1.1",
+    fap_description="App generates random moves to scramble a Rubik's cube.",
 )

BIN
img/1.png


BIN
img/2.png


+ 39 - 56
rubiks_cube_scrambler.c

@@ -8,38 +8,30 @@
 #include "scrambler.h"
 #include "furi_hal_random.h"
 
-bool scrambleStarted = false;
+int scrambleStarted = 0;
 char scramble_str[100] = {0};
 char scramble_start[100] = {0};
 char scramble_end[100] = {0};
-bool notifications_enabled = false;
+int notifications_enabled = 0;
 
-static void success_vibration()
-{
+static void success_vibration() {
     furi_hal_vibro_on(false);
     furi_hal_vibro_on(true);
     furi_delay_ms(50);
     furi_hal_vibro_on(false);
     return;
 }
-void split_array(char original[], int size, char first[], char second[])
-{
-    int32_t mid = size / 2;
-    if (size % 2 != 0)
-    {
+void split_array(char original[], int size, char first[], char second[]) {
+    int mid = size / 2;
+    if(size % 2 != 0) {
         mid++;
     }
-    int32_t first_index = 0, second_index = 0;
-    for (int32_t i = 0; i < size; i++)
-    {
-        if (i < mid)
-        {
+    int first_index = 0, second_index = 0;
+    for(int i = 0; i < size; i++) {
+        if(i < mid) {
             first[first_index++] = original[i];
-        }
-        else
-        {
-            if (i == mid && (original[i] == '2' || original[i] == '\''))
-            {
+        } else {
+            if(i == mid && (original[i] == '2' || original[i] == '\'')) {
                 continue;
             }
             second[second_index++] = original[i];
@@ -48,19 +40,23 @@ void split_array(char original[], int size, char first[], char second[])
     first[first_index] = '\0';
     second[second_index] = '\0';
 }
-void genScramble()
-{
-    scrambleReplace();
-    strcpy(scramble_str, printData());
-    split_array(scramble_str, strlen(scramble_str), scramble_start, scramble_end);
-}
 
-static void draw_callback(Canvas *canvas, void *ctx)
-{
+static void draw_callback(Canvas* canvas, void* ctx) {
     UNUSED(ctx);
     canvas_clear(canvas);
     canvas_set_font(canvas, FontPrimary);
     canvas_draw_str(canvas, 4, 13, "Rubik's Cube Scrambler");
+
+    if(scrambleStarted) {
+        genScramble();
+        scrambleReplace();
+        strcpy(scramble_str, printData());
+        if(notifications_enabled) {
+            success_vibration();
+        }
+        split_array(scramble_str, strlen(scramble_str), scramble_start, scramble_end);
+        scrambleStarted = 0;
+    }
     canvas_set_font(canvas, FontSecondary);
     canvas_draw_str_aligned(canvas, 64, 28, AlignCenter, AlignCenter, scramble_start);
     canvas_draw_str_aligned(canvas, 64, 38, AlignCenter, AlignCenter, scramble_end);
@@ -69,58 +65,45 @@ static void draw_callback(Canvas *canvas, void *ctx)
     elements_button_left(canvas, notifications_enabled ? "On" : "Off");
 };
 
-static void input_callback(InputEvent *input_event, void *ctx)
-{
-
+static void input_callback(InputEvent* input_event, void* ctx) {
     furi_assert(ctx);
-    FuriMessageQueue *event_queue = ctx;
+    FuriMessageQueue* event_queue = ctx;
     furi_message_queue_put(event_queue, input_event, FuriWaitForever);
 }
 
-int32_t rubiks_cube_scrambler_main(void *p)
-{
+int32_t rubiks_cube_scrambler_main(void* p) {
     UNUSED(p);
     InputEvent event;
 
-    FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
+    FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
 
-    ViewPort *view_port = view_port_alloc();
+    ViewPort* view_port = view_port_alloc();
 
     view_port_draw_callback_set(view_port, draw_callback, NULL);
 
     view_port_input_callback_set(view_port, input_callback, event_queue);
 
-    Gui *gui = furi_record_open(RECORD_GUI);
+    Gui* gui = furi_record_open(RECORD_GUI);
     gui_add_view_port(gui, view_port, GuiLayerFullscreen);
 
-    while (true)
-    {
+    while(true) {
         furi_check(furi_message_queue_get(event_queue, &event, FuriWaitForever) == FuriStatusOk);
 
-        if (event.key == InputKeyOk && event.type == InputTypeShort)
-        {
-            genScramble();
-            if (notifications_enabled)
-            {
-                success_vibration();
-            }
+        if(event.key == InputKeyOk && event.type == InputTypeShort) {
+            scrambleStarted = 1;
         }
-        if (event.key == InputKeyLeft && event.type == InputTypeShort)
-        {
-            if (notifications_enabled)
-            {
-                notifications_enabled = false;
-            }
-            else
-            {
-                notifications_enabled = true;
+        if(event.key == InputKeyLeft && event.type == InputTypeShort) {
+            if(notifications_enabled) {
+                notifications_enabled = 0;
+            } else {
+                notifications_enabled = 1;
                 success_vibration();
             }
         }
-        if (event.key == InputKeyBack)
-        {
+        if(event.key == InputKeyBack) {
             break;
         }
+        view_port_update(view_port);
     }
 
     furi_message_queue_free(event_queue);

+ 64 - 28
scrambler.c

@@ -12,55 +12,91 @@ Authors: Tanish Bhongade and RaZe
 
 // 6 moves along with direction
 char moves[6] = {'R', 'U', 'F', 'B', 'L', 'D'};
-char dir[4] = {'\'', '2'};
-const int32_t SLEN = 20;
+char dir[4] = {' ', '\'', '2'};
+const int SLEN = 20;
 #define RESULT_SIZE 100
-
-struct GetScramble
-{
-	char mainScramble[25][3];
+// Structure which holds main scramble
+struct GetScramble {
+    char mainScramble[25][3];
 };
-struct GetScramble a;
+struct GetScramble a; // Its object
+
+// Function prototypes to avoid bugs
+void scrambleReplace();
+void genScramble();
+void valid();
+int getRand(int upr, int lwr);
+char* printData();
+void writeToFile();
+
+// Main function
+/* int main(){
+	genScramble ();//Calling genScramble
+	scrambleReplace();//Calling scrambleReplace
+	valid();//Calling valid to validate the scramble
+	printData ();//Printing the final scramble
+	//writeToFile();//If you want to write to a file, please uncomment this
+
+	return 0;
+} */
+
+void genScramble() {
+    // Stage 1
+    for(int i = 0; i < SLEN; i++) {
+        strcpy(a.mainScramble[i], "00");
+    }
+    // This makes array like this 00 00 00.......
+}
 
+void scrambleReplace() {
+    // Stage 2
+    // Actual process begins here
 
-void scrambleReplace()
-{
     // Initialize the mainScramble array with all the possible moves
-    for (int32_t i = 0; i < SLEN; i++)
-    {
+    for(int i = 0; i < SLEN; i++) {
         a.mainScramble[i][0] = moves[furi_hal_random_get() % 6];
         a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
     }
 
-    /* // Perform the Fisher-Yates shuffle
-    for (int32_t i = 6 - 1; i > 0; i--)
-    {
-        int32_t j = rand() % (i + 1);
+    // Perform the Fisher-Yates shuffle
+    for(int i = 6 - 1; i > 0; i--) {
+        int j = rand() % (i + 1);
         char temp[3];
         strcpy(temp, a.mainScramble[i]);
         strcpy(a.mainScramble[i], a.mainScramble[j]);
         strcpy(a.mainScramble[j], temp);
-    } */
+    }
 
-    // Select the first 10 elements as the scramble, using only the first two elements of the dir array
-    for (int32_t i = 0; i < SLEN; i++)
-    {
+    // Select the first 10 elements as the scramble, using only the first three elements of the dir array
+    for(int i = 0; i < SLEN; i++) {
         a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
     }
-     for (int32_t i = 1; i < SLEN; i++) {
-        while ( a.mainScramble[i][0] == a.mainScramble[i - 2][0] || a.mainScramble[i][0] == a.mainScramble[i - 1][0]) {
-            a.mainScramble[i][0] = moves[furi_hal_random_get()%5];
+    for(int i = 1; i < SLEN; i++) {
+        while(a.mainScramble[i][0] == a.mainScramble[i - 2][0] ||
+              a.mainScramble[i][0] == a.mainScramble[i - 1][0]) {
+            a.mainScramble[i][0] = moves[furi_hal_random_get() % 5];
         }
     }
 }
 
-char *printData()
-{
+// Let this function be here for now till I find out what is causing the extra space bug in the scrambles
+void remove_double_spaces(char* str) {
+    int i, j;
+    int len = strlen(str);
+    for(i = 0, j = 0; i < len; i++, j++) {
+        if(str[i] == ' ' && str[i + 1] == ' ') {
+            i++;
+        }
+        str[j] = str[i];
+    }
+    str[j] = '\0';
+}
+char* printData() {
     static char result[RESULT_SIZE];
-    int32_t offset = 0;
-    for (int32_t loop = 0; loop < SLEN; loop++)
-    {
+    int offset = 0;
+    for(int loop = 0; loop < SLEN; loop++) {
         offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
     }
+    remove_double_spaces(result);
     return result;
-}
+}

+ 3 - 2
scrambler.h

@@ -1,2 +1,3 @@
-void scrambleReplace ();
-char *printData ();
+void scrambleReplace();
+void genScramble();
+char* printData();