1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream>
using namespace std;
// A function to pick a random number
int rollDie( int sides )
{
return 1 + rand() % sides;
}
// A function that utilizes the roll die function to put together a 4 digit number (numbers are 1-8)
int generateAnswer()
{
int value, answer;
cin >> value;
if (value == 0)
answer = 1000*rollDie(8) + 100*rollDie(8) + 10*rollDie(8) + rollDie(8);
else
answer = value;
return answer;
}
int nthDigit( int combination, int position )
{
int current; // current digit in the 1's place
while (position < 4)
{
combination /= 10; // knock off a digit
position++; // desired digit moved to the right
}
// Now that the desired digit is in the one's place,
// remove the leading digits.
return (combination % 10); // remainder modulo 10 is one's place
}
void clearNthDigit( int &combination, int position )
{
int current;
int digitValue;
current = 4;
digitValue = 1;
while (position < current)
{
digitValue *= 10;
current--;
}
combination -= digitValue * nthDigit( combination, position );
}
void evaluate( int guess, int answer, int &black, int &white )
{
black = 0;
white = 0;
for (int i=1; i<=4; i++)
if (nthDigit( guess, i ) == nthDigit( answer, i ) )
{
black++;
clearNthDigit( guess, i );
clearNthDigit( answer, i );
}
for (int i=1; i<=4; i++)
for (int j=1; j<=4 && nthDigit(guess,i)>0; j++)
if (nthDigit( guess,i ) == nthDigit( answer, j ) )
{
white++;
clearNthDigit( guess, i );
clearNthDigit( answer, j );
}
}
int* generateSearchSpace(int guesses[], int length)
{
int count = 0;
for (int a = 1; a <=8; ++a)
for (int b = 1; b <=8; ++b)
for (int c = 1; c <=8; ++c)
for (int d = 1; d<=8; ++d)
{
guesses[count] = 1000*a + 100*b + 10*c + d;
count++;
}
return guesses;
}
void trim(int guesses[], int length, int guess, int black, int white)
{
int wrongNumber[8];
int possibleNumber[4];
if (black == 0 && white == 0) //If no numbers are found to be correct or in the wrong position
{
for(int x = guess; x < length; x++)
guesses[x] = NULL;
}
if (black + white == 4)
{
for (int x=0; x<4; x++)
possibleNumber[x] = nthDigit(x, guess);
}
for (int x=0; x<length; x++)
for(int y=0; y<4; y++)
for(int z=0; z != NULL; z++)
if(nthDigit (y, guesses[x]) == wrongNumber[z])
guesses[x]=0;
}
int methodicalEliminate(int answer)
{
const int Length = 4096;
int list[Length];
int guess;
int x = 0;
int tries = 0;
while (x<Length && (list[x] == NULL || list[x] == 0))
x++;
guess = list[x];
generateSearchSpace (list, Length);
trim (list,Length,guess,black,white);
return tries;
}
void guessAndEliminate(int answer)
{
const int Length = 4096;
int list[Length];
int guess;
int x = 0;
//int tries
while (x<Length && (list[x] == NULL || list[x] == 0))
x++;
guess = list[x];
generateSearchSpace (list, Length);
//trim (list,Length,guess,black,white);
// return tries;
}
void guessThreeAndEliminate(int answer)
{
const int Length = 4096;
int list[Length];
int guess;
int x = 0;
//int tries
while (x<Length && (list[x] == NULL || list[x] == 0))
x++;
guess = list[x];
generateSearchSpace (list, Length);
//trim (list,Length,guess,black,white);
// return tries;
}
int main()
{
char yesOrNo = 'y';
while (yesOrNo == 'y')
{
cout << "Please enter a 4 digit combination, or press 0 for a random value: ";
cout << "Guessing at " << generateAnswer() << endl; // Displays random or user-selected 4 digit number
// int answer = generateAnswer();
//methodicalEliminate(answer);
//guessThreeThenEliminate(answer);
//GuessAndEliminate(answer);
cout << "Would you like to play another game? (y/n) ";
cin >> yesOrNo;
}
cout << "Thank you for playing!" << endl;
return 0;
}
|