scrambler.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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[furi_hal_random_get() % 6];
  55. a.mainScramble[i][1] = dir[furi_hal_random_get() % 3];
  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[furi_hal_random_get() % 3];
  70. }
  71. for (int i = 1; i < SLEN; i++) {
  72. while ( a.mainScramble[i][0] == a.mainScramble[i - 2][0] || a.mainScramble[i][0] == a.mainScramble[i - 1][0]) {
  73. a.mainScramble[i][0] = moves[furi_hal_random_get()%5];
  74. }
  75. }
  76. }
  77. // Let this function be here for now till I find out what is causing the extra space bug in the scrambles
  78. void remove_double_spaces(char *str) {
  79. int i, j;
  80. int len = strlen(str);
  81. for (i = 0, j = 0; i < len; i++, j++) {
  82. if (str[i] == ' ' && str[i + 1] == ' ') {
  83. i++;
  84. }
  85. str[j] = str[i];
  86. }
  87. str[j] = '\0';
  88. }
  89. char *printData()
  90. {
  91. static char result[RESULT_SIZE];
  92. int offset = 0;
  93. for (int loop = 0; loop < SLEN; loop++)
  94. {
  95. offset += snprintf(result + offset, RESULT_SIZE - offset, "%s ", a.mainScramble[loop]);
  96. }
  97. remove_double_spaces(result);
  98. return result;
  99. }