//lines: 90
//chars: 2087

package Organisms2.g2;

import Organisms2.*;
import java.awt.*;

public class Group2PlayerJ implements IFCPlayer
{
    private Organisms2 organisms;
    private static final int NE = 0, NW = 1, SE = 2, SW = 3;
    private double moveProbability = 0.25;
    private double reproductionThreshold = 1.0;
    private int rounds;
    private int generalDirection;

    public void register(Organisms2 organisms, int value)
    {
	this.organisms = organisms;
	generalDirection = (int) (Math.random() * 4);
    }

    public Move move(boolean [] foodPresent, int [] enemyPresent, int foodLeft, int energyLeft) throws Exception
    {
	rounds++;

	//if almost dead, conserve energy
	moveProbability = (energyLeft <= 10 * organisms.v())? 0: 0.25;
	
	//try to reproduce
	if((energyLeft >= reproductionThreshold * organisms.M()) && (rounds > 1))
	    {
		for(int i=1;i<foodPresent.length;i++)
		    if(foodPresent[i] && (enemyPresent[i] < 0))
			return new Move(_CREPRODUCE, i, 0);
		
		return new Move(_CREPRODUCE, (int) (Math.random() * 5) + 1, 0);
	    }
	
	//if food there remain
	if(foodLeft > 0)
	    return new Move(_CSTAYPUT);
	
	//check if food around us
	for(int i=0;i<foodPresent.length;i++)
	    if(foodPresent[i] && (enemyPresent[i] < 0))
		return new Move(i);
	
	//move
	if(Math.random() < moveProbability)
	    {
		int direction = 0;
		if(generalDirection == NW)
		    direction = (Math.random() < 0.5)? _CNORTH: _CWEST;
		else if(generalDirection == NE)
		    direction = (Math.random() < 0.5)? _CNORTH: _CEAST;
		else if(generalDirection == SW)
		    direction = (Math.random() < 0.5)? _CSOUTH: _CWEST;
		else
		    direction = (Math.random() < 0.5)? _CSOUTH: _CEAST;
		
		if(enemyPresent[direction] < 0)
		    return new Move(direction);
	    }
	
	//if all else fails, stayput
	return new Move(_CSTAYPUT);
    }

    public int externalState()
    {
	return 128;
    }

    public String name()
    {
	return "Pray for Mojo";
    }

    public Color color()
    {
	return Color.green;
    }

    public boolean interactive()
    {
	return false;
    }
}
