An algorithm used to guess digital games

Source: Internet
Author: User

//////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////////
////
// Software name: Guess 1.0 //
// OPERATOR: owl //
// Time: 2004.10.21 //
// Description: The computer generates any number between four 0-9 records that are not repeated ,//
// After that, it is input by the player. Based on the input, the computer compares two groups of numbers? //
// Provide information similar to xayb. A indicates the number and corresponding position //
// Both are correct. B indicates that the number is correct, but the position is incorrect. //
// Use X and Y to indicate the two conditions. If it is completely correct, "OK" is displayed "//
// If the input is 10 times, the correct result is not returned. Game failure ,//
// The program outputs the base number. //
////
//////////////////////////////////////// //////////////////////////////////////// /////////////////////////////////////
# Include <iostream>
# Include <cstdlib>
# Include <cctype>
# Include <vector>
# Include <algorithm>
Using namespace STD;
Const int vec_size = 4;
Const int err_max = 10;
Int play (void );
Int main (void)
{
Bool stat = true;
Srand (Time (null ));
Cout <"welcome to the guess digital game! "<Endl;
While (STAT = true)
{
Cout <"Press Y to enter the game and press any key to exit :";
Char c = cin. Get ();
If (C = 'y ')
Play ();
Else
Stat = false;
}
System ("pause ");
Return 0;
}
Const vector <int> & rand_data (vector <int> &)
{
For (INT I = 0; I <vec_size; ++ I)
{
Int T = rand () % 10;
If (find (A. Begin (), A. End (), t) = A. End ())
A. push_back (t );
Else
-- I;
}
Return;
}
Bool counter (const vector <int> &,
Const vector <int> & V)
{
Int x = 0, y = 0;

For (INT I = 0; I <vec_size; ++ I)
For (Int J = 0; j <vec_size; ++ J)
{
If (V [I] = A [J])
If (I = J)
X ++;
Else
Y ++;
}
If (x = vec_size)
Return true;

Cout <x <"A" <Y <"B" <Endl;
Return false;
}
Bool isdigit (int elem)
{
Char c = '0 ';
Const int step = C-0;
Return isdigit (ELEM + step );
}
Const vector <int> & in (vector <int> & V)
{
Cout <"Enter four numbers that are not repeated (0-9) and separate them with spaces! "
<Endl;
For (INT I = 0; I <vec_size; ++ I)
Cin> V [I];
For (INT I = 0; I <vec_size; ++ I)
{
If (isdigit (V [I]) = false)
{
Cerr <"must be a number between 0 and 9! Enter"
<I + 1
<"Digit :";
Cin> V [I];
}
For (Int J = 0; j <vec_size; ++ J)
If (V [I] = V [J] & I! = J)
{
Cerr <"error. Please enter"
<I + 1
<"Number! "
<Endl;
Cin> V [I];
-- I;
}
}
Return V;
}
Void out (vector <int> & A, bool Stat)
{
If (STAT = false)
{
Cout <"you lost. The base number for this operation is :";
For (size_t I = 0; I <A. Size (); ++ I)
Cout <A [I] <"";
Cout <Endl;
Cout <"Try again ~! "<Endl;
}
Else
Cout <"OK !!! Congratulations! "<Endl;

}
Int play (void)
{
Vector <int>;
Vector <int> V (vec_size );
Bool stat = false;
Rand_data ();
For (INT I = 0; I <err_max; ++ I)
{
In (v );
If (STAT = counter (A, V ))
Break;
}
Out (A, STAT );
Return 0;
}

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.