scrambler.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Authors: Tanish Bhongade and RaZe
  3. */
  4. #include <stdio.h>
  5. #include <furi.h>
  6. #include <gui/gui.h>
  7. #include "furi_hal_random.h"
  8. #include <input/input.h>
  9. #include <gui/elements.h>
  10. #include "scrambler.h"
  11. // 6 moves along with direction
  12. char moves[6] = {'R', 'U', 'F', 'B', 'L', 'D'};
  13. char dir[4] = {'\'', '2'};
  14. const int32_t SLEN = 20;
  15. #define RESULT_SIZE 100
  16. struct GetScramble
  17. {
  18. char mainScramble[25][3];
  19. };
  20. struct GetScramble a;
  21. void scrambleReplace()
  22. {
  23. // Initialize the mainScramble array with all the possible moves
  24. for (int32_t i = 0; i < SLEN; i++)
  25. {
  26. a.mainScramble[i][0] = moves[furi_hal_random_get() % 6];
  27. a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
  28. }
  29. /* // Perform the Fisher-Yates shuffle
  30. for (int32_t i = 6 - 1; i > 0; i--)
  31. {
  32. int32_t j = rand() % (i + 1);
  33. char temp[3];
  34. strcpy(temp, a.mainScramble[i]);
  35. strcpy(a.mainScramble[i], a.mainScramble[j]);
  36. strcpy(a.mainScramble[j], temp);
  37. } */
  38. // Select the first 10 elements as the scramble, using only the first two elements of the dir array
  39. for (int32_t i = 0; i < SLEN; i++)
  40. {
  41. a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
  42. }
  43. for (int32_t i = 1; i < SLEN; i++) {
  44. while ( a.mainScramble[i][0] == a.mainScramble[i - 2][0] || a.mainScramble[i][0] == a.mainScramble[i - 1][0]) {
  45. a.mainScramble[i][0] = moves[furi_hal_random_get()%5];
  46. }
  47. }
  48. }
  49. char *printData()
  50. {
  51. static char result[RESULT_SIZE];
  52. int32_t offset = 0;
  53. for (int32_t loop = 0; loop < SLEN; loop++)
  54. {
  55. offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
  56. }
  57. return result;
  58. }