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

Pages: 1-

Drunken Programming

Name: Anonymous 2010-01-14 19:39

I wrote this over the course of 20 minutes the other day when drunk off my anus. It appears to be some sort of primitive RPG. I tried to clean things up a little, but there wasn't really much I can do for it. Rest in peace, shitrpg.c.

Also, "programming under altered states of mind"-general thread.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int player[4] = {100};
int xp = 0;


void attack (int target, int chance, int damage, int foe)
{
    if ((rand() % 100 + 1) <= chance)
    {
        player[target] -= damage;
        if (foe == 0)
            printf("You hit for %d damage!\n", damage);
        else
            printf("Enemy hit you for %d damage!\n", damage);
    }
    else
    {
        if (foe == 0)
            printf("You miss!\n");
        else
            printf("It missed!\n");
    }
}

int isdead (int target)
{
    if (player[target] <= 0)
        return 1;
    else
        return 0;
}

int spawn ()
{
    int i, j;
    for (i = (rand() % 3 + 1), j = 1; j <= i; j++)
        player[j] = (rand() % 100 + 1);
    return i;
}

void display (int enemy)
{
    printf("Enemy %d: %3d HP\n", enemy, player[enemy]);
}

int choosetarget (int enemies)
{
    int x = 0;
    do
    {
        printf ("Choose target (1 - %d): ", enemies);
        scanf ("%d", &x);
    } while (x < 1 || x > enemies);
    return x;
}

int fighting ()
{
    int enemies = spawn ();
    int alive = enemies;
    int x;
   
    while (alive > 0)
    {
        for (x = 1; x <= enemies; x++)
            display (x);
           
        attack (choosetarget(enemies), 85, 35, 0);
        for (x = 1; x <= enemies; x++)
        {
            if (isdead(x) == 1)
                alive--;
        }
       
        if (alive <= 0)
        {
            return 1;
        }
       
        for (x = 1; x <= alive; x++)
            attack (0, 50, 10, 1);
        if (isdead(0))
        {
            return 0;
        }
    }
}

int battle ()
{
    if (fighting() == 1)
    {
        printf("You beat the monsters! You get %d xperience points!\n", 100);
        xp += 100;
        return 1;
    }
    else
    {
        printf("You died. You had %d xp.", xp);
        return 0;
    }
}

int main ()
{
    srand(time(NULL));
    int dead = 0;
   
    while (dead == 0)
    {
        if (battle() == 0)
            dead = 1;
    }
   
    return 0;
}

Name: Anonymous 2010-01-14 20:22

When you are drunk you should program in PHP so you are not sure why you feel so ashamed in the morning.

Name: Anonymous 2010-01-14 20:40

>if (player[target] <= 0)
>
        return 1;
>
    else
>
        return 0;

Why do people do this? It should be:

if (player[target] <= 0)
{
        return 1;
}
return 0;

Name: Anonymous 2010-01-14 20:41

>>3
I don't know, but I hate it. It's just as bad to have a ton of else if blocks where each one returns something. If control makes it to the else if, there's no way you could've shortcut in the first place and "if" is just fine there.

Name: Anonymous 2010-01-14 20:54

Why not just
bool isdead(int target)
{
   return (player[target] <= 0);
}
?

In fact, you use it both like this and in its demonstrated implementation in the fighting function.

Name: Anonymous 2010-01-14 20:58

I think we have a problem.


Enemy 1:  73 HP
Choose target (1 - 1): 1
You hit for 35 damage!
It missed!
Enemy 1:  38 HP
Choose target (1 - 1): 1
You hit for 35 damage!
Enemy hit you for 10 damage!
Enemy 1:   3 HP
Choose target (1 - 1): 1
You hit for 35 damage!
You beat the monsters! You get 100 xperience points!
Enemy 1:  83 HP
Choose target (1 - 1): 1
You hit for 35 damage!
It missed!
Enemy 1:  48 HP
Choose target (1 - 1): 1
You miss!
It missed!
Enemy 1:  48 HP
Choose target (1 - 1): 1
You hit for 35 damage!
It missed!
Enemy 1:  13 HP
Choose target (1 - 1): 1
You hit for 35 damage!
You beat the monsters! You get 100 xperience points!
Enemy 1:  51 HP
Enemy 2:  24 HP
Enemy 3:  77 HP
Choose target (1 - 3): 2
You hit for 35 damage!
It missed!
Enemy hit you for 10 damage!
Enemy 1:  51 HP
Enemy 2: -11 HP
Enemy 3:  77 HP
Choose target (1 - 3):


-11 HP? Really?

Name: Anonymous 2010-01-14 21:05

>>5

C doesn't have a bool type built in.

Name: Anonymous 2010-01-14 21:17

>>2
As someone who hates writing PHP, I once coded some auth system in PHP while drunk. It didn't look that bad when i reviewed it while sober and it seemed reasonably secure.

Name: Anonymous 2010-01-14 22:00

>>7
Oh, right.
I've been using C++ for so long, going back down to just C can get terribly jarring.  Sorry.

Name: Anonymous 2010-01-14 22:02

>>6
Even by D&D rules, that thing should be deader than dead.

Name: Anonymous 2010-01-14 22:13

>>10
"You splatter Hunk all over the common room. You are now alone in a room that looks like a vat of beef stroganoff exploded in it."

Name: Anonymous 2010-01-15 0:36

Enemy 1:  15 HP
Enemy 2:  68 HP
Enemy 3:  58 HP
Choose target (1 - 3): 1
You hit for 35 damage!
Enemy hit you for 10 damage!
It missed!
Enemy 1: -20 HP
Enemy 2:  68 HP
Enemy 3:  58 HP
Choose target (1 - 3): 3
You hit for 35 damage!
It missed!
Enemy 1: -20 HP
Enemy 2:  68 HP
Enemy 3:  23 HP
Choose target (1 - 3): 3
You hit for 35 damage!
You beat the monsters! You get 100 xperience points!


Hm. Seems it's got some bugs.

Name: Anonymous 2010-01-15 0:53

>>3,4
if (stmt) {
    return 1;
}
return 0;


is a standard practice in C, yes. So there's nothing wrong with it. But

if (stmt) {
    return 1;
} else {
    return 0;
}


is more legible, you are clearer about your intention.

Name: Anonymous 2010-01-15 1:38

>>13
No, it's really not. It might be if you're reading the code of morons or you are a moron yourself, but I consider those to be fringe cases.

It doesn't even aid maintainability. Suppose you switch that else into an else if, which retarded return coders will use instead of just plain if. Uh oh, now not all code paths return a value. It's possible that both if cases end up not branching into return code, especially if you bring goto into it.

For maximum flexibility, maintainability, and readability, there should be a return statement outside of all control statements. The only thing if (x) return 0; else return 1; tells me is that the person that wrote the code has an inept understanding of how logic works. If that's what you mean by "intention," then sure. Why not?

Name: Anonymous 2010-01-15 5:05

Name: Anonymous 2010-01-15 6:36

>>15

I /prog/'d in my mouth a little.

Name: Anonymous 2010-01-15 6:47

>>7
It does, for over 10 years now.

Name: Anonymous 2010-01-15 7:07

I completely agree with >>14.

Name: Anonymous 2010-01-15 7:12

>>17
i think'll you find it's not built in

Name: Anonymous 2010-01-15 7:17

>>19
How hard is it to #include <stdbool.h>

Name: Anonymous 2010-01-15 7:50

Whatever,
int isdead(int target)
{
   return (player[target] <= 0);
}

Name: Anonymous 2010-01-15 8:21

Just write a macro.

Name: Anonymous 2010-01-15 8:28

Jackie Chan - Drunken Programming Master (Jui progtuku kuen)

Plot: An undisciplined boy must learn Drunken Programming Style in order to stop an assassin.

Name: Anonymous 2010-01-15 18:04

>>23
Fund it.

Name: Anonymous 2010-01-16 13:57

>>24

Anus.

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