Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- using namespace std;
- void userInputCode()
- {};
- void userCreateCode(int code[], int num_digits, int int_code)
- {
- for (int i = num_digits - 1; i >= 0; i--) //Create code and store in array
- {
- code[i] = int_code % 10;
- int_code /= 10;
- }
- }
- void randomCode(int code[], int random_pool[], int num_digits, int size)
- {
- for (int i = 0; i < num_digits; i++)
- {
- int index = rand() % size;
- code[i] = random_pool[index];
- random_pool[index] = random_pool[size - 1];
- size--;
- }
- }
- void validateInput(int &cheat_code, int &num_digits) {
- bool keep_going = true;
- while (keep_going)
- {
- cout << "Enter number of digits in code (3, 4, or 5): ";
- cin >> num_digits;
- switch (num_digits)
- {
- case 3:
- case 4:
- case 5:
- keep_going = false;
- break;
- case 0:
- cheat_code = 1;
- keep_going = false;
- break;
- default:
- cout << "Please enter a valid input. " << endl;
- cout << endl;
- break;
- }
- }
- }
- int main(){
- srand(std::chrono::duration_cast<std::chrono::milliseconds>
- (std::chrono::system_clock::now().time_since_epoch()).count() % 2000000000);
- const int CODE_SIZE = 5;
- const int NUMBER_POOL = 10;
- int cheat_code = 0;
- int size = 10;
- int int_code;
- int num_digits = -1;
- int code[CODE_SIZE];
- int random_pool[NUMBER_POOL] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- //get the number of digits for the game
- validateInput(cheat_code, num_digits);
- if (cheat_code == 1)
- {
- cout << "Enter code: ";
- cin >> int_code;
- cout << "Number of digits in code: ";
- cin >> num_digits; //Get inputs from cheater
- userCreateCode(code, num_digits, int_code);
- }
- else
- {
- //Generate code of distinct digits
- randomCode(code, random_pool, num_digits, size);
- }
- //Output the correct code to guess with correct formatting
- cout << "Number to guess: ";
- for (int i = 0; i < num_digits; i++)
- {
- cout << code[i];
- if (i != num_digits - 1)
- cout << '-';
- }
- cout << endl;
- int number;
- int user_code[5];
- int user_digits[5];
- int index = 0;
- bool keepGoing = true;
- bool found = false;
- while (keepGoing)
- {
- if (found)
- {
- cout << "ERROR: The digits of the coded must be distinct." << endl;
- }
- found = false;
- cout << "Enter a code to guess: ";
- cin >> number;
- cout << number << endl;
- while (number != 0)
- {
- int temp = number;
- if (temp / (pow(10, num_digits - 1)) >= 10)
- {
- cout << "ERROR: Code must not exceed the number of digits.";
- cout << endl;
- break;
- }
- else
- {
- user_digits[index] = number % 10;
- number /= 10;
- index++;
- }
- }
- int x = 0;
- int y = 0;
- for (x = 0; x < num_digits; x++)
- {
- for (y = x + 1; y < num_digits; y++)
- {
- cout << "y: " << user_digits[x] << "\tx: " << user_digits[y] << endl;
- if (user_digits[x] == user_digits[y])
- {
- cout << "DUPLICATE";
- cout << endl;
- found = true;
- }
- }
- }
- if (!found)
- keepGoing = false;
- }
- //Output with zero at beginning
- int i = 0;
- for (int j = num_digits - 1; j >= 0; j--)
- {
- if (user_digits[num_digits - 1] == 0)
- {
- user_code[0] = 0;
- user_code[i + 1] = user_digits[j];
- }
- else
- user_code[i] = user_digits[j];
- i++;
- }
- int bull = 0;
- int cow = 0;
- for (int i = 0; i < num_digits; i++)
- {
- for (int j = 0; j < num_digits; j++)
- {
- if (user_code[i] == code[i])
- {
- bull++;
- i++;
- }
- else if (user_code[i] == code[j] && i != j)
- cow++;
- }
- }
- cout << '(' << bull << ") Bulls, and (" << cow << ") Cows";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement