/*
** Lomax Password Creator by
** Thomas Baetzler (bath0011@fh-karlsruhe.de)
*/ 

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
    char code[] = "AODX";
    int sect, live, cred;

    int sect01, sect23, sect4;
    int live0, live12, live23, live34;

    if( argc != 4 ){
        printf("Syntax: Lomax section/N lives/N credits/N\n");
    } else {

        sect = atoi( argv[1] );
        live = atoi( argv[2] );
        cred = atoi( argv[3] );
     
	/* There are only 22 sections in all */
        if( sect < 1 ) sect = 1;
        if( sect > 22 ) sect = 22;

        sect --; /* internal representation */

        /* Up to 31 lives are possible */
        if( live < 0 ) live = 0;
        if( live > 31 ) live = 31;

        /* Maximum of 3 continues */
        if( cred < 0 ) cred = 0;
        if( cred > 3 ) cred = 3;

        /* Mask out the relevant bits */
        live0 = live & 1;
        live12 = (live >> 1) & 3;
        live23 = (live >> 2) & 3;
        live34 = (live >> 3) & 3;

        sect01 = sect & 3;
        sect23 = (sect >> 2 ) & 3;
        sect4  = (sect >> 4 ) & 1;
    
        cred = cred & 3;


        printf("Code for Level %2d (%2d Lives, %d Continues): ", sect+1, live, cred);

        printf("%c",code[sect01 ^ sect4 ^ (live0<<1) ^ live34 ^ 2]);
        printf("%c",code[(cred ^ 3) ^ sect23 ^ live12 ]);
        printf("%c",code[sect01]);
        printf("%c",code[sect23]);
        printf("%c",code[(sect4 ^ 1) + (live0<<1)]);
        printf("%c",code[live12 ^ 3]);
        printf("%c",code[live34 ^ 1]);
        printf("%c\n",code[cred]);
    }
    
    exit( 0 );
}    
    




