0

I'm trying to make a solitaire game using structs and linked lists, I've started with reading cards from a file with colours and it's values, so I have a problem with such cards as 'A', 'J', 'D', 'K' their values are to be set to 1, 11, 12, 13 respectively. How do I do this in case of using following construction:

    #define J 11

    typedef struct card {
    char color[15];
    unsigned int value;
    struct card* pnext;
    struct card* pprev;
} cardsStruct;

struct card* pstart = NULL;
struct card* plast = NULL;

int main()
{
    FILE *myFile;
    cardsStruct card;
    if ((myFile = fopen("Cards.txt", "rb")) == NULL)
        return -1;
    while (!feof(myFile)){
        fscanf(myFile, "%s %u", card.color, &card.value);
        printf("%s %u\n", card.color, card.value);
    }
    fclose(myFile);
    return 0;
}

4 Answers4

0

In C, you can try to simulate the feel of maps from C++...

int characters[128] = {0};
characters['A'] = 1;
characters['J'] = 11;
characters['D'] = 12;
characters['K'] = 13;

And print it like this:

printf("%s %u\n", card.color, characters['K']);
Ruks
  • 3,886
  • 1
  • 10
  • 22
0

you need three forms of holding the cards:

  • In the text file

  • for program use

  • When displaying

I think the trick here is to do conversions. For example, in the file it is nicer to use letters (A,J,D/Q,K) so it will be human readable and also easy to edit. when displaying, you want it also to be human readable. In RAM, for program use, you better use an hexadecimal values so it will be easy to do calculations.

eyalm
  • 3,366
  • 19
  • 21
  • Yeah, the cards will be held in the text file as for example "RED J", the rules are as in classic solitaire, where you can only put a card on acard with a value bigger by 1, for example "3 on 4", "10 on J", to make this possible I need to make 'J' into '11' – James Toaster Dec 20 '18 at 09:03
  • No, not only in printing I'm going to use 'J' for user input command like "move red J to 4" and then it should compare card's value with the last card in fourth stack, whether the difference is 1 or not, how would it compare J to 10? – James Toaster Dec 20 '18 at 10:21
0
switch(card)
{
  case 'A':
     value = 1;
     break;
  case 'J':
     value = 11;
     break;
  /* ... */
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

In a comment you wrote:

the cards will be held in the text file as for example "RED J"

Obviously, you can't parse a letter with a format specifier for an unsigned integer.

So this is wrong:

fscanf(myFile, "%s %u", card.color, &card.value);

I suggest you read the file line wise (using fgets() or getline()), and try to parse it afterwards, e.g. using sscanf(). So if this fails to parse the number sscanf(in, "%14s %u", out), you can try to parse "%14s %c" afterwards.

A few other suggestions:

  • Don't mix the implementation of the linked list and the data type.
  • Define a different type to hold the list, than to represent the list node.
  • Split functionality in functions, especially taking input and parsing input.
  • Check and handle errors consistently.
  • Decouple the file representation of a card and the data representation in your program.
  • Create a function for deserialization, i.e., to convert the suit and rank strings to enum values or defined integer literals.
  • Create a function to convert suit to color, instead of storing a color string directly.
  • If you are new to C, then maybe it makes sense to work out the business logic in another simpler language first and then port it to C.

For reference and further reading:

moooeeeep
  • 31,622
  • 22
  • 98
  • 187