naked_code

my low-level programming blog

home

obfuscated bubble.c

On a Saturday night, I took inspiration from donut.c and wrote my own interpretation of it. It’s sloppy and not as clear, but it works.

gcc -o bubble bubble.c

and then ./bubble with a 80x28 sized terminal

                         #include <stdio.h>

                       typedef struct {float x;

                float y;float s;int o;int a;}s;s b[10];

            void ub(){for(int i=0;i<10;i++){if(b[i].a){//first

        b[i].y-=b[i].s;if(b[i].y<=0){b[i].a=0;}}else{if(rand()%100<5)

    {b[i].a=1;b[i].x=rand()%80;b[i].y=27;b[i].s=  0.1f+(rand()%10)/10.0f;

b[i].o=(rand()%3)+1;}}}}void db(){for(int i=0;i<10;i++){if(b[i].a){//bubble

    int x=(int)(b[i].x); int y=(int)(b[i].y);if(x>=0&&x<80&&y>=0&&y<28){

        printf("\033[%d;%dH",y+1,x+1);char bc;switch(b[i].o){

            case 1:bc='o';break;case 2:bc='0';break;case 3:

                bc='O';break;default:bc='o';break;}

                    printf("%c",    bc);}}}}

                      //nakedcodeneocities


                        void dl(){const
                  char*l="[nakedcode]bubbles.c";
            printf("\033[1;1H");for(int i=0;i<(80-strlen
      (l))/2;i++){putchar(' ');}printf("\033[3;35m%s\033[0m\n",
l);}void da(int a[],int o, int h1, int h2, int f){printf("\033[2J\033[H");
dl();for(int i=0;i<(28-3)/2;i++){putchar('\n');}for (int i=0;i<(80-o*4-1)/2;
    i++){putchar(' ');}for(int i=0;i<o;i++){if(i>0)putchar(' ');if(f==1){
      printf("\033[1;32m%3d\033[0m", a[i]);}else if(i==h1||i ==h2) {
        printf("\033[1;31m%3d\033[0m", a[i]);}else{printf("%3d",
            a[i]);}}putchar('\n');db();printf("\033[%d;%dH",
                     28,80);fflush(stdout);}


                void as(int a[],int o,
            int p1,int p2){for(int s=1;s<=5;s++)
        {ub();printf("\033[2J\033[H");dl();for(int i=
    0;i<(28-3)/2;i++){putchar('\n');}for(int i=0;i<(80-o*4-
    1)/2;i++){putchar(' ');}for (int i = 0; i < o; i++) {
        if (i > 0) putchar(' ');if (i == p1||i == p2) {
            if(i==p1){printf("\033[1;31m%*d\033[0m",
                3+(s*3)/5,a[i]);}else{printf


                  ("\033[1;31m%*d\033[0m",
                3-(s*3)/5,a[i]);}}else{printf
            ("%3d", a[i]);}}putchar('\n');db();
        printf("\033[%d;%dH",28,80);fflush(stdout);
    usleep(100000);}int t=a[p1];a[p1] = a[p2];a[p2] = t;
    da(a,o,p1,p2,0);int e=0;while(e<500000){usleep(100000);
e+=100000;ub();da(a, o, p1, p2, 0);}}void bs(int a[], int o) {
for (int i = 0; i < o - 1; i++) {for (int j = 0; j <o-i-1;j++) {
da(a, o, j, j + 1, 0);int e = 0;while (e < 500000) {usleep(100000);
    e += 100000;ub();da(a,o,j,j+1,0);}if (a[j] > a[j + 1]) {
    as(a, o, j, j + 1);}}}}int main() {int a[12]={7,3,1,2
        ,9,5,12,1,8,4,10,6};srand(time(NULL));printf
            ("\033[?25l");for(int i=0;i<10;i++){
                b[i].a=0;}bs(a,12);da(a,12,


          -1,-1,1);
    int e=0;while(e<500000)//
    {usleep(100000);e+=100000;
ub();da(a, 12, -1, -1, 1);}printf
    ("\033[?25h");return 0;}
    /*written by alexandros
          bartzos*/

these code blocks are supposedly bubbles…