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

Code off

Name: Anonymous 2009-07-28 13:59

Welcome to the first annual /prog/ Code off !

The language will be C.

Compeptitors include (in no particular order):

Mr. Ribs
Xarn
FV
Anonymous
W. T. Snacks

Rumor has it, that there may be a special guest appearance from either The Sussman or
Leah Culver.

What's the point of this, you ask?

It's to determine who is an EXPERT PROGRAMMER.

Marking criteria:

Optimization
Elegance

Bonus marks:
indentation

Good luck!

Name: Anonymous 2009-07-29 18:55

>>119
I happen to be an expert at this.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <windows.h>
#include <string.h>

//http://dis.4chan.org/read/prog/1248803966/100

HANDLE *hConsole;

void fill_frame(char *frame,char c){
    int x,y;
    for(y=0;y<20;++y){
        for(x=0;x<50;++x){
            frame[y*51+x] = c;
        }
        frame[y*51+50] = '\n';
    }
    frame[51*20] = 0;
}

///

inline int out_of_bounds(short x,short y){
    return x<0 || x>50 || y<0 || y>20;  
}

void add_square(short *squares){
    int i;
    for(i=0;i<1024;i+=4){
        if(out_of_bounds(squares[i],squares[i+1])){
            squares[i]   = 25;//x
            squares[i+1] = 10;//y
            squares[i+2] = 1;//x speed
            squares[i+3] = 1;//y speed
            break;
        }
    }
}

void move_squares(short *squares){
    short i,*x,*y;
    for(i=0;i<1024;i+=4){
        x = &squares[i];
        y = &squares[i+1];
        if(!out_of_bounds(*x,*y)){
            if(*x==0 || *x==50-1) squares[i+2]*=-1;
            if(*y==0 || *y==20-1) squares[i+3]*=-1;
            *x += squares[i+2];
            *y += squares[i+3];
        }
    }
}

///

void gotoxy(short x,short y){
    COORD coord = {x,y};
    SetConsoleCursorPosition(hConsole,coord);
}

void print_frame(char *frame){
    gotoxy(0,0);
    printf(frame);
    gotoxy(0,0);
}

void clear_scrn(){
    char frame[51*20];
    fill_frame(frame,' ');
    print_frame(frame);
}

void render_frame(char *frame,short *squares){
    short i,x,y;
    fill_frame(frame,' ');
    for(i=0;i<1024;i+=4){
        x = squares[i];
        y = squares[i+1];
        if(!out_of_bounds(x,y)){
            frame[y*51+x] = '#';
        }
    }
}

///

int main(int argc,char *argv[]){
    char frame[51*20];
    short squares[1024];
    memset(squares,-1,sizeof(short)*1024);
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
   
    clear_scrn();
    for(;;){
        while(_kbhit()>0){
            if(_getch()==EOF) exit(0);
            else add_square(squares);
        }
        move_squares(squares);
       
        fill_frame(frame,' ');
        render_frame(frame,squares);
        print_frame(frame);
        Sleep(1000);//32
    }
    return 0;
}

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