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