scrambler.c 2.4 KB

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