#ifndef _SUM_HORSE
#define _SUM_HORSE

#include "../shareddata.h"
#include "../horse.h"
#include "../random.h"

class SumDistHorse : public Horse
{
	
	private:
		bool near(int x1, int y1, int x2, int y2) { 
			return abs(x1 - x2) <= 1 && abs(y1 - y2) <= 1;
		}
		int dist(int x1, int y1, int x2, int y2) {
			return abs(x1 - x2) + abs(y1 - y2);
		}
	public:
		virtual int move(SharedData& sh)
		{
			int move = 0;
			int indeterminacy = -int(1e9);
			int sumdistance = 0; 
			for (int k = 0; k < 5; k ++)
			{
				int cur_indeterminacy = 0;
				int cur_dist = 0;
				bool has_near = 0;
				for (int i = 1; i <= sh.m; i ++)
					for (int j = 1; j <= sh.n; j ++)
						if (sh.can[i][j] && sh.isValidPosition(i + dx[k], j + dy[k]))
						{
							if (near(i + dx[k], j + dy[k], sh.hedgehogX, sh.hedgehogY))
								has_near = 1;
							cur_indeterminacy ++;
							cur_dist += dist(sh.hedgehogX, sh.hedgehogY, i + dx[k], j + dy[k]);
						}
				if (has_near)
					cur_indeterminacy --;
				if ((cur_dist > sumdistance || (cur_indeterminacy > indeterminacy && sumdistance == cur_dist)) && sh.isValidHorseMove(k))
				{
					sumdistance = cur_dist;
					indeterminacy = cur_indeterminacy;
					move = k;
				}
			}
			return move;
		}
};

#endif
