00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "nesgamegeniecode.hh"
00026 #include "../exceptions/invalidgamegeniecode.hh"
00027
00028 using namespace emuWorks;
00029
00030 const char NESGameGenieCode::ALPHABET[] = {
00031 'A', 'P', 'Z', 'L', 'G', 'I', 'T', 'Y',
00032 'E', 'O', 'X', 'U', 'K', 'S', 'V', 'N'
00033 };
00034
00035 NESGameGenieCode NESGameGenieCode::create(QString &code) {
00036 if (isValidCode(code)) {
00037 return NESGameGenieCode(code);
00038 }
00039
00040 throw InvalidGameGenieCodeException();
00041 }
00042
00043 bool NESGameGenieCode::isValidCode(QString &code) {
00044 NESGameGenieCode temp(code);
00045
00046 const char *alphabet = temp.getAlphabet();
00047 int count = temp.getAlphabetCount();
00048
00049 QString upper = code.toUpper();
00050 int length = upper.length();
00051
00052 if ((length != 6) && (length != 8)) {
00053 return false;
00054 }
00055
00056 for (int i = 0; i < length; i++) {
00057 bool found = false;
00058 QChar ch = code[i];
00059 char letter = ch.toAscii();
00060
00061 for (int j = 0; j < count; j++) {
00062 if (letter == alphabet[j]) {
00063 found = true;
00064 break;
00065 }
00066 }
00067
00068 if (!found) {
00069 return false;
00070 }
00071 }
00072
00073 return true;
00074 }
00075
00076 const char *NESGameGenieCode::getAlphabet() {
00077 return ALPHABET;
00078 }
00079