scrambler.c 2.8 KB

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