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.Put;
10 import ru.alphatech.icfpc.cards.Succ;
11 import ru.alphatech.icfpc.cards.Zero;
12 import ru.alphatech.icfpc.game.Batch.OperationType;
21 public abstract class AbstractPolicy {
23 protected ArrayList<Batch> batches = new ArrayList<Batch>();
25 public AbstractPolicy(){
28 public void setRegister(int registerIndex, int num ){
29 if( registerIndex < 4) {
30 Slot registerSlot = Run.getRegisterByIndex(registerIndex);
31 batches.addAll( numberIntoSlotField( registerSlot, num ) );
35 protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
37 ArrayList<Batch> retArray = new ArrayList<Batch>();
39 IValue targetValue = targetSlot.getValue();
41 if(targetValue instanceof ICard){
42 retArray.addAll(smartAlg(0, number, targetSlot));
44 Value numTargetValue = (Value) targetValue;
45 if( numTargetValue.getValue() == number ){
48 if( numTargetValue.getValue() > number ){
49 retArray.addAll( smartAlg(0, number, targetSlot) );
51 retArray.addAll( smartAlg(numTargetValue.getValue(), number, targetSlot) );
59 protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
60 ArrayList<Batch> retArray = new ArrayList<Batch>();
61 while( begin*2 < end){
62 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
66 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
72 protected static List<Batch> smartAlg(int begin, int end, Slot targetSlot){
73 ArrayList<Batch> retArray = new ArrayList<Batch>();
76 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
77 retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
78 retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
81 List<Batch> list1 = buildNumberOperations(0, end, targetSlot);
82 List<Batch> list2 = buildNumberOperations(begin, end, targetSlot);
83 if (list1.size() <= list2.size())
85 Collections.reverse(list1);
86 retArray.addAll(list1);
90 Collections.reverse(list2);
91 retArray.addAll(list2);
96 protected static List<Batch> buildNumberOperations(int begin, int end, Slot targetSlot){
97 List<Batch> list = new ArrayList<Batch>();
101 if (begin > 0 && 2*begin > end)
103 for (int i = 0; i < end - begin; i++)
105 list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
110 list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
113 list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
114 list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
121 public abstract ArrayList<Batch> getBatches();
123 public static void main(String[] args)
125 Slot slot = new Slot(1);
126 List<Batch> list = smartAlg(0, 8, slot);
127 System.out.println(list.toArray().toString());