// | Credits: Concept, Design, Programing, testing, bitching-               |
// |        Adam (Abe) Brown                                                |
// |-=Revision notes=-                                                      |
// |  15JAN2004- Core game created                                          |
// |  16JAN2004- Cleaned code up, added comments for further clarification  |
// |  ??FEB2004- Implemented cls and getch to give more of a "game" feel    |

#include <iostream>
#include <conio.h>
#include <ctime>
using namespace std;

// Print the static top wall
void topwall()
{
    cout << "_ _ _ _ _ _" << endl;
}

// Print hall way and contents (dynamic)
int hallway(int possition)
{
    // Set up func specific vars
    int counter;
    int pos_check1;
    int pos_check2;
    int enemy_chance;
    int enemy_pos;
    int enemy_type;
    
    // Set initial counter value
    counter = 1;
    
    // Main hall way print loop
    while(counter < 7)
    {
        // If possition = current print possition, print player
        if(possition == counter)
        {
            cout << "*";
        }
        else
        {
            // init Rand() function
            srand(time(NULL));
            // Set enemy_chance = a # between 0 and 6
            enemy_chance = rand() % 6;        
            // If printing space before player, chance of enemy
            if(possition-1 == counter)
            {
                // Decide of player will encounter an enemy
                if(enemy_chance < 2)
                {
                    // Decide if enemy will be plebe or rat
                    enemy_type = rand() % 6;
                    if(enemy_type > 4)
                    {
                        cout << "p";
                        enemy_pos = 0-counter;
                    }
                    else
                    {
                        cout << "r";
                        enemy_pos = counter;
                    }
                }
                // If not print empty space
                else
                {
                    cout << " ";
                }
            }
            // If printing space before player, chance of enemy
            else if(possition+1 == counter)
            {
                // Decide of player will encounter an enemy
                if(enemy_chance > 4)
                {
                    // Decide if enemy will be plebe or rat
                    enemy_type = rand() % 6;
                    if(enemy_type > 4)
                    {
                        cout << "p";
                        enemy_pos = 0-counter;
                    }
                    else
                    {
                        cout << "r";
                        enemy_pos = counter;
                    }
                }
                // If not print empty space
                else
                {
                    cout << " ";
                }
            }
            // If not print empty space
            else
            {
                cout << " ";
            }
        }
        
        // Print static space    
        cout << " ";
        
        // Increment counter one
        counter++;
    }
    
    cout << endl;
    return enemy_pos;
}

// Print the static bottom wall
void bottomwall()
{
    cout << "- - - - - -" << endl;
}

// Do move logic
int movelogic(char input, int possition)
{   
    if(input == 'a')
    {
        // If new possition won't be off the hallway set new possition
        if(possition != 1)
        {
            possition = possition - 1;
        }
        // New possition was off hallway, don't move
        else
        {
            cout << "-=Already at the end=-" << endl;
        }
    }
    else if(input == 'd')
    {
        // If new possition won't be off the hallway set new possition
        if(possition != 6)
        {
            possition = possition + 1;
        }
        // New possition was off hallway, don't move
        else
        {
            cout << "--Already at the end=-" << endl;
        }
    }
    
    return possition;
}

int main()
{
    system("cls");
    // Define core vars
    char input;
    int possition;
    int enemy_pos;
    int health;
    int merrits;
    int enemy_attack;
    int message;
    int enemy_type;
    int new_level;
    
    // Set up default var values
    possition = 1;
    health = 10;
    merrits = 0;
    message = 0;
    new_level = 0;
    
    // Print intro text
    cout << "Welcome to Hargaden Killer 2.1!" << endl << "By: Abe (aka Brown)" << endl;
    cout << endl << "  In this episode our hero Jack Middleton must fight his way out of the basement";
    cout << "of F Company smiting plebes and rats alike to reach his ultimate goal," << endl;
    cout << "the 3rd floor, and the lair of Hargaden!" << endl << endl;
    
    system("pause");
    
    cout << endl << "Game:" << endl << "Stomp Rats for merits and eat plebes for health!" << endl << "Get 5 Merrits to go up a floor." << endl;
    cout << endl << "Controls:" << endl << "q to exit" << endl << "a left <-" << endl << "d right ->" << endl;
    cout << endl;
    
    system("pause");
    
    cout << endl << endl;

    while(input != 'q')
    {
        system("cls");
        if(merrits < 15)
        {
            if(health > 0)
            {
                // Print user stats
                cout << endl;
        
                // Pick level title based on merrits
               if(merrits < 5)
                {
                    cout << "Level 1: F co. Basement";
                }
                else if(merrits < 10 && merrits > 4)
                {
                    cout << "Level 2: F co. 1st Floor";
                }
                else if(merrits >= 10)
                {
                    cout << "Level 3: F co. 2nd Floor";
                }
        
                cout << endl << "Health: " << health << endl << "Merrits: " << merrits << endl;
        
                
                
                // Print new floor notification text, reset new floor indication var
                if(new_level == 1)
                {
                    cout << "--=!NEW FLOOR!=--" << endl;
                    new_level = 0;
                }
                
                // Call static wall print
                topwall();
    
                // Call dynamic hall content
                enemy_pos = hallway(possition);
    
                // Call static wall print
                bottomwall();
                
                // Display action message
                if(message > 0)
                {
                    if(message == 1)
                    {
                        cout << endl << "The rat bit Jack and ran away." << endl;
                    }
                    else if(message == 2)
                    {
                        cout << endl << "Jack got 1 Merrit for stomping a rat to death!" << endl;
                    }
                    else if(message == 3)
                    {
                        cout << endl << "Jack got 2 Health for eating a Plebe!" << endl;
                    }
                    else if(message == 4)
                    {
                        cout << endl << "Jack is benevolent and let this one go, he is full on health any way." << endl;
                    }
                    message = 0;
                }
                
                // move option
                input = getch();

                // Call move logic
                possition = movelogic(input, possition);
        
                // Reset enemy type
                enemy_type = 0;
        
                // If enemy was plebe set enemy type var and rationalize enemy_pos var
                if(enemy_pos < 0)
                {
                    enemy_pos = enemy_pos * -1;
                    enemy_type = 1;
                }
                
                // If play engaged enemy
                if(possition == enemy_pos)
                {
                    // If enemy was rat
                    if(enemy_type == 0)
                    {
                        // init Rand() function
                        srand(time(NULL));
                        // Set enemy_chance = a # between 0 and 6
                        enemy_attack = rand() % 3;
                        // Rat bit player
                        if(enemy_attack > 1)
                        {
                            health = health-1;
                            message = 1;
                        }
                        // Player killed rat
                        else
                        {
                            merrits = merrits+1;
                            message = 2;
                            // If this merrit advanced play to new floor que new floor alert
                            if(merrits == 5 || merrits == 10)
                            {
                                 new_level = 1;
                            }
                        }
                    }
                    // Enemy was plebe
                    else
                    {
                        // If Hurt, eat plebe
                        if(health < 10)
                        {
                            // Player only needs 1 health so only give 1
                            if(health == 9)
                            {
                                health = 10;
                                message = 3;
                            }
                            // Give health normaly
                            else
                            {
                                health = health + 2;
                                message = 3;
                            }
                        }
                        // Not hurt, no health
                        else
                        {
                            message = 4;
                        }

                    }
                }
                // Reset enemy possition
                enemy_pos = 0;
           }
           // User has died
           else
           {
               cout << endl << "Jack has died!, the world is sad..." << endl;
               system("pause");
               return 0;
           }
        }
        else
        {
            // If first time, print initial text
            if(merrits == 15)
            {
                cout << endl << endl << "YOU HAVE REACHED THE 3RD FLOOR OF F COMPANY! THE LAIR OF HARGADEN IS AT HAND!" << endl;
                merrits = merrits++;
            }
            // Else print retry text
            else
            {
                cout << endl << endl  << "Well that was fun, want to try a different weapon?" << endl << "Select q to exit" << endl;
            }
            
            // Print weapon choices
            cout << "Choose your weapon and slay the beast!" << endl;
            cout << endl << "1. Sledge Hammer" << endl << "2. Molotov Cocktail" << endl << "3. M4A1 Colt Carbine" << endl;
            // User select weapon
            cout << "Choice:";
            cin >> input;
            
            // Display result text
            if(input == '1')
            {
                cout << endl << endl << "Picking up the massive Sledge Hammer you look into Hargaden's beady little eyes and see in them all that is wrong and soulless in the world. With one heaving swipe of iron and wood Hargaden's head explodes like a water melon, and the world was happy." << endl;
                cout << endl;
                system("pause");
            }
            else if(input == '2')
            {
                cout << endl << endl << "Our hero Jack Middleton enters the lair and lights his special Cocktail. \"I made this one especially for you, you beady eyed bastard.\" He says under his breath as he sneaks up behind the beast. Just as Hargaden turns around our hero smashes the flaming cocktail into the beast's face narrowly dodging the flames. Haraden burned and the world was happy." << endl;
                cout << endl;
                system("pause");
            }
            else if(input == '3')
            {
                cout << endl << endl << "Our hero Jack Middleton enters the lair and presses his back against the wall, the beast is close. He can hear the Hargaden breathing now, just around the corner. He drops his empty mag and locks and loads a fresh 30 round FMJ magazine. The sound rouses the beast! Wheeling around the corner Jack comes face to face with the Hargaden! He unleashes a hail of Hollow point rounds into his chest tearing out a hole the size of a basket ball in the back of the beast. \"Now at least the Plebes won't have you molesting them.\" Your hero sneers and turns his back on the bloody mess that was Hargaden. And the world was happy." << endl;
                cout << endl;
                system("pause");
             }
            else if(input == '4')
            {
                cout << endl << endl << "YOU HAVE CHOOSEN THE SECRET WEAPON! Our hero enters the lair and steps into the open in front of the best. \"I've been wanting to do this for the longest time you damn dirty ape!\" Jack leaps with in human speed at the beast \"Your ass is mine Hargaden!\" He savagely mauls it to the ground.  In one gruesome motion he punches his fist into the base of Hargaden's skull and rips his spine out. Our hero stands over the crumpled body of the defeated with spine in hand and smirks. \"It is a good day\" he says, and walks from the lair triumphantly. And the world was happy." << endl;
                cout << endl;
                system("pause");
             }
            else if(input != 'q')
            {
                cout << endl << endl << "wow.. you couldn't even make a selection from a simple list... Proud day for you and your family." << endl;
                cout << endl;
                system("pause");            
            }
        }
    }
    
    // Print good bye text
    cout << endl << "Thanks for playing Hargaden Killer 2.1!" << endl << "Look forward to more Hargaden slaughtering fun in Hargaden Killer 3.0!" << endl;
    system("pause");
    return 0;
}
