/***********************************************************************
 ** OldG4Strategy2a.java
 **
 ** A strategy designed to work well for one player.  Very efficient in
 ** terms of not retracing lines
 **
 ***********************************************************************/

package Rectangles;

import java.util.*;

public class OldG4Strategy2a extends OldG4Strategy {

    Rectangles rect;
    boolean edgeAttack = false;
    boolean released = false;

    int vertDir = 0;
    int horzDir = 0;

    int vertDirMax = 3;
    int horzDirMax = 2;

    char[] vertDirDef = {'S','N'};
    char[] horzDirDef = {'E','W'};

    boolean inStartPattern = true;
    char[] startPattern = {'E','S','W','N','N'};
    int startCounter = 0;

    int gridCounter = 0;
    int vertCounter = 0;
    int horzCounter = 0;

    boolean horz = true;

    int gridSize = 7;

    /* toPlace determines whether you place the robots or that is assigned */
    public OldG4Strategy2a(ArrayList robots, boolean toPlace, Rectangles __rect) {
	if (!toPlace)
	    myRobots = robots;
	else {
	     myRobots = new ArrayList();

	}

	rect = __rect;

    }

    /* alternate constructor */
    public OldG4Strategy2a(int firstIndex, int lastIndex, int size, Rectangles __rectangles) throws Exception {

	myRobots = new ArrayList();

	/* right now just place the (single) bot in the middle */
	if (firstIndex == lastIndex) {
	    OldG4Bot bot = new OldG4Bot(0);
	    bot.xpos = (size/2)-(gridSize/2);
	    bot.ypos = (size/2)-(gridSize/2);
	    myRobots.add(bot);
	}
	/* two bots */
	else if ((lastIndex-firstIndex)==1) {
	    OldG4Bot bot = new OldG4Bot(0);
	    bot.xpos = (size/3)-(gridSize/3);
	    bot.ypos = (size/3)-(gridSize/3);
	    myRobots.add(bot);
	    bot = new OldG4Bot(0);
	    bot.xpos = (2*size/3)-(gridSize/3);
	    bot.ypos = (2*size/3)-(gridSize/3);
	    myRobots.add(bot);
	}
	/* three bots */
	else if ((lastIndex-firstIndex)==2) {
	    OldG4Bot bot = new OldG4Bot(0);
	    bot.xpos = (size/3)-(gridSize/3);
	    bot.ypos = (size/3)-(gridSize/3);
	    myRobots.add(bot);
	    bot = new OldG4Bot(0);
	    bot.xpos = (size/2)-(gridSize/2);
	    bot.ypos = (2*size/3)-(gridSize/3);
	    myRobots.add(bot);
	    bot = new OldG4Bot(0);
	    bot.xpos = (2*size/3)-(gridSize/3);
	    bot.ypos = (size/3)-(gridSize/3);
	    myRobots.add(bot);
	}
	/* four bots */
	else if ((lastIndex-firstIndex)==3) {
	    OldG4Bot bot = new OldG4Bot(0);
	    bot.xpos = (size/3)-(gridSize/3);
	    bot.ypos = (size/3)-(gridSize/3);
	    myRobots.add(bot);
	    bot = new OldG4Bot(0);
	    bot.xpos = (2*size/3)-(gridSize/3);
	    bot.ypos = (2*size/3)-(gridSize/3);
	    myRobots.add(bot);
	    bot = new OldG4Bot(0);
	    bot.xpos = (2*size/3)-(gridSize/3);
	    bot.ypos = (size/3)-(gridSize/3);
	    myRobots.add(bot);
	    bot = new OldG4Bot(0);
	    bot.xpos = (size/3)-(gridSize/3);
	    bot.ypos = (2*size/3)-(gridSize/3);
	    myRobots.add(bot);
	}

	rect = __rectangles;
    }



    public ArrayList doStrategy() throws Exception{
	OldG4Bot theBot = (OldG4Bot)myRobots.get(0);

	char theDir = 'N';

	int[][] colors = rect.colors();

	/* does the opponent have 3/4 of the outside painted? */
	int edgesPainted = 0;
	for (int i=0; i<2; i++) {
	    boolean linePainted = true;
	    int xval = i*(rect.size()-1);
	    for (int j=0; j<rect.size(); j++) {
		int yval = j;
		if (colors[xval][xval]==-2) {
		    linePainted = false;
		}
	    }
	    if (linePainted)
		edgesPainted++;
	}
	for (int i=0; i<2; i++) {
	    boolean linePainted = true;
	    int xval = i*(rect.size()-1);
	    for (int j=0; j<rect.size(); j++) {
		int yval = j;
		if (colors[yval][xval]==-2) {
		    linePainted = false;
		}
	    }
	    if (linePainted)
		edgesPainted++;
	}
	if (edgesPainted >3)
	    edgeAttack = true;



	if (inStartPattern) {
	    theDir = startPattern[startCounter/gridSize];

	    startCounter++;
	    if (startCounter==(gridSize*5))
		inStartPattern = false;
	}
	else {
	    if (horz) {
		theDir = horzDirDef[horzDir];

		gridCounter++;
		if (gridCounter == gridSize) {   //done with segment
		    gridCounter = 0;
		    horz = false;            // switch directions
		    horzCounter++;
		    if (horzCounter == horzDirMax) {
			horzDir = (horzDir+1)%2;
			horzCounter = 0;
			horzDirMax++;
		    }
		}
	    }
	    else {               /* vertical move */
		theDir = vertDirDef[vertDir];
		gridCounter++;
		if (gridCounter == gridSize) {   //done with segment
		    gridCounter = 0;
		    boolean doubleNorth = ((vertDir == 1)&&
					   (vertCounter==((vertDirMax/2)-1)));
		    if (!doubleNorth)
		        horz = true;            // switch directions
		    vertCounter++;
		    if (vertCounter == vertDirMax) {
			vertDir = (vertDir+1)%2;
			vertCounter = 0;
			vertDirMax++;
		    }
		}
	    }
	}

	for (int i=0; i<myRobots.size(); i++) {

	    theBot = (OldG4Bot)myRobots.get(i);
	    /*
	    if ((i==0)&&(edgeAttack))
		theBot.move = 
	    */
	    theBot.move = theDir;

	}

	return myRobots;
    }


    public ArrayList doneRobots() {
	return null;
    }

}
