Friday, November 16, 2012

fizz buzz, now with obscurity!

I saw that some 60% of some job applicants cant fizzbuzz.

Let's see my attempt.



#include <cstdio>
void fizzbuzz(){
    int th,fi,i;
    for(th=0,fi=0,i=0;i<16;++i,th=(i%3==0),fi=(i%5==0),!((th&&puts("fizz"))|(fi&&puts("buzz")))&&(printf("%d\n",i)));
}

int main() {
    fizzbuzz();
    return 0;
}

Can you tweak this so that multiples of 15 fizzbuzz instead of fizz\nbuzz?
http://ideone.com/VwmEEt

Tuesday, June 19, 2012

Follow my GitHub page

Here are the things I work on in my spare time.
https://github.com/shanecandoit

Many of the earlier projects where simply attempts at learning to use git. :)

Thursday, June 7, 2012

Mages - a networked multiplayer game

Mages

How

We made this game using python, the pygame and twisted libraries. We collaborated using subversion and many emails. We created UML sequence and class diagrams. We wrote several user stories and used their functionality to organize our code.

The game is playable, the spellbook functionality is incomplete. The arena maps are customizable and read from a text file every game.

The game is turned based and can handle up to four players. Players can move and cast spells provided they stay within the limit of their action points per turn.

Watch

I show the documentation then fire up a server and client and demonstrate the gui and a single round.


Tuesday, June 5, 2012

Group Database Project - music manager

Database project

how it was made

Me and my teammates built this application using java and apache derby for our database. We collaborated using subversion.

demo and schema/uml

In the demo I add some songs, add them to a playlist, create and add tags, then delete them.
At the end of the video the schema and the uml diagrams are shown.



We implemented all the functionality in our design document and kept our schema and uml diagrams up to date.

We we happy to earn full credit!
Thanks to Andrew and Shun for the hard work and helpful feedback.

Wednesday, February 8, 2012

Screensaver block race

Here is the code:


//#ifdef __cplusplus
#include <cstdlib>


#define o(S) printf("%s\n",S);
#define os(S) printf("%s\n",S);
#define oi(S) printf("%d\n",S);
#define od(S) printf("%d\n",S);
#define of(S) printf("%f\n",S);
#define oc(S) printf("%c\n",S);
//#else
//    #include <stdlib.h>
//#endif
//#ifdef __APPLE__
//#include <SDL/SDL.h>
//#else
#include <SDL.h>
//#endif


SDL_Surface *screen;


struct rec
{
    int x,
    y,
    w,
    h;
    int color;
};


void fillRec(rec r, int color)
{
    SDL_Rect rect = {r.x, r.y, r.w, r.h};
    SDL_FillRect(screen, &rect, color);
}


int W,H;




class Rec
{
public:
    Rec()
    {
        o("Rec.ctor");
        r.x = 17 * rand() % 400;
        r.y = 17 * rand() % 200;
        dx=rand()%3;
        dy=rand()%3;
        r.w = 16;
        r.h = 16;
        r.color = rand() ;
    }
    void draw()
    {
        fillRec(r, r.color);
    }
    void updt()
    {
        r.x+=dx;
        r.y+=dy;
        if(r.x > W || r.x<16)
        {
            dx *= -1;
        }
        if(r.y > H || r.x<16)
        {
            dy*=-1;
        }
        if(rand()%1024==0){
            dx+=rand()%4-2;dy+=rand()%4-2;
            if(W<800 && rand()%256==0){W++;}
            if(H<600&&rand()%256==0){H++;}
        }
    }
private:
    rec r;
    int dx,dy;
};


int main(int argc, char **argv)
{
    W=H=400;


    // initialize SDL video
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("Unable to init SDL: %s\n", SDL_GetError());
        return 1;
    }


    // make sure SDL cleans up before exit
    atexit(SDL_Quit);


    // create a new window


    screen = SDL_SetVideoMode(800, 600, 16,
                              SDL_HWSURFACE | SDL_DOUBLEBUF);
    if(!screen)
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }


    // load an image
//    SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
//    if (!bmp)
//    {
//        printf("Unable to load bitmap: %s\n", SDL_GetError());
//        return 1;
//    }


    // centre the bitmap on screen
//    SDL_Rect dstrect;
//    dstrect.x = (screen->w - bmp->w) / 2;
//    dstrect.y = (screen->h - bmp->h) / 2;
    Rec rA[300];


    // program main loop
    bool done = false;


    int oldTicks,newTicks,longestTick=0;
    oldTicks=newTicks=longestTick=1;
    int sec=1000;
    int frames=0;


    while(!done)
    {
        int oldTicks = SDL_GetTicks();
        frames++;
        //if(frames%sec==0){frames=0;}


        // message processing loop
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            // check for messages
            switch(event.type)
            {


                    // exit if the window is closed
                case SDL_QUIT:
                    {
                        done = true;
                        break;
                    }
                    // check for keypresses
                case SDL_KEYDOWN:
                    {
                        // exit if ESCAPE is pressed
                        if(event.key.keysym.sym == SDLK_ESCAPE)
                            done = true;
                        break;
                    }
            } // end switch
        } // end of message processing


        // DRAWING STARTS HERE


        // clear screen
        //SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


        // draw bitmap
//        SDL_BlitSurface(bmp, 0, screen, &dstrect);
        for(int i=0;i<300;i++){rA[i].updt();}




        //r1.draw();
        for(int i=0;i<300;i++){rA[i].draw();}


        // DRAWING ENDS HERE


        // finally, update the screen :)
        //SDL_Flip(screen);
        newTicks = SDL_GetTicks();


        SDL_UpdateRect(screen,0,0,0,0);


    } // end main loop




    // free loaded bitmap
//    SDL_FreeSurface(bmp);


    // all is well ;)
    printf("Exited cleanly\n");
    return 0;
}

Here is how it starts out:
And here again after some time has passed. See that some have broken lose.
But who uses screensavers anymore?