SHARE
TWEET

Untitled

a guest Jun 12th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Mastermind.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include <time.h>  
  7.  
  8.  
  9. using namespace std;
  10.  
  11. MastermindDigits::MastermindDigits()
  12. {
  13.     this->digits = new int[4];
  14.     makeDigitsToGuess();
  15. }
  16. MastermindDigits::MastermindDigits(int digits)
  17. {
  18.     this->digits = new int[4];
  19.     setDigits(digits);
  20. }
  21.  
  22. MastermindDigits::~MastermindDigits()
  23. {
  24.     delete[] digits;
  25. }
  26.  
  27. void MastermindDigits::makeDigitsToGuess()
  28. {
  29.     srand(time(NULL));
  30.     //this->digits = new int[4];
  31.     int* randum = new int[4];
  32.     stringstream numb;
  33.     int inumb;
  34.  
  35.  
  36.     for (int i = 0; i < 4; i++) {
  37.         randum[i] = 1 + ( rand() % 6 );
  38.     }
  39.  
  40.     for (unsigned i = 0; i < 4; ++i) {
  41.         numb << randum[i];
  42.     }
  43.  
  44.     numb >> inumb;
  45.  
  46.     setDigits(inumb);
  47.    
  48.     }
  49. void MastermindDigits::setDigits(int dig)
  50. {
  51.     //this->digits = new int[4];
  52.     string number = to_string(dig);
  53.     for (int i = 0; i < 4; i++) {
  54.         this->digits[i] = (int)number[i];
  55.     }
  56. }
  57.  
  58. int MastermindDigits::locationRight(MastermindDigits& madi)
  59. {
  60.     int cLRight = 0;
  61.     for (int i = 0; i < 4; i++) {
  62.         if (this->digits[i] == madi.digits[i]) {
  63.             ++cLRight;
  64.         }
  65.     }
  66.     return cLRight;
  67. }
  68. int MastermindDigits::locationWrong(MastermindDigits& madi)
  69. {
  70.     int cLWrong = 0;
  71.     bool locRightCode[4] = { false };
  72.     bool locRightGuess[4] = { false };
  73.  
  74.     for (int i = 0; i < 4; i++) {
  75.         if (this->digits[i] == madi.digits[i]) {
  76.             locRightCode[i] = true;
  77.             locRightGuess[i] = true;
  78.         }
  79.     }
  80.  
  81.     for (int i = 0; i < 4; i++) {
  82.         for (int y = 0; y < 4; y++) {
  83.             if (this->digits[i] == madi.digits[y] && !locRightCode[i] && !locRightGuess[y]) {
  84.                 locRightGuess[y] = true;
  85.                 ++cLWrong;
  86.                 break;
  87.             }
  88.         }
  89.     }
  90.     return cLWrong;
  91. }
  92. bool MastermindDigits::isEqual(MastermindDigits& md) {
  93.     for (int i = 0; i < 4; i++) {
  94.         if (this->digits[i] != md.digits[i]) {
  95.             return false;
  96.         }
  97.     }
  98.     return true;
  99. }
  100.  
  101. int MastermindDigits::getdigits() {
  102.     stringstream numb;
  103.     int inumb;
  104.  
  105.     for (unsigned i = 0; i < 4; ++i) {
  106.         numb << digits[i]-48;
  107.     }
  108.  
  109.     numb >> inumb;
  110.  
  111.     return inumb;
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top