scrambler.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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[7] = {"RUFBLD"};
  13. char dir[4] = {" 2'"};
  14. // Scramble length
  15. const int SLEN = 10;
  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. // Random ints are generated and respective values are fed inside the array
  52. for (int i = 0; i < SLEN; i++)
  53. {
  54. a.mainScramble[i][0] = moves[getRand(5, 0)];
  55. a.mainScramble[i][1] = dir[getRand(2, 0)];
  56. }
  57. // But scramble is still isn't correct due to repeating moves
  58. }
  59. void valid()
  60. {
  61. // Stage 3
  62. // Variables for loop
  63. int loopOne, loopTwo;
  64. // This will actually start to make the scramble usable
  65. // It will remove stuff like R R F L, etc.
  66. for (loopOne = 1; loopOne < SLEN; loopOne++)
  67. {
  68. while (a.mainScramble[loopOne][0] == a.mainScramble[loopOne - 1][0])
  69. {
  70. a.mainScramble[loopOne][0] = moves[getRand(5, 0)];
  71. }
  72. }
  73. // This will further check it and remove stuff like R L R
  74. for (loopTwo = 2; loopTwo < SLEN; loopTwo++)
  75. {
  76. while ((a.mainScramble[loopTwo][0] == a.mainScramble[loopTwo - 2][0]) || (a.mainScramble[loopTwo][0]) == a.mainScramble[loopTwo - 1][0])
  77. {
  78. a.mainScramble[loopTwo][0] = moves[getRand(5, 0)];
  79. }
  80. }
  81. // Scramble generation complete
  82. }
  83. int getRand(int upr, int lwr)
  84. {
  85. int randNum;
  86. randNum = (rand() % (upr - lwr + 1)) + lwr;
  87. return randNum;
  88. }
  89. char *printData()
  90. {
  91. char *result = malloc(100);
  92. int offset = 0;
  93. for (int loop = 0; loop < SLEN; loop++)
  94. {
  95. offset += snprintf(result + offset, 100 - offset, "%s ", a.mainScramble[loop]);
  96. }
  97. return result;
  98. }