Просмотр исходного кода

fix: made the scrambler making faster

RaZe 3 лет назад
Родитель
Сommit
e55cf24380
2 измененных файлов с 45 добавлено и 45 удалено
  1. 5 25
      rubiks_cube_scrambler.c
  2. 40 20
      scrambler.c

+ 5 - 25
rubiks_cube_scrambler.c

@@ -1,14 +1,14 @@
 #include <stdio.h>
 #include <furi.h>
 #include <gui/gui.h>
-#include "furi_hal_random.h"
 #include <input/input.h>
 #include <gui/elements.h>
-#include "scrambler.h"
 #include <furi_hal.h>
 
-int scrambleStarted = 0;
+#include "scrambler.h"
+#include "furi_hal_random.h"
 
+int scrambleStarted = 0;
 char scramble[100] = {0};
 int notifications_enabled = 0;
 
@@ -28,32 +28,12 @@ static void draw_callback(Canvas *canvas, void *ctx)
     canvas_set_font(canvas, FontPrimary);
     canvas_draw_str(canvas, 4, 13, "Rubik's Cube Scrambler");
 
-    if (scrambleStarted == 1)
+    if (scrambleStarted)
     {
-        /*         const char* moves[] = {"U", "D", "L", "R", "F", "B"};
-                const char* directions[] = {"", "'", "2"};
-                int index = 0;
-                int prevMove = -1; // Initialize previous move to an invalid value
-                for(int i = 0; i < 10; i++) {
-                    int move;
-                    do {
-                        move = rand() % 6;
-                    } while(move == prevMove);
-                    int direction = rand() % 3;
-                    prevMove = move;
-                    index += snprintf(
-                        scramble + index,
-                        sizeof(scramble) - index,
-                        "%s%s ",
-                        moves[move],
-                        directions[direction]);
-                } */
         genScramble();
         scrambleReplace();
         valid();
-        char *data = printData();
-        strcpy(scramble, data);
-        free(data);
+        strcpy(scramble, printData());
         if (notifications_enabled)
         {
             success_vibration();

+ 40 - 20
scrambler.c

@@ -11,11 +11,10 @@ Authors: Tanish Bhongade and RaZe
 #include "scrambler.h"
 
 // 6 moves along with direction
-char moves[7] = {"RUFBLD"};
-char dir[4] = {" 2'"};
-// Scramble length
+char moves[6] = {'R', 'U', 'F', 'B', 'L', 'D'};
+char dir[4] = {' ', '\'', '2'};
 const int SLEN = 10;
-
+#define RESULT_SIZE 100
 // Structure which holds main scramble
 struct GetScramble
 {
@@ -54,17 +53,36 @@ void genScramble()
 
 void scrambleReplace()
 {
-	// Stage 2
-	// Actual process begins here
-	// Random ints are generated and respective values are fed inside the array
-	for (int i = 0; i < SLEN; i++)
-	{
-		a.mainScramble[i][0] = moves[getRand(5, 0)];
-		a.mainScramble[i][1] = dir[getRand(2, 0)];
-	}
-	// But scramble is still isn't correct due to repeating moves
+    // Stage 2
+    // Actual process begins here
+
+    // Initialize the mainScramble array with all the possible moves
+    for (int i = 0; i < SLEN; i++)
+    {
+        a.mainScramble[i][0] = moves[getRand(6, 0)];
+        a.mainScramble[i][1] = dir[getRand(3, 0)];
+    }
+
+    // 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 three elements of the dir array
+    for (int i = 0; i < SLEN; i++)
+    {
+        a.mainScramble[i][1] = dir[rand() % 3];
+    }
 }
 
+
+
+
 void valid()
 {
 	// Stage 3
@@ -101,11 +119,13 @@ int getRand(int upr, int lwr)
 
 char *printData()
 {
-	char *result = malloc(100);
-	int offset = 0;
-	for (int loop = 0; loop < SLEN; loop++)
-	{
-		offset += snprintf(result + offset, 100 - offset, "%s ", a.mainScramble[loop]);
-	}
-	return result;
+    static char result[RESULT_SIZE];
+    int offset = 0;
+    for (int loop = 0; loop < SLEN; loop++)
+    {
+        offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
+    }
+    return result;
 }
+
+