#include "MyVacuum.hh"

#include 
#include 

//
static MyPlayer player;

//
MyPlayer::MyPlayer()
{
  direction=JUST_STARTED;
  forwards=MOVED_ONCE;
}

//
MyPlayer::~MyPlayer()
{
}

//
void MyPlayer::reset()
{
  Player::reset();
  direction = JUST_STARTED;
  forwards=MOVED_ONCE;
}

//  
//  

const Action MyPlayer::process ( const Percept &percept )
{

  cout << "    My score is " << getScore() << "." << endl;

  // The most important thing is to suck up dirt.
  if (percept.isDirt()==1) return SUCK_UP;

  // We hit a wall.  There are 4 cases:
  // 1)  We're in the upper left corner.  We've traversed the
  //     whole room and need to get back.
  // 2)  We're in the lower right corner.  We've just started
  //     out and need to get into our zig zag pattern.
  // 3)  We're in zig zag mode having turned left last time.
  // 4)  We're in zig zag mode having turned right last time.

  if (percept.isBump()==1) 
    {
      // upper left corner
      if (forwards == TURNING)
	// Only occurs with odd number of columns
	// Here we've bumped - turned - forward - bump again
	// So we turn back towards home and head straight
	{
	  forwards = STRAIGHT;
	  return TURN_LEFT;
	}
      if (direction == JUST_STARTED)
	// Our first bump  Turn left and start zig-zag pattern
	{
	  direction = TURNED_RIGHT;
	  // Yep, we are turning left, but our telling ourselves
	  // that we turned right.  Just so our zig-zag is set
	  forwards = STRAIGHT;
	  return TURN_LEFT;
	}
      else if (direction == TURNED_LEFT)
	// We're zig zagging and turned right last time
	{
	  forwards = MOVED_ONCE;
	  direction = TURNED_RIGHT;
	  return TURN_RIGHT;
	}
      else
	// We turned left last time
	{
	  forwards = MOVED_ONCE;
	  direction = TURNED_LEFT;
	  return TURN_LEFT;
	}
    }

  // When home there are three cases:
  // 1)  We just started - just go straight
  // 2)  We've come out of a zig zag - have to finish leftmost column
  // 3)  We've finished our sweep and are done
  if (percept.isHome()==1)
    {
      // Initial case
      // Also case after second case
      if (forwards == MOVED_ONCE) {
	forwards = STRAIGHT;
	return GOFORWARD;
      }
      // We got here in the middle of a turn, so need one more row
      if (forwards == TURNING) 
	{
	  forwards = MOVED_ONCE;
	  return TURN_RIGHT;
	}
      // We must be done
      return TURN_OFF;
    }

  // The rest are cases when we perceive nothing

  // We're in the middle of a turn
  if (forwards == MOVED_ONCE)
    {
      forwards = TURNING;
      return GOFORWARD;
    }

  // Just went forward once after part of a turn.  Finish it.
  if (forwards == TURNING)
    {
      forwards = STRAIGHT;
      if (direction == TURNED_LEFT)
	{
	  return TURN_LEFT;
	}
      else if (direction == TURNED_RIGHT)
	{
	  return TURN_RIGHT;
	}
    }

  // default - we're just going forward
  return GOFORWARD;
}

void MyPlayer::done()
{
  cout << "    In the Final Analysis:" << endl;
  cout << "    My state is " << getState() << "." << endl;
  cout << "    My score is " << getScore() << "." << endl;  
}