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 19:57

>>100
I don't see any language restriction on this one, soo...

-module(progoff2).
-export([start/0]).

-define(SQUARE_SIZE, 50).
-define(WINDOW_WIDTH, 500).
-define(WINDOW_HEIGHT, 500).
-define(UPDATE_INTERVAL, 50).

start() ->
    G = gs:start(),
    gs:window(win, G, [{width, ?WINDOW_WIDTH},
        {height, ?WINDOW_HEIGHT}, {map, true}]),
    gs:canvas(canv, win, [{width, ?WINDOW_WIDTH},
        {height, ?WINDOW_HEIGHT}, {buttonpress, true}]),
    self() ! update,
    loop([]).

loop(Squares) ->
    receive
        update ->
            increment_squares(Squares),
            erlang:send_after(?UPDATE_INTERVAL, self(), update),
            loop(Squares);
        {gs, canv, buttonpress, _, [1,X,Y|_]} ->
            NewSquare = gs:rectangle(canv, [
                {coords, [{X, Y}, {X+?SQUARE_SIZE, Y+?SQUARE_SIZE}]},
                {fill, random_color()},
                {data, random_velocity()}
            ]),
            loop([NewSquare|Squares]);
        {gs, _, destroy, _, _} ->
            erlang:halt();
        {gs, _, Type, _, _} ->
            io:format("Got ~w~n", [Type]),
            loop(Squares)
    end.

increment_squares([Square|Rest]) ->
    [{Left, Top}, {Right, Bottom}] = gs:read(Square, coords),
    {XVel, YVel} = gs:read(Square, data),
    if (Left =< 0) and (XVel =< 0) or
        (Right >= ?WINDOW_WIDTH) and (XVel >= 0) or
        (Top =< 0) and (YVel =< 0) or
        (Bottom >= ?WINDOW_HEIGHT) and (YVel >= 0) ->
            gs:config(Square, {data, random_velocity()});
        true -> false
    end,
    gs:config(Square, {move, gs:read(Square, data)}),
    increment_squares(Rest);
increment_squares(_) -> false.

random_velocity() ->
    {5*(random:uniform()-0.5), 5*(random:uniform()-0.5)}.

random_color() ->
    lists:nth(random:uniform(6), [red,yellow,green,blue,red,orange]).

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