scrambler.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 = 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. 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. for (int i = 2; i < SLEN; i++) {
  74. while ( a.mainScramble[i][0] == a.mainScramble[i - 2][0] || a.mainScramble[i][0] == a.mainScramble[i - 1][0]) {
  75. a.mainScramble[i][0] = moves[rand()%5];
  76. }
  77. }
  78. // Scramble generation complete
  79. }
  80. int getRand(int upr, int lwr)
  81. {
  82. int randNum;
  83. randNum = (rand() % (upr - lwr + 1)) + lwr;
  84. return randNum;
  85. }
  86. // Let this function be here for now till I find out what is causing the extra space bug in the scrambles
  87. void remove_double_spaces(char *str) {
  88. int i, j;
  89. int len = strlen(str);
  90. for (i = 0, j = 0; i < len; i++, j++) {
  91. if (str[i] == ' ' && str[i + 1] == ' ') {
  92. i++;
  93. }
  94. str[j] = str[i];
  95. }
  96. str[j] = '\0';
  97. }
  98. char *printData()
  99. {
  100. static char result[RESULT_SIZE];
  101. int offset = 0;
  102. for (int loop = 0; loop < SLEN; loop++)
  103. {
  104. offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
  105. }
  106. remove_double_spaces(result);
  107. return result;
  108. }