-
Notifications
You must be signed in to change notification settings - Fork 0
/
robotics3AI.cpp
70 lines (63 loc) · 1.97 KB
/
robotics3AI.cpp
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
//Robotics class example 3 classes w/ AI
#include <iostream>
#include <cstdlib>
#include <ctime>
class robot
{
private:
int location;
public:
int move (int gameSize , int startPoint)
{
location = std::rand () % (gameSize - startPoint + 1) + startPoint; //Move robot to random location within our games range
std::cout << "Robot location: " << location << std::endl;
return location;
}
};
int setGoal (int gameSize)
{
int goal; // Create goal location
goal = std::rand () %gameSize + 1; // define where the goal is
return goal;
}
int gameBoard ()
{
int board;
std::cout << "How large of a range would you like the game to be? 1 - " << std::endl;
std::cin >> board;
return board;
}
int main ()
{
//Declare/define variables
int location = 0; // The robots location.
int gameArea; //the range of values rand will use
int startPoint = 1; // the starting point for values returned by rand
int goal;
// Game setuo
robot bot;
std::srand (std::time(NULL)); // Pass seed
gameArea = gameBoard ();
goal = setGoal (gameArea);
std::cout << "The robot's goal is: " << goal << std::endl; // Print the robots goal for debug purposes
//Game begin
while (true)
{
location = bot.move (gameArea , startPoint);
if (location == goal) // If we reach the goal
{
std::cout << "Goal reached: " << location << std::endl;
break; // Break out of the while loop
}
if (location < goal) // If the robots location is below goal then change the start point and range so that we dont guess lower numbers but stay within our game area
{
startPoint = location + 1;
gameArea = gameArea - location + 1;
}
if (location > goal) // If robot is above goal change the range so that we dont guess higher numbers
{
gameArea = gameArea - (gameArea - location) - 1;
}
}
return 0;
}