5 Replies - 1205 Views - Last Post: 28 November 2012 - 11:46 AM Rate Topic: -----

#1 Dwaltx87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-November 12

Shuffle and deal problem for war card game program

Posted 27 November 2012 - 08:00 PM

im having some serious issues trying to find out how to shuffle these two arrays simultaneously for this program i have looked everywhere and i am getting zero help i can't find anything that helps i am using C code and everything i find is C++


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

#define DECK 52                           /*Defining arrays*/
#define SUIT 52                      
#define PCARDS 52
#define PSUITS 52
#define CCARDS 52
#define CSUITS 52

void welcome(void);                      /*function prototypes*/

int main(void)
{
    char values[] = "23456789TJQKA";
    char suits[] = "CDHS";
    
    int rankcards(char value, char suit)         /*rank cards*/
    {
        int i,j;
        for(i=0;i<(DECK/SUIT);i++)
            if(values[i]==value)
                for(j=0;j<SUIT;j++)
                   if(suits[j]==suit)
               
    return(i*SUIT+j);
    }
    
    char suit(int card)
    {
         return( suits[card%SUIT]);
    }

    char value(int card)
    {    
         return(values[card/SUIT]);
    }    
    

  welcome();                 /*greeting function call*/
  
  printf("Press d to shuffle and deal. \n\nPress f to flip the cards. \n\nPress q to quit. \n\n");
 
  char choice=' ';
  choice=getch();
  
  if(choice=='d')
       /*shuffle function call*/
      /*function to deal*/
  if(choice=='f')
    /*function to flip card and determine who wins between the cards and war initiator function*/
  if(choice=='q')
     printf("Thank you for playing, have a nice day!");          /*terminate program*/
     exit(1);
                                                                                       
  
  system("PAUSE");	
  return 0;
}
 

void welcome(void)                                                                               /*Program Greeting*/
{
     printf("Welcome, this is a program that generates the card game War.\n");
     printf("You will play against a computer. Good luck and have fun!\n\n");
}
     
     



Is This A Good Question/Topic? 0
  • +

Replies To: Shuffle and deal problem for war card game program

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Shuffle and deal problem for war card game program

Posted 27 November 2012 - 08:11 PM

You cannot define functions within functions.

Review basic functions here: http://www.cplusplus...rial/functions/
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Shuffle and deal problem for war card game program

Posted 28 November 2012 - 05:08 AM

#define DECK 52                           /*Defining arrays*/
#define SUIT 52


for(i=0;i<(DECK/SUIT);i++)


A for loop will only run as long as the middle expression, in this case i < (DECK/SUIT) is true. How many times will this loop run, given the #defines?

We get lots of deck shuffling questions; the illustrious baavgai I know has demonstrated this in the past. I'd be very surprised if you couldn't find what you're looking for by searching the C/C++ Forum.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Shuffle and deal problem for war card game program

Posted 28 November 2012 - 05:45 AM

What can I tell you, I like cards.

I don't see a deck defined anywhere here. That would be what you'd need first.

I'd start with something like:
/* why the hell are these all 52?!? */

/* fine */
#define DECK_SIZE 52
/* not so good
#define SUIT 52
#define PCARDS 52
#define PSUITS 52
#define CCARDS 52
#define CSUITS 52

Let's stop right now.  There are 4 suits in a deck of standard cards
consisting of 13 "ranks" or "faces" or "values" or "types" of card.
I like face, because it pretty much doesn't clash with anything else.
*/

#define SUIT_SIZE 4
#define FACE_SIZE 13

typedef int Card;
typedef Card Deck[DECK_SIZE];

/*function prototypes*/

void welcome(void);
int getSuitValue(Card);
int getFaceValue(Card);
char getSuitName(Card);
char getFaceName(Card);
void initDeck(Deck);
void shuffleDeck(Deck);
void flipCards(Deck);
char menu();


Was This Post Helpful? 1
  • +
  • -

#5 Dwaltx87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-November 12

Re: Shuffle and deal problem for war card game program

Posted 28 November 2012 - 11:35 AM

They are all 52 because thats what my professor wanted so there are 52 slots for value for the deck player and computer hand and the same for the suit but this is my code now and i can't figure out how to randomly shuffle in a for loop.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>

#define DECK 52                           /*Defining arrays*/
#define SUIT 52                      
#define PCARDS 52
#define PSUITS 52
#define CCARDS 52
#define CSUITS 52

void welcome(void);                      /*function prototypes*/
void shuffle(int[],char[]);


int main(void)
{
    char values[] = "2 3 4 5 6 7 8 9 10 11 12 13 14";
    char suits[] = "C D H S";
    
    int a[DECK];                                       /*Not the best way to declare the arrays but i honestly couldn't figure it out*/
    int i;
    for(i=0; i<DECK; i++)
       a[1]=2; 
       a[2]=3;
       a[3]=4;
       a[4]=5;
       a[5]=6;
       a[6]=7;
       a[7]=8;
       a[8]=9;
       a[9]=10;
       a[10]=11;
       a[11]=12;
       a[12]=13;
       a[13]=14;
       a[14]=2;
       a[15]=3;
       a[16]=4;
       a[17]=5;
       a[18]=6;
       a[19]=7;
       a[20]=8;
       a[21]=9;
       a[22]=10;
       a[23]=11;
       a[24]=12;
       a[25]=13;
       a[26]=14;
       a[27]=2;
       a[28]=3;
       a[29]=4;
       a[30]=5;
       a[31]=6;
       a[32]=7;
       a[33]=8;
       a[34]=9;
       a[35]=10;
       a[36]=11;
       a[37]=12;
       a[38]=13;
       a[39]=14;
       a[40]=2;
       a[41]=3;
       a[42]=4;
       a[43]=5;
       a[44]=6;
       a[45]=7;
       a[46]=8;
       a[47]=9;
       a[48]=10;
       a[49]=11;
       a[50]=12;
       a[51]=13;
       a[52]=14;
       
    char b[SUIT];
    int j;
    for(j=0; j<SUIT; j++)
       b[1]='C'; 
       b[2]='C';
       b[3]='C';
       b[4]='C';
       b[5]='C';
       b[6]='C';
       b[7]='C';
       b[8]='C';
       b[9]='C';
       b[10]='C';
       b[11]='C';
       b[12]='C';
       b[13]='C';
       b[14]='D';
       b[15]='D';
       b[16]='D';
       b[17]='D';
       b[18]='D';
       b[19]='D';
       b[20]='D';
       b[21]='D';
       b[22]='D';
       b[23]='D';
       b[24]='D';
       b[25]='D';
       b[26]='D';
       b[27]='H';
       b[28]='H';
       b[29]='H';
       b[30]='H';
       b[31]='H';
       b[32]='H';
       b[33]='H';
       b[34]='H';
       b[35]='H';
       b[36]='H';
       b[37]='H';
       b[38]='H';
       b[39]='H';
       b[40]='S';
       b[41]='S';
       b[42]='S';
       b[43]='S';
       b[44]='S';
       b[45]='S';
       b[46]='S';
       b[47]='S';
       b[48]='S';
       b[49]='S';
       b[50]='S';
       b[51]='S';
       b[52]='S';

  welcome();                 /*greeting function call*/
  
  printf("Press d to shuffle and deal. \n\nPress f to flip the cards. \n\nPress q to quit. \n\n");
 
  char choice=' ';
  choice=getch();
  
  if(choice=='d')
       shuffle(a,B)/>;            /*shuffle and deal function call*/
      
  if(choice=='f')
       /*function to flip card and determine who wins between the cards and war initiator function*/
    
  if(choice=='q')
     printf("Thank you for playing, have a nice day!");          /*terminate program*/
     
     system("PAUSE");
     exit(1);
     
  	
  return 0;
}
 

void welcome(void)                                                                               /*Program Greeting*/
{
     printf("Welcome, this is a program that generates the card game War.\n");
     printf("You will play against a computer. Good luck and have fun!\n\n");
}
     
void shuffle(int a[],char b[])
{
    int n=0;
    int temp;
    srand(time(NULL));
    for(n=0;n<1000;n++)
        
        
    
            
}
     


Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Shuffle and deal problem for war card game program

Posted 28 November 2012 - 11:46 AM

int main(void) {
	/* only thing spaces get you is confusion */
	char values[] = "2 3 4 5 6 7 8 9 10 11 12 13 14";
	char suits[] = "C D H S";

	int a[DECK];
	int i;
	for(i=0; i<DECK; i++)
		a[1]=2; 
	/* in the above, you just loaded a[1] with 2 DECK times.
	Below, assign 14 to a[52], which doesn't exist.
	*/
		a[52]=14;



Arrays start at 0 and end at size-1. Your a has positions 0..51. Why are you loading 2..14 for card values? How does that make sense?

Using parallel arrays is just a completely bad idea. I wouldn't even bother trying to shuffle that.

Shuffling is simple. You seem to be gearing up for the brute force approach, with 1000. In such a method, you pick two positions at random and swap them. In your case, you'll have to do that with both arrays at the same time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1