pouët.net

Go to bottom

Behold! The interesting little programs thread.

category: code [glöplog]
Quote:
It's not about "being clever" (unless using ternary operators counts as "clever" in your book).


I'm sorry but if you consider the following as more readable or, let me quote, "quite clear":

Quote:

(inp[i]=='0')? outp[i-1]='0'+rand()%10: (inp[i]=='x')? outp[i-1]='a'+rand()%26:0;


when compared to:

Quote:

if (inp[i] == '0')
{
outp[i-1]='0' + rand() % 10;
}
else
if (inp[i]=='x')
{
outp[i-1]='a'+rand()%26;
}

(yeah, indents devoured by <quote>, sorry)


then I need your autograph, now.
added on the 2012-07-05 22:54:49 by kbi kbi
If indents get devoured by <quote>, why don't you use <code> instead? :P
Kinda self-defeating indeed. ;) Also optimized for posting size != optimized for readability. :P
added on the 2012-07-06 02:50:53 by tomaes tomaes
And about the whole "being clever" thing: Isn't that part of the demoscene DNA? To write stuff that is not exactly by the book all the time and kinda throw away most of the time? It's the strangest insult(?) in a community like this.
added on the 2012-07-06 03:07:58 by tomaes tomaes
the challenge isn't about making the source code smaller, not unless you use javascript anyway!111 (harrrrrrrrrr)
added on the 2012-07-06 06:32:06 by xwize xwize
Quote:
If indents get devoured by <quote>, why don't you use <code> instead? :P


Note taken :D
added on the 2012-07-06 09:14:55 by kbi kbi
Discarding the the return value of ternary expression is bad style unless you're obfuscating
Code: outp[i-1] = inp[i]=='0' ? '0'+rand()%10 : inp[i]=='x' ? 'a'+rand()%26 : 0;


added on the 2012-07-06 09:38:02 by 216 216
But that's like 3 lines!! That's not in the budget. One thing leads to another and you end up using the scroll wheel. ;)
added on the 2012-07-06 10:37:57 by tomaes tomaes
216: ... or unless you're one of the Boost guys and regularly need to have types resolved without evaluating the actual expression. But yes. :)

And otherwise what xwize said. The "clever" part applies to what the code DOES, not how it LOOKS. A good compiler should optimize away any significant difference between ternary operators and if/else chains anyway, and even if it doesn't get it all, the OOO execution of the host CPU will most probably level the last few remaining bumps in the playing field. And the demoscene was always about how the final result looks on the screen, not about being fluent in every Turing Tarpit on earth. :)
added on the 2012-07-06 10:47:54 by kb_ kb_
Point taken about the return value, although I think it should be...
Code: outp[i-1] = (inp[i]=='0')? outp[i-1]='0'+rand()%10: (inp[i]=='x')? outp[i-1]='a'+rand()%26: inp[i];

instead.
added on the 2012-07-06 10:48:42 by tomaes tomaes
Quote:
what the code DOES, not how it LOOKS

As far as scene stuff goes: Exactly my point. Also, sure, otherwise most democoders would be in trouble. :)

And the choice of TOs vs. if/else is - in this context - basically an aesthetic one. I often tend to the former, rather than the latter, at least for short & lazy code snippets on the intertubes. :) If you're going for readability in real projects(tm), you'd probably avoid them altogether.
added on the 2012-07-06 11:14:00 by tomaes tomaes
Working Boggle game (but without dictionary :)

Code: /* Mini boggle by Sasq 200x */ #include <stdio.h> #include <stdlib.h> #include <time.h> static char board[49], *b = board + 8; int check_board(char *w, int i, int v) { int *p, offs[] = {-9,-8,-7,-1,1,7,8,9,0}; for(p = offs; *p; p++) if((!*w) || ((*w == b[*p+i]) && !(v&1<<(*p+i)) && check_board(w+1, *p+i, v|1<<i))) return 1; return 0; } int main() { int i, dn=6, used[] = {4,3,4,2,2,1,0}; char input[80], *dice = "aeiouyaeiouetdbtgprlhmnkcswfhsqxzjkv"; srand(time(NULL)); for(i=0; i<=49; i++) board[i] = 0; for(i=0; i<16; used[dn]--, dn=6, i++) { while(!used[dn]) dn = rand()%6; b[i+(i&12)] = dice[dn*6+rand()%6]; } while(1) { for(i=0; i<4; i++) puts(&b[i*8]); gets(input); for(i=0; i<16; i++) if(b[i+(i&12)] == *input && check_board(input+1, i+(i&12), 0)) printf("Found at (%d,%d)\n", i%4+1, i/4+1); } return 0; }
added on the 2012-07-06 11:28:11 by sasq sasq
A simple game I coded in an hour or so, a long time ago. A simple experiment in random level generation. :)

Enjoy.

Code:#include <SDL/SDL.h> SDL_Surface*e;SDL_Rect f;SDL_Rect k={0,0,7,7};int p(int x,int y){return*((int*) e->pixels+x+y*480)==874069;}void g(){f.x=f.y=1,f.w=f.h=7;SDL_FillRect(e,0,5824\ 849);for(;f.x<480;f.x+=8){for(;f.y<480;f.y+=8)SDL_FillRect(e,&f,874069);f.y=1;} f.x=(rand()%60)*8+1;srand(time(0));f.y=(rand()%60)*8+1;int h=0;int i=rand()%23\ 99;while(h<2400){switch(rand()%4){case 0:if(f.y>9){f.y-=8;break;}continue;case 1:if(f.x<465){f.x+=8;break;}continue;case 2:if(f.y<465){f.y+=8;break;}continue; case 3:if(f.x>9)f.x-=8;else continue;}if(h==i)k.x=f.x,k.y=f.y;SDL_FillRect(e,&f ,5824849);++h;}SDL_FillRect(e,&k,255);}int main(int a,char**b){SDL_Event d;char z[99]="Crisp ";SDL_Init(32);SDL_Surface*c=SDL_SetVideoMode(480,480,32,0);e=SDL\ _CreateRGBSurface(0,480,480,32,0,0,0,0);int t=SDL_GetTicks();g();while(1){if(S\ DL_PollEvent(&d)){if(d.type==12)break;else{int x=d.button.x;int y=d.button.y;if (d.button.button==1&&x<f.x+13&&x>f.x-5&&y<f.y+13&&y>f.y-5){x=((x>>3)<<3)+1;y=(( y>>3)<<3)+1;if(!p(x+2,y+2)){f.x=x;f.y=y;if(f.x==k.x&&f.y==k.y){t=SDL_GetTicks() ;g();}}}}}SDL_BlitSurface(e,0,c,0);SDL_FillRect(c,&f,0);int u=SDL_GetTicks()-t; sprintf(z+6,"%d",20-u/1000);SDL_WM_SetCaption(z,z);if(u>20000)break;SDL_Flip(c) ;SDL_Delay(20);}SDL_FreeSurface(e);SDL_Quit();return 0;}


To compile (for example):
Code:gcc ???.c -o ??? `pkg-config --libs --cflags sdl`
added on the 2012-07-09 00:43:41 by decipher decipher
A little weird, but apparently Pouet's code tags inserted some weird spaces in appropriate places. Every line should be the same length except for the last line. Please fix accordingly before compiling…
added on the 2012-07-09 00:45:19 by decipher decipher
Decipher: Not only scrambled as heck, but also with typos?! :)

Anyway, here's a more readable version of the above.
added on the 2012-07-09 15:20:19 by tomaes tomaes
tomaes: the obfuscation was on purpose in order to fit the whole thing into a 80 by 24 terminal buffer. :) though, thanks a lot for the cleanup.
added on the 2012-07-09 17:39:38 by decipher decipher
For what it's worth: I tried some actual obfuscation / ansi art hybrid experiment. :)
added on the 2012-07-12 14:34:04 by tomaes tomaes
...and the plain text version. In case the above link goes down (cause I updated it slightly, new filename, yadayada...), it's on my DA.
added on the 2012-07-12 15:11:15 by tomaes tomaes
The most clever thing I ever wrote is this:

http://pouet.net/prod.php?which=33316
added on the 2012-07-12 16:00:11 by visy visy
visy wins thread!
added on the 2012-07-13 22:33:13 by skrebbel skrebbel

login

Go to top