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