Need help with a card game application

Hi,
I need to make a card game application for my class that uses a standard playing card deck, except all of the face cards have a value of 10, including the aces. It needs to have 4 players (one human vs 3 computer players), each player will be dealt 5 cards per round, and the winner of that round is whoever has the highest sum of all their cards. There needs to be 10 rounds, and whoever wins the most rounds is the winner. It needs to prompt the user for the name of each player and then print them out in order. It needs to deal a card to each player using a function called deal() by returning a randomly generated card. The application needs to print out the winner of each round, and at the end of 10 rounds identify who is the overall winner.

My professor also gave this as a hint:
playerCard[iPlayer][iCard] = deal()

char player[MAXPLAYERS][MAXCHAR];
int gameWinner[MAXGAMES];

use ==> gets(player[i])

This is the code I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#define DECKSIZE 52
#define MAXPLAYERS 4
#define MAXCHAR 40
#define MAXGAME 10


// function prototype
int deal();


int main(void)
{
	// declaration
	char player[MAXPLAYERS][MAXCHAR];
	int playerCard[MAXPLAYERS][DECKSIZE];
	int roundWinner[MAXGAME];

	// set seed for random number
	

	// read player names
	

	for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
	{
		printf("Enter the players name: ");
		gets_s(player[iPlayer]);
	}

	for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
		printf("players %d name %s\n", iPlayer, player[iPlayer]);
	// loop over number of games
	for (int i = 0; i < MAXGAME; i++)
	{
		printf("Round %d\n", i);
		for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
		{
                        srand((unsigned)time(NULL));
			printf("%s's Hand:", player[iPlayer]);
			for (int iCard = 0; iCard < 5; iCard++) 
			{
			
				playerCard[iPlayer][iCard] = deal();
				printf("%d ", playerCard);
			}
			
			printf("\n");
		}
		
	} // end for loop

	  // determine winner of game
	  // print out game results

	printf("\n\n");
	return (0);
} // end main()

	int deal(void)
	{
		// Declaration
		int deck[DECKSIZE] = {	2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
		2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
		2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,
		2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,};
		int cardNum = 0;
		int cardVal = 0;

		cardNum = rand() % 52;
		cardVal = deck[cardNum];

		return cardVal;
}

When it gets to the actual game part after the name-entry, it outputs all 10 rounds immediately which I'd prefer not to have, but what's worse is that all of the cards are all the same for every player, for every round. Can someone tell me what I'm doing wrong? Thanks.
Last edited on
Perhaps a better question might be, how am I using the statement "srand((unsigned)time(NULL));" incorrectly here?
closed account (D80DSL3A)
Yes, you are. srand should be called just once at the start of main(), but that's just 1 reason for the cards all the same every time.
Line 50: printf("%d ", playerCard);// name of array = address
should probably be: printf("%d ", playerCard[iPlayer][iCard]);
Thanks a lot! That made it work. My next question then is, is it possible to make it so that the user has to input something in order to go to the next round?
closed account (D80DSL3A)
Something simple like answer "go again?" with y/n?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// loop over number of games
char goAgain = 'y';// new var
	for (int i = 0; i < MAXGAME && goAgain == 'y'; i++)// note enhanced condition
	{
		printf("Round %d\n", i);
		for (int iPlayer = 0; iPlayer < MAXPLAYERS; iPlayer++)
		{
                        srand((unsigned)time(NULL));
			printf("%s's Hand:", player[iPlayer]);
			for (int iCard = 0; iCard < 5; iCard++) 
			{
			
				playerCard[iPlayer][iCard] = deal();
				printf("%d ", playerCard);
			}
			
			printf("\n");
		}
                printf("Go again (y/n)?");
                scanf( "%c", &goAgain );
	} // end for loop 
Last edited on
I think I've got it now, thanks for all your help!
Topic archived. No new replies allowed.