scrambler.c 2.7 KB

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