# Thread: My AI is AS (Artificial Stupidity)

1. ## My AI is AS (Artificial Stupidity)

Hello, my AI is not going so great. I can make the computer make a good first guess but then the guesses are way off. Is there anyway I could make a record of all the guesses, find the middle two, and find the difference?
Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>

int game()
{
using namespace std;

cout << "Welcome to High or Low - Beat Adam" << endl;
cout << "What is your first name?: ";
string name;
cin >> name;
cout << "Welcome " << name << ".";
Sleep(2000);
system("cls");
srand((unsigned)time(NULL));
bool win;
win = false;
char turn;
turn = 'o';
guessyou = 0;

while (win == false)
{

while (turn == 'o')
{
cout << name << "'s turn." << endl;
cout << "Guess a number between 1 and 1,000: ";
cin >> guess;
guessyou++;

{
cout << endl << "Congratulations! You have beaten computer Adam in " << guessyou << " guesses!";
whowon = 1;
win = true;
turn = 'x';
}

{
cout << "Too high!" << endl;
turn = 'a';
}

{
cout << "Too low!" << endl;
turn = 'a';
}

else
{
cout << "An unknown error occured." << endl;
cout << endl;
return 0;
}

while (turn == 'a')
{
cout << endl << "Adam's turn.";

{
guess = guess / 2;
guess = abs(guess);
cout << endl << "Adam guesses " << guess << "." << endl;

{
cout << "Ha ha! Adam won in " << guessadam << " guesses!" << endl;
whowon = 2;
win = true;
turn = 'x';
}

{
cout << "Too high!" << endl << endl;
turn = 'o';
}

{
cout << "Too low!" << endl << endl;
turn = 'o';
}

else
{
cout << "An unknown error occured." << endl;
cout << endl;
return 0;
}
}

{
guess = ((guess / 2) + guess);
guess = abs(guess);
cout << endl << "Adam guesses " << guess << ".";

{
cout << "Ha ha! Adam won in " << guessadam << " guesses!" << endl;
whowon = 2;
win = true;
turn = 'x';
}

{
cout << endl << "Too high!" << endl << endl;
turn = 'o';
}

{
cout << endl << "Too low!" << endl << endl;
turn = 'o';
}

else
{
cout << endl << "An unknown error occured." << endl;
cout << endl;
return 0;
}
}
}
}
}
}
int main()
{
using namespace std;

game();
cout << endl;
system("pause");
return 0;
}```

2. Adam, I can tell you something you should be doing differently. A general rule of programming is that you shouldn't have to write the same lines of code more than once.

I see here that you have the same lines of code in different if and else statements. All this does is extend the length of your code and make it very hard to read. It's possible to reduce the multiple occurances of code blocks to just one block.

And where do you actually assign "guess" when it's Adam's turn before you try to test it?

Code:
```        while (turn == 'a')
{
cout << endl << "Adam's turn.";

//**Where is the assignment?**

{```
Should be some kind of random assignment here. You might want to create 'max' and 'min' variables here.

With those two variables, you can generate a random number within a certain range, based on what Adam knows.

For instance, if he "knows" that 50 is too low and 100 is too high from guessing in the past, you can generate a random number between 50-100 by keeping track of what's too high and what's too low.

The way to implement something like that is:

Code:
```int range = (max-min)+1
guess = (rand()%range)+min```
In this, our range is 51. You can only have a remainder as high as 50 so this gives you a random number, 0-50...then adds min (50) to create a random number between 50-100

Hope that helps.

3. Guess is assigned by the other player's last guess. That's why it's so screwed up.