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