Jump to content

Help required for a state machine C program.

- - - - -

  • Please log in to reply
4 replies to this topic

#1
v17

v17

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,
I have written the below code for basic implementation of state machine.
My code has two sets of state machines:

1st set of states:
Idle,Initialize, Setup, Action.

State action has substates:
Searching, FindEnemy, AttackEnemy, EnemyDead.



#include <stdio.h>

void InIdle(void);

void InInit(void);

void InSetup(void);

void InAction(void);

void InFindEnemy(void);

void InAttackEnemy(void);

void InEnemyDead(void);


typedef enum

	{

		Idle=0, Initialize, Setup, Action

	} states;

states _state=Initialize;


/*Combat is substate of Action*/

typedef enum

	{

		Searching, FindEnemy, AttackEnemy, EnemyDead

	} Combat;

Combat _combat=FindEnemy;



int main()

{

printf("in main");


printf("\nPrinting State %d",_state);	


while(1)

switch(_state)

	{

	

	case Initialize:

	InInit();

	break;

	

	case Setup:

	InSetup();

	break;

	

	case Action:

	InAction();

	break;

	

	printf("Ready again :)");

	}

return 0;

}



void InInit()

{

printf("\nI am in Init");

_state=Setup;

}


void InSetup()

{

printf("\nI am in Setup");

_state=Action;

}


void InAction()

{

printf("\nI am in Action");

printf("\n**Substate %d",_combat);

switch(_combat)

{


	case FindEnemy:

	InFindEnemy();

	break;

	

	case AttackEnemy:

	InAttackEnemy();

	break;

	

	case EnemyDead:

	InEnemyDead();

	break;

}


}


void InFindEnemy()

{

printf("\nInFindEnemy");

_combat=AttackEnemy;

}


void InAttackEnemy()

{

printf("\nIn AttackEnemy ");

_combat=EnemyDead;

}


void InEnemyDead()

{

printf("\nIn EnemyDead");


_combat=Searching;

_state=Idle;


}

When I execute the code, I see the following output:

$ ./test.exe

in main

Printing State 1

I am in Init

I am in Setup

I am in Action

**Substate 1

InFindEnemy

I am in Action

**Substate 2

In AttackEnemy

I am in Action

**Substate 3

Basically I do not see prints that I have put in InEnemyDead() function

But if I modify the function:

void InEnemyDead()

{

printf("\nIn EnemyDead");


/*_combat=Searching;

_state=Idle;

*/

}

Basically I commented out _combat and _state.

Below is the output of the program:

$ ./test.exe

in main

Printing State 1

I am in Init

I am in Setup

I am in Action

**Substate 1

InFindEnemy

I am in Action

**Substate 2

In AttackEnemy

I am in Action

**Substate 3

[B]In EnemyDead[/B]

I am in Action

**Substate 3

In EnemyDead

I am in Action

**Substate 3

In EnemyDead

I am in Action

**Substate 3

In EnemyDead

I am in Action

**Substate 3

In EnemyDead

I am in Action

**Substate 3

In EnemyDead


Please help!
Thanks,
V17

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
When I ran it this was the output:

in main

Printing State 1

I am in Init

I am in Setup

I am in Action

**Substate 1

InFindEnemy

I am in Action

**Substate 2

In AttackEnemy

I am in Action

**Substate 3

In EnemyDead

I don't know what is causing it to not work for you. Out of curiosity, what compiler are you using?
Latinamne loqueris?

#3
v17

v17

    Newbie

  • Members
  • Pip
  • 2 posts
hi mebob,

Thanks for your response.
My c compiler version:

$ gcc --version
gcc (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)
Copyright © 2004 Free Software Foundation, Inc.

#4
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Your GCC is way out of date, you may want to update it. Also, have you tried compiling with other compilers?
Latinamne loqueris?

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Why did you comment out everything EXCEPT the printf you don't want to run?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users