scrambler.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. char mainScramble[25][3];
  18. };
  19. struct GetScramble a;
  20. void scrambleReplace() {
  21. // Initialize the mainScramble array with all the possible moves
  22. for(int32_t i = 0; i < SLEN; i++) {
  23. a.mainScramble[i][0] = moves[furi_hal_random_get() % 6];
  24. a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
  25. }
  26. /* // Perform the Fisher-Yates shuffle
  27. for (int32_t i = 6 - 1; i > 0; i--)
  28. {
  29. int32_t j = rand() % (i + 1);
  30. char temp[3];
  31. strcpy(temp, a.mainScramble[i]);
  32. strcpy(a.mainScramble[i], a.mainScramble[j]);
  33. strcpy(a.mainScramble[j], temp);
  34. } */
  35. // Select the first 10 elements as the scramble, using only the first two elements of the dir array
  36. for(int32_t i = 0; i < SLEN; i++) {
  37. a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
  38. }
  39. for(int32_t i = 1; i < SLEN; i++) {
  40. while(a.mainScramble[i][0] == a.mainScramble[i - 2][0] ||
  41. a.mainScramble[i][0] == a.mainScramble[i - 1][0]) {
  42. a.mainScramble[i][0] = moves[furi_hal_random_get() % 5];
  43. }
  44. }
  45. }
  46. char* printData() {
  47. static char result[RESULT_SIZE];
  48. int32_t offset = 0;
  49. for(int32_t loop = 0; loop < SLEN; loop++) {
  50. offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
  51. }
  52. return result;
  53. }