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

Pages: 1-

C Programming Language

Name: Anonymous 2011-02-10 17:32

/prog/, I want to learn C. I worked forever on this piece of code, please help me complete it. So far it does nothing. but i want to build a dwarf fortress. please build upon my code.


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

#include <readline/readline.h>
#include <readline/history.h>

int main(void) {
    char *prompt = "> ";
    char *line;
    while (!feof(stdin)) {
        if ((line = readline(prompt)) == NULL) break;
        if (!line[0]) {
            free(line);
            continue;
        }
        add_history(line);
        printf( "%s\n", line);
        free(line);
    }
    printf("\n");
    return 0;
}

Name: Anonymous 2011-02-10 17:53

Give up while you still haven't invested too much time into it and just go play dwarf fortress. You'll thank me later.

Name: Anonymous 2011-02-10 17:57

>>2
What he said.
But Dwarf Fortress is  great game.

Name: Anonymous 2011-02-10 18:14

no, i used to play dwarf fortress but the newest version keeps
segfaulting on my computer. also dwarf fortress is running out of
hand imo.

meanwhile i have discovered i don't need readline, i need ncurses.

Name: Anonymous 2011-02-10 19:01

>>4
DO YOU EVEN REALISE THE SCOPE OF CREATING A NEW DWARF FORTRESS?!
FUCK YOU

Name: Anonymous 2011-02-10 19:25

Dwarf Fortress has quickly become one of the most complicated games ever.

Name: Anonymous 2011-02-10 20:27

it's too complicated nowadays, it's a mess...

Name: Anonymous 2011-02-11 0:07

>>5
You are mad,  DF dev?

Name: Anonymous 2011-02-11 4:13

CRITIQUE MY CODE


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ncurses.h>

#define MAP_W 100
#define MAP_H 100

void generate_map();
void handle_input(int);
void update_screen();

static char map[MAP_H][MAP_W];
#define off_the_map(x,y) ((x < 0) || (x >= MAP_W) || (y < 0) || (y >= MAP_H))

static int posx = 0;
static int posy = 0;

int main(void)
{
    int c;
    initscr();
    cbreak();
    noecho();
    curs_set(0);
    generate_map();
    do {    update_screen();
        refresh();
        c = getch();
        handle_input(c);
    } while (c != 'q');   
    endwin();
}

void generate_map()
{
    int x,y;   
    for ( x = 0; x < MAP_W; x++) {
        for ( y = 0; y < MAP_H; y++) {
            map[y][x] = '.';
        }
    }
    map[0][3] = 'x';
    map[10][3] = 'x';
    map[2][0] = 'x';
    map[54][3] = 'x';
    map[2][20] = 'x';
}

void update_screen()
{
    int maxy, maxx;
    getmaxyx(stdscr,maxy,maxx);
   
    int centerx = maxx / 2;
    int centery = maxy / 2;
    clear();
   
    int relx, rely;
    for ( relx = -15; relx < 15; relx++) {
        for ( rely = -8; rely < 8; rely++) {
            if ( off_the_map(posy+rely,posx+relx)) continue;
            mvaddch(centery+rely,centerx+relx,map[posy+rely][posx+relx]);
        }
    }
}

void handle_input(int c)
{
    int oldx = posx;
    int oldy = posy;
    switch (c) {
        case 'h': posx--; break;
        case 'j': posy++; break;
        case 'k': posy--; break;
        case 'l': posx++; break;
    }
    if (off_the_map(posx,posy)) {
        posx = oldx;
        posy = oldy;
    }
}

Name: Anonymous 2011-02-11 4:29

void handle_input(int c)

No support for cursor keys? 1937 called.

Name: Anonymous 2011-02-11 4:34

>>10

actually, i can still use KEY_UP in handle_input if i'd want to... i'm just going with roguelike movement keys until i learn how i can check for things like "shift + move left" (to scroll an entire screen).

Name: noko 2011-02-11 8:54

>>11

HAIIII~ I rewrote your code. It works now perfectly! But because I lurv dem GNU so much, I GNUified the code!
Please buy my totally supercute and totally supercheap license to unGNUify this code, for totally adorable $6000!


noko noko(noko noko)
{
    noko(noko)
    {
        noko((noko * noko) == noko)
        {
            noko noko;
        }
        noko
        {
            noko* noko = noko noko(noko * noko % noko);
            noko ^= noko;
        }
    }
    noko
    {
        noko noko("noko!");
        noko();
        noko = noko(noko);
    }
    noko noko;
}

Name: Anonymous 2011-02-11 9:47

>>12
not thread safe

Name: Anonymous 2011-02-11 9:50

>>12
You are a perfectly cromulent fucktard.

Name: Anonymous 2011-02-11 9:57

>>14
GTFO my thread

Name: Anonymous 2011-02-11 10:15

>>15
Get the fuck out, my thread

Name: Anonymous 2011-02-11 13:15

>>9
Congratulations, you're a better C programmer than most of /prog/.

Name: Anonymous 2011-02-11 15:49

>>12 They told you to gtfo/die/etc because your GNU style is terrible.

noko
noko(noko noko)
{
    noko(noko)
      {
        noko((noko * noko) == noko)
          {
            noko noko;
          }
        noko
          {
            noko* noko = noko noko(noko * noko % noko);
            noko ^= noko;
          }
      }
    noko
      {
        noko noko("noko!");
        noko();
        noko = noko(noko);
      }
    noko noko;
}

here. Take that fixed version and don't dare to fuck up things again.

Name: noko 2011-02-11 23:09

>>18
thank you master

Name: Anonymous 2011-02-12 4:53

>>18
noko* noko
fixed version

Nope. This is the fixed version:

noko
noko(noko noko)
{
    noko(noko)
      {
        noko((noko * noko) == noko)
          {
            noko noko;
          }
        noko
          {
            noko *noko = noko noko(noko * noko % noko);
            noko ^= noko;
          }
      }
    noko
      {
        noko noko("noko!");
        noko();
        noko = noko(noko);
      }
    noko noko;
}

But you should use the One True Style:
noko noko(noko noko){
 noko(noko){
  noko((noko*noko)==noko){
   noko noko;
  }
  noko{
   noko *noko=noko noko(noko*noko%noko);
   noko ^= noko;
  }
 }
 noko{
  noko noko("noko!");
  noko();
  noko=noko(noko);
 }
 noko noko;
}[/code]

Name: Anonymous 2011-02-12 4:53

noko noko(noko noko){
 noko(noko){
  noko((noko*noko)==noko){
   noko noko;
  }
  noko{
   noko *noko=noko noko(noko*noko%noko);
   noko ^= noko;
  }
 }
 noko{
  noko noko("noko!");
  noko();
  noko=noko(noko);
 }
 noko noko;
}

Name: Anonymous 2011-02-18 13:16

that's cool and all, but check my doubles over there

Name: Anonymous 2013-01-19 14:30

/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