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

Pages: 1-4041-

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-26 22:41

>>1
read SICP

Name: Anonymous 2007-11-26 22:58

>>2

hai it still compiles and runs, so i'm happy.

Name: Anonymous 2007-11-26 23:15

Dijkstra would be turning over in his grave if he saw this

Name: Anonymous 2007-11-27 0:02

>>3
Read SICP or gtfo my /prog/

Name: Anonymous 2007-11-27 0:52

>>4
>>5

Explain what's so terrible about my code? I'm a noob, and I wrote it quick, so i'm sure there are better ways I could've gone about writing it.

As for reading SICP, I get bored fast with textbooks. Real fast. I think I may have ADD, actually... but anyway, the point is that I'd learn better if someone showed me the "proper" way to go about coding this.

Name: Anonymous 2007-11-27 1:01

>>6
the proper way is in SICP.

Name: Anonymous 2007-11-27 1:21

>>6
Shut the fuck up and read SICP. If you are not willing to put the time into SICP, then give me one good reason why we should put our time and intelligence into some cocky noob fuck who's here for a quick grab?

Fuck off.

Name: Anonymous 2007-11-27 1:33

>>7
>>8

I'm aware that SICP contains the knowledge I need, ok? But I find it downright impossible to read a textbook without either skimming liberally or having my mind wander. The best way for me to learn is for me to deconstruct, reverse engineer it through tinkering.

How am I a "cocky noob who's here for a quick grab"? It's not like i'm asking you to write a program for me to take credit for. I'm just trying to learn C++, sheesh. Any code you give me will just be used to improve and build upon my knowledge.

Name: Anonymous 2007-11-27 1:53

>>9
Have your mom read it to you. That way, you both will learn of SICP.

Name: Anonymous 2007-11-27 2:21

i am victorious

Name: Anonymous 2007-11-27 3:31

GOTOS CONSIDERED HARMFUL

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;
}

Name: Anonymous 2007-11-27 6:58

>>13
I lol'd at your improvement to system("pause")

Name: Anonymous 2007-11-27 7:24

>>14
You have way too much time on your hands

Name: Anonymous 2007-11-27 7:33

>>15
NO U

Name: Anonymous 2007-11-27 7:53

>>15
what? anybody should be able to rip something like this within 10 mins.

Name: Anonymous 2007-11-27 8:14

>>17
I ripped up something like this within 15 nanoseconds of my birth.

Name: Anonymous 2007-11-27 8:15

This is shit. From a butt.

Name: Anonymous 2007-11-27 8:16

Anybody should be able to rip loads of horrible code within 10 mins?

Name: Anonymous 2007-11-27 8:32

>>20

Sure, why not.

Name: Bacu !!DMsSxeOfR1ab7SB 2007-11-27 11:18

Spaghetti code is worse than copypasta.

use some functions, dammit.

only use goto for breaking out of a multi-level loop.

Name: Anonymous 2007-11-27 11:26

>>22
NO EXCEPTIONS?

Name: Anonymous 2007-11-27 11:26

>>22
And even then, use it sparingly.

Name: Anonymous 2007-11-27 11:27

>>23
No, use try .. catch for that.

Name: Anonymous 2007-11-27 11:38

case 1: goto poison; break;
Epic fail for using break after a GOTO.

Name: Anonymous 2007-11-27 11:45

>goto
lul wut

Name: Anonymous 2007-11-27 12:00

/prog/ needs a BAN HAMMER for shitfuckers who can't fucking code.  We're not your teacher, faggots.

Name: Anonymous 2007-11-27 12:30

>>13
std::aids

Name: Anonymous 2007-11-29 1:08


#ifdef WIN32
std::sout << "Your operating system is not supported." << std::endl;
#else
// Rest of program
#endif

Name: Anonymous 2007-11-29 2:52

>>29
Lol that is really funny. You are a funny guy.

Name: Anonymous 2007-11-29 3:40

>>31
That's not funny, my brother died that way.

Name: Anonymous 2007-11-29 4:13

>>32
Now that was funny.

Name: Anonymous 2007-11-29 4:16

>>33
Lurk moar.

Name: Anonymous 2007-11-29 6:02

>>34
That's not funny, my brother died that way.

Name: Anonymous 2007-11-29 6:11

>>35
Now that was funny.

Name: Anonymous 2007-11-29 6:15

MY OTHER CAR DIED THAT WAY

Name: Anonymous 2007-11-29 13:59

Gayest shit I have ever seen on /prog/.

Name: Anonymous 2009-03-18 2:14

Don't call me gay, but I need some mary jay!

Marijuana MUST be legalized.

Name: Anonymous 2010-12-06 9:39

Back to /b/, ``GNAA Faggot''

Name: Anonymous 2011-02-03 8:32

Name: Anonymous 2013-01-18 22:50

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

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