Pass by Reference

Pages: 12
So i have this poker game assignment and what I'm having a problem with is that I need to use in the second function I need to use the suitIndex number to figure out overall how many suits are in a hand. How do you use a int that is only local to that function in another function

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
  #include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1


void dealACard(char *suits[], char *faces[], int deck[][FACES]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES]);
void cardsInHand(char *suits[]);

main()  {
	char *suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };
	char *faces[13] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
	int deck[4][13] = { AVAILABLE };
	int suitType = 0;
	int i;
	srand(time(NULL));

	for (i = 0; i < 1; i++)
		dealAHand(suits, faces, deck);

	system("pause");
}

void dealAHand(char *suits[], char *faces[], int deck[][FACES])    {
	int i;

	for (i = 0; i < 5; i++)	{
		dealACard(suits, faces, deck);
		cardsInHand(suits);
	}
	printf("\n");
}

void dealACard(char *suits[], char *faces[], int deck[][FACES])    {
	int suitIndex, faceIndex;

	suitIndex = rand() % 4;
	faceIndex = rand() % 13;
	while (deck[suitIndex][faceIndex] == TAKEN) {
		suitIndex = rand() % 4;
		faceIndex = rand() % 13;
	}
	deck[suitIndex][faceIndex] = TAKEN;

	printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);

}

void cardsInHand(char *suits[])	{
	int suitsInMyHand[4];

	if (suits[SUITS] = suits[0])	{
		suitsInMyHand[0]++;
	}
	else if (suits[SUITS] = suits[1])	{
		suitsInMyHand[1]++;
	}
	else if (suits[SUITS] = suits[2])	{
		suitsInMyHand[2]++;
	}
	else if (suits[SUITS] = suits[3])	{
		suitsInMyHand[3]++;
	}
}
I'm having a problem with is that I need to use in the second function ...

Does that function happen to have a name? I don't know which function you're talking about.
Why do you have two topics open for the same problem.

http://www.cplusplus.com/forum/beginner/137504/
In function deal a card I am using suitIndex that randomly selects a suit. I need to count those 5 suits in function cardsInHand so I know if I have for example a flush. I just do know know how to get the info from suitIndex to work in another function
I need to count those 5 suits ...
4 suits, not 5. Your loops must go from [0 .. 4), not [0 .. 5).

You have a few other problems too. You don't have something that represents a hand. Basically, it's just an array of cards, where each card has a distinct suit/face.

As for cardsInHand, you need to pass in suitsInMyHand as a parameter instead of it being a local variable.
How do I go about passing in those parameters?
Is there anyone that can help me I understand which method I need to use just not how to do it.
In your if statements in lines 56-65, you're using the assignment operator = when I think you mean to use the equality operator ==? And if the suits array has 4 elements (0,1,2,3) and SUITS is defined to be 4, then suits[SUITS] is accessing an out of bounds element. What are you trying to do with the comparison here?

Right now the suitsInMyHand array is declared locally in the cardsInHand function. So when the function ends, local variables get destroyed. I think that's why kbw is suggesting you pass in the array as a parameter to the function (after having declared it earlier in the program) so that it persists after the cardsInHand function ends.
I realize that the number that I need to make my if statement work is in the dealACard function which is the suitIndex and I know I need to pass the data from one function to another through parameters but I do not know how to do this. I just need someone to show me how
I think that may have been what kbw was getting at with the comment that you don't have something that represents a hand. Are you really just checking for a flush where all 5 cards have the same suit?

I suppose you could declare the suitsInMyHand array in main and have the dealACard function call the cardsInHand function with that array and the current suitIndex. Increment the bucket of the suitsInMyHand array that matches the integer in suitIndex.

suitsInMyHand[suitIndex]++;

Then once all 5 cards have been dealt, have a function evaluate the hand to see if the value of any element of that array is 5, meaning a flush.
between the dealAHand function and the dealACard function i am dealing a hand and the suit index uses the suit array to provide the random suit. Now that number (0-3) is what I need to count up in the bottom function. Biggest question is how do I pass the data from one function to another through parameters. Unless I'm just really really not understanding what either of you are trying to help me with.

My assignment
You must use the code from the video to get started, and you must use the following:

Representation of a poker hand:

make 2 arrays: int suitsInMyHand[4], facesInMyHand[13]

Call them whatever you want, but an array of 4 ints that represents the suits, and an array of 13 ints that represents the faces in the hand are the required representation of a Hand.


suitsInHand is 4 counters that represents how many hearts, clubs, diamonds, spades are in the hand. These 4 counters must add up to 5 for a hand of 5 cards. For example if you have 5 hearts in the hand of cards, the array would have the values 5 0 0 0

facesInHand is 13 counters, that represent how many two’s, three’s, etc. you have in the hand. This must also total 5. For example, if you have a pair of 3’s, and three Kings’s, this array would have the values
020000000030

While dealing a hand of cards, keep a count of the suitsInHand, and facesInHand.
Are you required to have that separate function cardsInHand?

I'm thinking you could simply increment the appropriate bucket of the array after you deal the card and update the deck array with the latest taken card. You'd need to pass these arrays into the dealing function as you have the other arrays.

suitsInHand[suitIndex]++
facesInHand[faceIndex]++

Is there going to be another function to evaluate these arrays and determine a winning hand?
Well the instructor said to use small functions but I do like your idea. The next step is to set up the types of hands (flush, straight, pair, etc) and then figure out who is the winner. Also have to figure out if there is a tie like 2 hands with 2 pair each to determine who has the highest pair and who is the winner.

Assignment:

In this assignment we will work on the creation of a poker game. At a minimum your game must deal 2 hands of cards, and print out what poker hand each has.

It must also determine which hand has won the game. This is not difficult unless there is a tie.

For example, if both hands have 3 of a kind, that can be considered a tie. Write specific methods for breaking ties, for example a function that determines which 3 of a kind is higher.

Your code must be able to break at least one tie. (bonus points for more are described at the bottom).

This function will use the 2 arrays described above to determine if the hand has a flush, straight, etc. I have not included the parameters. You will need to pass in the hand of cards, and have the function pass back if there is a flush, straight, three of a kind, etc.

/*
analyzeHand: Determines whether the hand contains a
straight, a flush, four-of-a-kind,
and/or a three-of-a-kind; determines the
number of pairs; stores the results into
the external variables straight, flush,
four, three, and pairs.
*/

int straight, flush, four, three, pairs these are probably parameters of this function.


int num_consec = 0;
int face, suit;

straight = FALSE;
flush = FALSE;
four = FALSE;
three = FALSE;
pairs = 0;

// check for flush – 5 cards of the same suit

for (suit = 0; suit < SUITS; suit++)
if (suitsInHand[suit] == 5)
flush = TRUE;

// check for straight – eg. One each of 5,6,7,8,9
// locate the first card

face = 0;
while (facesInHand[face] == 0)
face++;

// count the consecutive non-zero faces from the starting place established

for (; face < FACES && facesInHand[face]; face++)
num_consec++;

if (num_consec == 5) {
straight = TRUE;
return; // we can leave. If you have a straight, you can’t have
// anything below.
}

// check for 4-of-a-kind, 3-of-a-kind, and pairs
// 3 of a kind and 4 of a kind are either true or false, you can’t ever have 2 of them,
// but pairs is a coutner, youc an have 1 pair or 2 pairs.

for (rank = 0; rank < NUM_RANKS; rank++) {
if (facesInHand[rank] == 4)
four = TRUE;
if (facesInHand[rank] == 3)
three = TRUE;
if (facesInHand[rank] == 2)
pairs++;
}



Here is a block of code to determine what hand is better than another. There are 9 different levels of a poker hand.

These are in order. A straight-flush is the best hand, and a high-card is the worst hand.

Notice that these variables (except pairs) are used as boolean values.


if (straight && flush)
printf("Straight flush\n\n");
else if (four)
printf("Four of a kind\n\n");
else if (three && pairs == 1)
printf("Full house\n\n");
else if (flush)
printf("Flush\n\n");
else if (straight)
printf("Straight\n\n");
else if (three)
printf("Three of a kind\n\n");
else if (pairs == 2)
printf("Two pairs\n\n");
else if (pairs == 1)
printf("Pair\n\n");
else
printf("High card\n\n");
And I just tried your suitsInMyHand[suitIndex]++ and it did not work so well cause it is not giving me the array and the suitIndex is not going into it
Can you post your updated code?
I've made some recent changes but I'm still getting some errors when I try to pass dealACard into suitsInHand.

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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SUITS 4
#define FACES 13
#define AVAILABLE 0
#define TAKEN 1


void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[]);
void dealAHand(char *suits[], char *faces[], int deck[][FACES]);
void suitsInHand(char *suits[], int suitsInMyHand[], void dealACard(int suitIndex));

main()  {
	char *suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };
	char *faces[13] = { "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
	int deck[4][13] = { AVAILABLE };
	int suitsInMyHand[4] = { 0 };
	int i;

	srand(time(NULL));

	for (i = 0; i < 4; i++)
		dealAHand(suits, faces, deck, suitsInMyHand);

	system("pause");
}

void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInMyHand[])    {
	int i;

	for (i = 0; i < 5; i++)	{
		dealACard(suits, faces, deck, suitsInMyHand);
		suitsInHand(suitsInMyHand, suits);
	}
	printf("\n");
}

void dealACard(char *suits[], char *faces[], int deck[][FACES])    {
	int suitIndex, faceIndex;

	suitIndex = rand() % 4;
	faceIndex = rand() % 13;
	while (deck[suitIndex][faceIndex] == TAKEN) {
		suitIndex = rand() % 4;
		faceIndex = rand() % 13;
	}
	deck[suitIndex][faceIndex] = TAKEN;

	
	printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);

}

void suitsInHand(int suitsInMyHand[], void dealACard(int suitIndex))	{
	int suitIndex = 0;

	if (suitsInMyHand[suitIndex] == 0)	{
		suitsInMyHand[0]++;
	}
	else if (suitsInMyHand[suitIndex] == 1)	{
		suitsInMyHand[1]++;
	}
	else if (suitsInMyHand[suitIndex] == 2)	{
		suitsInMyHand[2]++;
	}
	else if (suitsInMyHand[suitIndex] == 3)	{
		suitsInMyHand[3]++;
	}
}
I didn't mean to pass a function into another function.

You don't want to declare a new local variable in suitsInHand for suitIndex and initialize it to zero. You want to send the suitIndex and faceIndex values that you have in the dealACard function. Right now you're calling this from the dealAHand function which doesn't know anything about the local variables for suitIndex and faceIndex.
Should I do a new for statement in dealACard? Cause without suitsInHand in dealAHand it skips the suitsInHand function and moves to the next suit. Also if I do not declare suitIndex I get and unspecified error for suitIndex. How else ca n I get the suitIndex through the if else statements
I really really want to learn this cause I want a job eventually in the development field very badly but it is hard to wrap my head around this at times
You pass the suitIndex variable to the function. You don't need the if statements. The suitsInMyHand array is a counter. You don't want to check the suit type (0,1,2,3) against the current counter value.

At the beginning of the hand, the suitsInMyHand is all zeroes.

suitsInMyHand[0] = 0 //hearts counter
suitsInMyHand[1] = 0 //diamonds counter
suitsInMyHand[2] = 0 //spades counter
suitsInMyHand[3] = 0 //clubs counter

Let's say you deal a spade. So suitIndex will have the value of 2. Right now you are using an if statement to compare the value in suitsInMyHand[suitIndex], which for spades would be suitsInMyHand[2]. Since it's the first card of the hand, the counters are all at 0, so you'd match on suitsInMyHand[2] == 0, but increment suitsInMyHand[0], which adds a count to hearts.

What you need this function to do is increment the counter that corresponds to the suitIndex value.
void suitsInHand(int suitsInMyHand[], int suitIndex)
{
suitsInMyHand[suitIndex]++;
}
Pages: 12