cootie game

I worked on my code and it looks at the moment like this:


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
/*using namespace std;
int rollDice();
int applyRoll(int);
bool cootieComplete(int, int, int, int, int, int);
void printCootie(int);
int main(int argc, char *argv[])
{
   int diceRoll;
   int apply;
   srand(time(0));
   do {
   diceRoll = rollDice();
   apply = applyRoll(diceRoll);
   printCootie(diceRoll);
   while (!cootieComplete(apply)) ;
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

     int rollDice(){
     int diceRoll = rand() % 6 + 1;
     return diceRoll;
     }

    int applyRoll(int diceRoll){
        int numBody=0;
        int numHead=0;
        int numBodypart_3=0;
        int numEye=0;
        int numBodypart_5=0;
        int numLeg=0;
                   if (diceRoll == 1) 
                   numBody++;
                   
  
                   if (diceRoll == 2 && numBody == 1) 
                   //int numHead=0;
                   numHead++;
                   
    
                   if (diceRoll == 3 && numHead == 1) 
                   //int numBodypart_3=0;
                   numBodypart_3++;
                   
    
                   if (diceRoll == 4 && numHead == 1) 
                   //int numEye = 0;
                   numEye++;
                   
    
                   if (diceRoll == 5 && numHead == 1) 
                   //int numBodypart_5=0;
                   numBodypart_5++;
                   
    
                   if (diceRoll == 6 && numHead == 1) 
                   //int numLeg=0;
                   numLeg++;
                   
                   }

     bool cootieComplete(int numBody,int numHead,int numBodypart_3,int numEye,int numBodypart_5,int numLeg) {
         if (numBody!=0&&numHead!=0&&numBodypart_3!=0&&numEye!=0&&numBodypart_5!=0&&numLeg>=6) {
             return true;
          }
         else {
               return false;
          }
    } 

    void printCootie(int diceRoll){
  
                   if (diceRoll == 1) 
                   cout << "Body Added!" << endl;
                   
  
                   if (diceRoll == 2 && numBody == 1) 
                   cout << "Head Added!" << endl;
                   
    
                   if (diceRoll == 3 && numHead == 1) 
                   cout<< "Antennae, hat or bow Added!"<<endl;
                   
    
                   if (diceRoll == 4 && numHead == 1) 
                   cout<<"Eye Pice Added!" << endl;
                   
    
                   if (diceRoll == 5 && numHead == 1) 
                   cout<<"Tongue, teeth or lips Added!" << endl;
                   
    
                   if (diceRoll == 6 && numHead == 1) 
                   cout<<"Any One Leg Added!" << endl;
                   

     }*/




My Programm its a cootie game. Check this out http://en.wikipedia.org/wiki/Cootie_(game)!
My problem at the moment is that i need some more arguments for the function cootieComplete() and dont know how.
Last edited on
If you make all parts of the cootie global you can call cootieComplete without an argument:

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
int numBody=0;
int numHead=0;
int numBodypart_3=0;
int numEye=0;
int numBodypart_5=0;
int numLeg=0;

int rollDice();
void applyRoll(int);
bool cootieComplete();
void printCootie(int);
int main(int argc, char *argv[])
{
	int diceRoll;
	int apply;
	srand(time(0));
	do {
		diceRoll = rollDice();
		apply = applyRoll(diceRoll);
		printCootie(diceRoll);
	while (!cootieComplete()) ;   
	system("PAUSE");
	return EXIT_SUCCESS;
}

int rollDice(){
	int diceRoll = rand() % 6 + 1;
	return diceRoll;
}

void applyRoll(int diceRoll){
	if (diceRoll == 1) numBody++;                   
	if (diceRoll == 2 && numBody == 1) numHead++;                   
	if (diceRoll == 3 && numHead == 1) numBodypart_3++;                   
	if (diceRoll == 4 && numHead == 1) numEye++;                   
	if (diceRoll == 5 && numHead == 1) numBodypart_5++;	
	if (diceRoll == 6 && numHead == 1) numLeg++;
}

bool cootieComplete() {
	if (numBody!=0&&numHead!=0&&numBodypart_3!=0&&numEye!=0&&numBodypart_5!=0&&numLeg>=6) {
		return true;
	}
	else {
		return false;
	}
} 

void printCootie(int diceRoll){  
	if (diceRoll == 1) cout << "Body Added!" << endl;                   
	if (diceRoll == 2 && numBody == 1) cout << "Head Added!" << endl;                   
	if (diceRoll == 3 && numHead == 1) cout<< "Antennae, hat or bow Added!"<<endl;                   
	if (diceRoll == 4 && numHead == 1) cout<<"Eye Pice Added!" << endl;                   
	if (diceRoll == 5 && numHead == 1) cout<<"Tongue, teeth or lips Added!" << endl;                   
	if (diceRoll == 6 && numHead == 1) cout<<"Any One Leg Added!" << endl;                   
}


But in your version the cootie can have more than 1body, endless eyes, endless legs and so on .. it it correct?

Turn your cootie into a single object instead of a series of variables. A transparent struct, for example:
1
2
3
4
5
6
7
8
9
struct Cootie{
	int numBody;
	int numHead;
	int numBodypart_3;
	int numEye;
	int numBodypart_5;
	int numLeg;
};
 


Then you can create a single Cootie object the same way you would create an int or char.
 
Cootie newCootie;


And you could pass this to any of your functions as a single parameter, preferably as a reference or pointer, ie:

1
2
3
4
5
6
7
//function definition using reference
void applyRoll(int diceRoll, Cootie &c){
  if(diceRoll == 1) c.numBody++;
}

//Calling function
applyRoll(diceRoll, newCootie);


or:

1
2
3
4
5
6
7
//function definition using pointer
void applyRoll(int diceRoll, Cootie *c){
  if(diceRoll == 1) c->numBody++;
}

//Calling function
applyRoll(diceRoll, &newCootie);



Globals generally should be discouraged. Sometimes they're the least messy means of accomplishing a goal, but most of the time there are better ways.

If you're looking at having to pass a whole bunch of parameters around, you can probably put them in some form of container, whether that's wrapping them up into a discrete object (or a composite of multiple objects), or storing them in a generic container like a vector or list.

In this particular example, the ideal solution is to create a comprehensive cootie class that encapsulates all it's capabilities, as opposed to just a struct that container all the data
I need to solve it different!
MaikCAE and jRaskell thanxs for the help i really appreciate.

jRaskell I really like your solution but my prof want that the main looks like this

1
2
3
4
5
/*do {
   rollDice();
   applyRoll();
   printCootie();
while (!cootieComplete()) ;*/


We are not so far that we can use a class for the lab (its next). he wants what I use a lot of variables in the main cuz he dont want us to use globals.

So can you help me out, please?
Last edited on
If your variables are not global, then you need to pass them as parameters into the functions for those functions to have visibility to them (as I showed in my previous post). It's one way or the other.
You can still declare your variables in main(), then just pass them to the appropriate functions. If you store them in an array of ints, then you'd only have to pass one parameter instead of half a dozen, otherwise you're just looking at a large parameter list.
I was working the past few hours on the game and actually think its right but I get the message that 45 C:\Dev-Cpp\lab6.cpp expected `while' before "rollDice" which i dont get why.

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
/*#include <cstdlib>
#include <iostream>

using namespace std;

int rollDice();
void applyRoll(int diceRoll, 
               int& numBody,
               int& numHead,
               int& numBodypart_3,
               int& numEye,
               int& numBodypart_5,
               int& numLeg);
bool cootieComplete(int diceRoll, 
                    int numBody,
                    int numHead,
                    int numBodypart_3,
                    int numEye,
                    int numBodypart_5,
                    int numLeg);
void printCootie(int,int numBody,int numHead);

int main()
{
   int diceRoll;
   int numBody=0;
   int numHead=0;
   int numBodypart_3=0;
   int numEye=0;
   int numBodypart_5=0;
   int numLeg=0;
   srand(time(0));
   
   do {
   diceRoll = rollDice();
   applyRoll(diceRoll,numBody,numHead,numBodypart_3,numEye,numBodypart_5,numLeg);
   printCootie(diceRoll,numBody,numHead);
   while (!cootieComplete(diceRoll,numBody,numHead,numBodypart_3,numEye,numBodypart_5,numLeg)) ;
   
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

     int rollDice(){
     int diceRoll = rand() % 6 + 1;
     return diceRoll;
     }

    void applyRoll(int diceRoll,
                   int& numBody,
                   int& numHead,
                   int& numBodypart_3,
                   int& numEye,
                   int& numBodypart_5,
                   int& numLeg){
   
            if (diceRoll == 2 && numBody == 1) 
                   //int numHead=0;
                   numHead++;
                 
    
                   if (diceRoll == 3 && numHead == 1) 
                   //int numBodypart_3=0;
                   numBodypart_3++;
                  
    
                   if (diceRoll == 4 && numHead == 1) 
                   //int numEye = 0;
                   numEye++;
                  
    
                   if (diceRoll == 5 && numHead == 1) 
                   //int numBodypart_5=0;
                   numBodypart_5++;
                
    
                   if (diceRoll == 6 && numHead == 1) 
                   //int numLeg=0;
                   numLeg++;
                 
                   
                    return;
                   }

     bool cootieComplete(int numBody,
                         int numHead,
                         int numBodypart_3,
                         int numEye,
                         int numBodypart_5,
                         int numLeg) {
         if (numBody!=0&&numHead!=0&&numBodypart_3!=0&&numEye!=0&&numBodypart_5!=0&&numLeg>=6) {
             return true;
          }
         else {
               return false;
          }
    } 

    void printCootie(int diceRoll,int numBody,int numHead){
  
                   if (diceRoll == 1) 
                   cout << "Body Added!" << endl;
                   
  
                   if (diceRoll == 2 && numBody == 1) 
                   cout << "Head Added!" << endl;
                   
    
                   if (diceRoll == 3 && numHead == 1) 
                   cout<< "Antennae, hat or bow Added!"<<endl;
                   
    
                   if (diceRoll == 4 && numHead == 1) 
                   cout<<"Eye Pice Added!" << endl;
                   
    
                   if (diceRoll == 5 && numHead == 1) 
                   cout<<"Tongue, teeth or lips Added!" << endl;
                   
    
                   if (diceRoll == 6 && numHead == 1) 
                   cout<<"Any One Leg Added!" << endl;
                   

     }*/

can you check it out?
You missed a } in your do-while

1
2
3
4
5
do {
   diceRoll = rollDice();
   applyRoll(diceRoll,numBody,numHead,numBodypart_3,numEye,numBodypart_5,numLeg);
   printCootie(diceRoll,numBody,numHead);
   } while (!cootieComplete(diceRoll,numBody,numHead,numBodypart_3,numEye,numBodypart_5,numLeg)) ;
can u tell me whats wrong with my random() it only producing 1 and not 1to6
Topic archived. No new replies allowed.