0

I am trying to create a simplified version of the Rummy card game. I need to parse in the card abbreviations for example SA is Spades Ace. DT is Diamond 10 etc. I know there is an easier way to do this but that's how my assignment wants it done.

The sample execution would look like

rummy 3 S2 H9 C4... etc. include all 52 cards.

The number in argv[1] is the players in the game. How am I supposed to take the cards starting after the number and put them into an array?

My code so far

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

int main(int argc, char *argv[])
{
    int players = *argv[1];
    char deck[52];
    int i, j, pid, cid;
    if (players > '5' || players < '3')
    {
        printf("%c is not the allowed number of players, min is 3 and max is 5\n",*argv[1]); 
        exit(0);
    }

}
2
  • argv[1] is a pointer to "3", argv[2] is a pointer to "S2" etc. You should get familiar with strings and pointers. Commented Feb 24, 2016 at 8:07
  • Hi thanks for the comment, I know that argv[1] is 3 and 2 is S2 but how specifically in C can I put all the cards S2, H9 etc into an array. Thanks Commented Feb 24, 2016 at 8:10

3 Answers 3

3

Quick and dirty demonstration:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
  int players = atoi(argv[1]);
  char deck[52][3];
  int i, j, pid, cid;

  if (players > 5 || players < 3)
  {
    printf("%d is not the allowed number of players, min is 3 and max is 5\n", players);
    exit(0);
  }

  for (i = 0; i < argc - 2; i++)
  {
    strcpy(deck[i], argv[i+2]);
  }

  for (i = 0; i < argc - 2; i++)
  {
    printf("%s\n", deck[i]);
  }
}

Absolultely no sanity checks are done concerning the input. It's just for demonstration.

Sign up to request clarification or add additional context in comments.

Comments

1

Your int argc is the count of arguments. So you can indeed manually load all of these cards into an array if you so choose.

Assuming you execute the program like this:

example.exe rummy 3 S1 S2 S3 S4 A1 A2 A3 A4

You could then read the cards into an array like this (assuming that "rummy" is game type and "3" is some other control variable, you need to make sure of this)

int main(int argc, char *argv[])
{
char game[10] = argv[0];
int players = atoi(argv[1]);
char deck[52][3]; // an array of strings max lenght 3 (2 characters + required '\0' terminator
for (int i = 0; i < argc - 2; i++) // argc - 2 because we're accessing at i+2 so the last iteration will essentially access the last element
{
  strcpy(deck[i], argv[i+2]); // copy into actual array
}
return 0;
}

Now you got your cards in an array of char arrays called deck. Take note that this is completly only a sample and that it is not recommended for direct use. In an eventual program you must have sanity checks and validation against all possible cases (too many args, too little args, wrong args, etc.

Comments

0

All arguments passed on command line are stored in argv array. argv[0] is always the name of the program and next come your arguments if any (as null-terminated strings).

So assuming you have called this as:

rummy 3 S2 H9 C4

this is what argv contains:

argv[0] = "rummy"
argv[1] = "3"
argv[2] = "S2"
argv[3] = "H9"
argv[4] = "C4"

Inserting these into array is simple:

char args[5][10];
strncpy(args[0], argv[0], 10);

2 Comments

So should I just use argv instead of creating a new array for the cards?
You could, but then you're stuck with the characters as the format for the deck. It would be easier to do further processing on the deck if you convert them to numbers or number pairs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.