Function Help

Pages: 12
So i posted a previous thread about mapping, I got that down. This is the background to my assignment:

The rules of ESP are:
There is a deck of 18 cards that looks like this:
Each card has a color and a shape.
There are 3 shapes: CIRCLE, SQUARE, TRIANGLE
There are 6 colors: RED, BLUE, YELLOW, PURPLE, GREEN, ORANGE
For example: one card is BLUE SQUARE

The program allows the player to play as many rounds as they'd like.
The program gives the player the opportunity to quit after each round, by entering 'Q' or 'q'.

One round consists of:
The computer will randomly choose 5 cards from this deck (these are the 'correct' cards).
The player will guess 5 cards by entering a color and a shape for each. (Give them exactly 5 chances to guess in one round (see below for duplicate guesses).)
For each guess, display whether or not it matches one of the 'correct' cards. Cards do not have to be in the same order as the correct cards to win. Display whether or not it is a duplicate guess, which is not allowed.
After the player has made 5 guesses, the round is over, at which point
the program will display that round's information (see below) and ask if they want to play again.
At the end of each round, display the following:
⦁ the five cards the computer selected (correct cards) (color/shape of each card)
⦁ the five cards the player guessed (color/shape of each card)
⦁ the player’s ESP Level (you must have these 4 exact levels, but you can make up your own messages!)
0 matches: “Level 1: No ESP at all!”
1-2 matches: “Level 2: Some ESP, keep working on it!”
3-4 matches: “Level 3: Lots of ESP, try Powerball!”
5 matches: “Level 4: ESP expert! Host a TV show!”

Here are some tricky parts: (Plan to devote a good amount of time to solving these problems.)
⦁ make sure there are no duplicate cards generated for the computer (i.e. no duplicates in 'correct' cards)
⦁ make sure there are no false positives, i.e.
RED CIRCLE should not be a match if the computer chose RED SQUARE and GREEN CIRCLE.
⦁ Do not let the player choose a duplicate card (i.e. one already guessed before in that round). If they choose a duplicate, you must allow them to choose again. (i.e. always 5 non-duplicate guesses.)
⦁ Don’t let the player get duplicate matches for guessing the same correct card several times!
(similar to #3 above)
⦁ Remember that the cards do not have to be in the same order to match.



Now, my problem here is, when I run the program it compiles. It displays the instructions, says enter your card by shape then color. It does that, and just keeps cout Please choose a card..... infinitely when it should be only 5 times, returning whether the card the user chose matches the computers after every guess. What is wrong? BTW, i'm only halfway through the coding btw, I'm just having trouble with why this is happening.
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  #include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>


using namespace std;


void DisplayInstructions ( );  
void GenerateCorrectCards (int *);  
int GetGuess ( );  
bool  CheckMatch (int , int *); 
void DisplayCards (int *, int *); 
void DisplayESP (int); 



int main()
{
	
	int generatedCards[5];

	int const CARDS =18;
	

	int user[5];
	
	int i;
const char *deck [CARDS]={"red circle","red square","red triangle","blue circle","blue square",
	 "blue triangle","yellow circle","yellow square","yellow triangle","orange circle",
	 "orange square" ,"orange triangle","purple circle","purple square",
     "purple triangle","green circle","green square","green triangle"};
	
	
	srand ( time(NULL) );//Seeds the random generator once at the beginning
	
	
	DisplayInstructions();
		
   
	GenerateCorrectCards (generatedCards);
	for ( i=0; i<*generatedCards; i++){

         *(user + i) = GetGuess(); 
}
bool  CheckMatch (int user, int *GenerateCorrectCards);
	
	
	
//	for (char Z = 'Q' ; z == 'Q' || z == 'q';  ){
//	
//	cout<<"Do you want to quit?"
//}
//}
}
void DisplayInstructions()
{
	
	cout <<	"The computer will generate a deck of 18 cards.\n"
		 << "There are 3 shapes: Circle, square, and triangle.\n"
		 << "There are 6 colors: Red, blue, yellow, orange, purple, and green.\n"
		 << "Pick a shape and then a color.\n"
		 << "Do this 5 times and see if your predictions match what the computer picked,\n checking your ESP level!\n";
	
}



void GenerateCorrectCards(int *generatedCards)
{
	
	int i;
    int selectedInt;
    int alreadySelect = 0;
    int successfulSelections = 0;
    
    while (successfulSelections < 5) {
        selectedInt = (rand() % 18) + 1;

        if (successfulSelections==0) {
            *(generatedCards)=selectedInt;
        }
 
        for (int i=0;i<successfulSelections;i++) {
            if (*(generatedCards+i)==selectedInt) {
                alreadySelect=1;
                break;
            } else {
                alreadySelect=0;

                *(generatedCards+successfulSelections)=selectedInt;
            }
        }

        if (alreadySelect==0) {
            successfulSelections++;
        }
    }
}
    
    
    
	


int GetGuess ( )
{
	int const CARDS=18;
	std::string deck [CARDS]={"red circle","red square","red triangle","blue circle","blue square",
	 "blue triangle","yellow circle","yellow square","yellow triangle","orange circle",
	 "orange square" ,"orange triangle","purple circle","purple square",
     "purple triangle","green circle","green square","green triangle"};
     


std::string to_lower( std::string str ); // make everything lower case
 for (int i=0;i<5;i++)
    {
    	std::string color,shape,combo,userSelect;
    	
    	
    	cout<<"Please choose a card with a color first, and then a shape.";
	cin>>color>>shape;
	combo= color + " " +shape;
	combo=userSelect;
  if (userSelect=="red circle")
  		return 1;
  	else if	(userSelect=="red triangle")
  		return 2;
  	else if(userSelect=="red square")
  		return 3;
  	else if(userSelect=="blue circle")
  	
  	return 4;
  	else if (userSelect=="blue triangle")
  	
  	return 5;
  	else if(userSelect=="blue square")
  	
  	return 6;
  	else if(userSelect=="yellow circle")
  	
  	return 7;
  	else if(userSelect=="yellow triangle")
  	
  	return 8;
  	else if(userSelect=="yellow square")
  	
  	return 9;
  	else if (userSelect=="orange circle")
  	
  	return 10;
  	else if (userSelect=="orange triangle")
  	
  	return 11;
  	else if(userSelect=="orange square")
  	
  	return 12;
  	else if (userSelect=="purple circle")
  	
  	return 13;
  	else if (userSelect=="purple triangle")
  	
  	return 14;
  	else if (userSelect=="purple square")
  	
  	return 15;
  	else if(userSelect=="green circle")
  	
  	return 16;
  	else if ( 	userSelect=="green triangle")
 
  	return 17;
  	else if  (userSelect=="green square")
  	
  	return 18;
  	
  	break;
}
    }
    bool  CheckMatch (int user, int *generatedCards)
    {
    	
    	for (int i=0;i<5;i++) {
        if (*(generatedCards+i)==user) {
            cout << "Your card matches!" << "\n";
            return true;
        }
    }
    cout << "Sorry, your card does not match." << "\n";
    return false;
}
    	
void DisplayCards (int *, int *)
    	{
    		
    		
    		
    		
    	}
    	
    

Also, I have to use pointers, no arrays in the code other than declaring them. My teacher is a nazi about this for the assignment.
int generatedCards[5];

1
2
3
4
GenerateCorrectCards (generatedCards);
	for ( i=0; i<*generatedCards; i++){

         *(user + i) = GetGuess();


generatedCards is an array. Seems like you'd want to use the length of the array instead of dereferencing the array which has random numbers in it?

Edit: also - the instruction text is inconsistent.
The computer will generate a deck of 18 cards. 
There are 3 shapes: Circle, square, and triangle. 
There are 6 colors: Red, blue, yellow, orange, purple, and green.
Pick a shape and then a color. 
Do this 5 times and see if your predictions match what the computer picked, checking your ESP level! 
Please choose a card with a color first, and then a shape. 
Last edited on
That did fix my problem for it going infinite, but my bool Checkmatch function still isn't coming back saying whether the guesses are correct or not. I'm lost here.

Lol yes I did notice that too, for such a nazi she made quiet a few mistakes in the directions.
Is line 47 supposed to be the call to CheckMatch?
bool CheckMatch (int user, int *GenerateCorrectCards);

If yes, you'd want to leave off the return type and data types on the parameters. And GenerateCorrectCards is a function - did you mean to send the two arrays to compare the user and computer hands?

Sheesh lollll, that's how I know I've been looking at this code for too long, thank you for pointing out something so obvious lol. I changed it to

CheckMatch (user,generatedCards);

and now I'm getting the error

[Error] invalid conversion from 'int*' to 'int' [-fpermissive]

Edit: thank you for your help btw wildblue, its much appreciated!
Last edited on
user and generatedCards are the same type aren't they? the CheckMatch expects two different types here - a regular int, and a pointer to int.

bool CheckMatch (int , int *);
bool CheckMatch (int user, int *generatedCards)
Last edited on
Yes they're both int. How can I use the pointer to int to get the computer generated cards when i call the function? Gah, this program shall be the death of me. Sorry for the seemingly simple questions, my professor was out for 3 weeks and I learned pointers on my own, and they're quite confusing to learn in my opinion anyway.
Using
CheckMatch (user,*generatedCards);

I get the same error... hmm
If they're the same - both int arrays, wouldn't you want to treat them the same?
bool CheckMatch (int * , int *)?

The guesses don't have to be in the same order as the computer hand to win, so it sounds like you might want to do some sorting before comparing.

I see what your saying. The thing is my professor gave us the function prototypes and we have to use them as they are for full credit.



Hmm, I'm taking a break from this program. I need to refresh my brain lol. Thanks for the help I really appreciate it
Well - the other thought I had was that they want you to check each guess as it's entered. So instead of comparing array to array, you add a separate integer variable for the current user input, you check that to make sure it's not a duplicate, then you check that single integer against the computer's array. And you save that current guess into the user array so you can print out all their guesses later.
Gah I am still stuck here with the same issue. I tried to pass checkMatch *deck, and I get the same error. I'm so lost on how to pass my variables here and why it isn't working correctly...
checkMatch should check whether the number associated with the color/shape that the user just chose matches a card in the computer's hand?

The user number is the value returned by the getGuess function - sounds like that's the value you want to send to the checkMatch function to compare against the values in generatedCards.
Yes. My userselect returns the integer, but Im at a loss how to pass this through my bool function with the generated cards. I'm still getting the same error even though I know I need to pass generated cards through my bool.

Sorry for all these questions, youve been a big help wildblue.
currentGuess = GetGuess();
//check if currentGuess is a duplicate before updating user array

//if not duplicate, check if it matches computer hand
match = CheckMatch (currentGuess, GenerateCorrectCards)

if match is true - increment a match counter so you know the level of ESP
Gah I never would have got that lol, thank youu. So I'm assuming to declare currentGuess as an int, and then match a pointer to an int?


int currentGuess = GetGuess();


//check if currentGuess is a duplicate before updating user array

//if not duplicate, check if it matches computer hand
int *match = CheckMatch (currentGuess, GenerateCorrectCards);
match is not a int pointer - what is the return type of the checkMatch function?
bool CheckMatch (int , int *)

Returns true or false for a match
match will hold that true or false that's returned. If it's true, you update their ESP count.
Pages: 12