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:35:27 2011 +0400
1.3 @@ -0,0 +1,89 @@
1.4 +package ru.alphatech.icfpc.policy;
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 +
1.14 +/**
1.15 + *
1.16 + * TODO
1.17 + *
1.18 + * @author akhmetovlz
1.19 + *
1.20 + */
1.21 +public abstract class AbstractPolicy {
1.22 +
1.23 + protected ArrayList<Batch> batches = new ArrayList<Batch>();
1.24 +
1.25 + public AbstractPolicy(){
1.26 + }
1.27 +
1.28 + public void setRegister(int registerIndex, int num ){
1.29 + if( registerIndex < 4) {
1.30 + Slot registerSlot = Run.getRegisterByIndex(registerIndex);
1.31 + batches.addAll( numberIntoSlotField( registerSlot, num ) );
1.32 + }
1.33 + }
1.34 +
1.35 + protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
1.36 + {
1.37 + ArrayList<Batch> retArray = new ArrayList<Batch>();
1.38 +
1.39 + IValue targetValue = targetSlot.getValue();
1.40 +
1.41 + if(targetValue instanceof ICard){
1.42 + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
1.43 + }
1.44 +
1.45 + Value numTargetValue = (Value) targetValue;
1.46 + if( numTargetValue.getValue() == number ){
1.47 + return retArray;
1.48 + }else
1.49 + if( numTargetValue.getValue() > number ){
1.50 + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
1.51 + retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
1.52 + stupidAlg(0, number, targetSlot);
1.53 + }else{
1.54 + stupidAlg(numTargetValue.getValue(), number, targetSlot);
1.55 + }
1.56 +
1.57 + return retArray;
1.58 + }
1.59 +
1.60 +
1.61 + protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
1.62 + ArrayList<Batch> retArray = new ArrayList<Batch>();
1.63 + while( begin*2 < end){
1.64 + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
1.65 + begin *= 2;
1.66 + }
1.67 + while( begin != end){
1.68 + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
1.69 + begin ++;
1.70 + }
1.71 + return retArray;
1.72 + }
1.73 +
1.74 + protected ArrayList<Batch> smartAlg(int begin, int end, Slot targetSlot){
1.75 + ArrayList<Batch> retArray = new ArrayList<Batch>();
1.76 + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
1.77 + retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
1.78 + while(end >= 2){
1.79 + int d = end % 2;
1.80 + if( d == 0 ) {
1.81 + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
1.82 + } else {
1.83 + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
1.84 + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
1.85 + }
1.86 + end = end / 2;
1.87 + }
1.88 + return retArray;
1.89 + }
1.90 +
1.91 + public abstract ArrayList<Batch> getBatches();
1.92 +}