# Help with Penny Pitch Program

• 05-13-2007
Bgamer90
Help with Penny Pitch Program
I have to make this game for my class. It's called penny pitch and it's a popular game at amusement parks.

At random, 15 prizes will be layed out on screen inside a board that has 25 spaces (5 prizes in all... each prize has to be in 3 spaces), along with 1 penny being in 10 different spaces. The prizes and pennies will be in different spaces at random. If a penny (which is represented by a "x") is next to the names of all 3 of the same prizes on the board then the player wins that prize (they can win a doll,poster, game, ball, or/and a puzzle).

Here's what I have so far
Code:

```#include<a:mylib.h> #include<lvp\matrix.h> #include<lvp\vector.h> void DisplayMat(matrix<String> &mat)//Displays integer matrix by rows and columns { int a=0; char mt;   for(int x=0;x<mat.numrows();x++){ //row counter     for(int y=0;y<mat.numcols();y++){ //column counter     cout.setf(ios::right);     cout.width(3);     cout<<mat[x][y]<<"  ";     a++;     if(a&#37;5==0) //spacing     cout<<endl;     }   } } //bool Pennies(){ //matrix<String>mat(5,6)  //  mat[a][b]=h; //  cout<<"(x)"; //} void main(){   clrscr();   randomize();   cout<<"--Penny Pitch--"<<endl;   int a,b,o,C,D,E,F,G;   String h,c,d,e,f,g,z;   matrix<String>mat(5,6);     c="puzzle";     d="ball";     e="poster";     f="game";     g="doll";     z="x";   for(int x=0;x<mat.numrows();x++){   for(int y=0;y<mat.numcols();y++){     C=0;     D=0;     E=0;     F=0;     G=0;         o=1+random(6);         a=1+random(4);         b=1+random(4);       if(o==1)         h=c;         C++;       if(o==2)         h=d;         D++;       if(o==3)         h=e;         E++;       if(o==4)         h=f;         F++;       if(o==5)       h=g;       G++;     if(C=3)     cout<<"You won a puzzle!"<<endl;       if(D=3)       cout<<"You won a ball!"<<endl;         if(E=3)         cout<<"You won a poster!"<<endl;           if(F=3)           cout<<"you won a game!"<<endl;             if(G=3)             cout<<"you won a doll!"<<endl;   mat[a][b]=h;   }  }  DisplayMat(mat); // Pennies(); }```
It outputs the board (randomly). I need help though. What do I need to do to make 10 pennies randomly appear on the screen along with the prizes? What do I need to do to output what the player won and how do I make it so that there is 3 spaces out of the 25 in which each single prize is in (since the way I have it now, some prizes are in more or in less then 3 spaces; each of the 5 prizes needs to be in 3 spaces out of the 25)?

Any help will be appreciated :)
• 05-13-2007
froobly
I'm sorry I can't really answer your question, but I'm having a hard time reading your code, in part because your indenting is a bit confusing, but primarily because the variable names don't convey any meaning.

Here's a stab at cleaning up the code a bit:
Code:

```#include<a:mylib.h> #include<lvp\matrix.h> #include<lvp\vector.h> void DisplayMat(matrix<String> &mat)//Displays integer matrix by rows and columns {     int a=0;     char mt;     for(int x=0;x<mat.numrows();x++){ //row counter         for(int y=0;y<mat.numcols();y++){ //column counter             cout.setf(ios::right);             cout.width(3);             cout<<mat[x][y]<<"  ";             a++;             if(a%5==0) //spacing             cout<<endl;         }     } } /* //bool Pennies(){ //matrix<String>mat(5,6)  //  mat[a][b]=h; //  cout<<"(x)"; //} */ void main(){     int a,b,o,C,D,E,F,G;     String h,c,d,e,f,g,z;     matrix<String>mat(5,6);     c="puzzle";     d="ball";     e="poster";     f="game";     g="doll";     z="x";     clrscr();     randomize();     cout<<"--Penny Pitch--"<<endl;     for(int x=0;x<mat.numrows();x++){         for(int y=0;y<mat.numcols();y++){             C=0;             D=0;             E=0;             F=0;             G=0;             o=1+random(6);             a=1+random(4);             b=1+random(4);             if(o==1)                 h=c;             C++;             if(o==2)                 h=d;                 D++;             if(o==3)                 h=e;             E++;             if(o==4)                 h=f;                         F++;                         if(o==5)                 h=g;             G++;             if(C=3)                 cout<<"You won a puzzle!"<<endl;             if(D=3)                 cout<<"You won a ball!"<<endl;             if(E=3)                 cout<<"You won a poster!"<<endl;                         if(F=3)                 cout<<"you won a game!"<<endl;             if(G=3)                 cout<<"you won a doll!"<<endl;                         mat[a][b]=h;         }     }     DisplayMat(mat);     // Pennies(); }```
Now isn't that a little bit easier on the eyes?

A couple things jump out at me, first of which is:
Code:

```            if(o==1)                 h=c;             C++;             if(o==2)                 h=d;                 D++;             if(o==3)                 h=e;             E++;             if(o==4)                 h=f;                         F++;                         if(o==5)                 h=g;             G++;```
I'm guessing you meant for C, D, E, F and G to be incremented only if 'o' is the correct value. To do this, you need to enclose the two statements in curly braces.

Code:

```            if(o==1)             {                 h=c;                 C++;             }```
Now C will only be incremented if o is equal to 1.

Here's another thing that looks like a bug:
Code:

```            if(C=3)                 cout<<"You won a puzzle!"<<endl;```
You've used the assignment operator = instead of the equality comparator, ==, and as a result the if statement will always evaluate to true.

The classic example that was used in my college classes was
Code:

```if( underAttack = 1 ) {     FireTheMissiles(); }```
I'm pretty sure you wouldn't want that to happen, so make sure you're using the right one. An easy way to prevent this from happening is to phrase it this way:

Code:

``` if( 3 == c )     cout<<"You won a puzzle!"<<endl;```
That way, if you make a typo and accidentally put in
Code:

`if( 3 = c )`
the compiler will complain that you can't assign to the number 3 because it isn't a variable.

Now about those variables. h should probably be renamed to something more descriptive, like maybe prizeString (since that's what's being stored in there). As for c, d, e, f, g, z, those should be either similarly renamed, or done away with altogether.

```if( prizeId == 1 )   prizeString = "puzzle";```
```if( o == 1 )     h = c;```