Explanation
- Headers:
<iostream>
: For input and output operations.<cstdlib>
: Forrand()
andsrand()
functions.<ctime>
: Fortime()
function to seed the random number generator.
- Enum:
enum class State
: Defines the possible states for the AI (Idle, Patrol, Chase).
- AI Class:
- Attributes:
currentState
: Current state of the AI.playerDistance
: Simulated distance to the player.
- Methods:
update()
: Updates the AI’s state based on the player’s distance and prints the current state.simulatePlayerMovement()
: Simulates changes in player distance using a random value.
- Attributes:
- Main Function:
- Initialization:
- Seeds the random number generator.
- Creates an
AI
object.
- Simulation Loop:
- Runs for a fixed number of steps (10 in this case).
- Simulates player movement by changing the distance.
- Calls
update()
to adjust the AI’s state and print the result.
- Initialization:
Notes:
- Finite State Machine: This program demonstrates a simple finite state machine where the AI’s behavior changes based on the distance to the player.
- Random Distance: The
simulatePlayerMovement()
function generates a random distance to simulate player movement.