Basic flow: a. Initialize 4 non-repetitive numbers b. Players have eight opportunities for guess. The correct position is not correct to B, the number is correct and the position is correct as: the number to be guess is 1234, then the result of 1563 is 1A1B, 1 number is correct and the position is correct, 3 digital correct position is incorrect 8 chance Guess the number, you can get 4A0B for victory, otherwise the failure is not complicated, the point is not more complicated: the point is nothing more than: generates no repetition random number; II: How to verify the number of players entered, compare. The first point is described in another article. The most stupid method of verifying the number is to make a double cycle, up to 4 * 4 times will be done. However, it is too stupid that it doesn't mean. Just think about a slightly smart point, as long as 4 times, it is set: the original data is Source [4], generates a number of COMSOURCE, the binary representation is used in 10 bits, where the leftmost representation 9, the rightmost representation 0, then in the binary form of COMSource contains 4 1, different locations in different points, indicating which number, such as 1234, is represented as ... 0000110. The number input by the player is input [4], and is also converted to a similar thing, and 2 is represented as .... 100, omitted is 0. In this way, when compared, COMSource & INPUT [I]> 0 means that the number of players guess is in the original number, then compare Source [i] and INPUT [I], it is obtained whether the position is correct. The method of conversion is to do a shift operation, namely: comsource | = (1 << Source [i]) This program is actually made to this algorithm ~ ~ program structure and source code: divide 3 files: main. CPP, Guess.h, Gues.cpp. Imagine to be a class-free class that is unrelated to the interface, but unfortunately considering the interface problem too much, simply written directly into a consle form. If you change, you need to rewrite the INPUT and DISPLAY functions in the Gues class. a) main.cpp: #include
Int main () {guess girl; guess.run (); system ("pause"); Return 0;} This is not good, debugging under DEV-CPP. There is an idea to write gues into a static class, which seems to be more reasonable, but there is nothing necessary. b) Guess.h // Class Automatic Or Generated by DEV-C New Class Wizard
#ifndef guess_h # Define Guess_h
// no descriptionclass guess {public: // class constructor guess (); // class destructor ~ guess (); // Game runner void in (); // initialize the game void initialize (); // no description Bool Wannaplay (); // in Game Void Ingame (); // Get Player's INPUT VOID GETINPUT (); // Display Guess Result Void Display (int T = -1); // Game Result Void DisplayResult (); private: // THE Source Data That Generated for Each Game Int Source [4]; // The Data That Player Input for Each Turn. Int Input [8] [4]; // The Guess Result for Each Turn.first Indecates A, The Other For B Int Result [8] [2]; // bit type if souce, for comparition int coosource; // bit type of infut, for comparition int Cominput [4]; // Current Turn of the Game Int Turn; // Stay In Game or Not Bool Quit;}; # Endif // Guess_h Every variable Note is written, the interpretation of the function is placed in the back c) Guess.cpp first explain each function, then all the code. Run: Game control loops, this controls whether the player performs the next game, not the internal process of the game Initialize: Initializer. Initializing the original number, as well as other class members: ask the player to continue the game, console-based INGAME: Game internal control, the internal process of the game is controlled: Get the guess number of the player's input, based on consoledisplay: output designation wheel Supreme guess results DisplayResult: The structure of the output game constructor is empty because the game can be repeated, so the data initialization code is placed in Initialize, not in the constructor. Void guess :: run () {quit = false; // game loop while (! quit) {itialize (); // initialize game; if (! wannaplay ()) {// player exists: cout << "Thank you For playing! "<< Endl; quit = true;} else ingame (); // play in game}} Nothing is said.
Void Guess :: Initialize () {INT I, J; SRAND (NULL)); // Initialize the random number generator for (i = 0; i <4; i ) // Initialization game Data Cominput [i] = 0; for (j = 0; j <8; j ) {for (i = 0; i <4; i ) INPUT [J] [i] = 0; Result [J] [0 ] = 0; Result [J] [1] = 0;} comsource = 0; turn = -1; // init source int number = -1; // store 0-9 ten digital for (i = 0; i <10; i ) Numbers [i] = i 1; for (i = 0; i <4; i ) // Randomly draw 4 non-repetitive numbers in Source {Int Rander = RAND ()% (10 -i); source [i] = number = number; NumBers [rander] = number = Numbers [10-i-1];}} End Initialization Bool Guess :: Wannaplay () {While (TRUE) {cout << "DO YOU WANNA START A GAME? (Y / / N) "<< Endl; Char Answer; try {cin >> Answer; switch (answer) {copy 'y': case 'y': return true; case 'n': Case 'n': Return false; default: cout << "please answer 'y' or 'n'!" << endl; break;}} catch (...) {cout << "unknown error ... << endl; cout << "" please answer 'y' or 'n'! "<< endl;}}} Nothing said :: ingame () // {INT I, J; for (i = 0; i <4 ; i ) // Established comsource comsource = comsource | (1 <<
Source [i]); while (TURN <7) // Cycle {TURN ; getInput (); // Get the data IF (quit) return; for (i = 0; i <4; i ) // init Cominput to bit value for comparition {cominput [i] = 1 << Input [TURN] [i];} for (i = 0; i <4; i ) {IF ((Cominput [i] & comsource)> 0) // the INPUT [I] EXISTS in Source {IF (Input [TURN] [I] == Source [i]) // the location is right result [TURN] [0] ; else // only the value is // ONLY THE VALUE IS Right; Result [TURN] [1] ;}} IF (Result [TURN] [0]> = 4) // If the guess is successful, the game ends Break; Display (TURN); // Output this wheel guess Situation} DISPLAYRESULT (); // Output Game Case} End Void Guess :: GetInput () {String Instring; Int Check, Temp, Comtemp, I; CHAR C; While (TRUE) {cout << "Current Turn: <
Cout << "Result: << Source [0] << Source [1] << Source [2] << Source [3] << Endl; Cout <<" Your Guess History: << Endl; Display );} Game Results File Head plus #include "guess.h" // Class's header file # include