
// Game.java


/******************************************************************************
/ Game.java modellerar ett spel.
/
/*****************************************************************************/

class Game
{

	public static final int    MAX_ENTITIES = 4;
	public static final int    MIN_ROW = 1;
	public static final int    MAX_ROW = 6;
	public static final int    MIN_COLUMN = 1;
	public static final int    MAX_COLUMN = 6;
		
	// Spelets spelpjäser ----------------
	private Entity[]    ents = new Entity[MAX_ENTITIES];
	{
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < (MAX_ENTITIES / 2) ; j++)
			{
				ents[i * (MAX_ENTITIES / 2) + j] = new Entity (i * 5 + 1, j *
                                             2 + 1, "char", Entity.MAX_HEALTH);
			}
		}
			
	}
	
	public Game ()
	{
	}
	
	/**************************************************************************
	/ Return read-only copy of requested entity
	/*************************************************************************/
	public Entity getEntityRO (int idx)
	{
		return new Entity (ents[idx]);
	}
	
	/**************************************************************************
	/ Return requested entity
	/*************************************************************************/
	public Entity getEntity (int idx)
	{
		return ents[idx];
	}
	
	/**************************************************************************
	/ Let player 'challenger' challenge player 'challenged'.
	/*************************************************************************/
	public boolean doHit (int challenger, int challenged)
	{
		if (
			((challenger < 1) | (challenger > Game.MAX_ENTITIES)) |
			((challenged < 1) | (challenged > Game.MAX_ENTITIES))
		   )
			return false;
		else
		{
			System.out.println ("Ent #" + (challenged) + 
			                           " takes hit from ent #" + (challenger));
			ents[challenged - 1].takeHit (ents[challenger - 1]);
			return true;
		}
	}

	/**************************************************************************
	/ Move entity me to new row/col position
	/*************************************************************************/
	public boolean moveEntity (int me, int newRow, int newColumn)
	{
		if ((me < 1) | (me > Game.MAX_ENTITIES))
			return false;
			
		else if ((newRow < Game.MIN_ROW) || (newRow > Game.MAX_ROW))
			return false;
			
		else if ((newColumn < Game.MIN_COLUMN) || 
		         (newColumn > Game.MAX_COLUMN))
			return false;
			
		else
		{
			System.out.println ("Ent #" + (me) + " moving to R,C: " + newRow +
			                                                  "," + newColumn);
			ents[me - 1].setRow (newRow);
			ents[me - 1].setColumn (newColumn);
			return true;
		}
	}
	
	/**************************************************************************
	/ Return a string containing info about the game and its entities.
	/*************************************************************************/
	public String toString ()
	{
		StringBuilder    sb = new StringBuilder ("");
		sb.append ("Antal spelare : " + this.ents.length + "\r\n\r\n");
		sb.append ("Status:\r\n");
		sb.append ("------------------------------------\r\n");
		for (int i = 0; i < this.ents.length; i++)
		{
			sb.append ("Spelare " + (i + 1) + "\r\n");
			sb.append ("  Typ             : " + ents[i].getType () + "\r\n");
			sb.append ("  Position (R,K)  : " + ents[i].getRow () + "," + 
			                                    ents[i].getColumn () + "\r\n");
			sb.append ("  Halsa           : " + ents[i].getHealth () + "\r\n");
		}
		
		return new String (sb);
			   
	}
	
	
}

