// Yura Mamyrin

package risk.engine.ai;

import risk.engine.*;
import risk.engine.core.*;

import java.util.*;

/**
 * <p> Class for AIEasyPlayer </p>
 * @author Yura Mamyrin
 */

public class AICrap {

    protected Random r = new Random(); // this was always static

    protected RiskGame game;
    protected Player player;


    public String getBattleWon() {

	return "move all";

    }

    public String getTacMove() {

	return "nomove";

    }

    public String getTrade() {

	  Vector cards = player.getCards();

	  if (cards.size() >= 3 && !( game.getTradeCap()==true && cards.size() < 5 ) ) {

	    Card card1=null, card2=null, card3=null;

	    for (int a=0; a< cards.size() ; a++) {
		if (card1 != null && card2 != null && card3 != null) { break; }
		card1 = (Card)cards.elementAt(a);

		for (int b=(a+1); b< cards.size() ; b++) {
		    if (card1 != null && card2 != null && card3 != null) { break; }
		    card2 = (Card)cards.elementAt(b);

		    for (int c=(b+1); c< cards.size() ; c++) {
			if (card1 != null && card2 != null && card3 != null) { break; }
			card3 = (Card)cards.elementAt(c);

			if ( game.checkTrade(card1, card2, card3) ) { break; }
			else { card3=null; }

		    }
		}
	    }


	    if (card3 != null) {

		String output = "trade ";

		  if (card1.getName().equals("wildcard")) { output = output + card1.getName(); }
		  else { output = output + ((Country)card1.getCountry()).getColor(); }

		output = output+" ";

		  if (card2.getName().equals("wildcard")) { output = output + card2.getName(); }
		  else { output = output + ((Country)card2.getCountry()).getColor(); }

		output = output+" ";

		  if (card3.getName().equals("wildcard")) { output = output + card3.getName(); }
		  else { output = output + ((Country)card3.getCountry()).getColor(); }

		return output;

	    }

	    return "endtrade";
	  }

	  return "endtrade";

    }

    public String getPlaceArmies() {

		if ( game.NoEmptyCountries()==false ) {
		    return "autoplace";
		}
		else {
		    Vector t = player.getTerritoriesOwned();
		    Vector n;
		    String name="";

		    for (int a=0; a< t.size() ; a++) {

			n = ((Country)t.elementAt(a)).getNeighbours();

			for (int b=0; b< n.size() ; b++) {
			    if ( ((Country)n.elementAt(b)).getOwner() != player ) { name=((Country)t.elementAt(a)).getColor()+""; break; }
			}

			if ( !(name.equals("")) ) { break; }

		    }

		    if ( name.equals("") ) {
			return "placearmies " + ((Country)t.elementAt(0)).getColor() +" "+player.getExtraArmies()  ;
		    }

		    if (game.getSetup() ) {
			return "placearmies " + name +" "+player.getExtraArmies() ;
		    }

		    return "placearmies " + name +" 1";

		}

    }

    public String getAttack() {

	return "endattack";

    }

    public String getRoll() {

	    int n=((Country)game.getAttacker()).getArmies() - 1;

	    if (n > 3) {
		    return "roll "+3;
	    }

	    return "roll "+n;

    }

    public String getCapital() {

	    Vector t = player.getTerritoriesOwned();
	    return "capital " + ((Country)t.elementAt( r.nextInt(t.size()) )).getColor();

    }

}
