Sign in to follow this  

my first game and i have a problem

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is my first ever c++ game. it is a text based football game. its a pretty simple concept and i plan to improve on it as i get better. i have 1 problem so far from testing. my do while loop states that when down is greater than 4 it should end the game. but it allows for more than four downs. what did i do wrong?
//txt football version 1.0
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main( int argc,char *argv[])
{                                 // start main()
srand(time(0));                  // declare and init vars
int rsh_att =0;
int rsh_yds=0;
int pss_att=0;
int pss_yds=0;
int gain=0;
int down=1;
int scrimmage= 20;
int Oscrimmage= 20;
int fst_dwn_mrkr= Oscrimmage + 10;
int dst_to_goal;
int reply1;                    //replys and plys are user input
int reply2;
int ply1;
int ply2;
int offense[2];
int defense[2];
    do                  
    {                          // start do while loop
    cout<<"player 1 is offense. choose a type of play "
        <<" to run."
        <<" 1. run"
        <<" 2. pass"<<endl;
    cin>>reply1;
    if( reply1==1)             // start if statement
    {
    cout<<"choose a run play:"
        <<" 1. singleback "
        <<" 2. pro set"
        <<" 3. I form"
        <<" 4. shotgun"<<endl;
    cin>>ply1;
    
    switch(ply1)              // start switch case offense
    {
    case 1:offense[0]= 8;
         break;
    case 2:offense[0]= 8;
         break;
    case 3:offense[0]= 9;
         break;
    case 4:offense[0]= 6;
         break;
    default: offense[0]= 4;
    }                          // end switch case offense
    }                          // end if statement
    else                       // begin else
    {
    cout<<"choose a pass play:"
        <<" 5. single back"
        <<" 6. pro set"
        <<" 7. I form"
        <<" 8. shotgun"<<endl;
    cin>>ply1;
    
    switch(ply1)              //begin switch case offense
    {
    case 5: offense[1]= 7;
         break;
    case 6: offense[1]= 7;
         break;
    case 7: offense[1]= 6;
         break;
    case 8: offense[1]= 9;
         break;
    default: offense[1]= 5;
    }                         // end switch case offense
    }                          // end else 
    cout<<"player 2 choose a type of defense to play:"
        <<" 1. run"
        <<" 2. pass"<<endl;
    cin>>reply2;
    if(reply2==1)               // begin if statement
    {
    cout<<"choose a play:"
        <<" 1.4-3 run blitz"
        <<" 2.3-4 run blitz"
        <<" 3.4-4 crash"
        <<" 4.goaline crash"<<endl;
    cin>>ply2;
    
    switch(ply2)                // begin switch case defense
    {
    case 1: defense[0]= 7;
         break;
    case 2: defense[0]= 8;
         break;
    case 3: defense[0]= 8;
         break;
    case 4: defense[0]= 9;
         break;
    default: defense[0]= 6;
    }                          // end switch case defense
    }                          // end if statement
    else                       // begin else
    {
    cout<<"choose a play:"
        <<" 5.4-3 base"
        <<" 6 3-4 zone"
        <<" 7.nickel cover2"
        <<" 8.dime cover2"<<endl;
    cin>>ply2;
    
    switch(ply2)              // begin switch case defense
    {
    case 5: defense[1]= 6;
         break;
    case 6: defense[1]= 7;
         break;
    case 7: defense[1]= 8;
         break;
    case 8: defense[1]= 9;
         break;
    default: defense[1]=5;
    }                            //end switch case defense
    }                            // end else
    // check to see how the plays are "run"
    if(ply1<=4 && ply2 <=4)   // run vs run
    {
    defense[0]= defense[0]+1;
    gain= (offense[0]-defense[0])+ rand()%3;
    rsh_att= rsh_att =1;
    rsh_yds= rsh_yds + gain;
    scrimmage= scrimmage + gain;
    dst_to_goal= 100- scrimmage;
        if(dst_to_goal <=0)    // check for touchdown
        {                      // begin if
        cout<<" TOUCHDOWN!!! YOU WIN!!!"
            <<" you ran for"<<rsh_yds<<"yds on"<<rsh_att<<"carries!"
            <<" you passed for "<<pss_yds<<" yds on "<<pss_att<<""
            <<" attempts."<<endl;
        cin.ignore();
        return 0;
        }                      // close if
    if(scrimmage < fst_dwn_mrkr)  // begin if statement
    {
    down+=1;
    cout<<"you gained "<<gain<<" yds on that run!"
        <<" it is now down number "<<down<<" and you"
        <<" need "<<fst_dwn_mrkr - scrimmage<<" yds."<<endl;
    }                              // end if statement
    else if(scrimmage >= fst_dwn_mrkr) // begin else
    {
    Oscrimmage= scrimmage;
    cout<<"FIRST DOWN!!! nice run."<<endl;
    }                          // end else
    }                          // end if run vs run
    else if(ply1<= 4 && ply2 >=5)   // run vs pass
    {                               // begin else if
    offense[0]= offense[0] + 1;
    gain= (offense[0] - (defense[0]+4))+ rand()%7;
    rsh_att= rsh_att +1;
    rsh_yds= rsh_yds + gain;
    scrimmage= scrimmage + gain;
    dst_to_goal= 100 - scrimmage;
    if(dst_to_goal <= 0)       // check for touchdown
    {                          // begin if
    cout<<"TOUCHDOWN!!! YOU WIN!!!"
        <<" you ran for "<<rsh_yds<<" yds on "<<rsh_att<<" carries"
        <<" you passed for "<<pss_yds<<" yds on "<<pss_att<<" attempts"<<endl;
    cin.ignore();
    return 0;
    }                          // end if
     
if(scrimmage < fst_dwn_mrkr)
{                              // begin if
down+=1;
cout<<"you ran for"<<gain<<"yds"
    <<" it is now down number "<<down<<" and you need "
    <<fst_dwn_mrkr - scrimmage<<" yds "<<endl;
}                                 // end if
    else if(scrimmage >= fst_dwn_mrkr)
    {                             // begin else
    
    Oscrimmage= scrimmage;
    cout<<"FIRST DOWN! nice run"<<endl;
    }                             // end else
}                                 // end else if
else if(ply1 >= 5 && ply2 <= 4)  //pass vs run
{                                  //begin else if
offense[1]= offense[1]+1;
gain= (offense[1] - (defense[0]+4)) + rand()%10;
pss_att= pss_att + 1;
pss_yds= pss_yds + gain;
scrimmage= scrimmage + gain;
dst_to_goal= 100 - scrimmage;
    if(dst_to_goal <= 0)      // check for a touchdown
    {                         // begin if
    cout<<"TOUCHDOWN!!! YOU WIN!!!"
        <<"you ran for "<<rsh_yds<<" yds on "<<rsh_att<<" carries "
        <<"you passed for "<<pss_yds<<" yds on "<<pss_att<<" attempts "<<endl;
    cin.ignore();
    return 0;
    }                       // end if
        if(scrimmage < fst_dwn_mrkr)
        {                                 // begin if
        down+=1;
        cout<<"you threw for a "<<gain<<" yd gain "
            <<"it is now down number "<<down<<" and you"
            <<" need"<<fst_dwn_mrkr - scrimmage<<" yds. "<<endl;
        }                           // end if
        else if(scrimmage >= fst_dwn_mrkr)
        {                               // begin else
        
        Oscrimmage= scrimmage;
        cout<<"FIRST DOWN!!! nice pass"<<endl;
        }                                // end else
        }                                // end else if pass vs run
        else if(ply1 >= 5 && ply2 >=5)      // pass vs pass
        {                                // begin else
        defense[1]= defense[1] + 2;
        gain= offense[1] - defense[1] + rand()%5;
        pss_att= pss_att = 1;
        pss_yds= pss_yds = gain;
        scrimmage= scrimmage + gain;
        dst_to_goal= 100- scrimmage;
                if(dst_to_goal <=0)  // check for a touchdown
                {                   // begin if
                cout<<"TOUCHDOWN!!! YOU WIN!!!"
                    <<"you ran for "<<rsh_yds<<" yds on "<<rsh_att<<" rushes"
                    <<"you passed for "<<pss_yds<<" yds on "<<pss_att<<" attempts"<<endl;
                cin.ignore();
                return 0;
                }                   // end if
                if(scrimmage < fst_dwn_mrkr)
                {                           // begin if
                down+= 1;
                cout<<"you threw for a "<<gain<<" yd gain"
                    <<" it is now down number "<<down<<"and you"
                    <<" need "<<fst_dwn_mrkr - scrimmage<<" yds "<<endl;
                }                          // end if
                else if(scrimmage >= fst_dwn_mrkr)
                {                               // begin else
                
                Oscrimmage= scrimmage;
                cout<<"FIRST DOWN!!! nice pass"<<endl;
                }                   // end else
        }   //  end else pass vs pass
        } while(down <= 4 || dst_to_goal > 0);
        return 0;
        }            // close main

Share this post


Link to post
Share on other sites
Making it easier for the others to see ;)

//txt football version 1.0
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main( int argc,char *argv[])
{ // start main()
srand(time(0)); // declare and init vars
int rsh_att =0;
int rsh_yds=0;
int pss_att=0;
int pss_yds=0;
int gain=0;
int down=1;
int scrimmage= 20;
int Oscrimmage= 20;
int fst_dwn_mrkr= Oscrimmage + 10;
int dst_to_goal;
int reply1; //replys and plys are user input
int reply2;
int ply1;
int ply2;
int offense[2];
int defense[2];
do
{ // start do while loop
cout<<"player 1 is offense. choose a type of play "
<<" to run."
<<" 1. run"
<<" 2. pass"<<endl;
cin>>reply1;
if( reply1==1) // start if statement
{
cout<<"choose a run play:"
<<" 1. singleback "
<<" 2. pro set"
<<" 3. I form"
<<" 4. shotgun"<<endl;
cin>>ply1;

switch(ply1) // start switch case offense
{
case 1:offense[0]= 8;
break;
case 2:offense[0]= 8;
break;
case 3:offense[0]= 9;
break;
case 4:offense[0]= 6;
break;
default: offense[0]= 4;
} // end switch case offense
} // end if statement
else // begin else
{
cout<<"choose a pass play:"
<<" 5. single back"
<<" 6. pro set"
<<" 7. I form"
<<" 8. shotgun"<<endl;
cin>>ply1;

switch(ply1) //begin switch case offense
{
case 5: offense[1]= 7;
break;
case 6: offense[1]= 7;
break;
case 7: offense[1]= 6;
break;
case 8: offense[1]= 9;
break;
default: offense[1]= 5;
} // end switch case offense
} // end else
cout<<"player 2 choose a type of defense to play:"
<<" 1. run"
<<" 2. pass"<<endl;
cin>>reply2;
if(reply2==1) // begin if statement
{
cout<<"choose a play:"
<<" 1.4-3 run blitz"
<<" 2.3-4 run blitz"
<<" 3.4-4 crash"
<<" 4.goaline crash"<<endl;
cin>>ply2;

switch(ply2) // begin switch case defense
{
case 1: defense[0]= 7;
break;
case 2: defense[0]= 8;
break;
case 3: defense[0]= 8;
break;
case 4: defense[0]= 9;
break;
default: defense[0]= 6;
} // end switch case defense
} // end if statement
else // begin else
{
cout<<"choose a play:"
<<" 5.4-3 base"
<<" 6 3-4 zone"
<<" 7.nickel cover2"
<<" 8.dime cover2"<<endl;
cin>>ply2;

switch(ply2) // begin switch case defense
{
case 5: defense[1]= 6;
break;
case 6: defense[1]= 7;
break;
case 7: defense[1]= 8;
break;
case 8: defense[1]= 9;
break;
default: defense[1]=5;
} //end switch case defense
} // end else
// check to see how the plays are "run"
if(ply1<=4 && ply2 <=4) // run vs run
{
defense[0]= defense[0]+1;
gain= (offense[0]-defense[0])+ rand()%3;
rsh_att= rsh_att =1;
rsh_yds= rsh_yds + gain;
scrimmage= scrimmage + gain;
dst_to_goal= 100- scrimmage;
if(dst_to_goal <=0) // check for touchdown
{ // begin if
cout<<" TOUCHDOWN!!! YOU WIN!!!"
<<" you ran for"<<rsh_yds<<"yds on"<<rsh_att<<"carries!"
<<" you passed for "<<pss_yds<<" yds on "<<pss_att<<""
<<" attempts."<<endl;
cin.ignore();
return 0;
} // close if
if(scrimmage < fst_dwn_mrkr) // begin if statement
{
down+=1;
cout<<"you gained "<<gain<<" yds on that run!"
<<" it is now down number "<<down<<" and you"
<<" need "<<fst_dwn_mrkr - scrimmage<<" yds."<<endl;
} // end if statement
else if(scrimmage >= fst_dwn_mrkr) // begin else
{
Oscrimmage= scrimmage;
cout<<"FIRST DOWN!!! nice run."<<endl;
} // end else
} // end if run vs run
else if(ply1<= 4 && ply2 >=5) // run vs pass
{ // begin else if
offense[0]= offense[0] + 1;
gain= (offense[0] - (defense[0]+4))+ rand()%7;
rsh_att= rsh_att +1;
rsh_yds= rsh_yds + gain;
scrimmage= scrimmage + gain;
dst_to_goal= 100 - scrimmage;
if(dst_to_goal <= 0) // check for touchdown
{ // begin if
cout<<"TOUCHDOWN!!! YOU WIN!!!"
<<" you ran for "<<rsh_yds<<" yds on "<<rsh_att<<" carries"
<<" you passed for "<<pss_yds<<" yds on "<<pss_att<<" attempts"<<endl;
cin.ignore();
return 0;
} // end if

if(scrimmage < fst_dwn_mrkr)
{ // begin if
down+=1;
cout<<"you ran for"<<gain<<"yds"
<<" it is now down number "<<down<<" and you need "
<<fst_dwn_mrkr - scrimmage<<" yds "<<endl;
} // end if
else if(scrimmage >= fst_dwn_mrkr)
{ // begin else

Oscrimmage= scrimmage;
cout<<"FIRST DOWN! nice run"<<endl;
} // end else
} // end else if
else if(ply1 >= 5 && ply2 <= 4) //pass vs run
{ //begin else if
offense[1]= offense[1]+1;
gain= (offense[1] - (defense[0]+4)) + rand()%10;
pss_att= pss_att + 1;
pss_yds= pss_yds + gain;
scrimmage= scrimmage + gain;
dst_to_goal= 100 - scrimmage;
if(dst_to_goal <= 0) // check for a touchdown
{ // begin if
cout<<"TOUCHDOWN!!! YOU WIN!!!"
<<"you ran for "<<rsh_yds<<" yds on "<<rsh_att<<" carries "
<<"you passed for "<<pss_yds<<" yds on "<<pss_att<<" attempts "<<endl;
cin.ignore();
return 0;
} // end if
if(scrimmage < fst_dwn_mrkr)
{ // begin if
down+=1;
cout<<"you threw for a "<<gain<<" yd gain "
<<"it is now down number "<<down<<" and you"
<<" need"<<fst_dwn_mrkr - scrimmage<<" yds. "<<endl;
} // end if
else if(scrimmage >= fst_dwn_mrkr)
{ // begin else

Oscrimmage= scrimmage;
cout<<"FIRST DOWN!!! nice pass"<<endl;
} // end else
} // end else if pass vs run
else if(ply1 >= 5 && ply2 >=5) // pass vs pass
{ // begin else
defense[1]= defense[1] + 2;
gain= offense[1] - defense[1] + rand()%5;
pss_att= pss_att = 1;
pss_yds= pss_yds = gain;
scrimmage= scrimmage + gain;
dst_to_goal= 100- scrimmage;
if(dst_to_goal <=0) // check for a touchdown
{ // begin if
cout<<"TOUCHDOWN!!! YOU WIN!!!"
<<"you ran for "<<rsh_yds<<" yds on "<<rsh_att<<" rushes"
<<"you passed for "<<pss_yds<<" yds on "<<pss_att<<" attempts"<<endl;
cin.ignore();
return 0;
} // end if
if(scrimmage < fst_dwn_mrkr)
{ // begin if
down+= 1;
cout<<"you threw for a "<<gain<<" yd gain"
<<" it is now down number "<<down<<"and you"
<<" need "<<fst_dwn_mrkr - scrimmage<<" yds "<<endl;
} // end if
else if(scrimmage >= fst_dwn_mrkr)
{ // begin else

Oscrimmage= scrimmage;
cout<<"FIRST DOWN!!! nice pass"<<endl;
} // end else
} // end else pass vs pass
} while(down <= 4 || dst_to_goal > 0);
return 0;
} // close main

Share this post


Link to post
Share on other sites
to post code please either use the [ code ] [/ code] tags or use the source tags ([ source lang="cpp" ][ /source ]). Remove spaces from the start and end of brackets.

Is down starting from 0 or 1, if it is 0 based then it should only go up to 3 (not 4).

HTH

Share this post


Link to post
Share on other sites
Quote:
Original post by rherm23
thanks guys it worked now i just have to make a better play calling system cause i have played like 4 times and only gotten 1 first down.


did you figure out the problem? =)

btw, texans rule =P (well, no they don't, but whatever :(

Share this post


Link to post
Share on other sites
yeah i got the game to play a little better just have to make the overall gameplay a bit more enticing but hey other than "hello world" this is my first program. i rated you guys up for the rapid and responses.

oh and by the way im from san diego and a charger fan so i cant say much on the football subject we suck. hehe

Share this post


Link to post
Share on other sites
I think it's the while (down <= 4 || dst_to_goal > 0;)

Either one has to be true, and if you're still playing, the distance to the goal is always greater than 0. In your code, you're saying, "while either is true". Even if down is false, dst_to_goal can still be true, keeping the loop in action. An example of an infinite loop if you're not careful.

One thing you can do to see trouble spots is to use the debugger to see what the values are at the time that line of code is passed. You can also simplify your code to see what's breaking.

If you want the game to end when the player gets a touchdown, use &&. That way, both > 4th down and ball is in play have to be true. If one is falls, the loop ends.

EDIT: (wow, quick posters...)

EDIT2: Good luck in programming! I've always done better making text-based games myself... never quite got the hang of graphics (atleast scrolling them like in an RPG).

Also, here's a tip: Try to use more functions. That way, you don't have 1 function with a ton of code in it, and other pieces of code can use those functions.

Share this post


Link to post
Share on other sites
yeah i am going to use more functions next time for sure. i plan on either fixing up this football game or starting a text based baseball game with a lot more features player names from user input,batting averages,and the ability to save your game hopefully. i have been reading beginning c++ game programming and starting out with c++ so ill see how much i can add to the next game.

Share this post


Link to post
Share on other sites

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this