Code off
1
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!
121
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;
}
122
Name:
Anonymous
2009-07-29 19:00
This thread has made me realize that I am not an [b] [u] EXPERT PROGRAMMER [/b] [/b]
123
Name:
Anonymous
2009-07-29 19:29
124
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]).
125
Name:
Xarn
!Rmk.XarnE2!xGIX62dlJesBTK+
2009-07-29 19:58
/* gcc file.c `allegro-config --libs` */
#include <stdlib.h>
#include <time.h>
#include <allegro.h>
#define WIDTH 1280
#define HEIGHT 800
#define SQUARE 10
#define FOREGROUND 255, 255, 255
#define BACKGROUND 0, 0, 0
typedef struct {
int x;
int y;
int x_s;
int y_s;
} square;
int main()
{
int num_squares = 0, back, i;
square *s = NULL;
BITMAP *pad, *square;
allegro_init();
install_keyboard();
install_mouse();
install_timer();
if (set_gfx_mode(GFX_AUTODETECT, WIDTH, HEIGHT, 0, 0) != 0)
return 1;
srand((unsigned int)time(NULL));
pad = create_bitmap(WIDTH, HEIGHT);
back = makecol(BACKGROUND);
square = create_bitmap(SQUARE, SQUARE);
clear_to_color(square, makecol(FOREGROUND));
while (!keypressed()) {
if (mouse_b & 1) {
if (num_squares % 256 == 0)
s = realloc(s, sizeof(square) * (num_squares + 256));
s[num_squares].x = mouse_x;
s[num_squares].y = mouse_y;
s[num_squares].x_s = rand() % 64 - 50;
s[num_squares].y_s = rand() % 64 - 50;
++num_squares;
}
clear_to_color(pad, back);
for (i = 0; i < num_squares; ++i) {
s[i].x += s[i].x_s;
s[i].y += s[i].y_s;
if (s[i].x <= 0) {
s[i].x = 0;
s[i].x_s *= -1;
}
if (s[i].x >= WIDTH - SQUARE) {
s[i].x = WIDTH - SQUARE;
s[i].x_s *= -1;
}
if (s[i].y <= 0) {
s[i].y = 0;
s[i].y_s *= -1;
}
if (s[i].y >= HEIGHT - SQUARE) {
s[i].y = HEIGHT - SQUARE;
s[i].y_s *= -1;
}
draw_sprite(pad, square, s[i].x, s[i].y);
}
show_mouse(pad);
blit(pad, screen, 0, 0, 0, 0, WIDTH, HEIGHT);
rest(30);
}
free(s);
return 0;
} END_OF_MAIN()
126
Name:
Anonymous
2009-07-29 20:05
I happen to be an EXPERT at this.
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class a extends Applet implements Runnable, MouseListener{
int w,h;
public static final int BOX_SIZE=10,BOX_SPEED=10;
ArrayList<double[]> boxes=new ArrayList<double[]>();
BufferedImage buf; //for double buffering
public void init(){
w=getWidth();
h=getHeight();
buf=new BufferedImage(w,h,BufferedImage.TYPE_3BYTE_BGR);
addMouseListener(this);
new Thread(this).start();
}
public void paint(Graphics g){
g.setColor(Color.black);
g.fillRect(0, 0, w, h);
g.setColor(Color.white);
for(double[] box:boxes){
box[0]+=box[2];
box[1]+=box[3];
if(box[0]<0||box[0]>w-BOX_SIZE) box[2]*=-1;
if(box[1]<0||box[1]>h-BOX_SIZE) box[3]*=-1;
g.fillRect((int)box[0], (int)box[1], BOX_SIZE, BOX_SIZE);
}
}
public void run(){
while(true){
try{
paint(buf.getGraphics());
getGraphics().drawImage(buf, 0, 0, null);
Thread.sleep(10);
}catch(Exception e){}
}
}
public void mouseClicked(MouseEvent e) {
double[] box=new double[4];
box[0]=e.getX();
box[1]=e.getY();
box[2]=(Math.random()-.5)*BOX_SPEED;
box[3]=(Math.random()-.5)*BOX_SPEED;
boxes.add(box);
}
public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}public void mouseReleased(MouseEvent e){}
}
127
Name:
Anonymous
2009-07-29 20:25
>>124
The restriction specified in
>>1 still applies.
128
Name:
Anonymous
2009-07-29 21:42
129
Name:
Anonymous
2009-07-29 21:45
>>127
Lets fuck that restriction. Everyone can program in whatever the fuck they want.
130
Name:
Anonymous
2009-07-29 22:03
>>129
Excellent, I'll post my brainfuck solution in about a month
131
Name:
Anonymous
2009-07-29 22:42
>>121
VERSION 0.0.1 RELEASED
4a5
#include <conio.h>
9c10
< HANDLE *hConsole;
---
HANDLE hConsole;
105c106
< Sleep(1000);//32
---
Sleep(32);
132
Name:
Anonymous
2009-07-29 22:42
>>130
Brainfuck really isn't that hard to code in. The problem with wanting to draw stuff on the screen is that there's no way to interface with an OS.
133
Name:
!Double-Shot-Of-Java
2009-07-29 22:56
Here comes your second course of delectable, enterprise quality deliciousness! I'll skip the source and let you dive right into the applet:
http://www.geocities.com/valkarse/bouncy.html
Bon appetit!
134
Name:
Anonymous
2009-07-29 23:00
>>133
Not bad. You should show us the source though. I think this round is for any language.
135
Name:
Anonymous
2009-07-29 23:02
>>133
I cant stop staring at it lol
136
Name:
Anonymous
2009-07-29 23:04
>>133
You have a bug in the display, it only shows the first two numbers of the sprite amount i.e. 209 sprites would show 20
137
Name:
!Double-Shot-Of-Java
2009-07-29 23:12
>>136
Hmm, I'll try to fix it, but the bug doesn't happen on my screen. What browser/resolution are you running?
138
Name:
Anonymous
2009-07-29 23:20
>>136,137
Doesn't happen for me, either.
1280x1024
139
Name:
!Double-Shot-Of-Java
2009-07-29 23:22
http://www.geocities.com/valkarse/bouncy2.html
Fixed. I just gave the display some more space. You might have to clear your cache and reload your browser to view the new version.
140
Name:
Anonymous
2009-07-29 23:29
>>139
I like the uploading of submissions to websites! This should occur more often.
141
Name:
Anonymous
2009-07-29 23:29
>>139 That solved it ;)
For what it's worth, Firefox 3.5.1 1024x768
142
Name:
Anonymous
2009-07-29 23:45
>>139
Is his applet gonna hax my anus?
143
Name:
Anonymous
2009-07-29 23:47
>>142
Yeah I'm a little afraid too... can someone virus scan it first?
144
Name:
!Double-Shot-Of-Java
2009-07-29 23:50
>>142
>>143
It's an APPLET! You guys are noobs. ( ≖‿≖)
145
Name:
Anonymous
2009-07-30 0:28
>>144
I think it's Javascript. You've infected us all!
146
Name:
Anonymous
2009-07-30 0:37
>>143
Perhaps you should switch to a real operating system.
147
Name:
Anonymous
2009-07-30 1:49
Now which one of you guys is the sussman?
148
Name:
Anonymous
2009-07-30 1:55
149
Name:
Anonymous
2009-07-30 4:34
OP here,
i declare
>>56 the winner because all good programmers know not to try and reinvent the wheel.
150
Name:
Anonymous
2009-07-30 4:36
>>149
STFU you are not the OP, numbnuts.
151
Name:
Anonymous
2009-07-30 5:24
<html>
<head>
<style> * { margin: 0; padding: 0; } </style>
<script type="text/javascript">
function load() {
var fps = 60;
var boxWidth = 20;
var boxHeight = 20;
var speed = 750; //pixels per second
var canvas = document.getElementById("canvas");
var width = Math.max(document.documentElement["clientWidth"], document.body["scrollWidth"],
document.documentElement["scrollWidth"],document.body["offsetWidth"],document.documentElement["offsetWidth"]);
var height = Math.max(document.documentElement["clientHeight"], document.body["scrollHeight"],
document.documentElement["scrollHeight"],document.body["offsetHeight"],document.documentElement["offsetHeight"]);
canvas.width = width;
canvas.height = height;
var c = canvas.getContext("2d");
var tspeed = speed / fps;
var boxes = Array();
setInterval(function() { draw(); }, 1000/fps);
canvas.addEventListener("click", function(e) {
var direction = Math.random() * 2 * Math.PI;
var dx = tspeed * Math.cos(direction);
var dy = tspeed * Math.sin(direction);
boxes.push(new Box(e.pageX, e.pageY, dx, dy));
}, true);
function draw() {
c.fillStyle = "White";
c.fillRect(0, 0, width, height);
c.fillStyle = "rgb("+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+","+Math.floor(Math.random()*256)+")";
for(var i=0; i<boxes.length; i++) boxes[i].move();
}
var Box = function(x, y, dx, dy) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.move = function() {
if((x+boxWidth >= width && dx > 0) || (x < 0 && dx < 0)) dx = -dx;
if((y+boxHeight >= height && dy > 0) || (y < 0 && dy < 0)) dy = -dy;
x+=dx;
y+=dy;
c.fillRect(x, y, boxWidth, boxHeight);
}
}
}
</script>
</head>
<body onload="load();">
<canvas id="canvas"></canvas>
</body>
</html>
152
Name:
Anonymous
2009-07-30 6:04
>>150
The jerk store called; they ran out of
you!
153
Name:
Anonymous
2009-07-30 6:57
>>152
#Sudo suck my dick, jerkface!
154
Name:
Anonymous
2009-07-30 7:37
>>152
In Soviet Russia, you run out of the jerk store!
155
Name:
=+=*=F=R=O=Z=E=N==V=O=I=D=*=+=
!frozEn/KIg
2009-07-30 7:50
>>154 In Soviet Slashdot such dumb jokes gain +2 Insightful.
(I lost interest when it started parroting two-day old reddit stories and ramped up the slashvertisements volume).
_______________________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Velox Et Astrum gamedev forum:
http://etastrum.phpbb3now.com
There's nothing in the world so demoralizing as money.
156
Name:
Anonymous
2009-07-30 8:10
157
Name:
Anonymous
2009-07-30 8:38
Okay, this should work. The squares don't "appear" where you click because I couldn't solve the equation ;_; Add -lglut and -lGL when linking. Compilation might be tricky.
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SQUARE 1024
static unsigned int square_count;
static struct {
GLfloat data[16];
} square_data[MAX_SQUARE];
static GLint uniformTime;
static void
display()
{
GLfloat t = glutGet(GLUT_ELAPSED_TIME) * 0.001f;
glUniform1f(uniformTime, t);
glClear(GL_COLOR_BUFFER_BIT);
glVertexPointer(4, GL_FLOAT, 0, square_data);
glDrawArrays(GL_QUADS, 0, square_count * 4);
glutSwapBuffers();
}
static void
display_callback(int value)
{
display();
glutTimerFunc(16, display_callback, 0);
}
static void
square_add(int x, int y, int vx, int vy)
{
if (square_count >= MAX_SQUARE)
return;
GLfloat data[16] = { x - 8, y - 8, vx, vy, x + 8, y - 8, vx, vy, x + 8, y + 8, vx, vy, x - 8, y + 8, vx, vy };
memcpy(square_data[square_count++].data, data, sizeof(GLfloat) * 16);
}
static void
mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
square_add(x, y, (rand() % 400) - 200, (rand() % 400) - 200);
} else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
unsigned int i;
for (i = 0; i < MAX_SQUARE; ++i) {
square_add(rand() % 640, rand() % 480, (rand() % 400) - 200, (rand() % 400) - 200);
}
}
}
static const char* vert =
"#version 120\n"
"const mat4 orthoMatrix = "
"mat4(2.0 / 640.0, 0.0, 0.0, 0.0,"
"0.0, 2.0 / -480.0, 0.0, 0.0,"
"0.0, 0.0, -1.0, 0.0,"
"-1.0, 1.0, -0.0, 1.0);\n"
"uniform float t;\n"
"const vec2 windowSize = vec2(640.0f, 480.0f);\n"
"void main() {\n"
"vec2 dist = gl_Vertex.xy + gl_Vertex.zw * t;\n"
"ivec2 signs = ivec2(mod(dist / windowSize, 2));\n"
"gl_Position = orthoMatrix * vec4((2 * signs - 1) * ((signs * windowSize) - mod(dist, windowSize)), 0.0f, 1.0f);\n"
"}\n";
static const char* frag =
"#version 120\n"
"void main() {\n"
"gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\n";
int
main(int argc, char* argv[])
{
GLuint pr;
GLuint sh;
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutInitDisplayMode(0);
glutCreateWindow("/prog/");
pr = glCreateProgram();
sh = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(sh, 1, &vert, NULL);
glCompileShader(sh);
glAttachShader(pr, sh);
sh = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(sh, 1, &frag, NULL);
glCompileShader(sh);
glAttachShader(pr, sh);
glLinkProgram(pr);
glUseProgram(pr);
uniformTime = glGetUniformLocation(pr, "t");
glEnableClientState(GL_VERTEX_ARRAY);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutTimerFunc(16, display_callback, 0);
glutMainLoop();
return 0;
}
158
Name:
Anonymous
2009-07-30 9:17
>>157
Oops, forgot I was going to use a VBO. Also enabled double buffering to avoid flickering.
9,11d8
< static struct {
< GLfloat data[16];
< } square_data[MAX_SQUARE];
21c18
< glVertexPointer(4, GL_FLOAT, 0, square_data);
---
glVertexPointer(4, GL_FLOAT, 0, NULL);
40c37
< memcpy(square_data[square_count++].data, data, sizeof(GLfloat) * 16);
---
glBufferSubData(GL_ARRAY_BUFFER, square_count++ * sizeof(GLfloat) * 16, sizeof(GLfloat) * 16, data);
81a79
GLuint vbo;
85c83
< glutInitDisplayMode(0);
---
glutInitDisplayMode(GLUT_DOUBLE);
104a103,106
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 16 * MAX_SQUARE, NULL, GL_STATIC_DRAW);
159
Name:
Anonymous
2009-07-30 9:35
>>158
Protip: Use diff
-u .
160
Name:
Anonymous
2009-07-30 10:17
>>159
But then I'll have to come up with a file name ;_;
Newer Posts