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

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

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