Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mastermind.h"
- #include "arduino.h"
- int* generate_code(bool repeat, int length) {
- if (length < 1 || (!repeat && length >= 10)) {
- return NULL;
- }
- int *code = (int*) malloc ( length );
- if (repeat) {
- for (byte i = 0; i < length; i++) {
- code[i] = random(1, 9+1);
- }
- } else {
- for (byte i = 0; i < length; i++) {
- int cislo = random(1, 9+1);
- if (i != 0) {
- while ( true ) {
- bool is_found = false;
- for (byte j = 0; j < i; j++) {
- if (code[j] == cislo) {
- is_found = true;
- cislo = random(1, 9+1);
- break;
- }
- }
- if (!is_found) {
- break;
- }
- }
- }
- code[i] = cislo;
- }
- }
- return code;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement