src/main/java/ru/alphatech/icfpc/game/AbstractPolicy.java
author akhmetovlz
Sun, 19 Jun 2011 22:41:13 +0400
changeset 115 21f9fa7770cc
parent 101 4bb458c3548e
child 118 8fca5b204fb3
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 		IValue targetValue = targetSlot.getValue();
    42 		
    43 		if(targetValue instanceof ICard){
    44 			retArray.addAll(smartAlg(0, number, targetSlot));
    45 		}else{
    46 			Value numTargetValue = (Value) targetValue;
    47 			if( numTargetValue.getValue() == number ){
    48 				return retArray;
    49 			}else
    50 			if( numTargetValue.getValue() > number ){
    51 				retArray.addAll( smartAlg(0, number, targetSlot) );
    52 			}else{
    53 				retArray.addAll( smartAlg(numTargetValue.getValue(), number, targetSlot) );
    54 			}
    55 		}
    56 		
    57 		return retArray;
    58 	}
    59 	
    60 	
    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));
    65 			begin *= 2;
    66 		}
    67 		while( begin != end){
    68 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
    69 			begin ++;
    70 		}
    71 		return retArray;
    72 	}
    73 	
    74 	protected static List<Batch> smartAlg(int begin, int end, Slot targetSlot){
    75 		ArrayList<Batch> retArray = new ArrayList<Batch>();
    76 		
    77 		if(begin == 0){
    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));
    81 		}
    82 		
    83 		List<Batch> list1 = buildNumberOperations(0, end, targetSlot);
    84 		List<Batch> list2 = buildNumberOperations(begin, end, targetSlot);
    85 		if (list1.size() <= list2.size())
    86 		{
    87 			Collections.reverse(list1);
    88 			retArray.addAll(list1);
    89 		}
    90 		else
    91 		{
    92 			Collections.reverse(list2);
    93 			retArray.addAll(list2);
    94 		}
    95 		return retArray;
    96 	}
    97 	
    98 	protected static List<Batch> buildNumberOperations(int begin, int end, Slot targetSlot){
    99 		List<Batch> list = new ArrayList<Batch>();
   100 		
   101 		while(end >= 2){
   102 			int d = end % 2;
   103 			if (begin > 0 && 2*begin > end)
   104 			{
   105 				for (int i = 0; i < end - begin; i++)
   106 				{
   107 					list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
   108 				}
   109 				break;
   110 			}
   111 			if( d == 0 ) {
   112 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
   113 				end = end / 2;
   114 			} else {
   115 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Succ(), targetSlot));
   116 				list.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl() , targetSlot));
   117 				end = (end-1) / 2;
   118 			}
   119 		}
   120 		return list;
   121 	}
   122 	
   123 	/**
   124 	 * Корректировка позиций регистров
   125 	 * В самом лучшем случаи регистры это слоты 0 1 2 3 
   126 	 * Но могут быть изменены, этот метод корректирует ссылки на регистры в полтиках
   127 	 * @param registerIndex - индекс регитсра (не слота!)
   128 	 * @param processingSlot - слот где происходит рассчёт
   129 	 * @return
   130 	 */
   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) 
   134 		{
   135 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
   136 			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
   137 			
   138 			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
   139 		}
   140 		return retArray;
   141 	}
   142 	
   143 	public abstract ArrayList<Batch> getBatches();
   144 	
   145 	public static void main(String[] args)
   146 	{
   147 		Slot slot = new Slot(1);
   148 		List<Batch> list = smartAlg(0, 8, slot);
   149 		System.out.println(list.toArray().toString());
   150 	}
   151 }