[hints?]_password generator
ok, a lot of you surely remember those games without any memory extension (i mean no memory card or memory chip), where you had to fill passwords to save your game: think all these NES classics such as metroid, kid icarus, punch-out! etc...
> how do you think they are generated?
let's say you have a 16 slots long password to fill with "hidden" infos, how would you go with it? and also, it has to be efficient against cheaters...
i remember with the classics mentioned above, that i was able to try fake passwords (thanks to the password i had after a play, i only changed some values almost randomly), sometimes with luck, but in 95% of the time it displayed an error...
any idea?
> note i haven't look for it properly with sites about gamedev, maybe the answer is there, if so i'll post some links.
anyway, if you already have an idea or some knowledge with it, don't hesitate to post. thanks ^_^
Just been playing around with this....
Just wrote it for you :)
...not the cleanest/most efficient, but a simple fix.
You bascially need some kind of hash loop-up table. This code accepts a correctl ordered array of values to encode and spits out a string to decode.
I just split up the values in the resultant code with a simple delimiter - but you could be more fancy and do some BITWISE OPERATORS to mess the code up even more.
As well as delimeters, you could insert check digitis to ensure for example that the score variable is 3 digits long etc.
I might make a more sophisticated version of this..
PHP Code:
arrayLookupTable = [ 'g', '2', 's', '5', 'c', '8', 'q', 'v', 'a', 'l', 'p' ];
function createCode( level, lives, score, ammo ) {
var codeArray = new Array();
codeArray = addElement( encode(level), codeArray );
codeArray = addElement( encode(lives), codeArray );
codeArray = addElement( encode(score), codeArray );
codeArray = addElement( encode(ammo), codeArray );
return codeArray;
}
function addElement( element, codeArray ) {
// arrayLookupTable[10] acts as a delimiter
for( var i=0; i<element.length; i++ ) {
codeArray.push( element[i] );
}
codeArray.push( arrayLookupTable[10] );
return codeArray;
}
function encode( number ) {
var string = number.toString();
var stringArray = new Array;
for( var i=0; i<string.length; i++ ) {
stringArray[i] = string.charAt( i )
}
var encodeArray = new Array();
for( var i=0; i<stringArray.length; i++ ) {
encodeArray[i] = arrayLookupTable[ stringArray[i] ];
}
return encodeArray;
}
function decode( code ) {
var decode = new Array();
var element=0;
decode[element] = new Array();
for( var i=0; i<code.length-1; i++ ) {
if( code.charAt( i ) != 'p' ) {
decode[element].push( code.charAt( i ) );
} else if( code.charAt( i ) == 'p' ) {
element++;
decode[element] = new Array();
}
}
for( var i=0; i<decode.length; i++ ) {
decode[i] = resolveCode( decode[i] );
trace("decode: "+decode[i]);
}
}
function resolveCode( code ) {
var tempString;
for( var i=0; i<code.length; i++ ) {
for( var j=0; j<arrayLookupTable.length; j++ ) {
if( code[i] == arrayLookupTable[j] ) {
code[i] = j;
}
}
tempString += code[i].toString();
}
return tempString;
}
var level = 5;
var lives = 3;
var score = 475;
var ammo = 145;
code = createCode( level, lives, score, ammo );
trace("code: " + code);
var code = "8p5pcv8p2c8p";
decode( code );
chopsticks
p.s. you could do a similar array lookup table for characters if you wanted to save names etc...