src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
changeset 98 19dba9e387ce
parent 89 b6543dcda026
child 101 4bb458c3548e
     1.1 --- a/src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java	Sun Jun 19 17:53:59 2011 +0400
     1.2 +++ b/src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java	Sun Jun 19 18:42:07 2011 +0400
     1.3 @@ -1,6 +1,8 @@
     1.4  package ru.alphatech.icfpc.game;
     1.5  
     1.6  import java.util.ArrayList;
     1.7 +import java.util.Collections;
     1.8 +import java.util.List;
     1.9  
    1.10  import ru.alphatech.icfpc.cards.Attack;
    1.11  import ru.alphatech.icfpc.cards.Dbl;
    1.12 @@ -74,80 +76,57 @@
    1.13  		return retArray;
    1.14  	}
    1.15  	
    1.16 -	protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
    1.17 +	protected static List<Batch> smartAlg(int begin, int end, Slot targetSlot){
    1.18  		ArrayList<Batch> retArray = new ArrayList<Batch>();
    1.19  		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    1.20  		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    1.21 -		while(end >= 2){
    1.22 -			int d = end % 2;
    1.23 -			if( d == 0 ) {
    1.24 -				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    1.25 -			} else {
    1.26 -				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    1.27 -				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    1.28 -			}
    1.29 -			end = end / 2; 
    1.30 +		
    1.31 +		List<Batch> list1 = buildNumberOperations(0, end, targetSlot);
    1.32 +		List<Batch> list2 = buildNumberOperations(begin, end, targetSlot);
    1.33 +		if (list1.size() <= list2.size())
    1.34 +		{
    1.35 +			Collections.reverse(list1);
    1.36 +			retArray.addAll(list1);
    1.37 +		}
    1.38 +		else
    1.39 +		{
    1.40 +			Collections.reverse(list2);
    1.41 +			retArray.addAll(list2);
    1.42  		}
    1.43  		return retArray;
    1.44  	}
    1.45  	
    1.46 -	protected ArrayList<Batch> BcombineForFunctWith3ParamsInRegister(ICard function, Slot processingSlot){
    1.47 -		ArrayList<Batch> retArray = new ArrayList<Batch>();
    1.48 +	protected static List<Batch> buildNumberOperations(int begin, int end, Slot targetSlot){
    1.49 +		List<Batch> list = new ArrayList<Batch>();
    1.50  		
    1.51 -		if(!(function instanceof Attack || function instanceof Help)){
    1.52 -			throw new ICFPCException();
    1.53 +		while(end >= 2){
    1.54 +			int d = end % 2;
    1.55 +			if (begin > 0 && 2*begin > end)
    1.56 +			{
    1.57 +				for (int i = 0; i < end - begin; i++)
    1.58 +				{
    1.59 +					list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    1.60 +				}
    1.61 +				break;
    1.62 +			}
    1.63 +			if( d == 0 ) {
    1.64 +				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    1.65 +				end = end / 2;
    1.66 +			} else {
    1.67 +				list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    1.68 +				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    1.69 +				end = (end-1) / 2;
    1.70 +			}
    1.71  		}
    1.72 -		
    1.73 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), processingSlot));
    1.74 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, function,  processingSlot));
    1.75 -		
    1.76 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
    1.77 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
    1.78 -		
    1.79 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
    1.80 -
    1.81 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
    1.82 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
    1.83 -		
    1.84 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
    1.85 -		
    1.86 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
    1.87 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
    1.88 -		
    1.89 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
    1.90 -
    1.91 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
    1.92 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
    1.93 -		
    1.94 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
    1.95 -		
    1.96 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
    1.97 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
    1.98 -		
    1.99 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
   1.100 -		
   1.101 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   1.102 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   1.103 -		
   1.104 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
   1.105 -
   1.106 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   1.107 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   1.108 -		
   1.109 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   1.110 -		
   1.111 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   1.112 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   1.113 -		
   1.114 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   1.115 -		
   1.116 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   1.117 -		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   1.118 -		
   1.119 -		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
   1.120 -		
   1.121 -		return retArray;
   1.122 +		return list;
   1.123  	}
   1.124  	
   1.125  	public abstract ArrayList<Batch> getBatches();
   1.126 +	
   1.127 +	public static void main(String[] args)
   1.128 +	{
   1.129 +		Slot slot = new Slot(1);
   1.130 +		List<Batch> list = smartAlg(5, 12, slot);
   1.131 +		System.out.println(list.toArray().toString());
   1.132 +	}
   1.133  }