C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-15-2009, 05:05 PM   #1
Registered User
 
Join Date: Oct 2009
Location: British Columbia, B.C.
Posts: 16
Module programming with one dim. array

Hi,

I'm writing a program for blackjack and am using an array with and index of 53 the first 52 spaces are for the cards and the 53rd is for keeping track of how many cards are left in the "deck".
I have two function prototypes one is to "Initialize_Deck" (initialize the array) and another is to "Shuffle_Deck". I want the program to do these two functions as soon as the player decides that yes they want to play the game. How can I do this by calling the functions in the beginning of the main?? Is this possible?
melodia is offline   Reply With Quote
Old 11-15-2009, 06:09 PM   #2
Registered User
 
Join Date: Oct 2009
Location: British Columbia, B.C.
Posts: 16
/* function prototypes */
void clean (void);
char GetTestChoice();
void Initialize_Deck(int deck[]);
int Random_Number();
void Shuffle_Deck(int deck[]);
int Draw_One_Card(int deck[]);
void Display_Card(int card);
int Score_Card(int card);
int Users_Turn(int deck[]);
int Continue();
#define QUIT 'Q'
#define PLAY_GAME 'P'



#define DECK_SIZE 53
int deck[DECK_SIZE];
/************************************************** *******************

************************************************** *******************/

int main()
{

int choice;

printf(" Welcome! I'm happy to have someone to play blackjack with!\n\n");

do
{
choice = GetTestChoice();

switch (choice)
{
case PLAY_GAME:

Initialize_Deck(int deck[]);
Shuffle_Deck(int deck[]);

Users_Turn(int deck[]);


break;



default:
if (choice != QUIT)
printf("\n That is NOT a valid choice. Please try again...\n");
break;

} /* end switch */

} while (choice != QUIT);
printf("Finishing the program.\n");
system("PAUSE");
return 0;

}

void clean (void)
{
char dummy;
do
{
scanf("%c",&dummy); /* read a single char */
} while(dummy != '\n'); /* if it was the NEWLINE we are done */
}



char GetTestChoice()
{
char choice;

printf("\n\n Would you like to continue?\n");

printf("%c: Quit the game.\n", QUIT);
printf("%c: Play the game", PLAY_GAME);

scanf("%c", &choice);
clean(); /* empty the input buffer */
printf("\n\n");

return choice;
}

void Initialize_Deck(int deck[])
{
int i;
for (i=0; i < DECK_SIZE; i++)
{
deck[i]=i;
}
}


void Shuffle_Deck(int deck[])
{
int i;
int num;
int temp;

for (i=51; i > 0; i--)
{
num=Random_Number(i);
temp=deck[num];
deck[num]=deck[i];
deck[i]=temp;
}
}
int Users_Turn(int deck[])
{
int card;
int totalscore=0;
int score;
int choice;
do
{
choice=Continue();
while(totalscore < 21 && choice == 1)
{
do
{
card=Draw_One_Card(deck);
Display_Card(card);
score=Score_Card(card);
totalscore=totalscore+score;
printf("Your total score so far in this hand is: %i", totalscore);
}
}

}




int Random_Number(int upper)
{
return rand()%(upper+1);
}

int Draw_One_Card(int deck[])
{
if (deck[52]==0)
{
Shuffle_Deck(deck);
}
deck[52]--;

return (deck[deck[52]]);
}

void Display_Card(int card)
{

int suit = card/13;
int rank = card%13;
rank=rank+1;

switch(rank)
{
case 1:
printf("A");
break;

etc.....
}

switch(suit)
{
case 0:
printf("S");
break;
case 1:
printf("H");
break;
case 2:
printf("D");
break;
case 3:
printf("C");
break;
}
}
int Score_Card(int card)
{
int rank;
rank=card%13 + 1;

switch(rank)
{
case 1:
return rank;
break;

etc......
}


int Continue()
{
int choice;
printf("Would you like to draw a card?\n Enter 1 to draw:\n Enter 2 to not draw:\n");
scanf("%i", &choice);
clean();
return choice;
}

Last edited by melodia; 11-15-2009 at 06:32 PM. Reason: .
melodia is offline   Reply With Quote
Old 11-15-2009, 06:19 PM   #3
Registered User
 
Join Date: Oct 2009
Location: British Columbia, B.C.
Posts: 16
The only errors I am getting when compiling are in this function:
Code:
 
int Users_Turn(int deck[])
  {
       int card;
       int totalscore=0;
       int score;
       int choice;
       do
       {
       choice=Continue();
        }
       while(totalscore < 21 && choice == 1)
       {
           card=Draw_One_Card(deck);
           Display_Card(card);
           score=Score_Card(card);
           totalscore=totalscore+score;
           printf("Your total score so far in this hand is: %i", totalscore);
          
        }
  
  }
The compiler says " syntax error before '}' token"
which is the bracket following the while statement.

as well as syntax errors "before int" in all of the three function 'calls' in the beginning of the program where:

Initialize_Deck, Shuffle_Deck, and User_Turn being called in the switch statements.

Sorry for the multiple posts. I messed up and didn't want to leave it confusing. I've been working on this for the last week and a half morning noon and night. I thought a different prospective might help solve this...

Thanks again,

-Melodia

Last edited by melodia; 11-15-2009 at 06:27 PM. Reason: made an error in compiler message
melodia is offline   Reply With Quote
Old 11-15-2009, 06:44 PM   #4
Registered User
 
Join Date: Apr 2006
Posts: 1,193
The while loop error is because you can't seem to decide if you want a while loop or do while loop. If do while, you're missing a semicolon at the end. if while, then you need to get rid of the do.

The other errors are because the correct way to call those functions is simply "Initialize_Deck(deck);".
__________________
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.

Last edited by King Mir; 11-15-2009 at 06:47 PM.
King Mir is offline   Reply With Quote
Old 11-15-2009, 07:09 PM   #5
Registered User
 
Join Date: Oct 2009
Location: British Columbia, B.C.
Posts: 16
Returning value with switch statements

Thank you for the reply, I managed to fix the while loop at the top but now the ridiculous thing has a problem with the switch statement for "scoring the card"
Code:
int Score_Card(int card)
 {
     
     int rank=card%13 + 1;
     
     switch(rank)
     {
      case 1:
           return rank;
           break;
     
      case 2:
           return rank;
           break;
      
      case 3:
           return rank;
           break;
      
      case 4:
           return rank;
           break;
      
      case 5:
           return rank;
           break;
      
      case 6:
           return rank;
           break;
      
      case 7:
           return rank;
           break;
      
      case 8:
           return rank;
           break;
      
      case 9:
           return rank;
           break;
      
      case 10:
           return rank;
           break;
      
      case 11:
           return 10;
           break;
      
      case 12:
           return 10;
           break;
      
      case 13:
           return 10;
           break;
           }
     
}
I was under the impression that you could use this method of obtaining a return value through switch statements. However, the compiler says that "control reaches end of nonvoid function"

??????

is what this function says to do not allowed?Or am I missing some silly small thing??
melodia is offline   Reply With Quote
Old 11-15-2009, 07:23 PM   #6
Registered User
 
Join Date: Apr 2006
Posts: 1,193
That should be a warning, not an error.

The problem is that if card is not 1 though 13, then it doesn't know what to do. You can fix the warning by adding some kind of default behavior.

Also, instead of having a much of cases that repeat the same code you can do this:
Code:
switch(rank)
     {
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
      case 10:
           return rank;
           break;
      
      case 11:
      case 12:
      case 13:
           return 10;
           break;
           }
__________________
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
King Mir is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Searching Array for Longest Sequence of an Element w2look C Programming 7 11-25-2008 01:50 AM
2 dim array mrsirpoopsalot C Programming 4 09-15-2006 12:51 PM
1-D array jack999 C++ Programming 24 05-12-2006 07:01 PM
Quick question about SIGSEGV Cikotic C Programming 30 07-01-2004 07:48 PM
Array Program emmx C Programming 3 08-31-2003 12:44 AM


All times are GMT -6. The time now is 07:52 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22