Thread: Is it possible to create a simple text card game using C programming?

  1. #1
    Registered User
    Join Date
    Apr 2021
    Posts
    5

    Is it possible to create a simple text card game using C programming?

    Hi Everyone, I want to create a simple card game similar to 'war' that battles the user and the 'computer'. I was thinking there could be one round where the cards are shuffled and given to each player, players cards are then evaluated and the winner is chosen for that round.

    Whats the best way to do this? Can someone give me an example?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,580
    Sure, post your latest code.
    But make sure you use "copy as text" in your IDE and/or "paste as text" in your browser, to stop it looking a mess (as you deleted from your other posts).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    5

    Thanks for your reply

    Hi! i'm so sorry I deleted the content, I have no idea what I'm doing on this site, but I'm beginning to get used to it. I decided to make a card game that assigns each player 5 cards, but I have a issue- each time the game is played the players receive the same cards. How can I fix this? I appreciate all the help. I've been working on this for hours and can't figure it out.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    //constants declared
    #define NUMOFCARDS 52
    #define NUMOFPROPERTIES 2
    #define NUMOFSUITS 4
    #define NUMOFFACES 13
    #define PLAYERCARDS 2
    //declaration of arrays
    char *suit[NUMOFSUITS] = { "Hearts", "Spades", "Clubs", "Diamonds" };
    
    char *face[NUMOFFACES] =
        { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
      "Ten", "Jack", "Queen", "King"
    };
    
    //calling functions
    void PrintCard(int deck[NUMOFCARDS][NUMOFPROPERTIES], int i);
    void InitDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES]);
    void SwapCards(int deck[NUMOFCARDS][NUMOFPROPERTIES], int src, int dest);
    void ShuffleDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES]);
    int main()
    {
      //declaration of varaiables
      int deck[NUMOFCARDS][NUMOFPROPERTIES];
      int player1Deck[5];
      int src = 0;
      int dest = 0;
      int i;
      int players;
      int numPlayers = 0;
      int playerDeck[30];
      srand(time(NULL));
      InitDeck(deck);
      ShuffleDeck(deck);
      SwapCards(deck, src, dest);
      printf("Please enter the number of players (1-3): \n");
      scanf("%d", &numPlayers);
      for (players = 0; players < numPlayers; players++) {
        printf("Player %d\n", players + 1);
        for (i = 0; i < PLAYERCARDS; i++) {
          PrintCard(deck, i);
        }
        printf("\n");
      }
      return 0;
    }
    
    void InitDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES])
    {
      int suit;
      int face;
      int row = 0;
      for (suit = 0; suit < 4; suit++)
        for (face = 0; face < 13; face++) {
          deck[row][0] = suit;
          deck[row][1] = face;
          row++;
        }
    }
    
    void ShuffleDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES])
    {
      int src, dest, i;
      srand(time(NULL));
      for (i = 0; i < NUMOFCARDS; i++) {
        src = i;
        dest = rand() % NUMOFCARDS;
        SwapCards(deck, src, dest);
      }
    }
    
    void SwapCards(int deck[NUMOFCARDS][NUMOFPROPERTIES], int src, int dest)
    {
      int temp;
      temp = deck[src][0];
      deck[src][0] = deck[dest][0];
      deck[dest][0] = temp;
      temp = deck[src][1];
      deck[src][1] = deck[dest][1];
      deck[dest][1] = temp;
    }
    
    void PrintCard(int deck[NUMOFCARDS][NUMOFPROPERTIES], int i)
    {
      int tempsuit;
      int tempface;
      tempsuit = deck[0];
      tempface = deck[1];
      printf("Card %d = %s of %s\n", i + 1, face[tempface], suit[tempsuit]);
    }
    Last edited by Salem; 3 Weeks Ago at 02:38 PM. Reason: Removed crayola

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,580
    Your PrintCard is broken.
    Code:
    $ gcc bar.c
    bar.c: In function ‘PrintCard’:
    bar.c:90:12: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
       tempsuit = deck[0];
                ^
    bar.c:91:12: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
       tempface = deck[1];
                ^
    deck is a 2D array, so you need two subscripts.

    Calling srand() in ShuffleDeck is redundant. You're already calling srand() in the best place possible (at the start of main).
    Calling it more than once won't make rand() any better. Calling it too often will make rand() much worse.

    Aside from that, it works for me.
    Code:
    $ ./a.out 
    Please enter the number of players (1-3): 
    1
    Player 1
    Card 1 = Four of Spades
    Card 2 = Jack of Clubs
    
    $ ./a.out 
    Please enter the number of players (1-3): 
    1
    Player 1
    Card 1 = Jack of Hearts
    Card 2 = King of Spades
    
    $ ./a.out 
    Please enter the number of players (1-3): 
    1
    Player 1
    Card 1 = Five of Spades
    Card 2 = Seven of Hearts
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    321

    Post

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    char CARD[52][128] = {
        "1H","1C","1S","1D","2H","2C","2S","2D",
        "3H","3C","3S","3D","4H","4C","4S","4D",
        "5H","5C","5S","5D","6H","6C","6S","6D",
        "7H","7C","7S","7D","8H","8C","8S","8D",
        "9H","9C","9S","9D","10H","10C","10S","10D",
        "JH","JC","JS","JD","QH","QC","QS","QD",
        "KH","KC","KS","KD"
    };
    
    int random(int lower, int upper) {
        return (rand() % (upper - lower + 1)) + lower;
    }
    
    int main(int argc, char *argv[]) {
        srand( time(0) );
        printf( "%s", CARD[ random( 1, 52 ) ] );
        return 0;
    }
    Last edited by Structure; 3 Weeks Ago at 04:27 PM.
    "without goto we would be wtf'd"

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    321

    Post Card Game { War }

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    char CARD[52][128] = {
        "AH","AC","AS","AD","2H","2C","2S","2D",
        "3H","3C","3S","3D","4H","4C","4S","4D",
        "5H","5C","5S","5D","6H","6C","6S","6D",
        "7H","7C","7S","7D","8H","8C","8S","8D",
        "9H","9C","9S","9D","TH","TC","TS","TD",
        "JH","JC","JS","JD","QH","QC","QS","QD",
        "KH","KC","KS","KD"
    };
    
    char DECK[52][128];
    char HAND1[52][128];
    char HAND2[52][128];
    
    int player1Points = 0;
    int player2Points = 0;
    int difference = 0;
    
    int shuffle() {  
      for (int i=0;i<=51;i++) {
        int place = (rand() % (51 - 0 + 1)) + 0;
        if (strcmp(DECK[place],"null") == 0) {
            strcpy(DECK[place],CARD[i]);
            strcpy(CARD[i],"null");
        } else {
            for (int ai=0;ai<=51;ai++) {
                if (strcmp(DECK[ai],"null") == 0) {
                    strcpy(DECK[ai],CARD[i]);
                    strcpy(CARD[i],"null");
                    break;
                }
            }
        }
      }
    }
    
    int deal(int amount) {
      int c = 0;
      for (int j=0;j<=(amount-1);j++) {
        strcpy( HAND1[j] , DECK[c++] );
        strcpy( DECK[c-1], "null" );
        strcpy( HAND2[j] , DECK[c++] );    
        strcpy( DECK[c-1], "null" );
      }
    }
    
    int getValueOf(char type) {
        if (type == 'A') return 14;
        if (type == '2') return 2;
        if (type == '3') return 3;
        if (type == '4') return 4;
        if (type == '5') return 5;
        if (type == '6') return 6;
        if (type == '7') return 7;
        if (type == '8') return 8;
        if (type == '9') return 9;
        if (type == 'T') return 10;
        if (type == 'J') return 11;
        if (type == 'Q') return 12;
        if (type == 'K') return 13;    
        return 0;
    };
    
    int playWar() {
        for (int i=0;i<25;i++) {
            int p1 = getValueOf(HAND1[i][0]);
            int p2 = getValueOf(HAND2[i][0]);
            if (p1 > p2) player1Points += 1;
            if (p2 > p1) player2Points += 1;
            //printf("%i/%i, ", p1, p2 );
        };
        printf("Player 1: %i \nPlayer 2: %i \n", player1Points, player2Points );
    }
    
    int main(int argc, char *argv[]) {
        
        srand( time(0) );
    
        for (int i=0;i<=51;i++) {
            sprintf(DECK[i], "null");
            sprintf(HAND1[i], "null");
            sprintf(HAND2[i], "null");
        }
        
        shuffle(); deal( 26 ); playWar();
    
        return 0;
    }
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. War card game using C Programming help
    By Anton Dragon in forum C Programming
    Replies: 1
    Last Post: 3 Weeks Ago, 09:09 AM
  2. Text Based Card Game
    By Amrita Ramnauth in forum C Programming
    Replies: 1
    Last Post: 05-05-2016, 07:14 AM
  3. Problem with a very simple card game
    By Amos Bordowitz in forum C Programming
    Replies: 11
    Last Post: 04-26-2014, 12:41 PM
  4. Card game war, programming related-ish
    By colerelm in forum Tech Board
    Replies: 2
    Last Post: 11-10-2011, 08:22 AM
  5. Runoff -- Text Compressor or Card Game?
    By ygfperson in forum Contests Board
    Replies: 5
    Last Post: 09-03-2002, 08:01 PM

Tags for this Thread