Python to C++ Program Questions
Question: It's my second week of my junior year of college and my professor has given me
the assignment of taking a python program, which i'm very familiar with, and
turning it into a C++ program. I know next to nothing about C++ having only
had 1 lecture on the topic and my book not arriving for another couple days.
Here is the python program..

    
    
    import random
    import sys
    import time
    
    print( "BATTLE GAME" );
    
    done = False
    combatRound = 0
    
    playerHP = 20
    playerAtk = random.randint( 5, 8 )
    playerDef = random.randint( 5, 8 )
    playerAgi = random.randint( 5, 8 )
    
    enemyHP = 20
    enemyAtk = random.randint( 5, 7 )
    enemyDef = random.randint( 5, 7 )
    enemyAgi = random.randint( 5, 7 )
    
    delayLength = 0.5
    
    print()
    print( "-------------" )
    print( "FIGHTER STATS" )
    print( "-------------" )
    
    print()
    print( "PLAYER STATS:" )
    print( "HP " + str( playerHP ) )
    print( "ATK " + str( playerAtk ) )
    print( "DEF " + str( playerDef ) )
    print( "AGI " + str( playerAgi ) )
    
    print()
    print( "ENEMY STATS:" )
    print( "HP " + str( enemyHP ) )
    print( "ATK " + str( enemyAtk ) )
    print( "DEF " + str( enemyDef ) )
    print( "AGI " + str( enemyAgi ) )
    
    print()
    choice = input( "Ready to begin? (yes/no) " )
    
    while ( choice != "yes" ):
        print( "Well, what are you waiting for? \n" )
        choice = input( "Ready to begin? (yes/no) " )
    
    while ( done == False ):
        combatRound += 1
        print()
        print( "###########################################" )
        print( "-------------------------------------------" )
        print( "ROUND " + str( combatRound ) )
        print( "PLAYER:\t HP " + str( playerHP ) + " ATK " + str( playerAtk ) + " DEF " + str( playerDef ) + " AGI " + str( playerAgi ) )
        print( "ENEMY:\t HP " + str( enemyHP ) + " ATK " + str( enemyAtk ) + " DEF " + str( enemyDef ) + " AGI " + str( enemyAgi ) )
        print( "-------------------------------------------" )
        print( "1. Quick Attack" )
        print( "2. Standard Attack" )
        print( "3. Heavy Attack" )
        playerChoice = input( "What will you do? " )
        playerChoice = int( playerChoice )
    
        while ( playerChoice < 1 or playerChoice > 3 ):
            playerChoice = input( "Invalid choice, choose between 1 and 3: " )
            playerChoice = int( playerChoice )
    
        enemyChoice = random.randint( 1, 3 )
        print( "Enemy chooses option " + str( enemyChoice ) )
        print( "-------------------------------------------" )
    
        # Adjust player and enemy stats based on the type of attack they're doing
        adjustedPlayerAtk = playerAtk
        adjustedPlayerAgi = playerAgi
    
        adjustedEnemyAtk = enemyAtk
        adjustedEnemyAgi = enemyAgi
    
        if ( playerChoice == 1 ):
            adjustedPlayerAgi += 2
            adjustedPlayerAtk -= 1
        elif ( playerChoice == 3 ):
            adjustedPlayerAgi -= 1
            adjustedPlayerAtk += 2
    
        if ( enemyChoice == 1 ):
            adjustedEnemyAgi += 2
            adjustedEnemyAtk -= 1
        elif ( enemyChoice == 3 ):
            adjustedEnemyAgi -= 1
            adjustedEnemyAtk += 2
    
        # Begin battle
        print( "PLAYER attacks ENEMY!" )
        time.sleep( delayLength )
    
        randDiff = random.randint( 0, adjustedPlayerAgi )
        time.sleep( delayLength )
    
        if ( randDiff >= 1 ):
            # Calculate damage
            damage = int( adjustedPlayerAtk - enemyDef / 2 )
            print( "PLAYER hits ENEMY for " + str( damage ) + " damage!" )
            enemyHP -= damage
    
        else:
            print( "PLAYER misses!" )
    
        time.sleep( delayLength )
    
    
        print( "ENEMY attacks PLAYER!" )
        time.sleep( delayLength )
    
        randDiff = random.randint( 0, adjustedEnemyAgi )
        time.sleep( delayLength )
    
        if ( randDiff >= 1 ):
            # Calculate damage
            damage = int( adjustedEnemyAtk - playerDef / 2 )
            print( "ENEMY hits PLAYER for " + str( damage ) + " damage!" )
            playerHP -= damage
    
        else:
            print( "ENEMY misses!" )
    
        time.sleep( delayLength )
    
        # Check to see if either player is knocked out
        if ( playerHP <= 0 ):
            print( "PLAYER has fallen!" )
            done = True
    
        elif ( enemyHP <= 0 ):
            print( "ENEMY has fallen!" )
            done = True
    
    # Game main loop
    
    # At this point, either player or enemy has fallen.
    print()
    print( "    #####################################   " )
    print( " ####                                   ####" )
    print( "#           G A M E     O V E R             #" )
    print( "# ######################################### #" )
    print( "##                                         ##" )
    print()
    
    if ( playerHP <= 0 ):
        print( "Too bad, you lose!" )
    elif ( enemyHP <= 0 ):
        print( "Congratulations, you won!" )
    

And here is my C++ code

    
    
    #include <iostream>
    #include <string>
    #include <random>
    using namespace std;
    
    
        int playerHP = 20, int enemyHP = 20;
        int playerAtk =random.randint(5,8); //How do you do random in C+++
        int playerDef = random.randint(5,8);
        int playerAgi = random.randint(5,8);
        int enemyAtk = random.randint(5,7);
        int enemyDef = random.randint (5,7);
        int enemyAgi = random.randint (5,7);
        float delayLength=.5;
        cout<<"---------------"<< endl;
        cout<<"FIGHTER STATS"<< endl;
        cout<<"----------------"<<endl;
        cout<<"Player Stats:"<<endl;
        cout<<"HP "<<playerHP<<endl;
        cout<<"ATK "<<playerAtk<<endl;
        cout<<"DEF "<<playerDef<<endl;
        cout<<"AGI "<<playerAgi<<endl;
        cout<<"ENEMY STATS:"<<endl;
        cout<<"HP "<<enemyHP<<endl; 
        cout<<"ATK "<<enemyAtk<<endl;
        cout<<"DEF "<<enemyDef<<endl;
        cout<<"AGI "<<enemyAgi<<endl;
        while (playerHP>0 || enemyHP>0)
        {
            int combatRound=0;
            int playerChoice;
            int enemyChoice;
            int adjustedPlayerAtk;
            int adjustedPlayerAgi;
            int adjustedEnemyAtk;
            int adjustedEnemyAgi;
            int randDiff;
            int damage;
            int damage2;
            int randDiff2
            combatRound +=1
            cout<<""<<endl;
            cout<<"############################"<<endl;
            cout<<"----------------------------"<<endl;
            cout<<"Round "<<combatRound<<endl;
            cout<<"Player:"<<playerHP<<playerAtk<<playerAgi<<endl;
            cout<<"Enemy:"<<enemyHP<<enemyAtk<<enemyAgi<<endl;
            cout<<"----------------------------"<<endl;
            cout<<"1. Quick Attack"<<endl;
            cout<<"2. Standard Attack"<<endl;
            cout<<"3. Heavy Attack"<<endl;
            cout<<"What will you do? "<<endl;
            cin>>playerChoice;
            while playerChoice != 1 || 2 || 3
                cout<<"Invalid choice, choose between 1 and 3:"<<endl;
                cout<<"What will you do?"<<endl;
                cin>>playerChoice;
    
            enemyChoice=random.randint (1,3);
            cout<< "Enemy chooses option "<<enemyChoice<<endl;
            cout<<"--------------------------------"<<endl;
            if playerChoice ==1
                adjustedPlayerAgi +=2
                adjustedPlayerAtk -=1
            if playerChoice ==3
                adjustedPlayerAgi -=1
                adjustedPlayerAtk +=2
            if enemyChoice ==1
                adjustedEnemyAgi += 2
                adjustedEnemyAtk -= 1
            if enemyChoice ==3
                adjustedEnemyAgi -= 1
                adjustedEnemyAtk += 2
            cout<<"PLAYER attacks ENEMY!"<<endl;
            time.sleep( delayLength )  //How do you do this in C++?
            randDiff = random.randint(0, adjustedPlayerAgi)
            time.sleep(delayLength)
            if randDiff >=1
                damage=(adjustedPlayerAtk-enemyDef/2)
                enemyHP -= damage
            else
                cout<<"PLAYER misses!"<<endl;
            time.sleep (delayLength)
    
            cout<<"ENEMY attacks PLAYER!"<<endl;
            time.sleep(delayLength)
            randDiff2= random.randint( 0,adjustedEnemyAgi)
            time.sleep(delayLength)
            if randDiff >=1
                damage2= (adjustedEnemyAtk - playerDef/2)
                cout<<"ENEMY hits PLAYER for "<<damage2<<"damage!"<<endl;
                playerHP -= damage2
            else
                cout<<"ENEMY misses!"<<endl;
            time.sleep (delayLength)
    
        }
    
    cout<<""<<endl;
    cout<<"GAME OVER"<<endl;
    if playerHP <=0
        cout<<"Too bad you lose"<<endl;
    if enemyHP <=0
        cout<<"YOU WON"<<endl;
    

I honestly have no idea how correct or how wrong it is. I'm currently writing
it in notepad because the professor hasn't given us access to Visual Studios.
Any tips, tricks, or advice is EXTREMELY appreciated. Once again I apologize
for how nasty it looks but I really did do my best on it and I can't wait to
learn more about this new and exciting language.


Answer: To do random numbers in C++:

    
    
    rand() % 10 + 1
    

That will give you a random value between 1 and 10. `rand()` will give a
random number from 0, to `RAND_MAX` (32727); so use maths to turn all the
numbers into the range you want.

Also:

Make sure you use `;` at the end

`combatRound +=1` is one example of where you didn't.

Also make sure you use `(` and `)`

`if playerChoice == 1` is an example of where you didn't.

And: `while playerChoice != 1 || 2 || 3`

should be

`while( playerChoice != 1 && playerChoice != 2 && playerChoice != 3 )`

And finally:

To delay in C++:

If you use Windows (Also use `include <windows.h>`): `Sleep()` and use
milliseconds

And if you use UNIX use `sleep` and it is in seconds(use `include
<unistd.h>`).

