Advertisement
kknndd_

Untitled

May 7th, 2021
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include "mastermind.h"
  2. #include "arduino.h"
  3.  
  4. int* generate_code(bool repeat, int length) {
  5.  
  6. if (length < 1 || (!repeat && length >= 10)) {
  7. return NULL;
  8. }
  9.  
  10. int *code = (int*) malloc ( length );
  11.  
  12. if (repeat) {
  13. for (byte i = 0; i < length; i++) {
  14. code[i] = random(1, 9+1);
  15. }
  16. } else {
  17. for (byte i = 0; i < length; i++) {
  18. int cislo = random(1, 9+1);
  19. if (i != 0) {
  20. while ( true ) {
  21. bool is_found = false;
  22. for (byte j = 0; j < i; j++) {
  23. if (code[j] == cislo) {
  24. is_found = true;
  25. cislo = random(1, 9+1);
  26. break;
  27. }
  28. }
  29. if (!is_found) {
  30. break;
  31. }
  32. }
  33. }
  34. code[i] = cislo;
  35. }
  36. }
  37.  
  38. return code;
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement