Festive code

Pages: 12
Dec 20, 2010 at 10:58pm
In the spirit of the season, I wrote this Perl script.

You'll need the Perl interpreter (obviously) and the Perl curses module (Ubuntu: $ sudo apt-get install libcurses-perl) for it to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
				      ;
				     $_;
				   fill();
				  fill();use
			        Curses;fill();
			      use Time::HiRes qw
			    (usleep);initscr();$_;
			  if (has_colors()){strtcol();
			   }noecho();;;refresh();;;my
		          @words =("true love(s)","t".
			"urtledoves",  "French hens","".
		      "calling birds","golden rings" , "".
		    "geese a-laying","swans a-swimming", "".
		  "maids a-milking","ladies dancing","lords".
		" a-leaping","pipers piping");$_;$_;push(@words,
	      "drummers drumming");my $n = 1;while (true){ foreach
		my $line (@words){if(has_colors()){my$fgc = rand(7);
	      my $bgc = rand(7);until ($fgc != $bgc){$bgc = rand(7);;}
	    init_pair(1, $fgc, $bgc);attron(COLOR_PAIR(1));}printw("  ".
	  "\t\t\t\tMerry Christmas!\t\t\t        On the %2dth day of Ch".
	"ristmas, my true love gave to me: %2d %s", $n, $n, $line);$_;$_;if
      (has_colors()){attroff(COLOR_PAIR(1));}refresh();usleep(750000);clear();
    refresh();$n++;}}endwin();sub fill{};fill();fill();sub strtcol{start_color;}
				  fill();fill();
				  fill();fill();
				  fill();fill();

I tried to arrange it (very roughly) into a Christmas tree. It didn't really work but there's not much I could do.
I stopped being so lazy. This one looks a little better.

I hope you like it.


Edit: For some reason, sometimes the foreground colour and background colour are the same. I tried to stop it from happening but it didn't work.

I also tried to map each character to a larger, "ASCII-art" style, but that didn't really work either, since I couldn't figure out how to get the characters on the same line correctly.

Also, ignore the fact that it says "1th", "3th", etc. I'm kinda lazy sometimes :)
Last edited on Dec 21, 2010 at 2:52am
Dec 20, 2010 at 11:03pm
More Christmas code!
Dec 21, 2010 at 1:10am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#define D m();}
#define Merry m x=
#define from (
#define Bazzy );int main(){goto l;l
#define I struct

                 I
                 S
               {int
             p[2 ];S(
           int a, int b)
             {p[0]=a;;
          p[1]=b;}virtual
      std::string build()const
            {;; ; std::
          string r(1,'/');
       for(int i=0;i<p[1];i++
     )r+=(rand()%(3+2)?' ':'*');
           return r+'\\';
         }};struct T:public
       S{T(int a,int b):S(a,b
    ){}virtual std::string build
 ()const{return'|'+std::string(p[1]
            ,'_')+'|';}
            };struct U:
            public S{U(


int a,int           b):S(a,b){}        virtual            std::string             build()const             {return '/'+std::string
 (p[1],'_'         )+'\\' ;}};         struct P          {S*p;mutable           bool cp; P(int a,       int b):p(new S(a,b)),cp(!0
  ){}P(int        a,int b,char         c):p(c=='|'    ?(S*)new T(a,b)         :(S*)new     U(a,b)),     cp(true){}P( const P&o):p(
   o.p),cp(      true ){o.cp=          false; }P&operator=( const P&o         ){p=o.p;     cp=true;     o.cp=false;
    return *    this ;}~P(){           if(!!cp)  delete   p;}} ;std::         ostream&     operator      <<(std::ostream&
     os,const  P&p){ return            os<< std    ::     string (p.p         ->p[0],' ')<<((S*)p.p        )->build()<<'\n';}struct
       m{void operator-()              {m();}};           struct Xmas         {std::vector<P>v;Xmas           (){int i;for(i=0;i<4;
        i++){v.push_back               (P(13-i,           i*2));if(i%         2)v.push_back(P(13-i,                i*2));}v.push_back
      (P(13-i,i*2 ,'_'));v            .push_back          (P(12,2,'|'         ));;;;;}   friend std                         ::ostream
     &operator     <<(std::            ostream&           os,  const          Xmas& x)     {for(int                       unsigned i=
    0; i<x.v.       size();i           ++)os<<x           .v[i] ;int          w;return     os;w=0;}                operator m()const
  {std::cout         <<*this;          if(false           )std::cout          <<123456     ;int x=6       ;x=123;return m();}};void
 fooooooooo           ( double         xy){if(8           %7<0){char          ccc=xy<7     ?'a':'b'       ;int x=ccc+123;x=346;}}



Merry Xmas from Bazzy

:-D

Dec 21, 2010 at 2:48am
I knew you would post one, Bazzy, you're excellent at this sort of thing. I still can't figure out how you got the ":-D" at the end there to work.

It was really cool, though, very impressive.
Last edited on Dec 21, 2010 at 2:51am
Dec 21, 2010 at 9:09am
lines 49 and 51 are made using the macros defined on lines 5-8
Dec 21, 2010 at 9:26am
This is very impressive. Did you wrote some scripts or programs to generate the silhouette out ? I refuse to believe you do it manually :P

Nice work Bazzy! Merry Xmas :)
Dec 21, 2010 at 9:37am
@Bazzy,
I got that, but I didn't understand the hyphen.

1
2
3
Merry Xmas from Bazzy

:-D

becomes
m x = Xmas();int main(){goto l;l:-m();}
or
1
2
3
4
5
6
m x = Xmas();
int main() {
        goto l;
        l:
                -m();
}

I see now. You're just negating the return value of m();

@sohguanh,
You mean, do we use scripts to format the code into a shape? I don't know about Bazzy, but I do it by hand. I wouldn't know how to do it with a script.
Last edited on Dec 21, 2010 at 9:44am
Dec 21, 2010 at 10:41am
I do it by hand. I don't think there's any tool to do things like that
Dec 22, 2010 at 12:07am
Haha, mine's not as sleek as the ones you guys made!

Remember: randomised baubles are the best type of bauble :-)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

                                                ////
                                                ////
                                            int main() {
                                        int x = 16; int y = 8,
                                 start = x/2, stop = x/2; cout << endl;
                            srand((unsigned)time(0)); for(int j = 0; j<x;j++)
                     { if (j == start-1) cout << '{'; else if (j == start) cout << '@';
                                else if (j == start+1) cout << '}'; else
                          cout << ' ';} cout << endl; for(int i = 0; i < y; i++)
                    {for(int j = 0; j < x; j++) { if ((j<start)||(j>stop)) cout << ' ';
            else if ((rand() % 20) > 17) cout << 'o'; else cout<<'*';}start--;stop++;cout<<endl;}
    start = x/2 - 3;stop = x/2 + 3;for(int j=0;j<x;j++){if(j==start) cout<<'\\';else if((j>start)&&(j<stop))
                                    cout << '_'; else if (j == stop)
                                    cout << '/'; else cout << ' '; }
                                    cout << std::endl; cout << endl;
    
    cout << "Season's Greetings!" << endl << endl;
}
Dec 22, 2010 at 12:48am
This is awesome guys!!!! :) More examples!
Dec 22, 2010 at 2:13am
chrisname wrote: You mean, do we use scripts to format the code into a shape? I don't know about Bazzy, but I do it by hand. I wouldn't know how to do it with a script.

...

Bazzy wrote: I do it by hand. I don't think there's any tool to do things like that


Woah you all are really hardworking :P

Now since there seem to be a demand, anyone with idea of some home pet project may want to consider writing a tool to do formatting for that. Not only Xmas but any other greetings too !

Any takers :P
Dec 22, 2010 at 7:24am
I thought I'd make one too:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <windows.h>
#include <string>
using namespace std;

//Derp, forgot to put this in, so...
#define off {CONSOLE_CURSOR_INFO cci={50,false};SetConsoleCursorInfo(out,&cci);}

                                       //
                                     const
                                  unsigned int
                              mspf=500;DWORD read;
                                  string tree;
                             HANDLE out=GetStdHandle
                         (STD_OUTPUT_HANDLE);WORD clrs[]
                         ={FOREGROUND_RED,FOREGROUND_RED|
                                FOREGROUND_GREEN,
                          FOREGROUND_RED|FOREGROUND_GREEN
                    |FOREGROUND_BLUE};COORD xy[8]={{5,0},{4,1},
                             {6,2},{3,3},{7,3},{5,4},
                        {1,5},{7,5}};string repeat(char c,
                     unsigned num){string tmp;for(;num>0;--num
                           ){tmp+=c;}return tmp;}struct
                       x_mas{x_mas(){SetConsoleTextAttribute
                   (out,FOREGROUND_GREEN);tree+=repeat(' ',11);
               tree[5]='#';tree[6]='\n';tree+="*|\\\n";tree+=repeat(
                       ' ',3)+repeat('/',2);tree+="\\*\\\n"
                    +repeat(' ',2);tree+="/*/\\|*\\\n ";tree+=
                repeat('/',2)+repeat('\\',2);tree+="*|"+repeat('\\'
           ,3);tree+="\n/*\\|/"+repeat('\\',2)+"*\\/\\\n";tree+=repeat
                         (' ',11);tree[61]=tree[63]='|';
                 tree[62]='_';}}xmas;int main(){off unsigned ticks=
           GetTickCount(),trigger,x;SetConsoleTitle("Merry Christmas!");
       srand(GetTickCount());WriteConsole(out,tree.c_str(),tree.length(),&read,
                      NULL);SetConsoleTextAttribute(out,clrs[2]
	       );WriteConsole(out,"\n\nPress <ENTER> to exit",23,&read,
	  NULL);while(!(GetAsyncKeyState(VK_RETURN)>>7)){ticks=GetTickCount();
   if(ticks>trigger){trigger=ticks+mspf;for(x=0;x<8;++x){WriteConsoleOutputAttribute
                                  (out,clrs+rand()
				  %3,1,xy[x],&read
				  );}}}return 0;}

EDIT: IDEs are a b*** to make code look like this... My IDE kept replacing groups of 4 spaces with tabs, which causes my tree to look weird >:C
Last edited on Dec 22, 2010 at 7:28am
Dec 22, 2010 at 7:18pm
@tomhas430,
That's nice! I really like the algorithm to make random baubles.

@PIMaster,
I can't run yours right now (I'm on Linux), but I will later.
Dec 22, 2010 at 9:52pm
EDIT: IDEs are a b*** to make code look like this

Only if you have a bad IDE ;P

My IDE kept replacing groups of 4 spaces with tabs, which causes my tree to look weird


Which is why I never use tabs. In fact I have my IDE to the opposite and it converts tabs to groups of spaces.
Dec 22, 2010 at 10:08pm
Disch and thepedestrian (edit: and sohguanh) obviously didn't see the rule that I didn't mention about posting without contributing :)

I tried making another one, it was going to be a quine with the source formatted to say "MERRY XMAS" so that the program would print "MERRY XMAS" in code, but although I wrote the quine (I basically just copied one from Wikipedia) I couldn't do the formatting (not enough code) so I gave up.

@whitespace,
I find it really irritating when someone pads their code with spaces instead of tabs (unless it's 4 or less spaces, although I also find it irritating when people use < 4 spaced indents). It's much harder to re-indent code if you're using spaces or tabs. There are also other benefits to using tabs that I can't remember.
Last edited on Dec 22, 2010 at 10:08pm
Dec 22, 2010 at 11:56pm
I like spaces instead of tabs.. and I have my set up so that tab inserts 4 spaces, and backspace removes 4 spaces. So it's like tab, but the source shows up the same no matter where I open it.

It's funny... it's such a small thing, that doesn't actually matter... but I would feel hugely unsettled to use tabs! :P And I'm sure it's the other way around for people who use tabs and not spaces. Why is this?
Dec 23, 2010 at 2:06am
thomas430 wrote:
So it's like tab, but the source shows up the same no matter where I open it.

People not using 8-space tabs can't complain if my source code is incorrectly formatted, they shouldn't be using a stupid tab size.
Dec 23, 2010 at 9:35am
I use only tabs for formatting normally, and 1TBS. But for code-sculpting I would definitely use spaces only.
Dec 23, 2010 at 11:16am
Stop talking about tabs plz
:^P
Dec 23, 2010 at 11:27am
closed account (z05DSL3A)
Tab,

Tab tab tab tabaty tab?

Tab tab.
Pages: 12