Guess digital games-people give questions, computer guesses)

Source: Internet
Author: User
Tags random seed

/*
* File name: guessnum. h
* Program description:
* Common intelligent solutions for "guess numbers" games:
* Digit guessing game: There are four decimal digits. Generally, you can guess 8 times.
* AABB is returned each time. (A indicates that the number is correct and the position is correct. B indicates that the number is correct but the position is incorrect)
* For example, assume that the number to be guessed is 1234. If the player guesses 0134, 2a1b is returned (3 and 4 are a and 1 is B)
*
* Algorithm: the greedy algorithm is used to guess the number with the highest entropy.
*
* Another method for using decision trees is provided. For details, see guessnumall. cpp.
* Another program, guessnumgame. cpp, is used to simulate a number game.
*/
# Include <vector>
# Include <list>
# Include <string>
Using namespace STD;

Class guessnum
{
/**/
Public:
Guessnum (void );
Void guess (void );
Void printstack (void );

/**/
PRIVATE:
Bool confirmonoclonal (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 <string> m_setall;
List <string> m_setcurr;
Vector <string> m_stack;
Int m_times;
};
/*
[01/19-21:54:50]
Guess digital games:
? There are four decimal digits. Generally, you can guess eight times.
? AABB is returned each time. (A indicates that the number is correct and the position is correct. B indicates that the number is correct but the position is incorrect)
? For example, assume that the number to be guessed is 1234. If the player guesses 0134, 2a1b is returned (3 and 4 are a and 1 is B)

Ideas:
? Simulate the process of people guessing numbers. First, construct a set containing all possible numbers (10*9*8*7 = 5040), and first guess one, delete all nonconformities in the original set based on the returned xaxb, and then select the next one based on the returned xaxb. The principle of selecting the next one is to select the number that best distinguishes the remaining set, that is, the probability that the selected number returns a value similar to any of xaxb.
? I use the information method, namely:
?? For each number N in the Set (size), use each return value of N (from 0a0b to 4a0b, there are 15 types) to delete and check the remaining number, pi is used to indicate the I-th type of returned value, and then calculated based on the formula of the amount of information: Info (n) = Σ (PI/size) * log (PI/size, then take Max (Info (I) I = 0 .. size; the largest number corresponding to Info (n) is the selected number.

The following is one of the four full columns in a computer guess Program (0-9). Now I want to change it to (1-9) and choose one of the four full columns.
-------------------
My idea is to narrow down the scope of the complete set and exclude the four digits with 0 from the red section that I modified.
Modify the random number with 0 for the first random guess Filter
But the Modification result: the random number is still 0. the complete set is still 0. I think I have not learned C ++ before, but I am familiar with C. thank you very much for helping your younger brother give me some advice or give me some suggestions. because the program is in urgent need. modify it for me
/*
* File name: guessnum. cpp
* Program description: implement the guessnum class.
*/
# Pragma warning (Disable: 4786)
# Include <iostream>
# Include <algorithm>
# Include <ctime>
# Include <strstream>
# Include <cmath>
# Include "guessnum. H"

Const int guessnum: m_base [5] = {0, 5, 9, 12, 14 };

/**/

Guessnum: guessnum (void)
{
M_times = 0;

// Generate m_setall
Unsigned int num = 123;
Char A [10];
While (Num & lt; 10000)
{
Bool valid = true; // The number is not repeated.
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 <4; q ++)
If (A [Q] = '0') Valid = false; // The red part shows the content I added. to narrow down the full set, exclude numbers with 0.
If (valid) m_setall.push_back ();
Num ++;
} // # While (Num)
} // # Guessnum ()

/**/
Void guessnum: Guess (void)
{
// Reset the dataset
M_setcurr.clear ();

List <string >:: iterator itcopy = m_setall.begin ();
While (itcopy! = M_setall.end () m_setcurr.push_back (* itcopy ++ );
M_stack.clear ();
M_times = 0;

// Randomly generate the first guess
Srand (unsigned) Time (null); // Random Seed
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; // The red part is the content I added. I want to exclude the random 4-digit digits with 0.
If (valid) m_lastguess =;
}

While (! Guessnext ());
} // # Guess ()

/**/
Bool guessnum: guessnext (void)
{
M_times ++;

// Guess to obtain the result
String result;
Cout <"/n /n ";
Printstack ();
Cout <"nth" <m_times <"second guess:" <m_lastguess <Endl;
Cout <"Enter the result [? A? B]: ";

Bool valid = false;
While (! Valid)
{
Cin> result;

// Judge whether the input is valid
If (result. Length () = 4 &&
Result [0]> = '0 '&&
Result [0] <= '4 '&&
Result [2]> = '0 '&&
Result [2] <= '4 '&&
(Result [0] + result [2]) <= ('0' + '4 ')&&
Toupper (result [1]) = 'A '&&
Toupper (result [3]) = 'B ')
{
Valid = true;
}

If (! Valid) cout <"the input is invalid. Please enter again :";
} // # While

// Import the result to the stack
Strstream STRM;
STRM <"no." <m_times <"Times:" <m_lastguess <"->" <result <Endl;

String strtemp;
Getline (STRM, strtemp );
M_stack.push_back (strtemp );
If (result [0]! = '4 ')
{
// You have not guessed it. compress the dataset.
List <string >:: iterator itremove;
Itremove = m_setcurr.begin ();
While (itremove! = M_setcurr.end ())
{
If (! Confirmonoclonal (m_lastguess, * itremove, result ))
Itremove = m_setcurr.erase (itremove );
Else
Itremove ++;
}

//@@
If (m_setcurr.size () = 0)
{
Cerr <"check which input is incorrect! "<Endl;
Cerr <"program ended" <Endl;
Exit (1 );
}
Else if (m_setcurr.size () = 1)
{
M_lastguess = m_setcurr.front ();
Return false;
}

//@@
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, maximum information volume
String strmax; // The number of strings that provide the maximum amount of information
List <string >:: iterator iten = m_setcurr.begin ();

// Calculate the amount of information used by each number as the partition time, and obtain the one that provides the maximum amount of information
Double ecurr = 0;
Int ablist [15]; // 15 cases from 0a0b to 4a0b
Memset (ablist, 0, 15 * sizeof (INT)/sizeof (char ));
While (iten! = M_setcurr.end ())
{
List <string >:: iterator ittemp = m_setcurr.begin ();
While (ittemp! = M_setcurr.end ())
{
Ablist [getabvalue (* iten, * ittemp)] ++;
Ittemp ++;
}

// Statistics entropy

//@@

// Cout <"sigh .." <* iten <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 );
}
}

If (etemp> Emax)
{
Cout <"Here Emax =" <Emax <Endl;
Emax = etemp;
Strmax = * iten;
}

Iten ++;
} // # While (iten)
M_lastguess = strmax;
Return false;
} // # If (result [0]! = 4)
Else // result [0] = 4
{
Cout <"/N:" <m_lastguess <Endl;
Cout <"A total of" <m_times <"times" <Endl;
Printstack ();
Return true;
}
} // # Guessnext ()

/**/
Void guessnum: printstack (void)
{
For (INT I = 0; I <m_stack.size (); I ++) cout <m_stack [I] <Endl;
} // # Printstack ()

/**/
Inline bool guessnum: confirmonoclonal (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;
} // # Confirmonoclonal ()

/**/
Inline int guessnum: getabvalue (string first, 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 the program
# Include <iostream>
# Include "guessnum. H"
Using namespace STD;

Int main ()
{
Guessnum GN;
Cout <"guessnum object created..." <Endl;
GN. Guess ();
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.