/ ** File Name: Guessnum.h * Program Description: * Common game [guess number] smart solution: * Guess digital game: there are four decimal numbers, generally guess 8 times * Each time you return AABB (a Indicates that the number is correct and the position is correct, b means that the number is correct but the location is incorrect) * such as: Supplement To guess the number is 1234, if the player guess 0134 returns 2A1b (3, 4 is A, 1 is b) ** algorithm: Each time you select the maximum number of entropy, it is speculated that: Vector> #include #include
Class Guessnum {/ * * / public: Guessnum (Void); Void Guess (Void); Void PrintStack;
/ * * / Private: bool ConfirmAB (string first, string second, string ab); bool GuessNext (void); int GetABValue (string first, string second); const static int m_base [5]; string m_lastGuess; list
Ideas:? Simators guess the process of numbers, first construct a collection including all possible numbers (10 * 9 * 8 * 7 = 5040 species), first guess one, according to returning xaxb, put the original collection does not match Delete, then the next one is selected. Selecting a principle that: select the number of the remaining collections, namely: The return value obtained by the number selected is similar to the probability of any one of Xaxb. I use the amount of information, namely: ?? Each number n in the set set (Size), using each return value (from 0A0b to 4A0B, a total of 15 kinds), see the left How many of the following, use PI to returns a reference, then according to the information quantity formula: INFO (N) = σ (pi / size) * log (pi / size), then take MAX (INFO (I)) i = 0..size; the maximum number of INFO (N) is the number selected.
Below is a program in which 4 full-ranked programs (0-9) now I want to modify (1-9) in 4 full arrangements. --------- ---------- I modified the idea that narrowing the complete set of 4 digits to the 4 digits of 0 exclude the following red to the part to modify the first random number, the random number of filter belt 0 but modified results: The random number still has 0 appearance. Complete Works still have 0 here I want to be ignorant, I don't have to learn C , I don't understand, but I can help the little brother to point to maze or to make your modified method. Beware. Because Urgently need this program. Please help me modify / ** file name: Guessnum.cpp * Program Description: Guessnum class * / # pragma Warning (Disable: 4786) #include
/ * * /
Guessnum :: Guesnum (void) {m_times = 0;
// Generate m_setall unsigned int Num = 123; char A [10]; while (Num <10000) {bool valid = true; // Indicates that the number does not repeat Sprintf (A, "% 04u", Num); for (INT I = 0; i <4; i ) {for (int J = i 1; j <4; j ) IF (a [i] == a [j]) VALID = false;
For (int q = 0; q ) IF (a [q] == '0') Valid = false; / / red part wants to reduce the full set range exclusion with 0 Digital IF (Valid) ) m_setall.push_back (a); Num ;} // # while (num)} // # Guessnum ()
/ * / void guessnum :: guess {// Reset Data Set m_setCurr.clear ();
List
// randomly generate the first guess SRAND (NULL)); // Random number BOOL VALID = false; unsigned int icode; while (! Valid) {icode = rand (); while (icode> 9999 ) iCode / = 10;
Char a [10]; sprintf (a, "% 04u", icode); valid = true; for (int i = 0; i <4; i ) for (int J = i 1; j <4; j ) IF (a [i] == a [j]) valid = false; for (int q = 0; q <4; q ) IF (a [q] == '0') Valid = false; // red part For me, I want to exclude random 4-digit IF (Valid) m_lastguess = a;} while (! # Gues);} // # guess ()
/ * / BOOL GUESSNUM :: GuessNext (void) {m_times ;
// Speech for guess, get the speculation String Result; cout << "/ n / n / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N / N "; PrintStack (); cout <<" "<< m_times <<" session: "<< m_lastguess << Endl; cout <<" Please enter the result [ ? A? B]: ";
Bool valid = false; while (! value) {cin >> result
/ / Judgment whether the input is legal if (Result.Length () == 4 && results [0]> = '0' && results [0] <= '4' && results [2]> = '0' && results [2 ] <= '4' && (Result [0] Result [2]) <= ('0' '4') && TouPper (Result [1]) == 'A' && Toupper (Result [3]) == 'b') {valid = true;
If (! valid) cout << "Enter is not legal, please re-enter:";} // # while
// Put the result into Strstream Strm; strm << "" << m_times << "times:" << m_lastguess << "-> << result << endl;
String straTemp; getLine (strM, strtemp); m_stack.push_back (strTemp); if (Result [0]! = '4') {// has not yet guess, the data set is not guessing List
@@ cout << "m_setall.size () =" << m_setall.size () << endl; cout << "m_setCurr.size () =" << m_setcurr.size () << endl;
// Generate the next guess data double emax = 0; // max entropy, the maximum amount of information String strMax; // provides the maximum amount of information of the number list list
// Calculate the amount of information when you distinguishes each number, take the Double Ecurr = 0 of the maximum amount of information; int ablist [15]; // A total of 15 cases from 0A0B to 4A0B MEMSET (ablist, 0, 15 * SizeOf (int) / sizeof (char)); while (ney! = m_setcurr.end ()) {list
// Statistical entropy
@@
// cout << "SIGH .. << * ipen << Endl; double eTemp = 0; double ediv = 0; for (int i = 0; i <15; i ) {if (ablist [i]! = 0) // Avoid 0 * log0 {ediv = double (ablist [i]) / double (m_setcurr.size ()); ETEMP = -ediv * log (ediv);}} f (etemp> emax) {cout << "Here EMAX =" << Emax << Endl; EMAX = ETEMP; strMax = * ipen;
ITEN ;} // # while (ney) m_lastguess = strmax; return false;} // #} (result [0]! = 4) Else // Result [0] == 4 {cout << "/ n answer is : "<< m_lastguess << Endl; cout <<" a total of "<< m_times <<" times << Endl; printstack (); return true;}} // # guessnext ()
/ * * / void Guessnum :: PrintStack (void) {for (int i = 0; i / * / Inline Bool Guessnum :: ConfirMab (String first, string second, string ab) {int a = 0, b = 0; for (int i = 0; i <4; i ) {for (INT j = 0 ; J <4; J ) IF (First [i] == Second [J]) IF (i == J) A ; ELSE B ; IF (ab [0] == char (A '0') && ab [2] == CHAR (B '0')) Return True; Else Return False;} // # confirMab () / * * / inline int Guessnum :: getabValue (string second) {int a = 0; int b = 0; for (int i = 0; i <4; i ) {for (int J = 0; J <4; j ) IF (first [i] == second [j]) if (i == j) A ; ELSE B ;} return m_base [a] b;} // # getabvalue () // Test program #include Int main () {Guessnum Gn; cout << "Guessnum Object Created ... << Endl; gn.guess (); return 0;}