Password codes isn't that complicated, if you ask me.
Let's say I'm on level 5, with 32 out of 100 health points. I have the magic sword, but not the magic shield. Got 25 coins (999 is the maximum) and is infected with the horribly disease that makes NPCs run away from me.
5 ' my level
032 ' health out of 100
1 ' got the sword
0 ' I dont have the shield
025 ' 25 gold coins
1 ' I'm infected
Add them, as strings, together and we get this:
5032100251
Slap a "magic header to it" so we can easily check that it's not some madeup value. I use "42" in this example, since it's the meaning of life, the universe and everything:
425032100251
Now we have a nice little string there that is easily dividable by two, so let's group them into two and two, and calculate a simple check on each pair
42: 4+2 = 6
50: 5+0 = 5
32: 3+2 = 5
10: 1+0 = 1
02: 0+2 = 2
51: 5+1 = 6
Add these into a new string:
655126
Then append it to the string we had earlier:
425032100251655126
There's your magical password!
Want to parse it?
It got "42" at the beginning, so we can assume it's a valid password.
Cut out the last 6 numbers at the end, since it's our control number:
425032100251
655126
Now, calculate the pairs in the first string and check them against the control number:
42: 4+2 = 6
50: 5+0 = 5
32: 3+2 = 5
10: 1+0 = 1
02: 0+2 = 2
51: 5+1 = 6
That looks to be in order, so place the player on the beginning of level 5, with 32 as health, with the might sword, but not the shield, and make npcs run away from him/her.
Now, There's ways to improve on this:
1) Add some "magic numbers" in the first string (425032100251 -> 42503200100251 for example. There's a "00" in there now, after the health (032))
These should not change and as long as only you know about them it will be harder to break the algorithm and cheat.
2) Replace each number with a letter, we only have 10 (0-9) numbers, so select 10 letters to use in their place. Might be easier for the players to remember them.
3) You could slap a crc16/32 or MDA5 checksum at the end instead of my little control string. But the passwords will be a lot bigger.
4) Don't overdo it. (forget about md5 for example) I feel that my little way might feel a bit complicated, but the result isn't that bad. (feel free to show me how bad it is, if you think it's worth it) It's ok a player breaks it and cheat. It's not the end of the world.
Hope my little rant was of any use. I have never done it myself in a game, but I like to think about problems and how I would solve them. :)
Let's say I'm on level 5, with 32 out of 100 health points. I have the magic sword, but not the magic shield. Got 25 coins (999 is the maximum) and is infected with the horribly disease that makes NPCs run away from me.
5 ' my level
032 ' health out of 100
1 ' got the sword
0 ' I dont have the shield
025 ' 25 gold coins
1 ' I'm infected
Add them, as strings, together and we get this:
5032100251
Slap a "magic header to it" so we can easily check that it's not some madeup value. I use "42" in this example, since it's the meaning of life, the universe and everything:
425032100251
Now we have a nice little string there that is easily dividable by two, so let's group them into two and two, and calculate a simple check on each pair
42: 4+2 = 6
50: 5+0 = 5
32: 3+2 = 5
10: 1+0 = 1
02: 0+2 = 2
51: 5+1 = 6
Add these into a new string:
655126
Then append it to the string we had earlier:
425032100251655126
There's your magical password!
Want to parse it?
It got "42" at the beginning, so we can assume it's a valid password.
Cut out the last 6 numbers at the end, since it's our control number:
425032100251
655126
Now, calculate the pairs in the first string and check them against the control number:
42: 4+2 = 6
50: 5+0 = 5
32: 3+2 = 5
10: 1+0 = 1
02: 0+2 = 2
51: 5+1 = 6
That looks to be in order, so place the player on the beginning of level 5, with 32 as health, with the might sword, but not the shield, and make npcs run away from him/her.
Now, There's ways to improve on this:
1) Add some "magic numbers" in the first string (425032100251 -> 42503200100251 for example. There's a "00" in there now, after the health (032))
These should not change and as long as only you know about them it will be harder to break the algorithm and cheat.
2) Replace each number with a letter, we only have 10 (0-9) numbers, so select 10 letters to use in their place. Might be easier for the players to remember them.
3) You could slap a crc16/32 or MDA5 checksum at the end instead of my little control string. But the passwords will be a lot bigger.
4) Don't overdo it. (forget about md5 for example) I feel that my little way might feel a bit complicated, but the result isn't that bad. (feel free to show me how bad it is, if you think it's worth it) It's ok a player breaks it and cheat. It's not the end of the world.
Hope my little rant was of any use. I have never done it myself in a game, but I like to think about problems and how I would solve them. :)