1 package ru.alphatech.icfpc.game;
3 import java.util.ArrayList;
4 import java.util.Collections;
7 import ru.alphatech.icfpc.cards.Dbl;
8 import ru.alphatech.icfpc.cards.ICard;
9 import ru.alphatech.icfpc.cards.K;
10 import ru.alphatech.icfpc.cards.Put;
11 import ru.alphatech.icfpc.cards.S;
12 import ru.alphatech.icfpc.cards.Succ;
13 import ru.alphatech.icfpc.cards.Zero;
14 import ru.alphatech.icfpc.game.Batch.OperationType;
23 public abstract class AbstractPolicy {
25 protected ArrayList<Batch> batches = new ArrayList<Batch>();
27 public AbstractPolicy(){
30 public void setRegister(int registerIndex, int num ){
31 if( registerIndex < 4) {
32 Slot registerSlot = Run.getRegisterByIndex(registerIndex);
33 batches.addAll( numberIntoSlotField( registerSlot, num ) );
37 protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
39 ArrayList<Batch> retArray = new ArrayList<Batch>();
41 IValue targetValue = targetSlot.getValue();
43 if(targetValue instanceof ICard){
44 retArray.addAll(smartAlg(0, number, targetSlot));
46 Value numTargetValue = (Value) targetValue;
47 if( numTargetValue.getValue() == number ){
50 if( numTargetValue.getValue() > number ){
51 retArray.addAll( smartAlg(0, number, targetSlot) );
53 retArray.addAll( smartAlg(numTargetValue.getValue(), number, targetSlot) );
61 protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
62 ArrayList<Batch> retArray = new ArrayList<Batch>();
63 while( begin*2 < end){
64 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
68 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
74 protected static List<Batch> smartAlg(int begin, int end, Slot targetSlot){
75 ArrayList<Batch> retArray = new ArrayList<Batch>();
78 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
79 retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
80 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
83 List<Batch> list1 = buildNumberOperations(0, end, targetSlot);
84 List<Batch> list2 = buildNumberOperations(begin, end, targetSlot);
85 if (list1.size() <= list2.size())
87 Collections.reverse(list1);
88 retArray.addAll(list1);
92 Collections.reverse(list2);
93 retArray.addAll(list2);
98 protected static List<Batch> buildNumberOperations(int begin, int end, Slot targetSlot){
99 List<Batch> list = new ArrayList<Batch>();
103 if (begin > 0 && 2*begin > end)
105 for (int i = 0; i < end - begin; i++)
107 list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
112 list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
115 list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
116 list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
124 * Корректировка позиций регистров
125 * В самом лучшем случаи регистры это слоты 0 1 2 3
126 * Но могут быть изменены, этот метод корректирует ссылки на регистры в полтиках
127 * @param registerIndex - индекс регитсра (не слота!)
128 * @param processingSlot - слот где происходит рассчёт
131 protected ArrayList<Batch> correctRegister(int registerIndex, Slot processingSlot){
132 ArrayList<Batch> retArray = new ArrayList<Batch>();
133 for(int i=0; i<Run.getRegisterSlotNum(0); ++i)
135 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
136 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
138 retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
143 public abstract ArrayList<Batch> getBatches();
145 public static void main(String[] args)
147 Slot slot = new Slot(1);
148 List<Batch> list = smartAlg(0, 8, slot);
149 System.out.println(list.toArray().toString());