Hello, it's been a long time. Taking into consideration your suggestions I came up with the following code:
Code:
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <stdbool.h>
# pragma warning (disable:4996)
void ticket_init(int *ticket1);
void lotto_nums_gen(int *arr);
void shuffle(int *a);
void print_user(int *usr);
void game_main(int *usr, int *pc);
int no_null(int *arr);
void welcome_screen();
# define LOTTO_SIZE 40
# define TICKET_SIZE 6
int main()
{
srand(unsigned(time(NULL)));
int lotto[LOTTO_SIZE] = { 0, };
int *p_lotto = lotto;
int ticket[TICKET_SIZE] = { 0, };
int *p_ticket = ticket;
welcome_screen();
ticket_init(ticket);
lotto_nums_gen(p_lotto);
print_user(p_ticket);
game_main(p_ticket, p_lotto);
system("pause");
return 0;
}
void welcome_screen()
{
printf("========================================\n");
printf("======= WELCOME TO LOTTO GAME ========\n");
printf("========================================\n");
printf("=== Enter (%d) numbers to play(1-40) ===\n", TICKET_SIZE);
printf("== Wait until your ticket is complete ==\n");
printf("========================================\n");
}
void ticket_init(int *ticket1)
{
printf("Input: ");
for (int i = 0; i < TICKET_SIZE; i++)
{
while (true)
{
scanf("%d", &ticket1[i]);
if (ticket1[i] <= 0 || ticket1[i] > 40)
{
printf("Input (%d) is not within the range(1-40)\nEnter (%d out of %d) again:\n", ticket1[i], i+1, TICKET_SIZE);
}
else
{
printf("Input (%d out of %d) is successful\n", i + 1, TICKET_SIZE);
break;
}
}
}
}
void game_main(int *usr, int *pc)
{
int dup_n[TICKET_SIZE] = { 0, };
int *p_dup_n = dup_n;
int counter = 0;
int non_zero_cnt = 0;
while (true)
{
++counter;
printf("counter:(%d)...\n", counter);
print_user(usr);
shuffle(pc);
for (int i = 0; i < TICKET_SIZE; i++)
{
for (int j = 0; j < TICKET_SIZE; j++)
{
if (usr[i] == pc[j])
{
p_dup_n[i] = usr[i];
printf("Storing numbers:");
for (int i = 0; i < TICKET_SIZE; i++)
printf("%d ", p_dup_n[i]);
putchar('\n');
}
}
}
non_zero_cnt = no_null(p_dup_n);
if (non_zero_cnt > 0)
{
printf("No zero values found...\nExited with counter(%d)\n", counter);
break;
}
else
{
printf("Zero values exist...\nlooping again...\n");
continue;
}
}
}
void print_user(int *usr)
{
printf("USER: ");
for (int i = 0; i < TICKET_SIZE; i++)
printf("%d ", usr[i]);
putchar('\n');
}
void lotto_nums_gen(int *a)
{
for (int i = 0; i < LOTTO_SIZE; i++)
{
a[i] = i + 1;
printf("%d ", a[i]);
}
}
void shuffle(int *a)
{
for (int i = LOTTO_SIZE; i > 1; )
{
int r = rand() % i--;
int t = a[i]; a[i] = a[r]; a[r] = t;
}
printf("\nSHUFFLE: ");
for (int i = 0; i < TICKET_SIZE; i++)
printf("%d ", a[i]);
putchar('\n');
}
int no_null(int *a)
{
int cnt = 0;
for (int i = 0; i < TICKET_SIZE && cnt < TICKET_SIZE; i++)
if (a[i] != 0)
cnt++;
return cnt == TICKET_SIZE ? 1 : 0;
}