src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author akhmetovlz
Mon, 20 Jun 2011 01:59:45 +0400
changeset 124 302ed98cb64c
parent 120 f8c080bbb572
permissions -rw-r--r--
(no commit message)
     1 package ru.alphatech.icfpc.game;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collections;
     5 import java.util.List;
     6 
     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;
    15 
    16 /**
    17  * 
    18  * TODO 
    19  * 
    20  * @author akhmetovlz
    21  *
    22  */
    23 public abstract class AbstractPolicy {
    24 
    25 	protected ArrayList<Batch> batches = new ArrayList<Batch>();
    26 	
    27 	public AbstractPolicy(){
    28 	}	
    29 	
    30 	public void setRegister(int registerIndex, int num ){
    31 		if( registerIndex < 4) {
    32 			Slot registerSlot = Run.getRegisterByIndex(registerIndex);
    33 			batches.addAll( numberIntoSlotField( registerSlot, num ) );
    34 		}
    35 	}
    36 	
    37 	protected ArrayList<Batch> numberIntoSlotField(Slot targetSlot, int number)
    38 	{	
    39 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    40 		
    41 		if(number == 0){
    42 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    43 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    44 		}else{
    45 			retArray.addAll(smartAlg(0, number, targetSlot));
    46 		}
    47 		return retArray;
    48 	}
    49 	
    50 	
    51 	protected ArrayList<Batch> stupidAlg(int begin, int end, Slot targetSlot){
    52 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    53 		while( begin*2 < end){
    54 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
    55 			begin *= 2;
    56 		}
    57 		while( begin != end){
    58 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    59 			begin ++;
    60 		}
    61 		return retArray;
    62 	}
    63 	
    64 	protected static List<Batch> smartAlg(int begin, int end, Slot targetSlot){
    65 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    66 		
    67 		if(begin == 0){
    68 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
    69 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
    70 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    71 			
    72 			if(end == 0)
    73 				return retArray;
    74 		}
    75 		
    76 		List<Batch> list1 = buildNumberOperations(0, end, targetSlot);
    77 		List<Batch> list2 = buildNumberOperations(begin, end, targetSlot);
    78 		if (list1.size() <= list2.size())
    79 		{
    80 			Collections.reverse(list1);
    81 			retArray.addAll(list1);
    82 		}
    83 		else
    84 		{
    85 			Collections.reverse(list2);
    86 			retArray.addAll(list2);
    87 		}
    88 		return retArray;
    89 	}
    90 	
    91 	protected static List<Batch> buildNumberOperations(int begin, int end, Slot targetSlot){
    92 		List<Batch> list = new ArrayList<Batch>();
    93 		
    94 		while(end >= 2){
    95 			int d = end % 2;
    96 			if (begin > 0 && 2*begin > end)
    97 			{
    98 				for (int i = 0; i < end - begin; i++)
    99 				{
   100 					list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
   101 				}
   102 				break;
   103 			}
   104 			if( d == 0 ) {
   105 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
   106 				end = end / 2;
   107 			} else {
   108 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
   109 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
   110 				end = (end-1) / 2;
   111 			}
   112 		}
   113 		return list;
   114 	}
   115 	
   116 	/**
   117 	 * Корректировка позиций регистров
   118 	 * В самом лучшем случаи регистры это слоты 0 1 2 3 
   119 	 * Но могут быть изменены, этот метод корректирует ссылки на регистры в полтиках
   120 	 * @param registerIndex - индекс регитсра (не слота!)
   121 	 * @param processingSlot - слот где происходит рассчёт
   122 	 * @return
   123 	 */
   124 	protected ArrayList<Batch> correctRegister(int registerIndex, Slot processingSlot){
   125 		ArrayList<Batch> retArray = new ArrayList<Batch>();
   126 		for(int i=0; i<Run.getRegisterSlotNum(registerIndex); ++i) 
   127 		{
   128 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   129 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   130 			
   131 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   132 		}
   133 		return retArray;
   134 	}
   135 	
   136 	public abstract ArrayList<Batch> getBatches();
   137 	
   138 	public static void main(String[] args)
   139 	{
   140 		Slot slot = new Slot(1);
   141 		List<Batch> list = smartAlg(0, 8, slot);
   142 		System.out.println(list.toArray().toString());
   143 	}
   144 }