src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author akhmetovlz
Sun, 19 Jun 2011 13:35:27 +0400
changeset 51 d93ce04e0f7b
child 52 eea6fcf60b73
permissions -rw-r--r--
(no commit message)
     1 package ru.alphatech.icfpc.policy;
     2 
     3 import java.util.ArrayList;
     4 
     5 import ru.alphatech.icfpc.cards.Dbl;
     6 import ru.alphatech.icfpc.cards.ICard;
     7 import ru.alphatech.icfpc.cards.Put;
     8 import ru.alphatech.icfpc.cards.Succ;
     9 import ru.alphatech.icfpc.cards.Zero;
    10 
    11 /**
    12  * 
    13  * TODO 
    14  * 
    15  * @author akhmetovlz
    16  *
    17  */
    18 public abstract class AbstractPolicy {
    19 
    20 	protected ArrayList<Batch> batches = new ArrayList<Batch>();
    21 	
    22 	public AbstractPolicy(){
    23 	}	
    24 	
    25 	public void setRegister(int registerIndex, int num ){
    26 		if( registerIndex < 4) {
    27 			Slot registerSlot = Run.getRegisterByIndex(registerIndex);
    28 			batches.addAll( numberIntoSlotField( registerSlot, num ) );
    29 		}
    30 	}
    31 	
    32 	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
    33 	{	
    34 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    35 		
    36 		IValue targetValue = targetSlot.getValue();
    37 		
    38 		if(targetValue instanceof ICard){
    39 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    40 		}
    41 		
    42 		Value numTargetValue = (Value) targetValue;
    43 		if( numTargetValue.getValue() == number ){
    44 			return retArray;
    45 		}else
    46 		if( numTargetValue.getValue() > number ){
    47 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    48 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    49 			stupidAlg(0, number, targetSlot);
    50 		}else{
    51 			stupidAlg(numTargetValue.getValue(), number, targetSlot);
    52 		}
    53 		
    54 		return retArray;
    55 	}
    56 	
    57 	
    58 	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
    59 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    60 		while( begin*2 < end){
    61 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
    62 			begin *= 2;
    63 		}
    64 		while( begin != end){
    65 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    66 			begin ++;
    67 		}
    68 		return retArray;
    69 	}
    70 	
    71 	protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
    72 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    73 		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    74 		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    75 		while(end >= 2){
    76 			int d = end % 2;
    77 			if( d == 0 ) {
    78 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    79 			} else {
    80 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
    81 				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    82 			}
    83 			end = end / 2; 
    84 		}
    85 		return retArray;
    86 	}
    87 	
    88 	public abstract ArrayList<Batch> getBatches();
    89 }