Originally Posted by
krazzyjman
Code:
struct card {
int value;
char suit;
};
Since char type can hold at least values 0 to 127, it is more than enough for the value, too. So, char value; is more appropriate above.
Originally Posted by
krazzyjman
Code:
struct stack {
struct card tablecards[52];
int top;
};
Okay, the above defines a card stack. I just wish you intended your braces more carefully; the haphazard indentation makes it annoying to read.
Originally Posted by
krazzyjman
Code:
struct queue {
struct card playercards[52];
int font;
int numElements;
int size;
};
font? numElements? size? What are they?
Your queue size is fixed to 52 cards. You only need two fields: one to tell where the topmost card is, head or top, and one to tell where the bottommost card is, tail or bottom. Usually the latter is the index after the bottommost card, since that makes a lot of the math simpler.
Originally Posted by
krazzyjman
What purpose does the above have? It's just an array of 52 integers, definitely not a queue of any kind.
If you want a 52-card queue in variable queue, declare something like struct queue queue;
Note that queue is a confusing name. Your variable names should not describe what the variables are; they should describe what you use the variable for.
As a hint, I'd use struct queue deck[2]; so I'd get an up to 52-card queue for each player.
Originally Posted by
krazzyjman
Code:
inputFile = fopen("hi.txt", "r");
if (inputFile == NULL) {
printf("Does not exist\n");
}
That just prints a line but forges ahead! No, you need to abort your program. For example, if you include <errno.h> and <string.h> too at the start of your program, you can use
Code:
static const char inputFileName[] = "hi.txt";
inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
fprintf(stderr, "%s: %s.\n", inputFileName, strerror(errno));
return 1;
}
which prints the file name and the error message, describing exactly why the file could not be opened, then exits the program (with nonzero exit status, by returning from main()).
Originally Posted by
krazzyjman
Code:
fscanf(inputFile, "%d", &numgames);
fscanf() returns the number of successful conversions. Above, you really should check that it did get an integer, one that makes sense, using for example
Code:
if (fscanf(inputFile, "%d", &numgames) < 1) {
fprintf(stderr, "%s: File did not start with numgames.\n", inputFileName);
fclose(inputFile);
return 1;
}
if (numgames < 1) {
fprintf(stderr, "%s: No games to play.\n", inputFileName);
fclose(inputFile);
return 1;
}
if (numgames > the appropriate limit) {
fprintf(stderr, "%s: Too many games to play!\n", inputFileName);
fclose(inputFile);
return 1;
}
Originally Posted by
krazzyjman
Code:
for (i = 0; i < numgames; i++) {
fscanf("%d %c", &value, &suit);
}
Here, again, you should check that fscanf() does not return less than 2. If it does, then you didn't get both value and suit.
You read the value into variable value, and the suit into variable suit, but you overwrite them on the very next iteration.
Also, you should check the suit and value too. Perhaps
Code:
if ((value >= 1 && value <= 13) &&
(suit == 'H' || suit == 'D' ||
suit == 'C' || suit == 'S') {
/* This one is okay, save somewhere */
} else {
fprintf(stderr, "%s: Invalid card.\n", inputFileName);
fclose(inputFile);
return 1;
}
Finally, shouldn't you save them somewhere? In fact, weren't these supposed to be two different hands? Didn't I mention a hint in this message above somewhere?