scrambler.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // Structure which holds main scramble
  17. struct GetScramble
  18. {
  19. char mainScramble[25][3];
  20. };
  21. struct GetScramble a; // Its object
  22. // Function prototypes to avoid bugs
  23. void scrambleReplace();
  24. void genScramble();
  25. char *printData();
  26. // Main function
  27. /* int main(){
  28. genScramble ();//Calling genScramble
  29. scrambleReplace();//Calling scrambleReplace
  30. valid();//Calling valid to validate the scramble
  31. printData ();//Printing the final scramble
  32. //writeToFile();//If you want to write to a file, please uncomment this
  33. return 0;
  34. } */
  35. void genScramble()
  36. {
  37. // Stage 1
  38. for (int32_t i = 0; i < SLEN; i++)
  39. {
  40. strcpy(a.mainScramble[i], "00");
  41. }
  42. // This makes array like this 00 00 00.......
  43. }
  44. void scrambleReplace()
  45. {
  46. // Stage 2
  47. // Actual process begins here
  48. // Initialize the mainScramble array with all the possible moves
  49. for (int32_t i = 0; i < SLEN; i++)
  50. {
  51. a.mainScramble[i][0] = moves[furi_hal_random_get() % 6];
  52. a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
  53. }
  54. // Perform the Fisher-Yates shuffle
  55. for (int32_t i = 6 - 1; i > 0; i--)
  56. {
  57. int32_t j = rand() % (i + 1);
  58. char temp[3];
  59. strcpy(temp, a.mainScramble[i]);
  60. strcpy(a.mainScramble[i], a.mainScramble[j]);
  61. strcpy(a.mainScramble[j], temp);
  62. }
  63. // Select the first 10 elements as the scramble, using only the first three elements of the dir array
  64. for (int32_t i = 0; i < SLEN; i++)
  65. {
  66. a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
  67. }
  68. for (int32_t i = 1; i < SLEN; i++) {
  69. while ( a.mainScramble[i][0] == a.mainScramble[i - 2][0] || a.mainScramble[i][0] == a.mainScramble[i - 1][0]) {
  70. a.mainScramble[i][0] = moves[furi_hal_random_get()%5];
  71. }
  72. }
  73. }
  74. // Let this function be here for now till I find out what is causing the extra space bug in the scrambles
  75. void remove_double_spaces(char *str) {
  76. int32_t i, j;
  77. int32_t len = strlen(str);
  78. for (i = 0, j = 0; i < len; i++, j++) {
  79. if (str[i] == ' ' && str[i + 1] == ' ') {
  80. i++;
  81. }
  82. str[j] = str[i];
  83. }
  84. str[j] = '\0';
  85. }
  86. char *printData()
  87. {
  88. static char result[RESULT_SIZE];
  89. int32_t offset = 0;
  90. for (int32_t loop = 0; loop < SLEN; loop++)
  91. {
  92. offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
  93. }
  94. remove_double_spaces(result);
  95. return result;
  96. }