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

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: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.

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