scrambler.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <gui/gui.h>
  4. #include "furi_hal_random.h"
  5. #include <input/input.h>
  6. #include <gui/elements.h>
  7. #include "scrambler.h"
  8. //6 moves along with direction
  9. char moves[7]= {"RUFBLD"};
  10. char dir[4]={" 2'"};
  11. //Scramble length
  12. const int SLEN=10;
  13. //Structure which holds main scramble
  14. struct GetScramble{
  15. char mainScramble [25][3];
  16. };struct GetScramble a;//Its object
  17. //Function prototypes to avoid bugs
  18. void scrambleReplace ();
  19. void genScramble ();
  20. void valid ();
  21. int getRand (int upr,int lwr);
  22. char *printData ();
  23. void writeToFile ();
  24. //Main function
  25. /* int main(){
  26. genScramble ();//Calling genScramble
  27. scrambleReplace();//Calling scrambleReplace
  28. valid();//Calling valid to validate the scramble
  29. printData ();//Printing the final scramble
  30. //writeToFile();//If you want to write to a file, please uncomment this
  31. return 0;
  32. } */
  33. void genScramble (){
  34. //Stage 1
  35. for(int i=0; i<SLEN; i++) {
  36. strcpy(a.mainScramble[i],"00");
  37. }
  38. //This makes array like this 00 00 00.......
  39. }
  40. void scrambleReplace (){
  41. //Stage 2
  42. //Actual process begins here
  43. //Random ints are generated and respective values are fed inside the array
  44. for (int i = 0; i<SLEN; i++){
  45. a.mainScramble[i][0] = moves[getRand (5,0)];
  46. a.mainScramble[i][1] = dir[getRand (2,0)];
  47. }
  48. //But scramble is still isn't correct due to repeating moves
  49. }
  50. void valid (){
  51. //Stage 3
  52. //Variables for loop
  53. int loopOne, loopTwo;
  54. //This will actually start to make the scramble usable
  55. //It will remove stuff like R R F L, etc.
  56. for (loopOne=1;loopOne<SLEN;loopOne++){
  57. while (a.mainScramble[loopOne][0] == a.mainScramble[loopOne-1][0]){
  58. a.mainScramble[loopOne][0]=moves[getRand(5,0)];
  59. }
  60. }
  61. //This will further check it and remove stuff like R L R
  62. for (loopTwo=2; loopTwo<SLEN; loopTwo++){
  63. while ((a.mainScramble[loopTwo][0] == a.mainScramble[loopTwo-2][0]) || (a.mainScramble[loopTwo][0]) == a.mainScramble[loopTwo-1][0]){
  64. a.mainScramble[loopTwo][0]=moves[getRand(5,0)];
  65. }
  66. }
  67. //Scramble generation complete
  68. }
  69. int getRand(int upr, int lwr){
  70. int randNum;
  71. randNum=(rand() % (upr - lwr + 1)) + lwr;
  72. return randNum;
  73. }
  74. char *printData () {
  75. char *result = malloc(100);
  76. int offset = 0;
  77. for (int loop = 0; loop < SLEN; loop++) {
  78. offset += snprintf(result + offset, 100 - offset, "%s ", a.mainScramble[loop]);
  79. }
  80. return result;
  81. }