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 "model/nesgamegeniecode.hh"
00026
00027 using namespace ggencoder;
00028
00029 const char NESGameGenieCode::ALPHABET[] = {
00030 'A', 'P', 'Z', 'L', 'G', 'I', 'T', 'Y',
00031 'E', 'O', 'X', 'U', 'K', 'S', 'V', 'N'
00032 };
00033
00034 NESGameGenieCode NESGameGenieCode::create(const QString &code)
00035 throw(InvalidGameGenieCodeException) {
00036 if (isValidCode(code)) {
00037 return NESGameGenieCode(code);
00038 }
00039
00040 throw InvalidGameGenieCodeException();
00041 }
00042
00043 const char *NESGameGenieCode::getAlphabet() const {
00044 return ALPHABET;
00045 }
00046
00047 bool NESGameGenieCode::isValidCode(const QString &code) {
00048 NESGameGenieCode temp(code);
00049
00050 const char *alphabet = temp.getAlphabet();
00051 int count = temp.getAlphabetCount();
00052
00053 QString upper = code.toUpper();
00054 int length = upper.length();
00055
00056 if ((length != 6) && (length != 8)) {
00057 return false;
00058 }
00059
00060 for (int i = 0; i < length; i++) {
00061 bool found = false;
00062 QChar ch = code[i];
00063 char letter = ch.toAscii();
00064
00065 for (int j = 0; j < count; j++) {
00066 if (letter == alphabet[j]) {
00067 found = true;
00068 break;
00069 }
00070 }
00071
00072 if (!found) {
00073 return false;
00074 }
00075 }
00076
00077 return true;
00078 }
00079