# HG changeset patch # User akhmetovlz # Date 1308476127 -14400 # Node ID d93ce04e0f7b65bc1b7fc08feb0cb8419edcd4a8 # Parent 7e49c679fc70fb49d5168b5a9783a10088f171e0 (no commit message) diff -r 7e49c679fc70 -r d93ce04e0f7b src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java Sun Jun 19 13:35:27 2011 +0400 @@ -0,0 +1,89 @@ +package ru.alphatech.icfpc.policy; + +import java.util.ArrayList; + +import ru.alphatech.icfpc.cards.Dbl; +import ru.alphatech.icfpc.cards.ICard; +import ru.alphatech.icfpc.cards.Put; +import ru.alphatech.icfpc.cards.Succ; +import ru.alphatech.icfpc.cards.Zero; + +/** + * + * TODO + * + * @author akhmetovlz + * + */ +public abstract class AbstractPolicy { + + protected ArrayList batches = new ArrayList(); + + public AbstractPolicy(){ + } + + public void setRegister(int registerIndex, int num ){ + if( registerIndex < 4) { + Slot registerSlot = Run.getRegisterByIndex(registerIndex); + batches.addAll( numberIntoSlotField( registerSlot, num ) ); + } + } + + protected ArrayList numberIntoSlotField(Slot targetSlot, int number) + { + ArrayList retArray = new ArrayList(); + + IValue targetValue = targetSlot.getValue(); + + if(targetValue instanceof ICard){ + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); + } + + Value numTargetValue = (Value) targetValue; + if( numTargetValue.getValue() == number ){ + return retArray; + }else + if( numTargetValue.getValue() > number ){ + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); + retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot)); + stupidAlg(0, number, targetSlot); + }else{ + stupidAlg(numTargetValue.getValue(), number, targetSlot); + } + + return retArray; + } + + + protected ArrayList stupidAlg(int begin, int end, Slot targetSlot){ + ArrayList retArray = new ArrayList(); + while( begin*2 < end){ + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot)); + begin *= 2; + } + while( begin != end){ + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); + begin ++; + } + return retArray; + } + + protected ArrayList smartAlg(int begin, int end, Slot targetSlot){ + ArrayList retArray = new ArrayList(); + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot)); + retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot)); + while(end >= 2){ + int d = end % 2; + if( d == 0 ) { + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot)); + } else { + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot)); + retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot)); + } + end = end / 2; + } + return retArray; + } + + public abstract ArrayList getBatches(); +}