scrambler.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 int SLEN = 10;
  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. void valid();
  26. int getRand(int upr, int lwr);
  27. char *printData();
  28. void writeToFile();
  29. // Main function
  30. /* int main(){
  31. genScramble ();//Calling genScramble
  32. scrambleReplace();//Calling scrambleReplace
  33. valid();//Calling valid to validate the scramble
  34. printData ();//Printing the final scramble
  35. //writeToFile();//If you want to write to a file, please uncomment this
  36. return 0;
  37. } */
  38. void genScramble()
  39. {
  40. // Stage 1
  41. for (int i = 0; i < SLEN; i++)
  42. {
  43. strcpy(a.mainScramble[i], "00");
  44. }
  45. // This makes array like this 00 00 00.......
  46. }
  47. void scrambleReplace()
  48. {
  49. // Stage 2
  50. // Actual process begins here
  51. // Initialize the mainScramble array with all the possible moves
  52. for (int i = 0; i < SLEN; i++)
  53. {
  54. a.mainScramble[i][0] = moves[getRand(6, 0)];
  55. a.mainScramble[i][1] = dir[getRand(3, 0)];
  56. }
  57. // Perform the Fisher-Yates shuffle
  58. for (int i = 6 - 1; i > 0; i--)
  59. {
  60. int j = rand() % (i + 1);
  61. char temp[3];
  62. strcpy(temp, a.mainScramble[i]);
  63. strcpy(a.mainScramble[i], a.mainScramble[j]);
  64. strcpy(a.mainScramble[j], temp);
  65. }
  66. // Select the first 10 elements as the scramble, using only the first three elements of the dir array
  67. for (int i = 0; i < SLEN; i++)
  68. {
  69. a.mainScramble[i][1] = dir[rand() % 3];
  70. }
  71. }
  72. void valid()
  73. {
  74. // Stage 3
  75. // Variables for loop
  76. int loopOne, loopTwo;
  77. // This will actually start to make the scramble usable
  78. // It will remove stuff like R R F L, etc.
  79. for (loopOne = 1; loopOne < SLEN; loopOne++)
  80. {
  81. while (a.mainScramble[loopOne][0] == a.mainScramble[loopOne - 1][0])
  82. {
  83. a.mainScramble[loopOne][0] = moves[getRand(5, 0)];
  84. }
  85. }
  86. // This will further check it and remove stuff like R L R
  87. for (loopTwo = 2; loopTwo < SLEN; loopTwo++)
  88. {
  89. while ((a.mainScramble[loopTwo][0] == a.mainScramble[loopTwo - 2][0]) || (a.mainScramble[loopTwo][0]) == a.mainScramble[loopTwo - 1][0])
  90. {
  91. a.mainScramble[loopTwo][0] = moves[getRand(5, 0)];
  92. }
  93. }
  94. // Scramble generation complete
  95. }
  96. int getRand(int upr, int lwr)
  97. {
  98. int randNum;
  99. randNum = (rand() % (upr - lwr + 1)) + lwr;
  100. return randNum;
  101. }
  102. char *printData()
  103. {
  104. static char result[RESULT_SIZE];
  105. int offset = 0;
  106. for (int loop = 0; loop < SLEN; loop++)
  107. {
  108. offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
  109. }
  110. return result;
  111. }