Finite State Machine

Okay, I am making a finite state machine for a lab. I have here a 2 files with the code for the FSM. I know it isn't finished yet, I know what needs to be put in. The only things I would need help on are the errors that I get.

Warrior.h
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
#ifndef _WARRIOR_
#define _WARRIOR_
#include "State.h"

class Warrior
{
private:
	fsm_state* m_currentState;

private:
	void Update()
	{
		m_currentState->Execute(this);
	}

	void ChangeState(fsm_state *newState)<-//Here I had a const but when I added the m_currentState = newState it said that the value type cannot be assigned an entity type "fsm_state"
	{
		delete m_currentState;
		m_currentState = newState;
	}

public:<-//Keeps saying I need a declaration 
	bool EnemyFOV()
	{
		int loc = rand()%20;
		if(loc >= 50)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
		
};

#endif 


State.h
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
#ifndef _STATEPATTERN_
#define _STATEPATTERN_

#include <iostream>
#include <time.h>
#include "Warrior.h"

class fsm_state
{
public:
	virtual void Execute(Warrior * Warmaster = 0);
};

class Patrol_state : public fsm_state
{
	virtual void Execute(Warrior * Warmaster) = 0
	{
		if(Warmaster->EnemyFOV())
		{
			Warmaster->ChangeState(new Attack_State());<-//the ChangeState gives me an error that it's inaccessible. Also the Attack_State() gives me an error of "object of abstract class type "Attack_State" is not allowed"
		}
		else
		{
			Warmaster->Move();
		}
	}
};

class Attack_State : public fsm_state
{
	virtual void Execute(Warrior *Warmaster) = 0
	{
		if(Warmaster->EnemyFOV())
		{
			Warmaster->ChangeState(new Death_State());
		}
		else if(Warmaster->EnemyFOV())
		{
			Warmaster->ChangeState(new Patrol_State());
		}
		else
		{
			Warmaster->Attack();
	}
};

#endif 


If anyone can help me fix there errors because I've tried to figure out what it could be. Also, if anyone can give me an alternate way of doing this FSM. This will be greatly appreciated.
ChangeState is inaccessible because it isn't inherited - the access modifier is private in the base class. It needs to be at least protected for the subclass to inherit it.

I made a FSM at the end of last year. I don't have the code to hand, but I created a StateMachine object that contained pointers to the previous and current state. In each iteration of the loop, it would run the current state's Update function.

The state was an abstract base class with Enter, Update and Exit functions. This allows you to apply "one-off" settings when you enter and leave states. It gives a bit more flexibility.

I don't have the code to hand for this, I'm afraid. I can tell you that Matt Buckland's "Programming Game AI by Example" was an excellent resource.
Topic archived. No new replies allowed.