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-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?

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