| # This uses http://games.technoplaza.net/mpg/password.txt as a basis for its password algorithm | # This uses http://games.technoplaza.net/mpg/password.txt as a basis for its password algorithm | ||||
| import random | |||||
| import random, sys | |||||
| class MetroidState: | class MetroidState: | ||||
| def __init__(self): | def __init__(self): | ||||
| self.startLocation = 0 | self.startLocation = 0 | ||||
| self.bitfield = [] | self.bitfield = [] | ||||
| self.initializeBitfield() | self.initializeBitfield() | ||||
| self.fullbitfield = [] | |||||
| def initializeBitfield(self): | def initializeBitfield(self): | ||||
| self.bitfield = [] | self.bitfield = [] | ||||
| sl = "Start Location: {}".format(self.locations[self.startLocation]) | sl = "Start Location: {}".format(self.locations[self.startLocation]) | ||||
| dr = "Unlocked Doors: {}".format(self.openedDoors()) | dr = "Unlocked Doors: {}".format(self.openedDoors()) | ||||
| ms = "Missiles: {}".format(self.missileCount) | ms = "Missiles: {}".format(self.missileCount) | ||||
| bf = "Bitfield:\n{}".format(self.getBits()) | |||||
| return "\n".join([ic, mt, et, zb, kb, rs, sw, sl, dr, ms, bf]) | |||||
| pw = "Password: {}".format(self.password) | |||||
| # bf = "Bitfield:\n{}".format(self.getBits()) | |||||
| return "\n".join([ic, mt, et, zb, kb, rs, sw, sl, dr, ms, pw]) | |||||
| def randomize(self): | def randomize(self): | ||||
| # Items | # Items | ||||
| # checking = [] | # checking = [] | ||||
| # for i in range(16): | # for i in range(16): | ||||
| # checking.append(int("".join([str(x) for x in bitfield[i:i+8]]), 2)) | # checking.append(int("".join([str(x) for x in bitfield[i:i+8]]), 2)) | ||||
| decChecksum = sum(bitfield) | |||||
| # print("Full Bitfield: {}".format("".join([str(x) for x in bitfield]))) | |||||
| self.fullbitfield = "".join([str(x) for x in bitfield]) | |||||
| newBitfield = [] | |||||
| for i in range(17): | |||||
| j = i * 8 | |||||
| k = j + 8 | |||||
| word = self.fullbitfield[j:k][::-1] | |||||
| newBitfield.append(word) | |||||
| decChecksum = sum([int(x, 2) for x in newBitfield]) | |||||
| bitfield = "".join(newBitfield) | |||||
| binChecksum = bin(decChecksum).replace('0b','') | binChecksum = bin(decChecksum).replace('0b','') | ||||
| checksum = binChecksum[-8:] | checksum = binChecksum[-8:] | ||||
| while len(checksum) < 8: | while len(checksum) < 8: | ||||
| checksum = checksum + "0" | |||||
| print(checksum) | |||||
| checksum = "0" + checksum | |||||
| # print(checksum) | |||||
| for bit in checksum: | for bit in checksum: | ||||
| bitfield.append(int(bit)) | |||||
| print("Full Bitfield: {}".format("".join([str(x) for x in bitfield]))) | |||||
| print(len(bitfield)) | |||||
| bitfield += bit | |||||
| # print("Real Bitfield: {}".format(bitfield)) | |||||
| letters = [] | letters = [] | ||||
| letter = [] | letter = [] | ||||
| for bit in bitfield: | for bit in bitfield: | ||||
| letter.append(str(bit)) | |||||
| letter.append(bit) | |||||
| if len(letter) == 6: | if len(letter) == 6: | ||||
| print(letter) | |||||
| letters.append(self.alphabet[int("".join(letter),2)]) | letters.append(self.alphabet[int("".join(letter),2)]) | ||||
| letter = [] | letter = [] | ||||
| print(letters) | |||||
| words = [] | words = [] | ||||
| word = [] | word = [] | ||||
| print(letters) | |||||
| print(len(letters)) | |||||
| for lt in letters: | for lt in letters: | ||||
| word.append(lt) | word.append(lt) | ||||
| if len(word) == 6: | if len(word) == 6: | ||||
| words.append("".join(word)) | words.append("".join(word)) | ||||
| word = [] | word = [] | ||||
| words.append("".join(word)) | words.append("".join(word)) | ||||
| return " ".join(words) | |||||
| self.password = " ".join(words) | |||||
| return self.password | |||||
| def decodePassword(self, pwd): | |||||
| densePwd = pwd.replace(" ","") | |||||
| numPwd = [] | |||||
| for chr in densePwd: | |||||
| numPwd.append(self.alphabet.index(chr)) | |||||
| bitPwd = [bin(x).replace("0b","") for x in numPwd] | |||||
| longBitPwd = [] | |||||
| for word in bitPwd: | |||||
| longword = word | |||||
| while len(longword) < 6: | |||||
| longword = "0" + longword | |||||
| longBitPwd.append(longword) | |||||
| # print(self.fullbitfield) | |||||
| newBitfield = "".join(longBitPwd) | |||||
| # print(newBitfield) | |||||
| # print(newBitfield == self.fullbitfield) | |||||
| # print(len(newBitfield[:136])) | |||||
| # print(newBitfield[:136]) | |||||
| csm = sum([int(x) for x in newBitfield[:136]]) | |||||
| print(csm) | |||||
| # print(bin(csm).replace("0b","")) | |||||
| for i in range(len(newBitfield)): | |||||
| print(newBitfield[i], end="") | |||||
| if i%8 == 7: | |||||
| print(" ", end="") | |||||
| if i%64 == 63: | |||||
| print() | |||||
| def main(): | def main(): | ||||
| gs = MetroidState() | gs = MetroidState() | ||||
| gs.randomize() | gs.randomize() | ||||
| gs.createBitfield() | gs.createBitfield() | ||||
| pwd = gs.generatePassword() | |||||
| print(gs.toString()) | print(gs.toString()) | ||||
| print(gs.generatePassword()) | |||||
| # print(pwd) | |||||
| # gs.decodePassword(pwd) | |||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||
| main() | |||||
| if len(sys.argv) == 2: | |||||
| gs = MetroidState() | |||||
| gs.decodePassword(sys.argv[1]) | |||||
| else: | |||||
| main() |