#include "Ant.h"
#include <iostream.h>
#include "Dice.h"
	
Ant::Ant(int h, int v, direction d)
:	_horiz(h),
	_vert(v),
	_direction(d)
{
}

/*
Ant::Ant(const Ant &a)
:	_horiz(a._horiz),
	_vert(a._vert),
	_direction(a._direction)
{
}

Ant::~Ant(){
//nothing
}

Ant& Ant::operator =(const Ant& a)
{	if(this != &a)
	{	_horiz = a._horiz;
		_vert = a._vert;
		_direction = a._direction;
	}
	return *this;
}

*/

void Ant::move(void)
{	switch (_direction)
	{	case North: _vert ++; break;
		case East:	_horiz++; break;
		case South:	_vert --; break;
		case West:	_horiz--; break;
	}
}

void Ant::turnLeft(void)
{	switch (_direction)
	{	case North: _direction = West; break;
		case East:	_direction = North; break;
		case South:	_direction = East; break;
		case West:	_direction = South; break;
	}
}

void Ant::mark()
{	cout << " (" << _horiz << ',' << _vert << ") ";
}

void Ant::eat(Food &f)
{	f.eaten();
}

//********************  food  *******************************

Food::Food(int h, int v, int amount)
:	_horiz(h),
	_vert(v),
	_quantity(amount)
{
}

void Food::eaten()
{	if(_quantity > 0)
		_quantity--;
	else
		WARN("No food here.");
}
		
//********************  world  *******************************

World::World(int amountO_food)
: 	_food(), // up to 10 pieces
	_howMuch(0)
{	for(int i = 0; i< amountO_food; i++)
	{	Die d(5);
		_food[_howMuch++] = new Food(d.roll()-3, d.roll()-3, 1);
	}
}

void World::dump()
{	cout << endl;
	for(int i=0; i<_howMuch; i++)
	{	cout << '<' << ' '<< _food[i]->_horiz << ' '<< _food[i]->_vert << ' '<< _food[i]->_quantity << '>'<<endl;	
	}
}


World::World(const World & w)
{	while(_howMuch > 0)
		delete _food[--_howMuch];
	_food = w._food;
	_howMuch = w._howMuch;
}

World::~World(void)
{	while(_howMuch > 0)
		delete _food[--_howMuch];
}

World & World::operator = (const World & w)
{	if(this != &w)
	{	while(_howMuch > 0)
			delete _food[--_howMuch];
		_food = w._food;
		_howMuch = w._howMuch;
	}
	return *this;
}

Boolean World::foodAt(int h, int v)
{	for(int i = 0; i< _howMuch; i++){
		if(	h == _food[i]->_horiz && 
			v == _food[i]->_vert  &&
			_food[i]->_quantity > 0) 
			return true;
	}
	return false;
}

Boolean World::foodAt(Ant &a)
{	return foodAt(a._horiz, a._vert);
}

Food & World::getFood(int h, int v)
{	for(int i = 0; i< _howMuch; i++)
	{	if(h == _food[i]->_horiz && v == _food[i]->_vert) return *_food[i];
	}
	userERROR("No food here.");
	return *_food[0];
}

Food & World::getFood(Ant &a)
{	return getFood(a._horiz, a._vert);
}
		
		
