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