i am building a classic project in C, black jack. everything was fine until i wanted to implement replay ability in the game so you can play again so the whole game is surrounded now with a while loop that checks if end == false it continues unless the user changes end to true the loop breaks.
now my idea was that an outside function will take care of taking the response from the user if he wants to play again, if yes 'y' the function returns false, and if no 'n' it returns true (the function is called "bool play_again()").
the problem here is when i went back to the main function and tried to assign the bool value of end like this end = playagain(); so whatever the function returns it gets stored in the bool value end which controls the while loop.
i keep facing the problem of previous implicit declaration of 'play_again'.
don't bother with the code of the game itself, the problem occures in the if else statement that checks for the winner, the code above it is just for picking random cards.
this is the code for the game including the play_again function.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
bool end = false;
while (end == false)
{
int cards[] = {11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10};
srand(time(0));
int rand_card1 = rand() % 13;
int rand_card2 = rand() % 13;
int rand_card3 = rand() % 13;
int rand_card4 = rand() % 13;
int user_cards[] = {cards[rand_card1], cards[rand_card2]};
int comp_cards[] = {cards[rand_card3], cards[rand_card4]};
int user_total = user_cards[0] + user_cards[1];
int comp_total = comp_cards[0] + comp_cards[1];
printf("\nyour cards are %d and %d", user_cards[0], user_cards[1]);
printf("\nyour total is %d", user_total);
printf("\n\ncomputer's first card is %d", comp_cards[0]);
printf("\n======================================");
printf("\n\nwanna draw another card y or n? ");
char answer;
scanf("%c", &answer);
fflush(stdin);
printf("\n\n======================================");
if (answer == 'y')
{
int rand_Card = rand() % 13;
int user_cards[] = {cards[rand_card1], cards[rand_card2], cards[rand_Card]};
user_total = user_cards[0] + user_cards[1] + user_cards[2];
printf("\nyour cards are %d and %d and %d", user_cards[0], user_cards[1], user_cards[2]);
printf("\nyour total is %d", user_total);
if (comp_total < 12)
{
int rand_Card = rand() % 13;
int comp_cards[] = {cards[rand_card3], cards[rand_card4], cards[rand_Card]};
comp_total = comp_cards[0] + comp_cards[1] + comp_cards[2];
printf("\n\ncomputer's first two cards are %d and %d", comp_cards[0], comp_cards[1]);
}
else
{
printf("\n\ncomputer's first card is %d", comp_cards[0]);
}
}
else if (answer == 'n')
{
printf("\nyour cards are %d and %d", user_cards[0], user_cards[1]);
printf("\nyour total is %d", user_total);
if (comp_total < 12)
{
int rand_Card = rand() % 13;
int comp_cards[] = {cards[rand_card3], cards[rand_card4], cards[rand_Card]};
comp_total = comp_cards[0] + comp_cards[1] + comp_cards[2];
printf("\n\ncomputer's first two cards are %d and %d", comp_cards[0], comp_cards[1]);
}
else
{
printf("\n\ncomputer's first card is %d", comp_cards[0]);
}
}
printf("\n\n======================================");
printf("\nuser total is %d", user_total);
printf("\ncomp total is %d", comp_total);
if (user_total == 21)
{
printf("\nBlack jack!! you win");
end = play_again();
}
else if (comp_total == 21)
{
printf("\nBlack jack!! computer wins");
end = play_again();
}
else if (comp_total == 21 && user_total == 21)
{
printf("\nBlack jack!! both won (Rare case)");
end = play_again();
}
else if (user_total > 21)
{
printf("\nyou lose");
end = play_again();
}
else if (comp_total > 21)
{
printf("\ncomputer loses");
end = play_again();
}
else if (user_total > comp_total && user_total < 21)
{
printf("\nyou win");
end = play_again();
}
else if (comp_total > user_total && comp_total < 21)
{
printf("\ncomputer wins");
end = play_again();
}
}
return 0;
}
bool play_again()
{
bool end2 = false;
while (end2 == false)
{
printf("\n\n======================================");
printf("\nwanna play again, y or n? ");
char ans;
scanf("%c", &ans);
fflush(stdin);
if (ans == 'y')
{
end2 = true;
return false;
}
else if (ans == 'n')
{
end2 = true;
return true;
}
else
{
printf("\nenter a valid answer, y or n?? ");
}
}
}
any advice will help, thanks in advance.