Merge with 1ff9e4ee2b028103fbcd24600c37cf6b8cba1df7
authorAzamat S. Kalimoulline <turtle@bazon.ru>
Sun, 19 Jun 2011 13:42:53 +0400
changeset 599307831f7658
parent 58 43e507966634
parent 56 1ff9e4ee2b02
child 60 7b2179e85787
Merge with 1ff9e4ee2b028103fbcd24600c37cf6b8cba1df7
src/main/java/ru/alphatech/icfpc/policy/AbstractPolicy.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java	Sun Jun 19 13:42:53 2011 +0400
     1.3 @@ -0,0 +1,90 @@
     1.4 +package ru.alphatech.icfpc.game;
     1.5 +
     1.6 +import java.util.ArrayList;
     1.7 +
     1.8 +import ru.alphatech.icfpc.cards.Dbl;
     1.9 +import ru.alphatech.icfpc.cards.ICard;
    1.10 +import ru.alphatech.icfpc.cards.Put;
    1.11 +import ru.alphatech.icfpc.cards.Succ;
    1.12 +import ru.alphatech.icfpc.cards.Zero;
    1.13 +import ru.alphatech.icfpc.game.Batch.OperationType;
    1.14 +
    1.15 +/**
    1.16 + * 
    1.17 + * TODO 
    1.18 + * 
    1.19 + * @author akhmetovlz
    1.20 + *
    1.21 + */
    1.22 +public abstract class AbstractPolicy {
    1.23 +
    1.24 +	protected ArrayList<Batch> batches = new ArrayList<Batch>();
    1.25 +	
    1.26 +	public AbstractPolicy(){
    1.27 +	}	
    1.28 +	
    1.29 +	public void setRegister(int registerIndex, int num ){
    1.30 +		if( registerIndex < 4) {
    1.31 +			Slot registerSlot = Run.getRegisterByIndex(registerIndex);
    1.32 +			batches.addAll( numberIntoSlotField( registerSlot, num ) );
    1.33 +		}
    1.34 +	}
    1.35 +	
    1.36 +	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
    1.37 +	{	
    1.38 +		ArrayList<Batch> retArray = new ArrayList<Batch>();
    1.39 +		
    1.40 +		IValue targetValue = targetSlot.getValue();
    1.41 +		
    1.42 +		if(targetValue instanceof ICard){
    1.43 +			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    1.44 +		}
    1.45 +		
    1.46 +		Value numTargetValue = (Value) targetValue;
    1.47 +		if( numTargetValue.getValue() == number ){
    1.48 +			return retArray;
    1.49 +		}else
    1.50 +		if( numTargetValue.getValue() > number ){
    1.51 +			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    1.52 +			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    1.53 +			stupidAlg(0, number, targetSlot);
    1.54 +		}else{
    1.55 +			stupidAlg(numTargetValue.getValue(), number, targetSlot);
    1.56 +		}
    1.57 +		
    1.58 +		return retArray;
    1.59 +	}
    1.60 +	
    1.61 +	
    1.62 +	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
    1.63 +		ArrayList<Batch> retArray = new ArrayList<Batch>();
    1.64 +		while( begin*2 < end){
    1.65 +			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
    1.66 +			begin *= 2;
    1.67 +		}
    1.68 +		while( begin != end){
    1.69 +			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    1.70 +			begin ++;
    1.71 +		}
    1.72 +		return retArray;
    1.73 +	}
    1.74 +	
    1.75 +	protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
    1.76 +		ArrayList<Batch> retArray = new ArrayList<Batch>();
    1.77 +		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    1.78 +		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    1.79 +		while(end >= 2){
    1.80 +			int d = end % 2;
    1.81 +			if( d == 0 ) {
    1.82 +				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    1.83 +			} else {
    1.84 +				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    1.85 +				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    1.86 +			}
    1.87 +			end = end / 2; 
    1.88 +		}
    1.89 +		return retArray;
    1.90 +	}
    1.91 +	
    1.92 +	public abstract ArrayList<Batch> getBatches();
    1.93 +}
     2.1 --- a/src/main/java/ru/alphatech/icfpc/game/Run.java	Sun Jun 19 13:42:36 2011 +0400
     2.2 +++ b/src/main/java/ru/alphatech/icfpc/game/Run.java	Sun Jun 19 13:42:53 2011 +0400
     2.3 @@ -11,6 +11,9 @@
     2.4  import java.util.LinkedList;
     2.5  import java.util.List;
     2.6  
     2.7 +import ru.alphatech.icfpc.game.strategy.IStrategy;
     2.8 +import ru.alphatech.icfpc.game.strategy.SimplyStrategy;
     2.9 +
    2.10  public class Run
    2.11  {
    2.12  	private static boolean endgame = false;
    2.13 @@ -30,6 +33,8 @@
    2.14  		registers[3] = 3;
    2.15  	}
    2.16  	
    2.17 +	private static IStrategy strategy = new SimplyStrategy();
    2.18 +	
    2.19  	public static Slot getRegisterByIndex(int registerIdex){
    2.20  		return proponent.getSlot( registers[registerIdex] );
    2.21  	}
    2.22 @@ -80,9 +85,7 @@
    2.23  	
    2.24  	private static void writeMove()
    2.25      {
    2.26 -		System.out.print(1 + "\n");
    2.27 -		System.out.print("I" + "\n");
    2.28 -		System.out.print(0 + "\n");
    2.29 +		strategy.moving(proponent, opponent, System.out);
    2.30      }
    2.31  	
    2.32  	public static void main(String[] args) throws IOException
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/main/java/ru/alphatech/icfpc/game/strategy/IStrategy.java	Sun Jun 19 13:42:53 2011 +0400
     3.3 @@ -0,0 +1,14 @@
     3.4 +package ru.alphatech.icfpc.game.strategy;
     3.5 +
     3.6 +import java.io.PrintStream;
     3.7 +
     3.8 +import ru.alphatech.icfpc.game.Player;
     3.9 +
    3.10 +/**
    3.11 + * @author indvdum
    3.12 + * 19.06.2011 13:36:17
    3.13 + *
    3.14 + */
    3.15 +public interface IStrategy {
    3.16 +	public void moving(Player proponent, Player opponent, PrintStream out);
    3.17 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/main/java/ru/alphatech/icfpc/game/strategy/SimplyStrategy.java	Sun Jun 19 13:42:53 2011 +0400
     4.3 @@ -0,0 +1,21 @@
     4.4 +package ru.alphatech.icfpc.game.strategy;
     4.5 +
     4.6 +import java.io.PrintStream;
     4.7 +
     4.8 +import ru.alphatech.icfpc.game.Player;
     4.9 +
    4.10 +/**
    4.11 + * @author indvdum
    4.12 + * 19.06.2011 13:36:26
    4.13 + *
    4.14 + */
    4.15 +public class SimplyStrategy implements IStrategy {
    4.16 +
    4.17 +	@Override
    4.18 +	public void moving(Player proponent, Player opponent, PrintStream out) {
    4.19 +		out.print(1 + "\n");
    4.20 +		out.print("I" + "\n");
    4.21 +		out.print(0 + "\n");
    4.22 +	}
    4.23 +
    4.24 +}
     5.1 --- a/src/main/java/ru/alphatech/icfpc/policy/AbstractPolicy.java	Sun Jun 19 13:42:36 2011 +0400
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,96 +0,0 @@
     5.4 -package ru.alphatech.icfpc.policy;
     5.5 -
     5.6 -import java.util.ArrayList;
     5.7 -
     5.8 -import ru.alphatech.icfpc.cards.Dbl;
     5.9 -import ru.alphatech.icfpc.cards.ICard;
    5.10 -import ru.alphatech.icfpc.cards.Put;
    5.11 -import ru.alphatech.icfpc.cards.Succ;
    5.12 -import ru.alphatech.icfpc.cards.Zero;
    5.13 -import ru.alphatech.icfpc.game.Batch;
    5.14 -import ru.alphatech.icfpc.game.IValue;
    5.15 -import ru.alphatech.icfpc.game.Player;
    5.16 -import ru.alphatech.icfpc.game.Run;
    5.17 -import ru.alphatech.icfpc.game.Slot;
    5.18 -import ru.alphatech.icfpc.game.Value;
    5.19 -import ru.alphatech.icfpc.game.Batch.OperationType;
    5.20 -
    5.21 -/**
    5.22 - * 
    5.23 - * TODO 
    5.24 - * 
    5.25 - * @author akhmetovlz
    5.26 - *
    5.27 - */
    5.28 -public abstract class AbstractPolicy {
    5.29 -
    5.30 -	protected ArrayList<Batch> batches = new ArrayList<Batch>();
    5.31 -	
    5.32 -	public AbstractPolicy(){
    5.33 -	}	
    5.34 -	
    5.35 -	public void setRegister(int registerIndex, int num ){
    5.36 -		if( registerIndex < 4) {
    5.37 -			Slot registerSlot = Run.getRegisterByIndex(registerIndex);
    5.38 -			batches.addAll( numberIntoSlotField( registerSlot, num ) );
    5.39 -		}
    5.40 -	}
    5.41 -	
    5.42 -	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
    5.43 -	{	
    5.44 -		ArrayList<Batch> retArray = new ArrayList<Batch>();
    5.45 -		
    5.46 -		IValue targetValue = targetSlot.getValue();
    5.47 -		
    5.48 -		if(targetValue instanceof ICard){
    5.49 -			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    5.50 -		}
    5.51 -		
    5.52 -		Value numTargetValue = (Value) targetValue;
    5.53 -		if( numTargetValue.getValue() == number ){
    5.54 -			return retArray;
    5.55 -		}else
    5.56 -		if( numTargetValue.getValue() > number ){
    5.57 -			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    5.58 -			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    5.59 -			stupidAlg(0, number, targetSlot);
    5.60 -		}else{
    5.61 -			stupidAlg(numTargetValue.getValue(), number, targetSlot);
    5.62 -		}
    5.63 -		
    5.64 -		return retArray;
    5.65 -	}
    5.66 -	
    5.67 -	
    5.68 -	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
    5.69 -		ArrayList<Batch> retArray = new ArrayList<Batch>();
    5.70 -		while( begin*2 < end){
    5.71 -			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
    5.72 -			begin *= 2;
    5.73 -		}
    5.74 -		while( begin != end){
    5.75 -			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    5.76 -			begin ++;
    5.77 -		}
    5.78 -		return retArray;
    5.79 -	}
    5.80 -	
    5.81 -	protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
    5.82 -		ArrayList<Batch> retArray = new ArrayList<Batch>();
    5.83 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    5.84 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    5.85 -		while(end >= 2){
    5.86 -			int d = end % 2;
    5.87 -			if( d == 0 ) {
    5.88 -				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    5.89 -			} else {
    5.90 -				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    5.91 -				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    5.92 -			}
    5.93 -			end = end / 2; 
    5.94 -		}
    5.95 -		return retArray;
    5.96 -	}
    5.97 -	
    5.98 -	public abstract ArrayList<Batch> getBatches();
    5.99 -}
     6.1 --- a/src/main/java/ru/alphatech/icfpc/policy/VitalityBoostPolicy.java	Sun Jun 19 13:42:36 2011 +0400
     6.2 +++ b/src/main/java/ru/alphatech/icfpc/policy/VitalityBoostPolicy.java	Sun Jun 19 13:42:53 2011 +0400
     6.3 @@ -2,6 +2,7 @@
     6.4  
     6.5  import java.util.ArrayList;
     6.6  
     6.7 +import ru.alphatech.icfpc.game.AbstractPolicy;
     6.8  import ru.alphatech.icfpc.game.Batch;
     6.9  
    6.10  public class VitalityBoostPolicy extends AbstractPolicy {