I've posted my C code below for a card game (yep, i'm still on it). Now the neg() function is called from the turn() function. so using return; sends it back there, but i want it to go back to the start of the main function (basicaly start again). As with in the eval function i want it to go back to the beginning of the main function if the input is y.
Also when n is the input, i want the program to just print the line "ok see ya" then finish, but it keeps looping either to the turn function or the eval. :cry:
ignore the "path" bits and functions for now. that's for when images are used in java..
#include <stdio.h>
#include<time.h>
/*functions i'm going to use*/
void turn(int total, int other);
int draw(int total, int other);
int neg(int total, int other);
int eval(int total, int other);
/*variable for path*/
int path;
int main()
{
/*variables for cards*/
int card3;
int total;
int other;
/*intializing other with the random function*/
srand(time(NULL));
other=(rand()%22)+1;
/*letters of suit, plus random to choose which, then variables to store suit*/
char type[4]={'S','D','H','C'};
int t=(rand()%3)+1;
int u=(rand()%3)+1;
char suit=type[t];
char suit2=type[u];
/*values of cards and random to choose one, then variables to store card value*/
int set[8]={2,3,4,5,6,7,8,9};
int i=(rand()%7)+1;
int j=(rand()%7)+1;
int card=set[i];
int card1=set[j];
/*total of two cards*/
total = card+card1;
printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");
/*To display first card, using switch to match suit then go to each function to
find "path" of image, for when images used*/
switch(suit)
{
case 'S' : printf("First card is %d of Spades", card); spade(i);
break;
case 'D' : printf("First card is %d of Diamonds", card);
diamond(i); break;
case 'H' : printf("First card is %d of Hearts", card);
heart(i); break;
case 'C' : printf("First card is %d of Clubs", card);
heart(i); break;
}
/*show path value, just to see if it worked really*/
printf("path is %d\n", path);
/*switch for second card, same as above*/
switch(suit2)
{
case 'S' : printf("\tSecond card is %d of Spades\n", card1);
spade(j); break;
case 'D' : printf("\tSecond card is %d of Diamonds\n", card1);
diamond(j); break;
case 'H' : printf("\tSecond card is %d of Hearts\n", card1);
heart(j); break;
case 'C' : printf("\tSecond card is %d of Clubs\n", card1);
heart(j); break;
}
/*show total*/
printf("The total is: %d\n", total);
/*show path value, just to see if it worked really*/
printf("path is %d\n", path);
/*go to eval function*/
eval(total, other);
fputs("after eval", stdout);
return 0;
}
/*4 functions which will be used to find image path, in java game if that will
work*/
int spade(int p)
{
int set[8]={12,13,14,15,16,17,18,19};
path=set[p];
return path;
}
int diamond(int p)
{
int set[8]={12,13,14,15,16,17,18,19};
path=set[p];
return path;
}
int heart(int p)
{
int set[8]={12,13,14,15,16,17,18,19};
path=set[p];
return path;
}
int club(int p)
{
int set[8]={12,13,14,15,16,17,18,19};
path=set[p];
return path;
}
/*eval function which looks at the total made in main and decides where to go*/
int eval(int total, int other)
{ char again;
if (total==21)
{
printf("Waho! WINNER!!!!!!!!\n Play again?");
/*get response, clear newline as well*/
again = getchar();
while (getchar() != '\n');
/*(if they say yes, go back to main, if no exit*/
switch(again)
{ case 'y' : return; break;
case 'n' : printf("ok, see ya!\n"); break;
}
}
/*if total less than 21 go to turn function*/
while(total<21)
{ turn(total, other); }
/*If total bigger than 21 offer to play again*/
if (total>21);
{ printf("BUST! play again?\n");
/*get response, clear newline as well*/
again = getchar();
while (getchar() != '\n');
/*(if they say yes, go back to main, if no exit*/
switch(again)
{ case 'y' : return; break;
case 'n' : printf("ok, see ya!\n"); break;
}
}
}
/*turn function*/
void turn(int total, int other)
{ char ans;
/*get response, clearing newline*/
printf("Twist or Stick? (t/s)\n");
ans = getchar();
while (getchar() != '\n');
/*if "twist" (draw another card) go to draw function, if "stick" go to neg
function*/
switch(ans)
{ case 't' : draw(total, other); break;
case 's' : neg(total, other); break;
}
}
/*draw function*/
int draw(int total, int other)
{
/*letters of suit, plus random to choose which then variables to store suit*/
char type[4]={'S','D','H','C'};
int t=(rand()%3)+1;
char suit=type[t];
/*values of cards and random to choose one, then variables to store card value*/
int set[8]={2,3,4,5,6,7,8,9};
int i=(rand()%7)+1;
int card=set[i];
/*switch block to decide which suit to show as in main*/
switch(suit)
{
case 'S' : printf("Next card is %d of Spades", card); spade(i);
break;
case 'D' : printf("Next card is %d of Diamonds", card);
diamond(i); break;
case 'H' : printf("Next card is %d of Hearts", card);
heart(i); break;
case 'C' : printf("Next card is %d of Clubs", card);
heart(i); break;
}
/*total variable from above now equales total + card three*/
total+=card; printf("\nNew total: %d\n", total);
/*go back to the "eval" function with the new total
*/
eval(total, other);
}
/*neg function*/
int neg(int total, int other)
{ char win, lose;
/*if total is bigger than the other in the main function then say you won etc*/
if(total > other)
{ printf("Opponents cards: %d\n", other);
printf("you won! play again?\n");
win = getchar();
while (getchar() != '\n');
/*if the answer is yes, go back to main, if no finish. except it returns to the
turn function*/
switch(win)
{ case 'y' : return; break;
case 'n' : printf("ok, see ya!\n"); break;
}
}
/*if other in main is bigger than total say you lose etc*/
else if (other > total)
{printf("Opponents cards: %d\n", other);
printf("damn nice try. play again?\n");
lose = getchar();
while (getchar() != '\n');
/*if the answer is yes, go back to main, if no finish*/
switch(lose)
{ case 'y' : return main; break;
case 'n' : printf("ok, see ya!\n"); break;
}
}
}