Thread: array and pointing randomly

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    41

    array and pointing randomly

    so this is my problem:
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #define DECKSIZE 52
    #define VALUE 9
    #define FACE 4
    
    typedef struct {
            int value;
            char* suit;
            char* name;
    }Card;
    
    Card cards[DECKSIZE];
    char *faceName[]={"two","three","four","five","six","seven","eight","nine",
                      "ten","jack","queen","king","ace"};
    char *suitName[]={"spades","diamonds","hearts","clubs"};
    void printDeck(){
    int i;
    for(i=0;i<DECKSIZE;i++){
                    printf("%s of %s \n",cards[i].name,cards[i].suit);
                    if((i+1)%4==0  && i!=0) printf("--------------------\n\n");
    }
    }
    void shuffleDeck(){
    srand(time(NULL));
    int this;
    int that;
    Card temp;
    int i;
    for (i=0;i<10;i++){
            this=rand()%DECKSIZE;
            that=rand()%DECKSIZE;
    while(this==that)that=rand()%(52+1);
    //printf("shuffle  card%d with card %d\n", this, that);
    temp=cards[this];
    cards[this]=cards[that];
    cards[that]=temp;
    }
    }
    int main(){
            int suitCount=0;
            int faceCount=0;
            int i;
            for(i=0;i<DECKSIZE;i++){
                    if(faceCount<9){
                    cards[i].value=faceCount+2;
                    }else{
                            cards[i].value=10;
                    }
                    cards[i].suit=suitName[suitCount];
                    cards[i].name=faceName[faceCount++];
                    if(faceCount==13){
                            cards[i].value=11;
                            suitCount++;
                            faceCount=0;
                    }
            }
    {
    
    //printDeck();
    //shuffleDeck();
    //printDeck();
    //printDeck();
    
            }/*
    int c;
    int rdm();
            {printf("To play blackjack press 1:");scanf("%d", &c);}
    if (c==1)
    do{
            printf("this still works\n");
    break;
    }while(c==1);
    if(c!=1)
    do{
            printf("Please only press 1.\n");
    break;
    }while(c<=1);
    */
    int P_hand, D_hand;
    srand(time(NULL));
    P_hand=rand() %52+1;
    D_hand=rand() %52+1;
    printf("%s of %s,----%s of %s\n ",&P_hand, &P_hand,&P_hand,&P_hand );
    
    shuffleDeck();
    printf("%s of %s,----%s of %s\n",&D_hand,faceName[0],suitName[2],faceName[5],suitName[1]);
    
    shuffleDeck();
            return 0;
    }
    
    }}
    ;

    so my question is how do i only print out 2 RANDOM cards on one line and 2 RANDOM cards on another?
    with the part i wrote down i can print out the whole deck in random order with the shuffle function that i wrote. but i just cant figure out how to modify this function to only print out what i need.

    keep in mind that this is a start to a blackjack game so here is a visual of my goal:

    seven of spades, ten of diamonds
    ---------------------------------------------
    six of hearts, four of clubs

    hit or stand?

    let me know if you have any advice
    Last edited by gloworm; 04-12-2010 at 01:03 PM. Reason: Whole code..

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If your deck is shuffled, you don't want random cards, you want the first cards off the top of the deck.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by tabstop View Post
    If your deck is shuffled, you don't want random cards, you want the first cards off the top of the deck.
    so do i just need to change my IF statement? and if so to what? im really new to this.....

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, no. If your deck is shuffled, just print [0], [1], [2], and [3].

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    41
    Quote Originally Posted by tabstop View Post
    Well, no. If your deck is shuffled, just print [0], [1], [2], and [3].
    is there any way to copy my code from ONYX and paste to windows so i can show you the whole code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointing to an array
    By gloworm in forum C Programming
    Replies: 4
    Last Post: 04-11-2010, 11:46 AM
  2. Replies: 6
    Last Post: 11-03-2009, 01:23 PM
  3. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. array of pointers each pointing to an array
    By muttski in forum C Programming
    Replies: 6
    Last Post: 05-01-2002, 02:03 PM

Tags for this Thread