Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

hai guyz i can program

Name: Anonymous 2007-11-26 22:35

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    init:
         string player_a;
         string player_b;
         int a_hp = 100;
         int b_hp = 100;
         int a_poison = 0;
         int b_poison = 0;
         int attack = 0;
         int turn = 0;
    name:
         cout << "Name PLAYER 1: ";
         cin >> player_a;
         cout << "Name PLAYER 2: ";
         cin >> player_b;
         cout << "------------------" << endl;
    disp:
         if (a_hp < 1) {
               goto b_win;  
         }
         if (b_hp < 1) {
               goto a_win;  
         }
         cout << player_a << ": " << a_hp << endl;
         cout << player_b << ": " << b_hp << endl;
         cout << "------------------" << endl;
         if (turn == 0) {
                  turn = 1;
                  goto a_turn;
         }
         if (turn == 1) {
                  turn = 0;
                  goto b_turn;        
         }
    a_turn:
           cout << player_a << "'s turn:" << endl;
           if (a_poison == 1) {
                        cout << "* * * * * * * * * * * * * *" << endl;
                        cout << "You've been hurt by poison!" << endl;
                        cout << " * * * * * * * * * * * * *" << endl;
                        a_hp -= 2;
           }
           cout << "1. Poison Strike" << endl;
           cout << "2. Fury Slash" << endl;
           cout << "Your move: ";
           cin >> attack;
           cout << "------------------" << endl;
           switch (attack) {
                  case 1: goto poison; break;
                  case 2: goto slash; break;
                  default: goto fail; break;      
           }
    b_turn:
           cout << player_b << "'s turn:" << endl;
           if (b_poison == 1) {
                        cout << "* * * * * * * * * * * * * *" << endl;
                        cout << "You've been hurt by poison!" << endl;
                        cout << " * * * * * * * * * * * * *" << endl;
                        b_hp -= 2;
           }
           cout << "1. Poison Strike" << endl;
           cout << "2. Fury Slash" << endl;
           cout << "Your selection: ";
           cin >> attack;
           cout << "------------------" << endl;
           switch (attack) {
                  case 1: goto poison; break;
                  case 2: goto slash; break;
                  default: goto fail; break;      
           }
    poison:
           if (turn == 1) {
                    b_poison = 1;       
           }
           if (turn == 0) {
                    a_poison = 1;
           }
           goto disp;
    slash:
          if (turn == 1) {
                    b_hp -= 10;       
           }
           if (turn == 0) {
                    a_hp -= 10;
           }
           goto disp;
    fail:
         cout << "lol n00b!" << endl;
         cout << "------------------" << endl;
         goto disp;
    a_win:
          cout << "*****************************" << endl;
          cout << player_a << " is victorious!!!" << endl;
          cout << "*****************************" << endl;
          system("PAUSE");
          return 0;
    b_win:
          cout << "*****************************" << endl;
          cout << player_b << " is victorious!!!" << endl;
          cout << "*****************************" << endl;
          system("PAUSE");
          return 0;
}

Name: Anonymous 2007-11-27 6:43

Your code without the goto's:


#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[])
{
    std::string player_a;
    std::string player_b;
    int a_hp = 100;
    int b_hp = 100;
    int a_poison = 0;
    int b_poison = 0;
    int attack = 0;
    int turn = 0;
   
    std::cout << "Name PLAYER 1: ";
    std::cin >> player_a;
    std::cout << "Name PLAYER 2: ";
    std::cin >> player_b;
    std::cout << "------------------" << std::endl;
   
    while (a_hp > 0 && b_hp > 0)
    {
        std::cout << player_a << ": " << a_hp << std::endl;
        std::cout << player_b << ": " << b_hp << std::endl;
        std::cout << "------------------" << std::endl;
        if (turn == 0) {
            std::cout << player_a << "'s turn:" << std::endl;
            if (a_poison == 1) {
                std::cout << "* * * * * * * * * * * * * *" << std::endl;
                std::cout << "You've been hurt by poison!" << std::endl;
                std::cout << " * * * * * * * * * * * * *" << std::endl;
                a_hp -= 2;
                a_poison = 0;
            }
            std::cout << "1. Poison Strike" << std::endl;
            std::cout << "2. Fury Slash" << std::endl;
            std::cout << "Your move: ";
            std::cin >> attack;
            std::cout << "------------------" << std::endl;
            switch (attack) {
                case 1: b_poison = 1; break;
                case 2: b_hp -=  10; break;
                default:
                    std::cout << "lol n00b!" << std::endl;
                    std::cout << "------------------" << std::endl;
                    break;     
            }
        }
        else if (turn == 1) {
            std::cout << player_b << "'s turn:" << std::endl;
            if (b_poison == 1) {
                std::cout << "* * * * * * * * * * * * * *" << std::endl;
                std::cout << "You've been hurt by poison!" << std::endl;
                std::cout << " * * * * * * * * * * * * *" << std::endl;
                b_hp -= 2;
                b_poison = 0;
            }
            std::cout << "1. Poison Strike" << std::endl;
            std::cout << "2. Fury Slash" << std::endl;
            std::cout << "Your selection: ";
            std::cin >> attack;
            std::cout << "------------------" << std::endl;
            switch (attack) {
                case 1: a_poison = 1; break;
                case 2: a_hp -= 10;; break;
                default:
                    std::cout << "lol n00b!" << std::endl;
                    std::cout << "------------------" << std::endl;
                    break;     
            }
        }
        turn = 1 - turn;
    }

    std::cout << "*****************************" << std::endl;
    if (a_hp > 0 && b_hp < 1) {
        std::cout << player_a << " is victorious!!!" << std::endl;
    }
    else if (b_hp > 0 && a_hp < 1) {
        std::cout << player_b << " is victorious!!!" << std::endl;
    }
    else {
        std::cout << "Both players killed each other!!!" << std::endl;
    }
    std::cout << "*****************************" << std::endl;
   
    #ifdef WIN32
    std::system("PAUSE");
    #endif
    return 0;
}



Also, using functions, code reduced further:


#include <cstdlib>
#include <iostream>

void doturn (std::string name, int& hp1, int& hp2, int poison1, int& poison2)
{
    int attack;

    std::cout << name << "'s turn:" << std::endl;
    if (poison1 == 1) {
        std::cout << "* * * * * * * * * * * * * *" << std::endl;
        std::cout << "You've been hurt by poison!" << std::endl;
        std::cout << " * * * * * * * * * * * * *" << std::endl;
        hp1 -= 2;
    }
    std::cout << "1. Poison Strike" << std::endl;
    std::cout << "2. Fury Slash" << std::endl;
    std::cout << "Your move: ";
    std::cin >> attack;
    std::cout << "------------------" << std::endl;
    switch (attack) {
        case 1: poison2 = 1; break;
        case 2: hp2 -=  10; break;
        default:
            std::cout << "lol n00b!" << std::endl;
            std::cout << "------------------" << std::endl;
            break;     
    }   
}

int main(int argc, char *argv[])
{
    std::string player_a;
    std::string player_b;
    int a_hp = 100;
    int b_hp = 100;
    int a_poison = 0;
    int b_poison = 0;
    int attack = 0;
    int turn = 0;
   
    std::cout << "Name PLAYER 1: ";
    std::cin >> player_a;
    std::cout << "Name PLAYER 2: ";
    std::cin >> player_b;
    std::cout << "------------------" << std::endl;
   
    while (a_hp > 0 && b_hp > 0)
    {
        std::cout << player_a << ": " << a_hp << std::endl;
        std::cout << player_b << ": " << b_hp << std::endl;
        std::cout << "------------------" << std::endl;
        doturn(player_a, a_hp, b_hp, a_poison, b_poison);
        doturn(player_b, b_hp, a_hp, b_poison, a_poison);
        turn = 1 - turn;
    }

    std::cout << "*****************************" << std::endl;
    if (a_hp > 0 && b_hp < 1) {
        std::cout << player_a << " is victorious!!!" << std::endl;
    }
    else if (b_hp > 0 && a_hp < 1) {
        std::cout << player_b << " is victorious!!!" << std::endl;
    }
    else {
        std::cout << "Both players killed each other!!!" << std::endl;
    }
    std::cout << "*****************************" << std::endl;
   
    #ifdef WIN32
    std::system("PAUSE");
    #endif
    return 0;
}


And, with the use of a class, the code can be cleaned up again:


#include <cstdlib>
#include <iostream>

class Player {
public:
    int hp;
    int poison;
    std::string name;
   
    Player () : hp(100), poison(0) {}
   
    void doturn (Player& p)
    {
        int attack;

        std::cout << name << "'s turn:" << std::endl;
        if (poison == 1) {
            std::cout << "* * * * * * * * * * * * * *" << std::endl;
            std::cout << "You've been hurt by poison!" << std::endl;
            std::cout << " * * * * * * * * * * * * *" << std::endl;
            hp -= 2;
        }
        std::cout << "1. Poison Strike" << std::endl;
        std::cout << "2. Fury Slash" << std::endl;
        std::cout << "Your move: ";
        std::cin >> attack;
        std::cout << "------------------" << std::endl;
        switch (attack) {
            case 1: p.poison = 1; break;
            case 2: p.hp -=  10; break;
            default:
                std::cout << "lol n00b!" << std::endl;
                std::cout << "------------------" << std::endl;
                break;     
        }   
    }
};

int main(int argc, char *argv[])
{
    int turn = 0;
    Player a;
    Player b;

    std::cout << "Name PLAYER 1: ";
    std::cin >> a.name;
    std::cout << "Name PLAYER 2: ";
    std::cin >> b.name;
    std::cout << "------------------" << std::endl;
   
    while (a.hp > 0 && b.hp > 0)
    {
        std::cout << a.name << ": " << a.hp << std::endl;
        std::cout << a.name << ": " << b.hp << std::endl;
        std::cout << "------------------" << std::endl;
        a.doturn(b);
        b.doturn(a);
        turn = 1 - turn;
    }

    std::cout << "*****************************" << std::endl;
    if (a.hp > 0 && b.hp < 1) {
        std::cout << a.name << " is victorious!!!" << std::endl;
    }
    else if (b.hp > 0 && a.hp < 1) {
        std::cout << b.name << " is victorious!!!" << std::endl;
    }
    else {
        std::cout << "Both players killed each other!!!" << std::endl;
    }
    std::cout << "*****************************" << std::endl;
   
    #ifdef WIN32
    std::system("PAUSE");
    #endif
    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List