Thread: Help with Penny Pitch Program

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    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
    Last edited by Bgamer90; 05-13-2007 at 01:47 PM. Reason: Posted Wrong Code.

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    5
    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.

    You must admit that it's a lot easier to read
    Code:
    if( prizeId == 1 )
      prizeString = "puzzle";
    than
    Code:
    if( o == 1 )
        h = c;
    If you plan to use these strings anywhere else, you'll want them in variables, and in that case, just give the variables real names, like puzzleString or gameString, etc.

    Try a few of these things and repost your code, and I'll see what I can do about randomizing the field.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM