Dasturiy injinering
Download 198.17 Kb.
|
1 2
Bog'liqamaliy ish dif
- Bu sahifa navigatsiya:
- } else mastermind(10, secret_code(NULL)); } if(ac == 5){ if(my_strcmp("-c", argv[1]) == 0 digit(argv[2]) str_len(argv[2]) == 4){
- } else mastermind(10, secret_code(NULL)); } } 3. Xulosa: Mastermind
KODI: #include #include #include #include #include #ifndef my_mastermind #define my_mastermind int str_int(char *); void *my_strcpy(char *, char *); int str_len(char *); int my_strcmp(char* , char *); bool digit_r (char *); bool digit(char *); int *integer_arr(char *); int *secret_code(char *); int wellplaced_p(int *, int *); int misplaced_p(int *, int *); void mastermind(int, int *); #endif int str_int(char *p){ return atoi(p); } void* my_strcpy(char* param_1, char* param_2){ int i; i = 0; while(param_2[i] != '\0'){ param_1[i] = param_2[i]; i++; } return 0; } int str_len(char *param_1){ int count = 0; for(int i = 0; param_1[i] != '\0'; i++){ count++; } return count; } int my_strcmp(char* param_1, char* param_2){ while((* param_1 != '\0' && *param_2 != '\0') && *param_1 -*param_2 == 0){ param_1++; param_2++; } return *param_1 - *param_2; } bool digit_r(char *param_1){ for(int i = 0; param_1[i] != '\0'; i++){ if(!(param_1[i] >= '0' && param_1[i] <= '9')){ return false; } } return true; } bool digit(char* param_1){ for(int i = 0; param_1[i] != '\0'; i++){ if(!(param_1[i] >= '0' && param_1[i] <= '7')){ return false; } } return true; } int *integer_arr(char *p){ int *r = malloc(4); int num = str_int(p); r[0] = num / 1000; r[1] = (num - r[0]*1000)/100; r[2] = (num - r[0]*1000 - r[1]*100)/10; r[3] = (num - r[0]*1000 - r[1]*100 - r[2]*10); return r; } int *secret_code(char *p){ int *r = malloc(4); if(p == NULL) { srand (time(NULL)); r[0] = rand() % 8; r[1] = rand() % 8; r[2] = rand() % 8; r[3] = rand() % 8; } else { r = integer_arr(p); } return r; } int wellplaced_p(int *arr, int *ans){ int count=0; for(int l=0; l<4; l++){ if(ans[l] == arr[l]) count++; } return count; } int misplaced_p(int *arr, int *ans){ int r_count = 0, count[4] = {0, 0, 0, 0};; for (int i = 0; i < 4; i++) { for (int l=0; l<4; l++){ if (ans[i] == arr[l]) count[i]++; } } for (int l=0; l < 4; l++){ if(count[l]>=1) r_count++; } return r_count - wellplaced_p(arr, ans); } void mastermind(int t, int *arr){ printf("Will you find the secret code?\n"); for (int l=0; l < t; l++){ char *answer; printf("Round %d\n> ", l); scanf("%s", answer); if(digit(answer) && str_len(answer) == 4){ int *ans = integer_arr(answer); int wellplaced_count = wellplaced_p(arr, ans); int misplaced_count = misplaced_p(arr, ans); if(wellplaced_count == 4){ printf("Congratz! You did it!"); break; }else{ printf("Well placed pieces: %d\n", wellplaced_count); printf("Misplaced pieces: %d\n\n", misplaced_count); } } else { printf("Wrong input!\n\n"); } } } int main(int ac, char **argv){ if(ac == 1) mastermind(10, secret_code(NULL)); if(ac == 3) { if(my_strcmp("-c", argv[1]) == 0 && digit(argv[2]) && str_len(argv[2]) == 4){ mastermind(10, secret_code(argv[2])); } else mastermind(10, secret_code(NULL)); } if(ac == 5){ if(my_strcmp("-c", argv[1]) == 0 && digit(argv[2]) && str_len(argv[2]) == 4){ if(my_strcmp("-t", argv[3]) == 0 && digit_r(argv[4])){ mastermind(str_int(argv[4]), secret_code(argv[2])); } else mastermind(10, secret_code(argv[2])); } else mastermind(10, secret_code(NULL)); } } 3. Xulosa: Mastermind o’yinining mobil ilovasining dasturiy taminot moduli tuzildi.O’yin barcha shu sohaga qiziquvchilar uchun zo’r qilib ishlandi. Foydalingan adabiyotlar va tarmoqlar 1.https://codereview.stackexchange.com/questions/138339/a-simple-mastermind-game-in-c Download 198.17 Kb. Do'stlaringiz bilan baham: |
1 2
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling